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_
|
#define DIGRAPH_H_
|
||||||
|
|
||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
|
#include "algorithm/breadth_first_search.h"
|
||||||
#include <algorithm>
|
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
@@ -13,24 +11,49 @@ class Digraph : public Graph<T> {
|
|||||||
public:
|
public:
|
||||||
Digraph() = default;
|
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
|
// Reverse graph directions
|
||||||
auto reverse();
|
auto reverse();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
|
Digraph<T>::Digraph(std::map<T, std::set<T>> G) {
|
||||||
Graph<T>::adjMatrix = digraph;
|
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>
|
template<typename T>
|
||||||
auto Digraph<T>::reverse() {
|
auto Digraph<T>::reverse() {
|
||||||
std::map<T, std::set<T>> revMatrix;
|
std::map<T, std::set<T>> revMatrix;
|
||||||
|
|
||||||
for (const auto& v : Graph<T>::adjMatrix) {
|
for (const auto& u : this->adjMatrix) {
|
||||||
for (const auto& u : v.second) {
|
for (const auto& v : u.second) {
|
||||||
revMatrix[u].insert(v.first);
|
revMatrix[v].insert(u.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,38 +3,38 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <iostream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
|
// Forward declerations
|
||||||
|
template<typename T> class Graph;
|
||||||
|
template<typename T> std::ostream& operator<<(std::ostream& os, Graph<T>& G);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Graph {
|
class Graph {
|
||||||
public:
|
public:
|
||||||
~Graph();
|
~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)
|
// Add edge e(u,v)
|
||||||
virtual void insert(const T& u, const T& v);
|
virtual void insert(const T& u, const T& v) = 0;
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// Remove edge e(u,v)
|
// 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
|
// Return num. of vertices
|
||||||
virtual std::uint16_t V();
|
std::uint16_t V();
|
||||||
|
|
||||||
// Return num. of edges
|
// Return num. of edges
|
||||||
virtual std::uint16_t E();
|
std::uint16_t E();
|
||||||
|
|
||||||
// Output graph
|
|
||||||
virtual void output();
|
|
||||||
|
|
||||||
// Adjacency matrix representation
|
// Adjacency matrix representation
|
||||||
std::map<T, std::set<T>> adjMatrix;
|
std::map<T, std::set<T>> adjMatrix;
|
||||||
|
|
||||||
|
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -42,27 +42,6 @@ Graph<T>::~Graph() {
|
|||||||
adjMatrix.clear();
|
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>
|
template<typename T>
|
||||||
std::uint16_t Graph<T>::V() {
|
std::uint16_t Graph<T>::V() {
|
||||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||||
@@ -78,14 +57,18 @@ std::uint16_t Graph<T>::E() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Graph<T>::output() {
|
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
|
||||||
for (const auto& v : adjMatrix) {
|
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
||||||
for (const auto& u : v.second) {
|
for (const auto& u : G.adjMatrix) {
|
||||||
std::cout << v.first << "->" << u << '|';
|
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
|
} // namespace graph
|
||||||
|
|||||||
30
graph/scc.h
30
graph/scc.h
@@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include "digraph.h"
|
#include "digraph.h"
|
||||||
|
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -12,22 +10,34 @@ class SCC : public Digraph<T> {
|
|||||||
public:
|
public:
|
||||||
SCC() = default;
|
SCC() = default;
|
||||||
|
|
||||||
SCC(std::map<T, std::set<T>> scc)
|
SCC(std::map<T, std::set<T>> G, T id) : Digraph<T>(G), id(id) { normalize(); }
|
||||||
: Digraph<T>::Digraph(scc) { proxy = scc.begin()->first; }
|
|
||||||
|
|
||||||
//
|
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
||||||
T representative() { return proxy; };
|
|
||||||
|
T id;
|
||||||
|
|
||||||
bool operator==(const SCC& o) const;
|
bool operator==(const SCC& o) const;
|
||||||
private:
|
private:
|
||||||
// Each SCC has a representative vertex that helps
|
// For every vertex in the SCC, if there is an edge between an in-scc vertex
|
||||||
// answer strong connectivity queries in O(1) time
|
// and an out-scc vertex erase the edge between them and the out-scc vertex
|
||||||
T proxy;
|
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>
|
template<typename T>
|
||||||
bool SCC<T>::operator==(const SCC& o) const{
|
bool SCC<T>::operator==(const SCC& o) const{
|
||||||
return proxy == o.proxy;
|
return id == o.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}; // namespace graph
|
}; // namespace graph
|
||||||
|
|||||||
@@ -10,48 +10,63 @@ using namespace graph;
|
|||||||
TEST_SUITE("Graph") {
|
TEST_SUITE("Graph") {
|
||||||
|
|
||||||
TEST_CASE("Digraph::reverse") {
|
TEST_CASE("Digraph::reverse") {
|
||||||
// 1 --> 2 --> 4 --> 1
|
// 1 --> 2 --> 3 --> 1
|
||||||
// 2 --> 3 --> 5 --> 7 --> 3
|
// 3 --> 4 --> 5 --> 3
|
||||||
// 5 --> 9 --> 6 --> 8 --> 9
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 7 --> 9
|
||||||
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
|
// 12 --> 10
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
G.insert(1, 2);
|
||||||
G.insert(2, 3);
|
G.insert(2, 3);
|
||||||
G.insert(2, 4);
|
G.insert(3, 1);
|
||||||
G.insert(3, 5);
|
G.insert(3, 4);
|
||||||
G.insert(4, 1);
|
G.insert(4, 5);
|
||||||
G.insert(5, 7);
|
G.insert(5, 3);
|
||||||
G.insert(5, 9);
|
G.insert(2, 6);
|
||||||
G.insert(6, 8);
|
G.insert(6, 7);
|
||||||
G.insert(7, 3);
|
G.insert(7, 8);
|
||||||
G.insert(8, 9);
|
G.insert(7, 9);
|
||||||
G.insert(9, 6);
|
G.insert(8, 6);
|
||||||
|
G.insert(6, 9);
|
||||||
|
G.insert(9, 10);
|
||||||
|
G.insert(10, 11);
|
||||||
|
G.insert(11, 12);
|
||||||
|
G.insert(12, 13);
|
||||||
|
G.insert(13, 9);
|
||||||
|
G.insert(12, 10);
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
||||||
|
|
||||||
auto R = G.reverse();
|
auto reverse = G.reverse();
|
||||||
|
|
||||||
std::map<std::uint16_t, std::set<std::uint16_t>> X = {
|
std::map<std::uint16_t, std::set<std::uint16_t>> expected = {
|
||||||
{1, {4}},
|
{1, {3}},
|
||||||
{2, {1}},
|
{2, {1}},
|
||||||
{3, {2, 7}},
|
{3, {2, 5}},
|
||||||
{4, {2}},
|
{4, {3}},
|
||||||
{5, {3}},
|
{5, {4}},
|
||||||
{6, {9}},
|
{6, {2, 8}},
|
||||||
{7, {5}},
|
{7, {6}},
|
||||||
{8, {6}},
|
{8, {7}},
|
||||||
{9, {5, 8}}
|
{9, {6, 7, 13}},
|
||||||
|
{10, {9, 12}},
|
||||||
|
{11, {10}},
|
||||||
|
{12, {11}},
|
||||||
|
{13, {12}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(R, X);
|
CHECK_EQ(reverse, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Graph::V and Graph::E") {
|
TEST_CASE("Graph::V and Graph::E") {
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 3 --> 1
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 3 --> 4 --> 5 --> 3
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 7 --> 9
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
// 12 --> 10
|
// 12 --> 10
|
||||||
Graph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
G.insert(1, 2);
|
||||||
G.insert(2, 3);
|
G.insert(2, 3);
|
||||||
G.insert(3, 1);
|
G.insert(3, 1);
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
#include <doctest/doctest.h>
|
|
||||||
|
|
||||||
#include "tree/breadth_first_tree.h"
|
|
||||||
|
|
||||||
using namespace graph;
|
|
||||||
using namespace tree;
|
|
||||||
|
|
||||||
TEST_SUITE("Tree") {
|
|
||||||
TEST_CASE("Breadth First Tree") {
|
|
||||||
|
|
||||||
// 1 --> 2 --> 5 --> 7 --> 2
|
|
||||||
// 1 --> 4 --> 3 --> 1
|
|
||||||
// 4 --> 6 --> 3
|
|
||||||
Digraph<std::uint16_t> G;
|
|
||||||
G.insert(1, 2);
|
|
||||||
G.insert(1, 4);
|
|
||||||
G.insert(2, 5);
|
|
||||||
G.insert(3, 1);
|
|
||||||
G.insert(4, 3);
|
|
||||||
G.insert(4, 6);
|
|
||||||
G.insert(5, 7);
|
|
||||||
G.insert(6, 3);
|
|
||||||
G.insert(7, 2);
|
|
||||||
|
|
||||||
BreadthFirstTree<std::uint16_t> tree(G, 1);
|
|
||||||
|
|
||||||
std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
|
||||||
{1, {2, 4}},
|
|
||||||
{2, {5}},
|
|
||||||
{4, {3, 6}},
|
|
||||||
{5, {7}}
|
|
||||||
};
|
|
||||||
|
|
||||||
CHECK_EQ(tree.adjMatrix, exp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#ifndef BREADTH_FIRST_TREE_H_
|
|
||||||
#define BREADTH_FIRST_TREE_H_
|
|
||||||
|
|
||||||
#include "directed_rooted_tree.h"
|
|
||||||
#include "algorithm/breadth_first_search.h"
|
|
||||||
using namespace graph;
|
|
||||||
|
|
||||||
namespace tree {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class BreadthFirstTree : public DirectedRootedTree<T> {
|
|
||||||
public:
|
|
||||||
BreadthFirstTree() = default;
|
|
||||||
|
|
||||||
BreadthFirstTree(Digraph<T> G, T root);
|
|
||||||
|
|
||||||
BreadthFirstTree(std::map<T, std::set<T>> G)
|
|
||||||
: DirectedRootedTree<T>::DirectedRootedTree(G) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
|
||||||
auto bfs = algo::BreadthFirstSearch<T>(G).execute(root);
|
|
||||||
this->adjMatrix = bfs;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace tree
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef DIRECTED_ROOTED_TREE_H_
|
|
||||||
#define DIRECTED_ROOTED_TREE_H_
|
|
||||||
|
|
||||||
#include "tree.h"
|
|
||||||
using namespace graph;
|
|
||||||
|
|
||||||
namespace tree {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class DirectedRootedTree : public Tree<T> {
|
|
||||||
public:
|
|
||||||
DirectedRootedTree() = default;
|
|
||||||
|
|
||||||
DirectedRootedTree(std::map<T, std::set<T>> G)
|
|
||||||
: Tree<T>::Tree(G) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace tree
|
|
||||||
|
|
||||||
#endif
|
|
||||||
19
tree/tree.h
19
tree/tree.h
@@ -1,19 +0,0 @@
|
|||||||
#ifndef TREE_H_
|
|
||||||
#define TREE_H_
|
|
||||||
|
|
||||||
#include "graph/digraph.h"
|
|
||||||
using namespace graph;
|
|
||||||
|
|
||||||
namespace tree {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class Tree : public Digraph<T> {
|
|
||||||
public:
|
|
||||||
Tree() = default;
|
|
||||||
|
|
||||||
Tree(std::map<T, std::set<T>> G) : Digraph<T>::Digraph(G) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace tree
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user