Make class Graph abstract and make derived classes Digraph and SCC
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include "graph/graph.h"
|
||||
using namespace graph;
|
||||
#include "graph/digraph.h"
|
||||
#include "graph/scc.h"
|
||||
#include "algorithm/tarjan.h"
|
||||
#include "algorithm/bfs.h"
|
||||
|
||||
using namespace graph;
|
||||
|
||||
TEST_SUITE("Algorithm.") {
|
||||
|
||||
TEST_CASE("Tarjan T_1") {
|
||||
// 1 --> 2 --> 4 --> 1
|
||||
// 2 --> 3 --> 5 --> 7 --> 3
|
||||
// 5 --> 9 --> 6 --> 8 --> 9
|
||||
Graph<std::uint16_t> G;
|
||||
Digraph<std::uint16_t> G;
|
||||
G.insert(1, 2);
|
||||
G.insert(2, 3);
|
||||
G.insert(2, 4);
|
||||
@@ -28,26 +30,23 @@ TEST_SUITE("Algorithm.") {
|
||||
|
||||
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());
|
||||
});
|
||||
|
||||
for (auto& scc : result) {
|
||||
scc.output();
|
||||
}
|
||||
|
||||
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") {
|
||||
// 1 --> 2 --> 5 --> 7 --> 2
|
||||
// 1 --> 4 --> 3 --> 1
|
||||
// 4 --> 6 --> 3
|
||||
Graph<std::uint16_t> G;
|
||||
Digraph<std::uint16_t> G;
|
||||
G.insert(1, 2);
|
||||
G.insert(1, 4);
|
||||
G.insert(2, 5);
|
||||
@@ -63,24 +62,17 @@ TEST_SUITE("Algorithm.") {
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("BFS T_1") {
|
||||
// 1 --> 2 --> 5 --> 7 --> 2
|
||||
// 1 --> 4 --> 3 --> 1
|
||||
// 4 --> 6 --> 3
|
||||
Graph<std::uint16_t> G;
|
||||
Digraph<std::uint16_t> G;
|
||||
G.insert(1, 2);
|
||||
G.insert(1, 4);
|
||||
G.insert(2, 5);
|
||||
|
||||
Reference in New Issue
Block a user