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