Add operator<< for graph types, add contains query for digraph types and normalize scc
This commit is contained in:
@@ -2,9 +2,7 @@
|
||||
#define DIGRAPH_H_
|
||||
|
||||
#include "graph.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include "algorithm/breadth_first_search.h"
|
||||
|
||||
namespace graph {
|
||||
|
||||
@@ -13,24 +11,49 @@ class Digraph : public Graph<T> {
|
||||
public:
|
||||
Digraph() = default;
|
||||
|
||||
Digraph(std::map<T, std::set<T>> digraph);
|
||||
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>> digraph) {
|
||||
Graph<T>::adjMatrix = digraph;
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
for (const auto& u : this->adjMatrix) {
|
||||
for (const auto& v : u.second) {
|
||||
revMatrix[v].insert(u.first);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user