67 lines
1.3 KiB
C++
67 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::map<T, std::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::map<T, std::set<T>> G) {
|
|
this->adjMatrix = G;
|
|
}
|
|
|
|
template<typename T>
|
|
bool Digraph<T>::contains(const T& u, const T& v) {
|
|
return algo::BreadthFirstSearch<T>().query(u, v);
|
|
}
|
|
|
|
template<typename T>
|
|
void Digraph<T>::insert(const T& u, const T& v) {
|
|
this->adjMatrix[u].insert(v);
|
|
this->adjMatrix[v];
|
|
}
|
|
|
|
template<typename T>
|
|
void Digraph<T>::remove(const T& u, const T& v) {
|
|
this->adjMatrix[u].erase(v);
|
|
//if (this->adjMatrix[u].size() == 0)
|
|
// this->adjMatrix.erase(u);
|
|
}
|
|
|
|
template<typename T>
|
|
auto Digraph<T>::reverse() {
|
|
std::map<T, std::set<T>> revMatrix;
|
|
|
|
for (const auto& u : this->adjMatrix) {
|
|
for (const auto& v : u.second) {
|
|
revMatrix[v].insert(u.first);
|
|
}
|
|
}
|
|
|
|
return revMatrix;
|
|
}
|
|
|
|
} // namespace graph
|
|
|
|
#endif |