Add method that identifies if a vertex is part of this component
This commit is contained in:
22
graph/scc.h
22
graph/scc.h
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
#include "digraph.h"
|
#include "digraph.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -14,27 +17,36 @@ public:
|
|||||||
|
|
||||||
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
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;
|
T id;
|
||||||
|
|
||||||
bool operator==(const SCC& o) const;
|
bool operator==(const SCC& o) const;
|
||||||
private:
|
private:
|
||||||
// For every vertex in the SCC, if there is an edge between an in-scc vertex
|
// Erase all edges that include vertices outside this SCC
|
||||||
// and an out-scc vertex erase the edge between them and the out-scc vertex
|
|
||||||
void normalize();
|
void normalize();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void SCC<T>::normalize() {
|
void SCC<T>::normalize() {
|
||||||
for (const auto& u : this->adjMatrix) {
|
for (const auto& u : this->vertices()) {
|
||||||
for (const auto& v : u.second) {
|
for (const auto& v : this->adjMatrix[u]) {
|
||||||
if (!this->adjMatrix.count(v)) {
|
if (!this->adjMatrix.count(v)) {
|
||||||
this->adjMatrix[u.first].erase(v);
|
this->adjMatrix[u].erase(v);
|
||||||
this->adjMatrix.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>
|
template<typename T>
|
||||||
bool SCC<T>::operator==(const SCC& o) const{
|
bool SCC<T>::operator==(const SCC& o) const{
|
||||||
return id == o.id;
|
return id == o.id;
|
||||||
|
|||||||
Reference in New Issue
Block a user