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

View File

@@ -1,6 +1,6 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include "algorithm/decremental_scc.h" #include "algorithm/roditty_zwick.h"
#include "algorithm/frigioni.h" #include "algorithm/frigioni.h"
#include "algorithm/italiano.h" #include "algorithm/italiano.h"
@@ -8,7 +8,7 @@ using namespace graph;
TEST_SUITE("Decremental Reachability Test") { TEST_SUITE("Decremental Reachability Test") {
TEST_CASE("DecrementalSCC") { TEST_CASE("RodittyZwick") {
// 1 --> 2 --> 3 --> 1 // 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3 // 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6 // 2 --> 6 --> 7 --> 8 --> 6
@@ -37,30 +37,30 @@ TEST_SUITE("Decremental Reachability Test") {
REQUIRE_EQ(G.adjMatrix.size(), 13); REQUIRE_EQ(G.adjMatrix.size(), 13);
algo::DecrementalSCC<std::uint16_t> decremental(G); algo::RodittyZwick<std::uint16_t> rodittyZwick(G);
decremental.init(); rodittyZwick.init();
SUBCASE("DecrementalSCC::query") { SUBCASE("RodittyZwick::query") {
CHECK_EQ(decremental.query(1, 5), true); CHECK_EQ(rodittyZwick.query(1, 5), true);
CHECK_EQ(decremental.query(1, 7), false); CHECK_EQ(rodittyZwick.query(1, 7), false);
CHECK_EQ(decremental.query(1, 9), false); CHECK_EQ(rodittyZwick.query(1, 9), false);
} }
SUBCASE("DecrementalSCC::remove") { SUBCASE("RodittyZwick::remove") {
decremental.remove(12, 10); rodittyZwick.remove(12, 10);
CHECK_EQ(decremental.query(12, 10), true); CHECK_EQ(rodittyZwick.query(12, 10), true);
CHECK_EQ(decremental.query(11, 10), true); CHECK_EQ(rodittyZwick.query(11, 10), true);
decremental.remove(9, 10); rodittyZwick.remove(9, 10);
CHECK_EQ(decremental.query(12, 13), false); CHECK_EQ(rodittyZwick.query(12, 13), false);
CHECK_EQ(decremental.query(10, 12), false); CHECK_EQ(rodittyZwick.query(10, 12), false);
decremental.remove(2, 6); rodittyZwick.remove(2, 6);
CHECK_EQ(decremental.query(5, 8), false); CHECK_EQ(rodittyZwick.query(5, 8), false);
CHECK_EQ(decremental.query(6, 8), true); CHECK_EQ(rodittyZwick.query(6, 8), true);
} }
} }