Add operator<< for graph types, add contains query for digraph types and normalize scc
This commit is contained in:
28
graph/breadth_first_tree.h
Normal file
28
graph/breadth_first_tree.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef BREADTH_FIRST_TREE_H_
|
||||
#define BREADTH_FIRST_TREE_H_
|
||||
|
||||
#include "digraph.h"
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
class BreadthFirstTree : public Digraph<T> {
|
||||
public:
|
||||
BreadthFirstTree() = default;
|
||||
|
||||
BreadthFirstTree(std::map<T, std::set<T>> G, T root)
|
||||
: BreadthFirstTree<T>(Digraph<T>(G), root) {}
|
||||
|
||||
BreadthFirstTree(Digraph<T> G, T root);
|
||||
|
||||
T root;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
||||
this->adjMatrix = algo::BreadthFirstSearch<T>(G.adjMatrix).execute(root);
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
#endif
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,38 +3,38 @@
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
namespace graph {
|
||||
|
||||
// Forward declerations
|
||||
template<typename T> class Graph;
|
||||
template<typename T> std::ostream& operator<<(std::ostream& os, Graph<T>& G);
|
||||
|
||||
template<typename T>
|
||||
class Graph {
|
||||
public:
|
||||
~Graph();
|
||||
|
||||
// Return true if there is a path from u to v
|
||||
virtual bool contains(const T& u, const T& v) = 0;
|
||||
|
||||
// Add edge e(u,v)
|
||||
virtual void insert(const T& u, const T& v);
|
||||
|
||||
// Return true if e(u,v) exists
|
||||
virtual bool contains(const T& u, const T& v);
|
||||
|
||||
// Return true if vertex u exists
|
||||
virtual bool contains(const T& u);
|
||||
virtual void insert(const T& u, const T& v) = 0;
|
||||
|
||||
// Remove edge e(u,v)
|
||||
virtual void remove(const T& u, const T& v);
|
||||
virtual void remove(const T& u, const T& v) = 0;
|
||||
|
||||
// Return num. of vertices
|
||||
virtual std::uint16_t V();
|
||||
std::uint16_t V();
|
||||
|
||||
// Return num. of edges
|
||||
virtual std::uint16_t E();
|
||||
|
||||
// Output graph
|
||||
virtual void output();
|
||||
std::uint16_t E();
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
|
||||
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -42,27 +42,6 @@ Graph<T>::~Graph() {
|
||||
adjMatrix.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::insert(const T& u, const T& v) {
|
||||
Graph<T>::adjMatrix[u].insert(v);
|
||||
Graph<T>::adjMatrix[v];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Graph<T>::contains(const T& u, const T& v) {
|
||||
return adjMatrix[u].contains(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Graph<T>::contains(const T& u) {
|
||||
return adjMatrix.contains(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::remove(const T& u, const T& v) {
|
||||
adjMatrix[u].erase(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||
@@ -78,14 +57,18 @@ std::uint16_t Graph<T>::E() {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::output() {
|
||||
for (const auto& v : adjMatrix) {
|
||||
for (const auto& u : v.second) {
|
||||
std::cout << v.first << "->" << u << '|';
|
||||
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
|
||||
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
||||
for (const auto& u : G.adjMatrix) {
|
||||
if (!u.second.empty()) {
|
||||
for (const auto& v : u.second) {
|
||||
os << u.first << "->" << v << ' ';
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
30
graph/scc.h
30
graph/scc.h
@@ -3,8 +3,6 @@
|
||||
|
||||
#include "digraph.h"
|
||||
|
||||
#include <ranges>
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
@@ -12,22 +10,34 @@ class SCC : public Digraph<T> {
|
||||
public:
|
||||
SCC() = default;
|
||||
|
||||
SCC(std::map<T, std::set<T>> scc)
|
||||
: Digraph<T>::Digraph(scc) { proxy = scc.begin()->first; }
|
||||
SCC(std::map<T, std::set<T>> G, T id) : Digraph<T>(G), id(id) { normalize(); }
|
||||
|
||||
//
|
||||
T representative() { return proxy; };
|
||||
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
||||
|
||||
T id;
|
||||
|
||||
bool operator==(const SCC& o) const;
|
||||
private:
|
||||
// Each SCC has a representative vertex that helps
|
||||
// answer strong connectivity queries in O(1) time
|
||||
T proxy;
|
||||
// For every vertex in the SCC, if there is an edge between an in-scc vertex
|
||||
// and an out-scc vertex erase the edge between them and the out-scc vertex
|
||||
void normalize();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void SCC<T>::normalize() {
|
||||
for (const auto& u : this->adjMatrix) {
|
||||
for (const auto& v : u.second) {
|
||||
if (!this->adjMatrix.count(v)) {
|
||||
this->adjMatrix[u.first].erase(v);
|
||||
this->adjMatrix.erase(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SCC<T>::operator==(const SCC& o) const{
|
||||
return proxy == o.proxy;
|
||||
return id == o.id;
|
||||
}
|
||||
|
||||
}; // namespace graph
|
||||
|
||||
Reference in New Issue
Block a user