Finish Tarjan algorithm and add tests
This commit is contained in:
73
test/algorithm_test.cc
Normal file
73
test/algorithm_test.cc
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
#include "algorithm/tarjan.h"
|
||||
|
||||
TEST_SUITE("Algorithms") {
|
||||
|
||||
TEST_CASE("Tarjan T_1") {
|
||||
|
||||
Graph<std::uint16_t> G;
|
||||
G.insert(1, 2);
|
||||
G.insert(2, 4);
|
||||
G.insert(2, 3);
|
||||
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 result = tarjan.run();
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
std::for_each(result.begin(), result.end(), [&](std::vector<std::uint16_t>& row) {
|
||||
std::sort(row.begin(), row.end());
|
||||
});
|
||||
|
||||
std::vector<std::vector<std::uint16_t>> expected{
|
||||
{1, 2, 4},
|
||||
{3, 5, 7},
|
||||
{6, 8, 9}
|
||||
};
|
||||
|
||||
CHECK_EQ(expected, result);
|
||||
}
|
||||
|
||||
TEST_CASE("Tarjan T_2") {
|
||||
|
||||
Graph<std::uint16_t> G;
|
||||
G.insert(1, 4);
|
||||
G.insert(1, 2);
|
||||
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 result = tarjan.run();
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
std::for_each(result.begin(), result.end(), [&](std::vector<std::uint16_t>& row) {
|
||||
std::sort(row.begin(), row.end());
|
||||
});
|
||||
|
||||
std::vector<std::vector<std::uint16_t>> expected{
|
||||
{1, 3, 4, 6},
|
||||
{2, 5, 7}
|
||||
};
|
||||
|
||||
CHECK_EQ(expected, result);
|
||||
}
|
||||
}
|
||||
@@ -2,33 +2,22 @@
|
||||
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
#include "algorithm/tarjan.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
TEST_SUITE("Graph test.") {
|
||||
|
||||
TEST_SUITE("Testing Graph.") {
|
||||
|
||||
TEST_CASE("Insert vertices/edges.") {
|
||||
TEST_CASE("Insertion.") {
|
||||
Graph<std::uint16_t> G;
|
||||
//G.insert(1);
|
||||
//G.insert(1, 2);
|
||||
//G.insert(1, 4);
|
||||
//G.insert(1, 6);
|
||||
//G.insert(2, 4);
|
||||
//G.insert(2, 5);
|
||||
//G.insert(3, 4);
|
||||
//G.insert(3, 6);
|
||||
//G.insert(4, 6);
|
||||
//G.insert(5, 6);
|
||||
|
||||
G.insert(1, 2);
|
||||
G.insert(1, 4);
|
||||
G.insert(1, 6);
|
||||
G.insert(2, 4);
|
||||
G.insert(2, 5);
|
||||
G.insert(3, 4);
|
||||
G.insert(3, 6);
|
||||
G.insert(4, 6);
|
||||
G.insert(5, 6);
|
||||
|
||||
Vertex<std::uint16_t> v1(1);
|
||||
Vertex<std::uint16_t> v2(3);
|
||||
Vertex<std::uint16_t> v3(5);
|
||||
algorithm::Tarjan<std::uint16_t> tarjan(G);
|
||||
tarjan.findSCC();
|
||||
|
||||
// CHECK_EQ(G.connected(1, 5), true);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user