Update decremental scc algorithm

This commit is contained in:
stefiosif
2022-09-12 00:08:45 +03:00
parent 5c1b13b400
commit 42f824a249
3 changed files with 57 additions and 151 deletions

View File

@@ -3,25 +3,30 @@
#include "algorithm/roditty_zwick.h"
#include "algorithm/tarjan.h"
#include "tree/breadth_first_tree.h"
#include "graph/breadth_first_tree.h"
using namespace graph;
using namespace tree;
namespace algo {
template<typename T>
class DecrementalSCC : public RodittyZwick<T> {
public:
DecrementalSCC() = default;
DecrementalSCC(Digraph<T> G) : G(G) {}
void init();
void findSCC();
// Return true if u and v are in the same SCC
bool query(const T& u, const T& v);
// Remove edge (u,v) and update A accordingly for fast checking query
void remove(const T& u, const T& v);
void setGraph(Digraph<T> G);
private:
Digraph<T> G;
@@ -33,7 +38,7 @@ private:
std::map<T, BreadthFirstTree<T>> outTree;
// Connect each representative with its SCC
std::map<T, SCC<T>> connection;
std::map<T, SCC<T>> SCC;
};
template<typename T>
@@ -43,20 +48,18 @@ void DecrementalSCC<T>::init() {
template<typename T>
void DecrementalSCC<T>::findSCC() {
auto SCCs = Tarjan<T>(G).execute();
auto SCCs = Tarjan<T>(G.adjMatrix).execute();
for (auto& C : SCCs) {
const auto& w = C.representative();
const auto& w = C.id;
for (const auto& v : C.adjMatrix)
A[v.first] = w;
for (const auto& v : std::views::keys(C.adjMatrix))
A[v] = w;
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
outTree[w] = BreadthFirstTree<T>(C, w);
inTree[w] = BreadthFirstTree<T>(C.reverse(), w);
connection[w] = C;
SCC[w] = C;
}
}
@@ -67,27 +70,30 @@ bool DecrementalSCC<T>::query(const T& u, const T& v) {
template<typename T>
void DecrementalSCC<T>::remove(const T& u, const T& v) {
G.adjMatrix[u].erase(v);
const auto& w = A[u];
SCC[w].remove(u, v);
G.remove(u, v);
// If u and v are not in the same SCC, do nothing
if (A[u] != A[v]) return;
const auto& w = A[u];
connection[w].remove(u, v);
// If edge (u,v) is not contained in both inTree and outTree do nothing
if (!inTree[w].adjMatrix[u].contains(v) &&
!outTree[w].adjMatrix[u].contains(v))
return;
// Update In(w) and Out(w) if they contain the edge
if (inTree[w].contains(u, v) || outTree[w].contains(u, v)) {
auto C = connection[w];
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
}
// Update In(w) and Out(w)
outTree[w] = BreadthFirstTree<T>(SCC[w], w);
inTree[w] = BreadthFirstTree<T>(SCC[w].reverse(), w);
// If a SCC is broken, compute all SCCs again
if (!inTree[w].contains(u) || !outTree[w].contains(v)) {
if (!inTree[w].adjMatrix.count(u) || !outTree[w].adjMatrix.count(v))
findSCC();
}
}
template<typename T>
void DecrementalSCC<T>::setGraph(Digraph<T> G) {
this->G = G;
}
}; // namespace algo

View File

@@ -65,7 +65,7 @@ void Tarjan<T>::strongConnect(const T& u) {
finished = (w == u);
} while (!finished);
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink) });
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink + 1) });
}
}