Add new graph example

This commit is contained in:
stefiosif
2022-07-12 14:44:43 +03:00
parent 601f80c8d4
commit 0f857a6c9e
4 changed files with 105 additions and 46 deletions

View File

@@ -1,10 +1,7 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include "graph/digraph.h"
#include "graph/scc.h"
#include "algorithm/tarjan.h" #include "algorithm/tarjan.h"
#include "algorithm/bfs.h" #include "algorithm/breadth_first_search.h"
#include "algorithm/decremental_scc.h"
using namespace graph; using namespace graph;
@@ -30,7 +27,7 @@ TEST_SUITE("Algorithm") {
REQUIRE_EQ(G.vertices.size(), 9); REQUIRE_EQ(G.vertices.size(), 9);
algo::Tarjan<std::uint16_t> tarjan(G); algo::Tarjan<std::uint16_t> tarjan(G);
auto res = tarjan.findSCC(); auto res = tarjan.execute();
std::vector<std::vector<std::uint16_t>> exp = { std::vector<std::vector<std::uint16_t>> exp = {
{6, 8, 9}, {6, 8, 9},
@@ -63,7 +60,7 @@ TEST_SUITE("Algorithm") {
REQUIRE_EQ(G.vertices.size(), 7); REQUIRE_EQ(G.vertices.size(), 7);
algo::Tarjan<std::uint16_t> tarjan(G); algo::Tarjan<std::uint16_t> tarjan(G);
auto res = tarjan.findSCC(); auto res = tarjan.execute();
std::vector<std::vector<std::uint16_t>> exp = { std::vector<std::vector<std::uint16_t>> exp = {
{2, 5, 7}, {2, 5, 7},
@@ -92,8 +89,8 @@ TEST_SUITE("Algorithm") {
G.insert(6, 3); G.insert(6, 3);
G.insert(7, 2); G.insert(7, 2);
algo::BFS<std::uint16_t> bfs(G); algo::BreadthFirstSearch<std::uint16_t> bfs(G);
auto tree = bfs.run(1); auto tree = bfs.execute(1);
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = { /*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
{1, {2, 4}}, {1, {2, 4}},
@@ -115,36 +112,31 @@ TEST_SUITE("Algorithm") {
CHECK_EQ(tree, exp); CHECK_EQ(tree, exp);
} }
TEST_CASE("Roditty Zwick A1 T1 ") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1 TEST_CASE("BFS") {
// 4 --> 6 --> 3 // 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 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(1, 4); G.insert(2, 3);
G.insert(2, 5);
G.insert(3, 1); G.insert(3, 1);
G.insert(4, 3); G.insert(3, 4);
G.insert(4, 6); G.insert(4, 5);
G.insert(5, 7); G.insert(5, 3);
G.insert(6, 3); G.insert(2, 6);
G.insert(7, 2); G.insert(6, 7);
G.insert(7, 8);
algo::DecrementalSCC<std::uint16_t> rz(G); G.insert(8, 6);
rz.init(); G.insert(6, 9);
G.insert(9, 10);
std::vector<std::vector<std::uint16_t>> exp = { G.insert(10, 11);
{2, 5, 7}, G.insert(11, 12);
{1, 3, 4, 6} G.insert(12, 13);
}; G.insert(13, 9);
G.insert(12, 10);
CHECK_EQ(rz.query(1, 2), false);
CHECK_EQ(rz.query(1, 3), true);
CHECK_EQ(rz.query(1, 7), false);
CHECK_EQ(rz.query(2, 3), false);
CHECK_EQ(rz.query(2, 5), true);
CHECK_EQ(rz.query(5, 7), true);
//CHECK_EQ(rz.query(1, 2), true);
} }
} }

View File

@@ -1,7 +1,6 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include "graph/scc.h" #include "graph/scc.h"
#include "graph/digraph.h"
#include "algorithm/tarjan.h" #include "algorithm/tarjan.h"
#include <vector> #include <vector>
@@ -44,7 +43,6 @@ TEST_SUITE("Graph") {
}; };
CHECK_EQ(RG.adjMatrix, exp); CHECK_EQ(RG.adjMatrix, exp);
} }
TEST_CASE("SCC T1") { TEST_CASE("SCC T1") {
@@ -67,7 +65,7 @@ TEST_SUITE("Graph") {
REQUIRE_EQ(G.vertices.size(), 9); REQUIRE_EQ(G.vertices.size(), 9);
algo::Tarjan<std::uint16_t> tarjan(G); algo::Tarjan<std::uint16_t> tarjan(G);
auto sccs = tarjan.findSCC(); auto sccs = tarjan.execute();
} }
TEST_CASE("SCC T2") { TEST_CASE("SCC T2") {
@@ -88,6 +86,37 @@ TEST_SUITE("Graph") {
REQUIRE_EQ(G.vertices.size(), 7); REQUIRE_EQ(G.vertices.size(), 7);
algo::Tarjan<std::uint16_t> tarjan(G); algo::Tarjan<std::uint16_t> tarjan(G);
auto sccs = tarjan.findSCC(); auto sccs = tarjan.execute();
}
TEST_CASE("SCC T3") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(2, 3);
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(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.vertices.size(), 13);
algo::Tarjan<std::uint16_t> tarjan(G);
auto sccs = tarjan.execute();
} }
} }

View File

@@ -0,0 +1,43 @@
#include <doctest/doctest.h>
#include "algorithm/decremental_scc.h"
#include "algorithm/decremental_tc.h"
using namespace graph;
TEST_SUITE("Roditty and Zwick") {
TEST_CASE("Roditty Zwick A1 T1 ") {
// 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);
algo::DecrementalSCC<std::uint16_t> rz(G);
rz.init();
std::vector<std::vector<std::uint16_t>> exp = {
{2, 5, 7},
{1, 3, 4, 6}
};
CHECK_EQ(rz.query(1, 2), false);
CHECK_EQ(rz.query(1, 3), true);
CHECK_EQ(rz.query(1, 7), false);
CHECK_EQ(rz.query(2, 3), false);
CHECK_EQ(rz.query(2, 5), true);
CHECK_EQ(rz.query(5, 7), true);
//CHECK_EQ(rz.query(1, 2), true);
}
}

View File

@@ -1,12 +1,7 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include "graph/scc.h"
#include "graph/digraph.h"
#include "algorithm/tarjan.h"
#include "tree/breadth_first_tree.h" #include "tree/breadth_first_tree.h"
#include <vector>
using namespace graph; using namespace graph;
using namespace tree; using namespace tree;
@@ -27,7 +22,7 @@ TEST_SUITE("Tree") {
G.insert(6, 3); G.insert(6, 3);
G.insert(7, 2); G.insert(7, 2);
BreadthFirstTree<std::uint16_t> tree(G); BreadthFirstTree<std::uint16_t> tree(G, 1);
std::map<std::uint16_t, std::set<std::uint16_t>> exp = { std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
{1, {2, 4}}, {1, {2, 4}},