diff --git a/algorithm/dmscc.h b/algorithm/dmscc.h deleted file mode 100644 index 4296a86..0000000 --- a/algorithm/dmscc.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DMSCC_H_ -#define DMSCC_H_ - -#include "graph/graph.h" -using namespace graph; -#include "tarjan.h" - -namespace algo { - -// A randomized decremental algorithm for maintaining SCCs -template -class DMSCC { -public: - DMSCC() = default; -}; - -}; // namespace algo - -#endif \ No newline at end of file diff --git a/algorithm/roditty_zwick.h b/algorithm/roditty_zwick.h new file mode 100644 index 0000000..b6e61ec --- /dev/null +++ b/algorithm/roditty_zwick.h @@ -0,0 +1,55 @@ +#ifndef RODITTY_ZWICK_H_ +#define RODITTY_ZWICK_H_ + +#include "graph/digraph.h" +#include "graph/scc.h" +#include "algorithm/tarjan.h" +#include "algorithm/bfs.h" + +#include + +using namespace graph; + +namespace algo { + +// A randomized decremental algorithm for maintaining SCCs +template +class RodittyZwick { +public: + RodittyZwick(Digraph G) : G(G) {} + + // + void sccDecrementalMaintenance(); + + // + void tcDecrementalMaintenance(); +private: + Digraph G; +}; + +template +void RodittyZwick::sccDecrementalMaintenance() { + std::map A; + Tarjan tarjan(G); + + auto sccs = tarjan.run(); + + for (auto& scc : sccs) { + const auto& root = scc.representative(); + BFS bfs(scc); + auto spt = bfs.run(root); + for (const auto& vertex : scc.vertices) { + A[vertex] = root; + } + + } +} + +template +void RodittyZwick::tcDecrementalMaintenance() { + +} + +}; // namespace algo + +#endif \ No newline at end of file diff --git a/graph/digraph.h b/graph/digraph.h index 03ef92b..6c3d0d5 100644 --- a/graph/digraph.h +++ b/graph/digraph.h @@ -4,6 +4,7 @@ #include "graph.h" #include +#include namespace graph { @@ -12,10 +13,19 @@ class Digraph : public Graph { public: Digraph() = default; + Digraph(std::map> digraph); + // Reverse graph directions Digraph reverse(); }; +template +Digraph::Digraph(std::map> digraph) { + Graph::adjMatrix = digraph; + auto kv = std::views::keys(Graph::adjMatrix); + Graph::vertices = std::set{ kv.begin(), kv.end() }; +} + template Digraph Digraph::reverse() { return Digraph(); diff --git a/graph/scc.h b/graph/scc.h index 44fbce5..21b175d 100644 --- a/graph/scc.h +++ b/graph/scc.h @@ -12,8 +12,13 @@ class SCC : public Graph { public: SCC(std::map> scc); + // + T representative() { return proxy; }; private: - T root; + // Each SCC has a representative vertex that helps + // answer strong connectivity queries in O(1) time + T proxy; + }; template @@ -21,7 +26,7 @@ SCC::SCC(std::map> scc) { Graph::adjMatrix = scc; auto kv = std::views::keys(Graph::adjMatrix); Graph::vertices = std::set{ kv.begin(), kv.end() }; - root = scc.begin()->first; + proxy = scc.begin()->first; } }; // namespace graph diff --git a/test/algorithm_test.cc b/test/algorithm_test.cc index d75f13a..40c25c4 100644 --- a/test/algorithm_test.cc +++ b/test/algorithm_test.cc @@ -4,6 +4,7 @@ #include "graph/scc.h" #include "algorithm/tarjan.h" #include "algorithm/bfs.h" +#include "algorithm/roditty_zwick.h" using namespace graph; @@ -91,8 +92,8 @@ TEST_SUITE("Algorithm") { G.insert(6, 3); G.insert(7, 2); - algo::BFS bfs(G, 1); - auto tree = bfs.run(); + algo::BFS bfs(G); + auto tree = bfs.run(1); /*std::map> exp = { {1, {2, 4}}, @@ -111,6 +112,25 @@ TEST_SUITE("Algorithm") { {5, {7}} }; - CHECK_EQ(tree, exp); + CHECK_EQ(tree.adjMatrix, exp); + } + + TEST_CASE("Roditty Zwick A1 T1 ") { + // 1 --> 2 --> 5 --> 7 --> 2 + // 1 --> 4 --> 3 --> 1 + // 4 --> 6 --> 3 + Digraph G; + G.insert(1, 2); + G.insert(1, 4); + G.insert(2, 5); + G.insert(3, 1); + G.insert(4, 3); + G.insert(4, 6); + G.insert(5, 7); + G.insert(6, 3); + G.insert(7, 2); + + algo::RodittyZwick rz(G); + rz.sccDecrementalMaintenance(); } } \ No newline at end of file