diff --git a/graph/digraph.h b/graph/digraph.h index 6c3d0d5..92ecaf2 100644 --- a/graph/digraph.h +++ b/graph/digraph.h @@ -28,7 +28,15 @@ Digraph::Digraph(std::map> digraph) { template Digraph Digraph::reverse() { - return Digraph(); + std::map> revMatrix; + + for (const auto& v : Graph::adjMatrix) { + for (const auto& u : v.second) { + revMatrix[u].insert(v.first); + } + } + + return revMatrix; } } // namespace graph diff --git a/test/graph_test.cc b/test/graph_test.cc index ff95237..0c22b3c 100644 --- a/test/graph_test.cc +++ b/test/graph_test.cc @@ -10,6 +10,43 @@ using namespace graph; TEST_SUITE("Graph") { + TEST_CASE("Reverse") { + // 1 --> 2 --> 4 --> 1 + // 2 --> 3 --> 5 --> 7 --> 3 + // 5 --> 9 --> 6 --> 8 --> 9 + Digraph G; + G.insert(1, 2); + G.insert(2, 3); + G.insert(2, 4); + G.insert(3, 5); + G.insert(4, 1); + G.insert(5, 7); + G.insert(5, 9); + G.insert(6, 8); + G.insert(7, 3); + G.insert(8, 9); + G.insert(9, 6); + + REQUIRE_EQ(G.vertices.size(), 9); + + auto RG = G.reverse(); + + std::map> exp = { + {1, {4}}, + {2, {1}}, + {3, {2, 7}}, + {4, {2}}, + {5, {3}}, + {6, {9}}, + {7, {5}}, + {8, {6}}, + {9, {5, 8}} + }; + + CHECK_EQ(RG.adjMatrix, exp); + + } + TEST_CASE("SCC T1") { // 1 --> 2 --> 4 --> 1 // 2 --> 3 --> 5 --> 7 --> 3