65 lines
1.3 KiB
C++
65 lines
1.3 KiB
C++
#ifndef DIGRAPH_H_
|
|
#define DIGRAPH_H_
|
|
|
|
#include "graph.h"
|
|
#include "algorithm/breadth_first_search.h"
|
|
|
|
namespace graph {
|
|
|
|
template<typename T>
|
|
class Digraph : public Graph<T> {
|
|
public:
|
|
Digraph() = default;
|
|
|
|
Digraph(std::unordered_map<T, std::unordered_set<T>> G);
|
|
|
|
// Return true if there is a path from u to v
|
|
bool contains(const T& u, const T& v);
|
|
|
|
// Add edge e(u,v)
|
|
void insert(const T& u, const T& v);
|
|
|
|
// Remove edge e(u,v)
|
|
void remove(const T& u, const T& v);
|
|
|
|
// Reverse graph directions
|
|
auto reverse();
|
|
};
|
|
|
|
template<typename T>
|
|
Digraph<T>::Digraph(std::unordered_map<T, std::unordered_set<T>> G) {
|
|
this->adjList = G;
|
|
}
|
|
|
|
template<typename T>
|
|
bool Digraph<T>::contains(const T& u, const T& v) {
|
|
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
|
|
}
|
|
|
|
template<typename T>
|
|
void Digraph<T>::insert(const T& u, const T& v) {
|
|
this->adjList[u].insert(v);
|
|
this->adjList[v];
|
|
}
|
|
|
|
template<typename T>
|
|
void Digraph<T>::remove(const T& u, const T& v) {
|
|
this->adjList[u].erase(v);
|
|
}
|
|
|
|
template<typename T>
|
|
auto Digraph<T>::reverse() {
|
|
std::unordered_map<T, std::unordered_set<T>> revMatrix;
|
|
|
|
for (const auto& u : this->vertices()) {
|
|
for (const auto& v : this->adjList[u]) {
|
|
revMatrix[v].insert(u);
|
|
}
|
|
}
|
|
|
|
return revMatrix;
|
|
}
|
|
|
|
} // namespace graph
|
|
|
|
#endif |