Files
reachability-algorithms/graph/digraph.h
2022-07-13 23:04:59 +03:00

44 lines
799 B
C++

#ifndef DIGRAPH_H_
#define DIGRAPH_H_
#include "graph.h"
#include <algorithm>
#include <ranges>
namespace graph {
template<typename T>
class Digraph : public Graph<T> {
public:
Digraph() = default;
Digraph(std::map<T, std::set<T>> digraph);
// Reverse graph directions
auto reverse();
};
template<typename T>
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
Graph<T>::adjMatrix = digraph;
auto kv = std::views::keys(Graph<T>::adjMatrix);
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
}
template<typename T>
auto Digraph<T>::reverse() {
std::map<T, std::set<T>> revMatrix;
for (const auto& v : Graph<T>::adjMatrix) {
for (const auto& u : v.second) {
revMatrix[u].insert(v.first);
}
}
return revMatrix;
}
} // namespace graph
#endif