From 2175ac0ca9f3d1c4ca6b13d7f34df5f528488735 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Fri, 10 Feb 2023 18:20:00 +0200 Subject: [PATCH] Add contains method for single vertex and hash function for SCCs --- include/graph/digraph.h | 26 ++++++++++++++++++++++++++ include/graph/graph.h | 20 +++----------------- include/graph/scc.h | 26 ++++++++++++++++++-------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/include/graph/digraph.h b/include/graph/digraph.h index 371659a..9a88d98 100644 --- a/include/graph/digraph.h +++ b/include/graph/digraph.h @@ -24,6 +24,12 @@ public: // Reverse graph directions auto reverse(); + + // + auto contains(const T& u); + + friend std::ostream& operator<<<>(std::ostream& os, Digraph& G); + }; template @@ -60,6 +66,26 @@ auto Digraph::reverse() { return revMatrix; } +template +auto Digraph::contains(const T& u) { + return this->adjList.count(u); +} + +template +std::ostream& operator<<(std::ostream& os, Digraph& G) { + os << "V: " << G.V() << " E: " << G.E() << '\n'; + for (const auto& u : G.vertices()) { + if (!G.adjList[u].empty()) { + for (const auto& v : G.adjList[u]) { + os << u << "->" << v << ' '; + } + os << '\n'; + } + } + os << '\n'; + return os; +} + } // namespace graph #endif \ No newline at end of file diff --git a/include/graph/graph.h b/include/graph/graph.h index c30462b..1058bfd 100644 --- a/include/graph/graph.h +++ b/include/graph/graph.h @@ -27,7 +27,7 @@ public: virtual void remove(const T& u, const T& v) =0; // Return graph vertices - auto vertices(); + auto vertices() const; // Return num. of vertices std::uint16_t V(); @@ -38,11 +38,10 @@ public: // Adjacency matrix representation std::unordered_map> adjList; - friend std::ostream& operator<<<>(std::ostream& os, Graph& G); }; template -auto Graph::vertices() { +auto Graph::vertices() const{ return std::views::keys(adjList); } @@ -60,20 +59,7 @@ std::uint16_t Graph::E() { return edges; } -template -std::ostream& operator<<(std::ostream& os, Graph& G) { - os << "V: " << G.V() << " E: " << G.E() << '\n'; - for (const auto& u : this->vertices()) { - if (!this->adjList[u].empty()) { - for (const auto& v : this->adjList[u]) { - os << u << "->" << v << ' '; - } - os << '\n'; - } - } - os << '\n'; - return os; -} + } // namespace graph diff --git a/include/graph/scc.h b/include/graph/scc.h index 8d51af1..d1878a2 100644 --- a/include/graph/scc.h +++ b/include/graph/scc.h @@ -18,11 +18,14 @@ public: SCC(Digraph G, T id) : id(id) { normalize(); } - // Return true if v is part of this SCC - bool member(const T& v); + // Return true if u is part of this SCC + bool contains(const T& u) const; // Representative vertex of this SCC - T id; + T id{}; + + // + std::unordered_map> neighboorList; bool operator==(const SCC& o) const; private: @@ -34,25 +37,32 @@ template void SCC::normalize() { for (const auto& u : this->vertices()) { for (const auto& v : this->adjList[u]) { - if (!this->adjList.count(v)) { + if (!this->contains(v)) { this->adjList[u].erase(v); this->adjList.erase(v); + neighboorList[u].insert(v); } } } } template -bool SCC::member(const T& v) { - const auto& V = this->vertices(); - return std::find(V.begin(), V.end(), v) != V.end(); +bool SCC::contains(const T& u) const { + return this->adjList.count(u); } template -bool SCC::operator==(const SCC& o) const{ +bool SCC::operator==(const SCC& o) const { return id == o.id; } +template +struct HashSCC { + std::size_t operator()(const SCC& C) const { + return static_cast(C.id); + } +}; + }; // namespace graph #endif \ No newline at end of file