137 lines
2.9 KiB
C++
137 lines
2.9 KiB
C++
#include <doctest/doctest.h>
|
|
|
|
#include "graph/digraph.h"
|
|
#include "graph/scc.h"
|
|
#include "algorithm/tarjan.h"
|
|
#include "algorithm/bfs.h"
|
|
#include "algorithm/roditty_zwick.h"
|
|
#include "algorithm/decremental_maintenance.h"
|
|
|
|
using namespace graph;
|
|
|
|
TEST_SUITE("Algorithm") {
|
|
|
|
TEST_CASE("Tarjan T1") {
|
|
// 1 --> 2 --> 4 --> 1
|
|
// 2 --> 3 --> 5 --> 7 --> 3
|
|
// 5 --> 9 --> 6 --> 8 --> 9
|
|
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);
|
|
|
|
REQUIRE_EQ(G.vertices.size(), 9);
|
|
|
|
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
auto res = tarjan.run();
|
|
|
|
std::vector<std::vector<std::uint16_t>> exp = {
|
|
{6, 8, 9},
|
|
{3, 5, 7},
|
|
{1, 2, 4}
|
|
};
|
|
|
|
for (auto i = 0; i < res.size(); i++) {
|
|
auto kv = std::views::keys(res[i].adjMatrix);
|
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
|
|
true);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Tarjan T2") {
|
|
// 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);
|
|
|
|
REQUIRE_EQ(G.vertices.size(), 7);
|
|
|
|
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
auto res = tarjan.run();
|
|
|
|
std::vector<std::vector<std::uint16_t>> exp = {
|
|
{2, 5, 7},
|
|
{1, 3, 4, 6}
|
|
};
|
|
|
|
for (auto i = 0; i < res.size(); i++) {
|
|
auto kv = std::views::keys(res[i].adjMatrix);
|
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
|
|
true);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("BFS 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::BFS<std::uint16_t> bfs(G);
|
|
auto tree = bfs.run(1);
|
|
|
|
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
|
{1, {2, 4}},
|
|
{2, {5}},
|
|
{3, {}},
|
|
{4, {3, 6}},
|
|
{5, {7}},
|
|
{6, {}},
|
|
{7, {}}
|
|
};*/
|
|
|
|
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);
|
|
}
|
|
|
|
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::DecrementalMaintenance<std::uint16_t> rz(G);
|
|
rz.init();
|
|
}
|
|
} |