38#include <gtest/gtest.h>
53 std::map<TestDigraph::Node*, size_t> pos;
55 for (
auto it = order.get_it(); it.has_curr(); it.next_ne(), ++idx)
56 pos[it.get_curr()] = idx;
61 auto arc = it.get_curr();
62 auto src = g.get_src_node(arc);
63 auto tgt = g.get_tgt_node(arc);
65 if (pos.find(src) == pos.end() || pos.find(tgt) == pos.end())
68 if (pos[src] >= pos[tgt])
77TEST(TopologicalSortDFS, EmptyGraph)
83 EXPECT_TRUE(result.is_empty());
86TEST(TopologicalSortDFS, SingleNode)
89 auto n = g.insert_node(
"A");
94 ASSERT_EQ(result.size(), 1);
95 EXPECT_EQ(result.get_first(), n);
98TEST(TopologicalSortDFS, TwoNodesOneArc)
101 auto a = g.insert_node(
"A");
102 auto b = g.insert_node(
"B");
108 ASSERT_EQ(result.size(), 2);
111 EXPECT_EQ(result.get_first(), a);
112 EXPECT_EQ(result.get_last(), b);
115TEST(TopologicalSortDFS, LinearChain)
118 auto a = g.insert_node(
"A");
119 auto b = g.insert_node(
"B");
120 auto c = g.insert_node(
"C");
121 auto d = g.insert_node(
"D");
130 ASSERT_EQ(result.size(), 4);
134TEST(TopologicalSortDFS, DiamondDAG)
144 auto a = g.insert_node(
"A");
145 auto b = g.insert_node(
"B");
146 auto c = g.insert_node(
"C");
147 auto d = g.insert_node(
"D");
157 ASSERT_EQ(result.size(), 4);
161TEST(TopologicalSortDFS, DisconnectedComponents)
164 auto a = g.insert_node(
"A");
165 auto b = g.insert_node(
"B");
166 auto c = g.insert_node(
"C");
167 auto d = g.insert_node(
"D");
175 ASSERT_EQ(result.size(), 4);
179TEST(TopologicalSortDFS, ComplexDAG)
186 auto a = g.insert_node(
"A");
187 auto b = g.insert_node(
"B");
188 auto c = g.insert_node(
"C");
189 auto d = g.insert_node(
"D");
190 auto e = g.insert_node(
"E");
191 auto f = g.insert_node(
"F");
204 ASSERT_EQ(result.size(), 6);
208TEST(TopologicalSortDFS, OperatorOverload)
211 auto a = g.insert_node(
"A");
212 auto b = g.insert_node(
"B");
219 ASSERT_EQ(result.
size(), 2);
225TEST(QTopologicalSortBFS, EmptyGraph)
231 EXPECT_TRUE(result.is_empty());
234TEST(QTopologicalSortBFS, SingleNode)
237 auto n = g.insert_node(
"A");
242 ASSERT_EQ(result.size(), 1);
243 EXPECT_EQ(result.get_first(), n);
246TEST(QTopologicalSortBFS, TwoNodesOneArc)
249 auto a = g.insert_node(
"A");
250 auto b = g.insert_node(
"B");
256 ASSERT_EQ(result.size(), 2);
260TEST(QTopologicalSortBFS, LinearChain)
263 auto a = g.insert_node(
"A");
264 auto b = g.insert_node(
"B");
265 auto c = g.insert_node(
"C");
266 auto d = g.insert_node(
"D");
275 ASSERT_EQ(result.size(), 4);
279TEST(QTopologicalSortBFS, DiamondDAG)
282 auto a = g.insert_node(
"A");
283 auto b = g.insert_node(
"B");
284 auto c = g.insert_node(
"C");
285 auto d = g.insert_node(
"D");
295 ASSERT_EQ(result.size(), 4);
301TEST(QTopologicalSortRanks, EmptyGraph)
306 auto result = sorter.
ranks(g);
307 EXPECT_TRUE(result.is_empty());
310TEST(QTopologicalSortRanks, SingleNode)
313 auto n = g.insert_node(
"A");
316 auto result = sorter.
ranks(g);
318 ASSERT_EQ(result.size(), 1);
319 ASSERT_EQ(result.get_first().size(), 1);
320 EXPECT_EQ(result.get_first().get_first(), n);
323TEST(QTopologicalSortRanks, LinearChain)
330 auto nodes = g.nodes();
335 auto result = sorter.
ranks(g);
338 ASSERT_EQ(result.size(), 3);
339 for (
auto it = result.get_it(); it.has_curr(); it.next_ne())
340 EXPECT_EQ(it.get_curr().size(), 1);
343TEST(QTopologicalSortRanks, ParallelNodes)
351 auto a = g.insert_node(
"A");
352 auto b = g.insert_node(
"B");
353 auto c = g.insert_node(
"C");
354 auto d = g.insert_node(
"D");
361 auto result = sorter.
ranks(g);
363 ASSERT_EQ(result.size(), 2);
366 EXPECT_EQ(result.get_first().size(), 1);
367 EXPECT_EQ(result.get_first().get_first(), a);
370 EXPECT_EQ(result.get_last().size(), 3);
373TEST(QTopologicalSortRanks, DiamondRanks)
383 auto a = g.insert_node(
"A");
384 auto b = g.insert_node(
"B");
385 auto c = g.insert_node(
"C");
386 auto d = g.insert_node(
"D");
394 auto result = sorter.
ranks(g);
396 ASSERT_EQ(result.size(), 3);
398 auto it = result.get_it();
401 EXPECT_EQ(it.get_curr().size(), 1);
402 EXPECT_EQ(it.get_curr().get_first(), a);
406 EXPECT_EQ(it.get_curr().size(), 2);
410 EXPECT_EQ(it.get_curr().size(), 1);
411 EXPECT_EQ(it.get_curr().get_first(), d);
414TEST(QTopologicalSortRanks, DisconnectedComponents)
418 auto a = g.insert_node(
"A");
419 auto b = g.insert_node(
"B");
420 auto c = g.insert_node(
"C");
421 auto d = g.insert_node(
"D");
427 auto result = sorter.
ranks(g);
429 ASSERT_EQ(result.size(), 2);
432 EXPECT_EQ(result.get_first().size(), 2);
435 EXPECT_EQ(result.get_last().size(), 2);
440TEST(QTopologicalSortOperators, PerformOverload)
443 auto a = g.insert_node(
"A");
444 auto b = g.insert_node(
"B");
451 ASSERT_EQ(result.
size(), 2);
454TEST(QTopologicalSortOperators, RanksOverloadDynDlist)
457 auto a = g.insert_node(
"A");
458 auto b = g.insert_node(
"B");
465 ASSERT_EQ(result.
size(), 2);
468TEST(QTopologicalSortOperators, RanksOverloadDynList)
471 auto a = g.insert_node(
"A");
472 auto b = g.insert_node(
"B");
479 ASSERT_EQ(result.
size(), 2);
484TEST(TopologicalSortStress, LargeDAG)
487 constexpr size_t N = 1000;
491 for (
size_t i = 0; i <
N; ++i)
494 for (
size_t i = 0; i <
N - 1; ++i)
501 ASSERT_EQ(result.size(),
N);
509 ASSERT_EQ(result.size(),
N);
514TEST(TopologicalSortStress, WideDAG)
517 constexpr size_t WIDTH = 100;
520 auto source = g.insert_node(
"source");
521 for (
size_t i = 0; i <
WIDTH; ++i)
523 auto sink = g.insert_node(std::to_string(i));
524 g.insert_arc(source, sink);
537TEST(TopologicalSortComparison, BothAlgorithmsProduceValidOrder)
541 auto a = g.insert_node(
"A");
542 auto b = g.insert_node(
"B");
543 auto c = g.insert_node(
"C");
544 auto d = g.insert_node(
"D");
545 auto e = g.insert_node(
"E");
546 auto f = g.insert_node(
"F");
562 EXPECT_EQ(dfs_result.size(), 6);
563 EXPECT_EQ(bfs_result.size(), 6);
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & get_last()
Return a modifiable reference to the last element.
T & get_first()
Return a modifiable reference to the first element.
Generic directed graph (digraph) wrapper template.
Node belonging to a double circular linked list with header node.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Number Theoretic Transform over Z / MOD Z.
Computes topological ordering using breadth-first search (Kahn's algorithm).
List< typename GT::Node * > perform(const GT &g)
Compute topological ordering using BFS (Kahn's algorithm).
RankList< List< typename GT::Node * > > ranks(const GT &g)
Compute rank-based topological ordering.
Computes topological ordering using depth-first search.
List< typename GT::Node * > perform(const GT &g)
Compute topological ordering using DFS.
DynArray< Graph::Node * > nodes
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
Topological sorting algorithms for directed acyclic graphs (DAGs).
bool is_valid_topological_order(const TestDigraph &g, const List &order)
Generic graph and digraph implementations.