Files
reachability-algorithms/graph/digraph.h

75 lines
1.4 KiB
C++

#ifndef DIGRAPH_H_
#define DIGRAPH_H_
#include "graph.h"
#include "algorithm/breadth_first_search.h"
#include <ranges>
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();
// Return vertices
auto vertices();
};
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>(this->adjMatrix).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);
}
template<typename T>
auto Digraph<T>::reverse() {
std::map<T, std::set<T>> revMatrix;
for (const auto& u : vertices()) {
for (const auto& v : this->adjMatrix[u]) {
revMatrix[v].insert(u);
}
}
return revMatrix;
}
template<typename T>
auto Digraph<T>::vertices() {
return std::views::keys(this->adjMatrix);
}
} // namespace graph
#endif