Add contains method for single vertex and hash function for SCCs
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user