42#include <gtest/gtest.h>
53 return g.insert_node(
value);
59 return g.insert_arc(src, tgt,
value);
65TEST(TarjanTest, EmptyGraph)
72 EXPECT_TRUE(sccs.is_empty());
83 EXPECT_TRUE(tarjan.
is_dag(g));
97TEST(TarjanTest, SingleNodeNoLoop)
106 EXPECT_EQ(sccs.size(), 1u);
107 EXPECT_EQ(sccs.get_first().size(), 1u);
108 EXPECT_EQ(sccs.get_first().get_first(), n1);
113 EXPECT_EQ(sizes.
size(), 1u);
120 EXPECT_TRUE(tarjan.
is_dag(g));
133TEST(TarjanTest, SingleNodeWithLoop)
143 EXPECT_EQ(sccs.size(), 1u);
144 EXPECT_EQ(sccs.get_first().size(), 1u);
150 EXPECT_FALSE(tarjan.
is_dag(g));
167TEST(TarjanTest, TwoNodesNoCycle)
178 EXPECT_EQ(sccs.size(), 2u);
183 EXPECT_EQ(sizes.
size(), 2u);
184 for (
auto sz : sizes)
189 EXPECT_TRUE(tarjan.
is_dag(g));
201TEST(TarjanTest, TwoNodesWithCycle)
213 EXPECT_EQ(sccs.size(), 1u);
214 EXPECT_EQ(sccs.get_first().size(), 2u);
218 EXPECT_FALSE(tarjan.
is_dag(g));
248 EXPECT_EQ(sccs.size(), 4u);
251 EXPECT_TRUE(tarjan.
is_dag(g));
272 EXPECT_EQ(sccs.size(), 1u);
273 EXPECT_EQ(sccs.get_first().size(), 3u);
276 EXPECT_FALSE(tarjan.
is_dag(g));
313 EXPECT_EQ(sccs.size(), 2u);
318 EXPECT_EQ(sizes.
size(), 2u);
320 for (
auto sz : sizes)
322 EXPECT_EQ(total, 5u);
334TEST(TarjanTest, ConnectedComponentsWithArcs)
351 auto cross1 =
add_arc(g, b, c);
352 auto cross2 =
add_arc(g, a, d);
361 EXPECT_EQ(blk_list.
size(), 2u);
364 EXPECT_EQ(arc_list.
size(), 2u);
367 bool found_cross1 =
false, found_cross2 =
false;
368 for (
auto arc : arc_list)
375 EXPECT_TRUE(found_cross1);
376 EXPECT_TRUE(found_cross2);
382TEST(TarjanTest, ComputeCycleFromSource)
408TEST(TarjanTest, ComputeCycleInTestDigraphClass)
421 bool found = finder(g, path);
426 auto path2 = finder(g);
427 EXPECT_FALSE(path2.is_empty());
430 auto path3 = finder(g, a);
431 EXPECT_FALSE(path3.is_empty());
453 EXPECT_EQ(sccs.size(), 4u);
456 EXPECT_TRUE(tarjan.
is_dag(g));
463TEST(TarjanTest, ComplexNestedCycles)
486 EXPECT_EQ(sccs.size(), 1u);
487 EXPECT_EQ(sccs.get_first().size(), 6u);
500TEST(TarjanTest, DynDlistOverloads)
513 tarjan(g, blk_list, arc_list);
514 EXPECT_EQ(blk_list.
size(), 1u);
520 EXPECT_EQ(blks.
size(), 1u);
527TEST(TarjanTest, DisconnectedComponents)
551 EXPECT_EQ(sccs.size(), 4u);
556 EXPECT_EQ(sizes.
size(), 4u);
559 size_t count_3 = 0, count_1 = 0;
560 for (
auto sz : sizes)
567 EXPECT_EQ(count_3, 1u);
568 EXPECT_EQ(count_1, 3u);
583 const size_t N = 100;
586 for (
size_t i = 0; i <
N; ++i)
590 auto it =
nodes.get_it();
591 auto prev = it.get_curr();
593 for (it.next(); it.has_curr(); it.next())
595 auto curr = it.get_curr();
605 EXPECT_EQ(sccs.size(), 1u);
606 EXPECT_EQ(sccs.get_first().size(),
N);
647 EXPECT_EQ(sccs.size(), 3u);
650 for (
auto &scc : sccs)
651 EXPECT_EQ(scc.size(), 2u);
663TEST(TarjanTest, NumConnectedComponents)
691TEST(TarjanTest, NullSourceValidation)
699 EXPECT_THROW((
void) tarjan.
compute_cycle(g,
nullptr, path), std::domain_error);
705TEST(TarjanTest, FilterAccessor)
713 [[maybe_unused]]
const auto &const_filter = const_tarjan.
get_filter();
752 EXPECT_EQ(sccs1.size(), 1u);
757 EXPECT_EQ(sccs2.size(), 1u);
761 tarjan3 = std::move(tarjan2);
763 EXPECT_EQ(sccs3.size(), 1u);
773 return a->get_info() != 0;
777TEST(TarjanTest, CustomArcFilter)
801TEST(TarjanTest, LargeGraphStress)
804 const size_t N = 1000;
807 std::vector<TestDigraph::Node*>
nodes(
N);
808 for (
size_t i = 0; i <
N; ++i)
812 for (
size_t i = 0; i <
N; ++i)
830TEST(TarjanTest, MultipleDisjointCycles)
833 const size_t NUM_CYCLES = 10;
834 const size_t CYCLE_SIZE = 5;
836 std::vector<TestDigraph::Node*> first_nodes;
838 for (
size_t c = 0; c < NUM_CYCLES; ++c)
840 std::vector<TestDigraph::Node*> cycle_nodes(CYCLE_SIZE);
841 for (
size_t i = 0; i < CYCLE_SIZE; ++i)
842 cycle_nodes[i] =
add_node(g,
static_cast<int>(c * CYCLE_SIZE + i));
844 first_nodes.push_back(cycle_nodes[0]);
846 for (
size_t i = 0; i < CYCLE_SIZE; ++i)
847 add_arc(g, cycle_nodes[i], cycle_nodes[(i + 1) % CYCLE_SIZE]);
861 for (
auto sz : sizes)
862 EXPECT_EQ(sz, CYCLE_SIZE);
868TEST(TarjanTest, CyclePathVerification)
894 EXPECT_GE(path_len, 2u);
900TEST(TarjanTest, CompleteDigraph)
905 std::vector<TestDigraph::Node*>
nodes(
N);
906 for (
size_t i = 0; i <
N; ++i)
910 for (
size_t i = 0; i <
N; ++i)
911 for (
size_t j = 0; j <
N; ++j)
950 EXPECT_TRUE(tarjan.
is_dag(g2));
956TEST(TarjanTest, ListsThatHoldBlocksKeepThem)
969 ASSERT_EQ(blocks.
size(), 1u);
970 ASSERT_TRUE(
arcs.is_empty());
984 ASSERT_EQ(blocks.
size(), 5u);
985 std::vector<std::pair<size_t, size_t>> sizes;
986 for (
auto & blk : blocks)
988 sizes.emplace_back(blk.get_num_nodes(), blk.get_num_arcs());
989 std::vector<TestDigraph::Node *>
nodes;
990 for (
auto it = blk.get_node_it(); it.has_curr(); it.next_ne())
991 nodes.push_back(it.get_curr());
996 for (
auto it = blk.get_arc_it(); it.has_curr(); it.next_ne())
998 EXPECT_TRUE(mine(blk.get_src_node(it.get_curr())));
999 EXPECT_TRUE(mine(blk.get_tgt_node(it.get_curr())));
1002 const std::vector<std::pair<size_t, size_t>>
expected =
1003 { {2, 2}, {1, 0}, {2, 2}, {1, 0}, {2, 2} };
1004 std::sort(sizes.begin(), sizes.end());
1006 std::sort(sorted.begin(), sorted.end());
1007 EXPECT_EQ(sizes, sorted);
1008 ASSERT_EQ(blocks.
get_first().get_num_nodes(), 2u);
1009 EXPECT_EQ(blocks.
get_first().get_num_arcs(), 2u);
1012 ASSERT_EQ(
arcs.size(), 2u);
1023TEST(TarjanExtended, StarGraphOutward)
1026 const size_t N = 50;
1029 for (
size_t i = 1; i <=
N; ++i)
1031 auto spoke =
add_node(g,
static_cast<int>(i));
1040 EXPECT_TRUE(tarjan.
is_dag(g));
1047TEST(TarjanExtended, StarGraphInward)
1050 const size_t N = 50;
1053 for (
size_t i = 1; i <=
N; ++i)
1055 auto spoke =
add_node(g,
static_cast<int>(i));
1063 EXPECT_TRUE(tarjan.
is_dag(g));
1072 const size_t N = 20;
1075 std::vector<TestDigraph::Node *> rim(
N);
1077 for (
size_t i = 0; i <
N; ++i)
1079 rim[i] =
add_node(g,
static_cast<int>(i + 1));
1085 for (
size_t i = 0; i <
N; ++i)
1086 add_arc(g, rim[i], rim[(i + 1) %
N]);
1102 const size_t DEPTH = 6;
1104 std::vector<TestDigraph::Node *>
nodes;
1107 for (
size_t level = 0; level < DEPTH - 1; ++level)
1109 size_t start = (1 << level) - 1;
1110 size_t end = (1 << (level + 1)) - 1;
1112 for (
size_t i = start; i < end && i <
nodes.size(); ++i)
1115 nodes.push_back(left);
1119 nodes.push_back(right);
1129 EXPECT_TRUE(tarjan.
is_dag(g));
1135TEST(TarjanExtended, MultipleSelfLoops)
1138 const size_t N = 20;
1140 for (
size_t i = 0; i <
N; ++i)
1142 auto n =
add_node(g,
static_cast<int>(i));
1151 EXPECT_FALSE(tarjan.
is_dag(g));
1161 const size_t NUM_SCCS = 10;
1162 const size_t SCC_SIZE = 3;
1164 std::vector<TestDigraph::Node *> first_of_scc;
1166 for (
size_t s = 0; s < NUM_SCCS; ++s)
1168 std::vector<TestDigraph::Node *> scc_nodes(SCC_SIZE);
1169 for (
size_t i = 0; i < SCC_SIZE; ++i)
1170 scc_nodes[i] =
add_node(g,
static_cast<int>(s * SCC_SIZE + i));
1172 first_of_scc.push_back(scc_nodes[0]);
1175 for (
size_t i = 0; i < SCC_SIZE; ++i)
1176 add_arc(g, scc_nodes[i], scc_nodes[(i + 1) % SCC_SIZE]);
1180 add_arc(g, first_of_scc[s - 1], scc_nodes[0]);
1191 for (
auto sz : sizes)
1192 EXPECT_EQ(sz, SCC_SIZE);
1198TEST(TarjanExtended, DiamondPatternSCCs)
1245 const size_t LEFT_SIZE = 10;
1246 const size_t RIGHT_SIZE = 10;
1248 std::vector<TestDigraph::Node *> left(LEFT_SIZE);
1249 std::vector<TestDigraph::Node *> right(RIGHT_SIZE);
1251 for (
size_t i = 0; i < LEFT_SIZE; ++i)
1252 left[i] =
add_node(g,
static_cast<int>(i));
1254 for (
size_t i = 0; i < RIGHT_SIZE; ++i)
1255 right[i] =
add_node(g,
static_cast<int>(LEFT_SIZE + i));
1258 for (
size_t i = 0; i < LEFT_SIZE; ++i)
1259 for (
size_t j = 0; j < RIGHT_SIZE; ++j)
1260 add_arc(g, left[i], right[j]);
1266 EXPECT_TRUE(tarjan.
is_dag(g));
1272TEST(TarjanExtended, StronglyConnectedBipartite)
1275 const size_t LEFT_SIZE = 5;
1276 const size_t RIGHT_SIZE = 5;
1278 std::vector<TestDigraph::Node *> left(LEFT_SIZE);
1279 std::vector<TestDigraph::Node *> right(RIGHT_SIZE);
1281 for (
size_t i = 0; i < LEFT_SIZE; ++i)
1282 left[i] =
add_node(g,
static_cast<int>(i));
1284 for (
size_t i = 0; i < RIGHT_SIZE; ++i)
1285 right[i] =
add_node(g,
static_cast<int>(LEFT_SIZE + i));
1288 for (
size_t i = 0; i < LEFT_SIZE; ++i)
1289 for (
size_t j = 0; j < RIGHT_SIZE; ++j)
1291 add_arc(g, left[i], right[j]);
1292 add_arc(g, right[j], left[i]);
1308 const size_t ROWS = 5;
1309 const size_t COLS = 5;
1311 std::vector<std::vector<TestDigraph::Node *>> grid(ROWS,
1312 std::vector<TestDigraph::Node *>(COLS));
1314 for (
size_t r = 0;
r < ROWS; ++
r)
1315 for (
size_t c = 0; c < COLS; ++c)
1316 grid[
r][c] =
add_node(g,
static_cast<int>(
r * COLS + c));
1319 for (
size_t r = 0;
r < ROWS; ++
r)
1320 for (
size_t c = 0; c < COLS; ++c)
1332 EXPECT_TRUE(tarjan.
is_dag(g));
1338TEST(TarjanExtended, GridWithCycles)
1341 const size_t ROWS = 4;
1342 const size_t COLS = 4;
1344 std::vector<std::vector<TestDigraph::Node *>> grid(ROWS,
1345 std::vector<TestDigraph::Node *>(COLS));
1347 for (
size_t r = 0;
r < ROWS; ++
r)
1348 for (
size_t c = 0; c < COLS; ++c)
1349 grid[
r][c] =
add_node(g,
static_cast<int>(
r * COLS + c));
1352 for (
size_t r = 0;
r < ROWS; ++
r)
1353 for (
size_t c = 0; c < COLS; ++c)
1378TEST(TarjanExtended, DeepLinearChain)
1381 const size_t DEPTH = 500;
1383 std::vector<TestDigraph::Node *>
nodes(DEPTH);
1384 for (
size_t i = 0; i < DEPTH; ++i)
1387 for (
size_t i = 0; i + 1 < DEPTH; ++i)
1394 EXPECT_TRUE(tarjan.
is_dag(g));
1400TEST(TarjanExtended, ConsistencyBetweenOverloads)
1438 EXPECT_EQ(sizes.
size(), node_lists.size());
1439 EXPECT_EQ(sizes.
size(), blocks.
size());
1440 EXPECT_EQ(sizes.
size(), 2u);
1443 size_t total_from_sizes = 0;
1444 for (
auto sz : sizes)
1445 total_from_sizes += sz;
1447 size_t total_from_lists = 0;
1448 for (
auto &list : node_lists)
1449 total_from_lists += list.size();
1451 size_t total_from_blocks = 0;
1452 for (
auto &block : blocks)
1453 total_from_blocks += block.get_num_nodes();
1455 EXPECT_EQ(total_from_sizes, 5u);
1456 EXPECT_EQ(total_from_lists, 5u);
1457 EXPECT_EQ(total_from_blocks, 5u);
1460 EXPECT_EQ(inter_arcs.
size(), 1u);
1466TEST(TarjanExtended, CycleDetectionConsistency)
1472 for (
int i = 0; i < 10; ++i)
1483 for (
auto sz : sizes)
1493 const size_t N = 2000;
1495 std::vector<TestDigraph::Node *>
nodes(
N);
1496 for (
size_t i = 0; i <
N; ++i)
1500 for (
size_t i = 0; i <
N; ++i)
1504 for (
size_t i = 0; i <
N; i += 7)
1517 EXPECT_EQ(sizes.
size(), 1u);
1527 const size_t NUM_SCCS = 500;
1529 for (
size_t i = 0; i < NUM_SCCS; ++i)
1531 auto a =
add_node(g,
static_cast<int>(i * 2));
1532 auto b =
add_node(g,
static_cast<int>(i * 2 + 1));
1544 for (
auto sz : sizes)
1551TEST(TarjanExtended, CyclePathDetails)
1584TEST(TarjanExtended, EmptyGraphEdgeCases)
1592 EXPECT_TRUE(tarjan.
is_dag(g));
1597 EXPECT_TRUE(node_lists.
is_empty());
1636TEST(TarjanExtended, TournamentGraph)
1641 std::vector<TestDigraph::Node *>
nodes(
N);
1642 for (
size_t i = 0; i <
N; ++i)
1647 for (
size_t i = 0; i <
N; ++i)
1648 for (
size_t j = i + 1; j <
N; ++j)
1650 if ((i + j) % 2 == 0)
1661 EXPECT_GE(num_sccs, 1u);
1662 EXPECT_LE(num_sccs,
N);
1671 const size_t N = 50;
1673 std::vector<TestDigraph::Node *>
nodes(
N);
1674 for (
size_t i = 0; i <
N; ++i)
1678 for (
size_t i = 0; i <
N; ++i)
1679 for (
size_t j = 0; j <
N; ++j)
1680 if (i != j && (i * 7 + j * 3) % 5 < 3)
1688 for (
auto &scc : sccs)
1689 total += scc.size();
1690 EXPECT_EQ(total,
N);
1696TEST(TarjanExtended, ComputeCycleNoCycle)
1708 bool found = finder(g, path);
1709 EXPECT_FALSE(found);
1711 auto path2 = finder(g);
1712 EXPECT_TRUE(path2.is_empty());
1714 auto path3 = finder(g, a);
1715 EXPECT_TRUE(path3.is_empty());
1725 struct Counted_Copies
1727 static inline long alive = 0;
1729 Counted_Copies() noexcept { ++alive; }
1730 Counted_Copies(
const Counted_Copies &)
noexcept { ++alive; }
1731 Counted_Copies & operator = (
const Counted_Copies &)
noexcept =
default;
1732 ~Counted_Copies() { --alive; }
1742TEST(TarjanTest, KeepsNoFrameAfterAnOperation)
1748 std::vector<TestDigraph::Node *>
nodes;
1749 for (
int i = 0; i < n; ++i)
1751 for (
int i = 0; i + 1 < n; ++i)
1755 Counted_Copies::alive = 0;
1758 const long kept = Counted_Copies::alive;
1761 EXPECT_EQ(Counted_Copies::alive, kept);
1765 EXPECT_EQ(cycle.
size(), 3u);
1766 EXPECT_EQ(Counted_Copies::alive, kept);
1769 EXPECT_EQ(Counted_Copies::alive, kept);
1772 EXPECT_EQ(Counted_Copies::alive, kept);
1774 EXPECT_EQ(Counted_Copies::alive, 0);
Tarjan's algorithm for strongly connected components.
size_t size_t int32_t value
Determines if a digraph contains a cycle and constructs it.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
constexpr bool is_empty() const noexcept
Number Theoretic Transform over Z / MOD Z.
Iterator on nodes and arcs of a path.
size_t size() const noexcept
Return the path length in nodes.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
bool is_empty() const noexcept
Return true if the path is empty.
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
bool has_computation() const noexcept
Check if a computation has been performed.
SA & get_filter() noexcept
Returns the arc filter used by this instance.
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
bool test_connectivity(const GT &g)
Tests whether the digraph is strongly connected.
void connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Computes the strongly connected components (SCCs) of a digraph.
bool compute_cycle(const GT &g, Path< GT > &path)
Finds and constructs a cycle in the digraph, if one exists.
GT * get_graph() const noexcept
Get the graph of the last computation.
bool is_dag(const GT &g)
Determines whether the directed graph is acyclic (a DAG).
size_t num_connected_components(const GT &g)
Returns the number of strongly connected components in the graph.
Minimal std::expected-style result type for C++20.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
bool between(const Point &a, const Point &b, const Point &c)
Arc of graph implemented with double-linked adjacency lists.
bool operator()(TestDigraph::Arc *a) const noexcept
TestDigraph::Arc * add_arc(TestDigraph &g, TestDigraph::Node *src, TestDigraph::Node *tgt, int value=0)
TestDigraph::Node * add_node(TestDigraph &g, int value)
Generic graph and digraph implementations.