Add operator<< for graph types, add contains query for digraph types and normalize scc

This commit is contained in:
stefiosif
2022-09-09 17:50:28 +03:00
parent 4d189f269c
commit a4ddc3fbe7
9 changed files with 144 additions and 189 deletions

View File

@@ -3,8 +3,6 @@
#include "digraph.h"
#include <ranges>
namespace graph {
template<typename T>
@@ -12,22 +10,34 @@ class SCC : public Digraph<T> {
public:
SCC() = default;
SCC(std::map<T, std::set<T>> scc)
: Digraph<T>::Digraph(scc) { proxy = scc.begin()->first; }
SCC(std::map<T, std::set<T>> G, T id) : Digraph<T>(G), id(id) { normalize(); }
//
T representative() { return proxy; };
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
T id;
bool operator==(const SCC& o) const;
private:
// Each SCC has a representative vertex that helps
// answer strong connectivity queries in O(1) time
T proxy;
// 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
void normalize();
};
template<typename T>
void SCC<T>::normalize() {
for (const auto& u : this->adjMatrix) {
for (const auto& v : u.second) {
if (!this->adjMatrix.count(v)) {
this->adjMatrix[u.first].erase(v);
this->adjMatrix.erase(v);
}
}
}
}
template<typename T>
bool SCC<T>::operator==(const SCC& o) const{
return proxy == o.proxy;
return id == o.id;
}
}; // namespace graph