Add method that identifies if a vertex is part of this component

This commit is contained in:
stefiosif
2022-09-21 19:32:00 +03:00
parent 7345a28945
commit 5f9b225609

View File

@@ -3,6 +3,9 @@
#include "digraph.h"
#include <algorithm>
#include <functional>
namespace graph {
template<typename T>
@@ -14,27 +17,36 @@ public:
SCC(Digraph<T> 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<typename T>
void SCC<T>::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<typename T>
bool SCC<T>::member(const T& v) {
const auto& V = this->vertices();
return std::find(V.begin(), V.end(), v) != V.end();
}
template<typename T>
bool SCC<T>::operator==(const SCC& o) const{
return id == o.id;