Add contains method for single vertex and hash function for SCCs

This commit is contained in:
stefiosif
2023-02-10 18:20:00 +02:00
parent 54eee41f7d
commit 2175ac0ca9
3 changed files with 47 additions and 25 deletions

View File

@@ -18,11 +18,14 @@ public:
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
// Return true if v is part of this SCC
bool member(const T& v);
// Return true if u is part of this SCC
bool contains(const T& u) const;
// Representative vertex of this SCC
T id;
T id{};
//
std::unordered_map<T, std::unordered_set<T>> neighboorList;
bool operator==(const SCC& o) const;
private:
@@ -34,25 +37,32 @@ 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)) {
if (!this->contains(v)) {
this->adjList[u].erase(v);
this->adjList.erase(v);
neighboorList[u].insert(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();
bool SCC<T>::contains(const T& u) const {
return this->adjList.count(u);
}
template<typename T>
bool SCC<T>::operator==(const SCC& o) const{
bool SCC<T>::operator==(const SCC& o) const {
return id == o.id;
}
template<typename T>
struct HashSCC {
std::size_t operator()(const SCC<T>& C) const {
return static_cast<std::size_t>(C.id);
}
};
}; // namespace graph
#endif