Rename decrementalscc to rodittyzwick as it is the core algorithm

This commit is contained in:
stefiosif
2022-09-25 17:33:47 +03:00
parent 95e7d56c1e
commit fa65e54376
2 changed files with 48 additions and 48 deletions

View File

@@ -1,7 +1,7 @@
#ifndef DECREMENTAL_SCC_H_
#define DECREMENTAL_SCC_H_
#ifndef RODITTY_ZWICK_H_
#define RODITTY_ZWICK_H_
#include "algorithm/roditty_zwick.h"
#include "algorithm/decremental_reachability.h"
#include "algorithm/tarjan.h"
#include "graph/breadth_first_tree.h"
@@ -10,69 +10,69 @@ using namespace graph;
namespace algo {
template<typename T>
class DecrementalSCC : public RodittyZwick<T> {
class RodittyZwick : public DecrementalReachability<T> {
public:
DecrementalSCC() = default;
RodittyZwick() = default;
DecrementalSCC(Digraph<T> G) : G(G) {}
RodittyZwick(Digraph<T> G) { this->G = G; }
void init();
//
void init() override;
//
void findSCC();
// Return true if u and v are in the same SCC
bool query(const T& u, const T& v);
bool query(const T& u, const T& v) override;
// Remove edge (u,v) and update A accordingly for fast checking query
void remove(const T& u, const T& v);
void remove(const T& u, const T& v) override;
void setGraph(Digraph<T> G);
private:
Digraph<T> G;
// Array used to answer strong connectivity queries in O(1) time
std::map<T, T> A;
// Connect each representative with its SCC
std::map<T, SCC<T>> C;
// Maintain in-out bfs trees
std::map<T, BreadthFirstTree<T>> inTree;
std::map<T, BreadthFirstTree<T>> outTree;
// Connect each representative with its SCC
std::map<T, SCC<T>> SCC;
};
template<typename T>
void DecrementalSCC<T>::init() {
void RodittyZwick<T>::init() {
findSCC();
}
template<typename T>
void DecrementalSCC<T>::findSCC() {
auto SCCs = Tarjan<T>(G.adjMatrix).execute();
void RodittyZwick<T>::findSCC() {
auto SCCs = Tarjan<T>(this->G.adjMatrix).execute();
for (auto& C : SCCs) {
const auto& w = C.id;
for (auto& SCC : SCCs) {
const auto& w = SCC.id;
for (const auto& v : std::views::keys(C.adjMatrix))
for (const auto& v : std::views::keys(SCC.adjMatrix))
A[v] = w;
outTree[w] = BreadthFirstTree<T>(C, w);
inTree[w] = BreadthFirstTree<T>(C.reverse(), w);
outTree[w] = BreadthFirstTree<T>(SCC, w);
inTree[w] = BreadthFirstTree<T>(SCC.reverse(), w);
SCC[w] = C;
C[w] = SCC;
}
}
template<typename T>
bool DecrementalSCC<T>::query(const T& u, const T& v) {
bool RodittyZwick<T>::query(const T& u, const T& v) {
return A[u] == A[v];
}
template<typename T>
void DecrementalSCC<T>::remove(const T& u, const T& v) {
void RodittyZwick<T>::remove(const T& u, const T& v) {
const auto& w = A[u];
SCC[w].remove(u, v);
G.remove(u, v);
C[w].remove(u, v);
this->G.remove(u, v);
// If u and v are not in the same SCC, do nothing
if (A[u] != A[v]) return;
@@ -83,8 +83,8 @@ void DecrementalSCC<T>::remove(const T& u, const T& v) {
return;
// Update In(w) and Out(w)
outTree[w] = BreadthFirstTree<T>(SCC[w], w);
inTree[w] = BreadthFirstTree<T>(SCC[w].reverse(), w);
outTree[w] = BreadthFirstTree<T>(C[w], w);
inTree[w] = BreadthFirstTree<T>(C[w].reverse(), w);
// If a SCC is broken, compute all SCCs again
if (!inTree[w].adjMatrix.count(u) || !outTree[w].adjMatrix.count(v))
@@ -92,7 +92,7 @@ void DecrementalSCC<T>::remove(const T& u, const T& v) {
}
template<typename T>
void DecrementalSCC<T>::setGraph(Digraph<T> G) {
void RodittyZwick<T>::setGraph(Digraph<T> G) {
this->G = G;
}