diff --git a/graph/digraph.h b/graph/digraph.h index 0c3bcaf..21dba0c 100644 --- a/graph/digraph.h +++ b/graph/digraph.h @@ -4,8 +4,6 @@ #include "graph.h" #include "algorithm/breadth_first_search.h" -#include - namespace graph { template @@ -26,9 +24,6 @@ public: // Reverse graph directions auto reverse(); - - // Return vertices - auto vertices(); }; template @@ -56,7 +51,7 @@ template auto Digraph::reverse() { std::map> revMatrix; - for (const auto& u : vertices()) { + for (const auto& u : this->vertices()) { for (const auto& v : this->adjMatrix[u]) { revMatrix[v].insert(u); } @@ -65,11 +60,6 @@ auto Digraph::reverse() { return revMatrix; } -template -auto Digraph::vertices() { - return std::views::keys(this->adjMatrix); -} - } // namespace graph #endif \ No newline at end of file diff --git a/graph/graph.h b/graph/graph.h index e67bd27..7b7738b 100644 --- a/graph/graph.h +++ b/graph/graph.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace graph { @@ -17,13 +18,16 @@ public: ~Graph(); // Return true if there is a path from u to v - virtual bool contains(const T& u, const T& v) = 0; + virtual bool contains(const T& u, const T& v) =0; // Add edge e(u,v) - virtual void insert(const T& u, const T& v) = 0; + virtual void insert(const T& u, const T& v) =0; // Remove edge e(u,v) - virtual void remove(const T& u, const T& v) = 0; + virtual void remove(const T& u, const T& v) =0; + + // Return graph vertices + auto vertices(); // Return num. of vertices std::uint16_t V(); @@ -42,6 +46,11 @@ Graph::~Graph() { adjMatrix.clear(); } +template +auto Graph::vertices() { + return std::views::keys(adjMatrix); +} + template std::uint16_t Graph::V() { return static_cast(adjMatrix.size()); @@ -50,8 +59,8 @@ std::uint16_t Graph::V() { template std::uint16_t Graph::E() { std::uint16_t edges = 0; - for (const auto& v : adjMatrix) { - edges += static_cast(v.second.size()); + for (const auto& u : vertices()) { + edges += static_cast(adjMatrix[u].size()); } return static_cast(edges); } @@ -59,10 +68,10 @@ std::uint16_t Graph::E() { template std::ostream& operator<<(std::ostream& os, Graph& G) { os << "V: " << G.V() << " E: " << G.E() << '\n'; - for (const auto& u : G.adjMatrix) { - if (!u.second.empty()) { - for (const auto& v : u.second) { - os << u.first << "->" << v << ' '; + for (const auto& u : this->vertices()) { + if (!this->adjMatrix[u].empty()) { + for (const auto& v : this->adjMatrix[u]) { + os << u << "->" << v << ' '; } os << '\n'; } diff --git a/test/graph_test.cc b/test/graph_test.cc index 01fc995..9b7bc89 100644 --- a/test/graph_test.cc +++ b/test/graph_test.cc @@ -35,7 +35,7 @@ TEST_SUITE("Graph") { G.insert(13, 9); G.insert(12, 10); - REQUIRE_EQ(G.adjMatrix.size(), 13); + REQUIRE_EQ(G.V(), 13); auto reverse = G.reverse(); @@ -85,8 +85,6 @@ TEST_SUITE("Graph") { G.insert(13, 9); G.insert(12, 10); - REQUIRE_EQ(G.adjMatrix.size(), 13); - CHECK_EQ(G.V(), 13); CHECK_EQ(G.E(), 18); } @@ -116,7 +114,7 @@ TEST_SUITE("Graph") { G.insert(8, 9); G.insert(9, 5); - REQUIRE_EQ(G.adjMatrix.size(), 9); + REQUIRE_EQ(G.V(), 9); auto SCCs = algo::Tarjan(G.adjMatrix).execute();