Move headers into include folder and add single header to include all reachability algorithms

This commit is contained in:
stefiosif
2022-10-14 18:38:01 +03:00
parent dc7fa93a6a
commit 92cf8950c2
14 changed files with 11 additions and 2 deletions

58
include/graph/scc.h Normal file
View 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