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

@@ -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

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

View File

@@ -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 << ' ';
}
std::cout << '\n';
os << '\n';
}
std::cout << '\n';
}
os << '\n';
return os;
}
} // namespace graph

View File

@@ -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

View File

@@ -10,48 +10,63 @@ using namespace graph;
TEST_SUITE("Graph") {
TEST_CASE("Digraph::reverse") {
// 1 --> 2 --> 4 --> 1
// 2 --> 3 --> 5 --> 7 --> 3
// 5 --> 9 --> 6 --> 8 --> 9
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 7 --> 9
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(2, 3);
G.insert(2, 4);
G.insert(3, 5);
G.insert(4, 1);
G.insert(5, 7);
G.insert(5, 9);
G.insert(6, 8);
G.insert(7, 3);
G.insert(8, 9);
G.insert(9, 6);
G.insert(3, 1);
G.insert(3, 4);
G.insert(4, 5);
G.insert(5, 3);
G.insert(2, 6);
G.insert(6, 7);
G.insert(7, 8);
G.insert(7, 9);
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 = {
{1, {4}},
std::map<std::uint16_t, std::set<std::uint16_t>> expected = {
{1, {3}},
{2, {1}},
{3, {2, 7}},
{4, {2}},
{5, {3}},
{6, {9}},
{7, {5}},
{8, {6}},
{9, {5, 8}}
{3, {2, 5}},
{4, {3}},
{5, {4}},
{6, {2, 8}},
{7, {6}},
{8, {7}},
{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") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 7 --> 9
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Graph<std::uint16_t> G;
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(2, 3);
G.insert(3, 1);

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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