Move headers into include folder and add single header to include all reachability algorithms

This commit is contained in:
stefiosif
2022-10-14 18:38:01 +03:00
parent dc7fa93a6a
commit 92cf8950c2
14 changed files with 11 additions and 2 deletions

View 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::unordered_map<T, std::unordered_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->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
}
} // namespace graph
#endif

65
include/graph/digraph.h Normal file
View File

@@ -0,0 +1,65 @@
#ifndef DIGRAPH_H_
#define DIGRAPH_H_
#include "graph.h"
#include "algorithm/breadth_first_search.h"
namespace graph {
template<typename T>
class Digraph : public Graph<T> {
public:
Digraph() = default;
Digraph(std::unordered_map<T, std::unordered_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::unordered_map<T, std::unordered_set<T>> G) {
this->adjList = G;
}
template<typename T>
bool Digraph<T>::contains(const T& u, const T& v) {
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
}
template<typename T>
void Digraph<T>::insert(const T& u, const T& v) {
this->adjList[u].insert(v);
this->adjList[v];
}
template<typename T>
void Digraph<T>::remove(const T& u, const T& v) {
this->adjList[u].erase(v);
}
template<typename T>
auto Digraph<T>::reverse() {
std::unordered_map<T, std::unordered_set<T>> revMatrix;
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjList[u]) {
revMatrix[v].insert(u);
}
}
return revMatrix;
}
} // namespace graph
#endif

86
include/graph/graph.h Normal file
View File

@@ -0,0 +1,86 @@
#ifndef GRAPH_H_
#define GRAPH_H_
#include <unordered_map>
#include <unordered_set>
#include <ostream>
#include <ranges>
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) =0;
// Remove edge e(u,v)
virtual void remove(const T& u, const T& v) =0;
// Return graph vertices
auto vertices();
// Return num. of vertices
std::uint16_t V();
// Return num. of edges
std::uint16_t E();
// Adjacency matrix representation
std::unordered_map<T, std::unordered_set<T>> adjList;
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
};
template<typename T>
Graph<T>::~Graph() {
adjList.clear();
}
template<typename T>
auto Graph<T>::vertices() {
return std::views::keys(adjList);
}
template<typename T>
std::uint16_t Graph<T>::V() {
return static_cast<std::uint16_t>(adjList.size());
}
template<typename T>
std::uint16_t Graph<T>::E() {
std::uint16_t edges = 0;
for (const auto& u : vertices()) {
edges += static_cast<std::uint16_t>(adjList[u].size());
}
return static_cast<std::uint16_t>(edges);
}
template<typename T>
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
os << "V: " << G.V() << " E: " << G.E() << '\n';
for (const auto& u : this->vertices()) {
if (!this->adjList[u].empty()) {
for (const auto& v : this->adjList[u]) {
os << u << "->" << v << ' ';
}
os << '\n';
}
}
os << '\n';
return os;
}
} // namespace graph
#endif

58
include/graph/scc.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef SCC_H_
#define SCC_H_
#include "digraph.h"
#include <algorithm>
#include <functional>
namespace graph {
template<typename T>
class SCC : public Digraph<T> {
public:
SCC() = default;
SCC(std::unordered_map<T, std::unordered_set<T>> G, T id)
: Digraph<T>(G), id(id) { normalize(); }
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
// Return true if v is part of this SCC
bool member(const T& v);
// Representative vertex of this SCC
T id;
bool operator==(const SCC& o) const;
private:
// Erase all edges that include vertices outside this SCC
void normalize();
};
template<typename T>
void SCC<T>::normalize() {
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjList[u]) {
if (!this->adjList.count(v)) {
this->adjList[u].erase(v);
this->adjList.erase(v);
}
}
}
}
template<typename T>
bool SCC<T>::member(const T& v) {
const auto& V = this->vertices();
return std::find(V.begin(), V.end(), v) != V.end();
}
template<typename T>
bool SCC<T>::operator==(const SCC& o) const{
return id == o.id;
}
}; // namespace graph
#endif