Commit 137eb9fd authored by Tiago Peixoto's avatar Tiago Peixoto

Fix clear_vertex() and remove_edge(s, t, g) in adj_list<>

parent 44739a41
...@@ -548,39 +548,76 @@ inline Vertex add_vertex(adj_list<Vertex>& g) ...@@ -548,39 +548,76 @@ inline Vertex add_vertex(adj_list<Vertex>& g)
template <class Vertex> template <class Vertex>
inline void clear_vertex(Vertex v, adj_list<Vertex>& g) inline void clear_vertex(Vertex v, adj_list<Vertex>& g)
{ {
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[v]; if (!g._keep_epos)
for (size_t i = 0; i < oes.size(); ++i)
{ {
Vertex t = oes[i].first; typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[v];
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[t]; for (size_t i = 0; i < oes.size(); ++i)
for (size_t j = 0; j < ies.size(); ++j)
{ {
if (ies[j].first == v) Vertex t = oes[i].first;
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[t];
for (size_t j = 0; j < ies.size(); ++j)
{ {
g._free_indexes.push_back(ies[j].second); if (ies[j].first == v)
ies.erase(ies.begin() + j); {
g._free_indexes.push_back(ies[j].second);
ies.erase(ies.begin() + j);
}
} }
} }
} g._n_edges -= oes.size();
g._n_edges -= oes.size(); oes.clear();
oes.clear();
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[v]; typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[v];
for (size_t i = 0; i < oes.size(); ++i) for (size_t i = 0; i < oes.size(); ++i)
{
Vertex s = ies[i].first;
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[s];
for (size_t j = 0; j < oes.size(); ++j)
{ {
if (oes[j].first == v) Vertex s = ies[i].first;
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[s];
for (size_t j = 0; j < oes.size(); ++j)
{ {
g._free_indexes.push_back(oes[j].second); if (oes[j].first == v)
oes.erase(oes.begin() + j); {
g._free_indexes.push_back(oes[j].second);
oes.erase(oes.begin() + j);
}
} }
} }
g._n_edges -= ies.size();
ies.clear();
}
else
{
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[v];
for (size_t i = 0; i < oes.size(); ++i)
{
Vertex t = oes[i].first;
size_t idx = oes[i].second;
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[t];
const std::pair<int32_t, int32_t>& pos = g._epos[idx];
g._epos[ies.back().second].second = pos.second;
ies[pos.second] = ies.back();
ies.pop_back();
g._free_indexes.push_back(idx);
}
g._n_edges -= oes.size();
oes.clear();
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[v];
for (size_t i = 0; i < ies.size(); ++i)
{
Vertex s = ies[i].first;
size_t idx = ies[i].second;
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[s];
const std::pair<int32_t, int32_t>& pos = g._epos[idx];
g._epos[oes.back().second].first = pos.first;
oes[pos.first] = oes.back();
oes.pop_back();
g._free_indexes.push_back(idx);
}
g._n_edges -= ies.size();
ies.clear();
} }
g._n_edges -= ies.size();
ies.clear();
} }
// O(V + E) // O(V + E)
...@@ -689,22 +726,29 @@ template <class Vertex> ...@@ -689,22 +726,29 @@ template <class Vertex>
inline void remove_edge(Vertex s, Vertex t, inline void remove_edge(Vertex s, Vertex t,
adj_list<Vertex>& g) adj_list<Vertex>& g)
{ {
typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[s]; if (!g._keep_epos)
for (size_t i = 0; i < oes.size(); ++i)
{ {
if (t == oes[i].first) typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[s];
for (size_t i = 0; i < oes.size(); ++i)
{ {
g._free_indexes.push_back(oes[i].second); if (t == oes[i].first)
oes.erase(oes.begin() + i); {
g._n_edges--; g._free_indexes.push_back(oes[i].second);
oes.erase(oes.begin() + i);
g._n_edges--;
}
} }
}
typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[t]; typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[t];
for (size_t i = 0; i < ies.size(); ++i) for (size_t i = 0; i < ies.size(); ++i)
{
if (s == ies[i].first)
ies.erase(ies.begin() + i);
}
}
else
{ {
if (s == ies[i].first) remove_edge(edge(s, t, g).first, g);
ies.erase(ies.begin() + i);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment