diff --git a/graph/scc.h b/graph/scc.h index 94592e2..3ede212 100644 --- a/graph/scc.h +++ b/graph/scc.h @@ -3,6 +3,9 @@ #include "digraph.h" +#include +#include + namespace graph { template @@ -14,27 +17,36 @@ public: SCC(Digraph G, T id) : id(id) { normalize(); } + // Return true if v is part of this SCC + bool member(const T& v); + + // Representative vertex of this SCC T id; bool operator==(const SCC& o) const; private: - // For every vertex in the SCC, if there is an edge between an in-scc vertex - // and an out-scc vertex erase the edge between them and the out-scc vertex + // Erase all edges that include vertices outside this SCC void normalize(); }; template void SCC::normalize() { - for (const auto& u : this->adjMatrix) { - for (const auto& v : u.second) { + for (const auto& u : this->vertices()) { + for (const auto& v : this->adjMatrix[u]) { if (!this->adjMatrix.count(v)) { - this->adjMatrix[u.first].erase(v); + this->adjMatrix[u].erase(v); this->adjMatrix.erase(v); } } } } +template +bool SCC::member(const T& v) { + const auto& V = this->vertices(); + return std::find(V.begin(), V.end(), v) != V.end(); +} + template bool SCC::operator==(const SCC& o) const{ return id == o.id;