Move headers into include folder and add single header to include all reachability algorithms
This commit is contained in:
58
include/graph/scc.h
Normal file
58
include/graph/scc.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#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::unordered_map<T, std::unordered_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
|
||||
Reference in New Issue
Block a user