From d199b7de1753b47cb749b90213ecf46a15779418 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Wed, 29 Jun 2022 17:33:27 +0300 Subject: [PATCH] Add tests for same SCC O(1) query --- algorithm/decremental_scc.h | 10 +++++----- algorithm/tarjan.h | 4 ++-- test/algorithm_test.cc | 16 ++++++++++++++-- test/graph_test.cc | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/algorithm/decremental_scc.h b/algorithm/decremental_scc.h index 6c018dc..6cc447c 100644 --- a/algorithm/decremental_scc.h +++ b/algorithm/decremental_scc.h @@ -36,14 +36,14 @@ private: std::map, Digraph>> SPT; // Connect each representative with its SCC - std::map> SCCs; + std::map> connection; }; template void DecrementalSCC::init() { - auto tarjan = Tarjan(G).run(); + auto SCCs = Tarjan(G).findSCC(); - for (auto& C : tarjan) { + for (auto& C : SCCs) { const auto& w = C.representative(); // Create shortest-paths out-tree/in-tree @@ -55,7 +55,7 @@ void DecrementalSCC::init() { A[v] = w; // Link SCC with its representative w - SCCs[w] = C; + connection[w] = C; } } @@ -77,7 +77,7 @@ void DecrementalSCC::remove(const T& u, const T& v) { return; // Update In(w) and Out(w) - auto inTree = BFS(SCCs[w]).run(w); + auto inTree = BFS(connection[w]).run(w); SPT[w] = std::make_pair(inTree, inTree.reverse()); if (!SPT[w].second.vertices.contains(u) || diff --git a/algorithm/tarjan.h b/algorithm/tarjan.h index 06bd8ab..80fbc83 100644 --- a/algorithm/tarjan.h +++ b/algorithm/tarjan.h @@ -17,7 +17,7 @@ public: Tarjan(Digraph G) : G(G) {} // - std::vector> run(); + std::vector> findSCC(); // void strongConnect(const T& v); @@ -68,7 +68,7 @@ void Tarjan::strongConnect(const T& v) { } template -std::vector> Tarjan::run() { +std::vector> Tarjan::findSCC() { for (const auto& v : G.vertices) { if (p[v].index == -1) { strongConnect(v); diff --git a/test/algorithm_test.cc b/test/algorithm_test.cc index 2f64fec..c5477fb 100644 --- a/test/algorithm_test.cc +++ b/test/algorithm_test.cc @@ -30,7 +30,7 @@ TEST_SUITE("Algorithm") { REQUIRE_EQ(G.vertices.size(), 9); algo::Tarjan tarjan(G); - auto res = tarjan.run(); + auto res = tarjan.findSCC(); std::vector> exp = { {6, 8, 9}, @@ -63,7 +63,7 @@ TEST_SUITE("Algorithm") { REQUIRE_EQ(G.vertices.size(), 7); algo::Tarjan tarjan(G); - auto res = tarjan.run(); + auto res = tarjan.findSCC(); std::vector> exp = { {2, 5, 7}, @@ -133,6 +133,18 @@ TEST_SUITE("Algorithm") { algo::DecrementalSCC rz(G); rz.init(); + std::vector> exp = { + {2, 5, 7}, + {1, 3, 4, 6} + }; + + CHECK_EQ(rz.query(1, 2), false); + CHECK_EQ(rz.query(1, 3), true); + CHECK_EQ(rz.query(1, 7), false); + CHECK_EQ(rz.query(2, 3), false); + CHECK_EQ(rz.query(2, 5), true); + CHECK_EQ(rz.query(5, 7), true); + //CHECK_EQ(rz.query(1, 2), true); } } \ No newline at end of file diff --git a/test/graph_test.cc b/test/graph_test.cc index 0c22b3c..c6a380e 100644 --- a/test/graph_test.cc +++ b/test/graph_test.cc @@ -67,7 +67,7 @@ TEST_SUITE("Graph") { REQUIRE_EQ(G.vertices.size(), 9); algo::Tarjan tarjan(G); - auto sccs = tarjan.run(); + auto sccs = tarjan.findSCC(); } TEST_CASE("SCC T2") { @@ -88,6 +88,6 @@ TEST_SUITE("Graph") { REQUIRE_EQ(G.vertices.size(), 7); algo::Tarjan tarjan(G); - auto sccs = tarjan.run(); + auto sccs = tarjan.findSCC(); } } \ No newline at end of file