Add operator<< for graph types, add contains query for digraph types and normalize scc

This commit is contained in:
stefiosif
2022-09-09 17:50:28 +03:00
parent 4d189f269c
commit a4ddc3fbe7
9 changed files with 144 additions and 189 deletions

View File

@@ -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);
}
}