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

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