57 lines
1.1 KiB
C++
57 lines
1.1 KiB
C++
#ifndef SCC_H_
|
|
#define SCC_H_
|
|
|
|
#include "digraph.h"
|
|
|
|
#include <algorithm>
|
|
#include <functional>
|
|
|
|
namespace graph {
|
|
|
|
template<typename T>
|
|
class SCC : public Digraph<T> {
|
|
public:
|
|
SCC() = default;
|
|
|
|
SCC(std::map<T, std::set<T>> G, T id) : Digraph<T>(G), 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;
|
|
|
|
bool operator==(const SCC& o) const;
|
|
private:
|
|
// Erase all edges that include vertices outside this SCC
|
|
void normalize();
|
|
};
|
|
|
|
template<typename T>
|
|
void SCC<T>::normalize() {
|
|
for (const auto& u : this->vertices()) {
|
|
for (const auto& v : this->adjList[u]) {
|
|
if (!this->adjList.count(v)) {
|
|
this->adjList[u].erase(v);
|
|
this->adjList.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;
|
|
}
|
|
|
|
}; // namespace graph
|
|
|
|
#endif |