43#include <gtest/gtest.h>
49#if defined(__unix__) || defined(__APPLE__)
75 constexpr size_t N = 100000;
86# if defined(__unix__) || defined(__APPLE__)
88 ASSERT_EQ(pthread_attr_init(&attr), 0);
89 ASSERT_EQ(pthread_attr_setstacksize(&attr, 256 * 1024), 0);
90 auto start = [](
void *arg) ->
void *
92 (*
static_cast<F *
>(arg))();
96 const int created = pthread_create(&thread, &attr, start, &f);
97 pthread_attr_destroy(&attr);
98 ASSERT_EQ(created, 0);
99 ASSERT_EQ(pthread_join(thread,
nullptr), 0);
108 std::vector<typename GT::Node *> build_chain(
GT & g,
size_t n,
bool closed)
110 std::vector<typename GT::Node *>
nodes;
112 for (
size_t i = 0; i < n; ++i)
114 for (
size_t i = 0; i + 1 < n; ++i)
123TEST(DeepChain, TarjanFindsTheComponentsOfAnOpenChain)
126 build_chain(g,
N,
false);
133 run_with_small_stack([&]
138 is_dag = tarjan.
is_dag(g);
147 EXPECT_EQ(sizes.
size(),
N);
148 EXPECT_TRUE(sizes.
all([](
size_t s) { return s == 1; }));
149 EXPECT_EQ(blocks.
size(),
N);
150 EXPECT_TRUE(blocks.
all([](
const Chain_Digraph & b) { return b.get_num_nodes() == 1; }));
155TEST(DeepChain, TarjanFindsTheComponentOfAClosedChain)
158 build_chain(g,
N,
true);
162 bool connected =
false;
166 run_with_small_stack([&]
175 ASSERT_EQ(blocks.
size(), 1u);
176 EXPECT_EQ(blocks.
get_first().get_num_nodes(),
N);
177 EXPECT_EQ(blocks.
get_first().get_num_arcs(),
N);
178 EXPECT_TRUE(
between.is_empty());
179 ASSERT_EQ(lists.
size(), 1u);
181 EXPECT_TRUE(connected);
184 EXPECT_EQ(cycle.
size(),
N + 1);
190TEST(DeepChain, CutNodesAndBridgesOfAPathAndOfACycle)
193 build_chain(path,
N,
false);
195 build_chain(cycle,
N,
true);
197 size_t path_bridges = 0, cycle_bridges = 1;
198 Chain_Graph cut_graph;
200 run_with_small_stack([&]
203 cc(path.get_first_node(), path_cuts);
207 cycle_cc(cycle.get_first_node(), cycle_cuts);
211 EXPECT_EQ(path_cuts.
size(),
N - 2);
212 EXPECT_EQ(cut_graph.get_num_nodes(),
N - 2);
213 EXPECT_EQ(cut_graph.get_num_arcs(),
N - 3);
214 EXPECT_EQ(cross.
size(), 2u);
216 EXPECT_EQ(path_bridges,
N - 1);
217 EXPECT_EQ(cycle_bridges, 0u);
222TEST(DeepChain, PaintsAndCopiesTheBlockOfACycle)
224 for (
bool utils : {
false,
true})
226 SCOPED_TRACE(utils ?
"tpl_graph_utils.H" :
"Compute_Cut_Nodes");
228 auto nodes = build_chain(g,
N,
true);
229 auto hung = g.insert_node(
N);
230 g.insert_arc(
nodes[0], hung,
N);
231 size_t cuts = 0, block_nodes = 0, block_arcs = 0, blocks = 0;
232 run_with_small_stack([&]
240 block_nodes = block.get_num_nodes();
241 block_arcs = block.get_num_arcs();
251 block_nodes = block.get_num_nodes();
252 block_arcs = block.get_num_arcs();
258 Chain_Graph cut_graph;
260 all.compute_blocks(block_list, cut_graph, cross);
261 for (
auto it = block_list.
get_it(); it.has_curr(); it.next_ne())
262 blocks += it.get_curr().get_num_nodes() > 0;
265 EXPECT_EQ(block_nodes,
N - 1);
266 EXPECT_EQ(block_arcs,
N - 2);
269 EXPECT_EQ(blocks, 2u);
275TEST(DeepChain, CopiesTheComponentOfAPath)
278 auto nodes = build_chain(g,
N,
false);
279 size_t sub_arcs = 0, list_nodes = 0, components = 0, utils_nodes = 0;
280 run_with_small_stack([&]
289 list_nodes = list.
size();
292 utils_nodes = blocks.get_first().get_num_nodes();
294 EXPECT_EQ(sub_arcs,
N - 1);
295 EXPECT_EQ(list_nodes,
N);
296 EXPECT_EQ(components, 1u);
297 EXPECT_EQ(utils_nodes,
N);
301TEST(DeepChain, FindsAndTestsThePathOfAChain)
304 auto p = build_chain(path,
N,
false);
306 auto c = build_chain(cycle,
N,
true);
307 size_t found = 0, free_found = 0, dft = 0, dft_class = 0, tree_arcs = 0,
309 bool to_end =
false, class_to_end =
false, cycle_found =
false, class_cycle =
false;
310 bool path_acyclique =
false, cycle_acyclique =
true, class_acyclique =
false,
311 class_has_cycle =
false;
312 run_with_small_stack([&]
316 found = found_path.
size();
318 dft = depth_first_traversal<Chain_Graph>(path, p[0],
nullptr);
331 class_tree_arcs = tree.get_num_arcs();
334 EXPECT_EQ(free_found,
N);
336 EXPECT_EQ(dft_class,
N);
338 EXPECT_TRUE(class_to_end);
339 EXPECT_TRUE(cycle_found);
340 EXPECT_TRUE(class_cycle);
341 EXPECT_TRUE(path_acyclique);
342 EXPECT_FALSE(cycle_acyclique);
343 EXPECT_TRUE(class_acyclique);
344 EXPECT_TRUE(class_has_cycle);
345 EXPECT_EQ(tree_arcs,
N - 1);
346 EXPECT_EQ(class_tree_arcs,
N - 1);
350TEST(DeepChain, KosarajuAndTopologicalSortOfAChain)
352 Chain_Digraph open, closed;
353 auto nodes = build_chain(open,
N,
false);
354 build_chain(closed,
N,
true);
355 size_t open_components = 0, closed_components = 0, blocks = 0, sorted = 0;
356 size_t sorted_in_list = 0;
357 Chain_Digraph::Node * first =
nullptr;
358 Chain_Digraph::Node * first_in_list =
nullptr;
359 run_with_small_stack([&]
366 blocks = blk_list.
size();
369 sorted = order.
size();
374 sorted_in_list = listed.
size();
375 first_in_list = listed.get_first();
377 EXPECT_EQ(open_components,
N);
378 EXPECT_EQ(closed_components, 1u);
379 EXPECT_EQ(blocks, 1u);
380 EXPECT_EQ(sorted,
N);
381 EXPECT_EQ(first,
nodes[0]);
382 EXPECT_EQ(sorted_in_list,
N);
383 EXPECT_EQ(first_in_list,
nodes[0]);
409TEST(DeepChain, BuildsTheTreeOfAPath)
412 auto nodes = build_chain(g,
N,
false);
413 size_t height = 0, class_height = 0;
414 run_with_small_stack([&]
416 auto tree = graph_to_tree_node<Chain_Graph, size_t, Key_Of_Node>(g,
nodes[0]);
419 height = height_of_chain(tree);
420 class_height = height_of_chain(class_tree);
424 EXPECT_EQ(height,
N);
425 EXPECT_EQ(class_height,
N);
430TEST(DeepChain, BuildsTheDfLowTreeOfACycle)
433 auto nodes = build_chain(g,
N,
true);
434 size_t height = 0, non_tree_arcs = 0;
435 long low_of_last = -2;
436 run_with_small_stack([&]
440 height = height_of_chain(
root);
441 non_tree_arcs =
arcs.size();
443 while (last->get_left_child() !=
nullptr)
444 last = last->get_left_child();
445 low_of_last = last->get_key().low;
448 EXPECT_EQ(height,
N);
449 EXPECT_EQ(non_tree_arcs, 1u);
450 EXPECT_EQ(low_of_last, 0);
456TEST(DeepChain, HopcroftKarpFollowsALongAugmentingPath)
459 std::vector<Chain_Graph::Node *> left(
N + 1), right(
N + 2);
460 for (
size_t i = 1; i <=
N; ++i)
461 left[i] = g.insert_node(i);
462 left[0] = g.insert_node(
size_t(0));
463 for (
size_t i = 1; i <=
N + 1; ++i)
464 right[i] = g.insert_node(
N + i);
465 g.insert_arc(left[0], right[1],
size_t(0));
466 for (
size_t i = 1; i <=
N; ++i)
468 g.insert_arc(left[i], right[i], 2 * i - 1);
469 g.insert_arc(left[i], right[i + 1], 2 * i);
473 run_with_small_stack([&]
477 matched = matching.
size();
479 EXPECT_EQ(matched,
N + 1);
485TEST(DeepChain, TestsThePlanarityOfACycle)
488 build_chain(g,
N,
true);
489 bool planar =
false, embedded =
false;
491 run_with_small_stack([&]
495 options.embedding_allow_bruteforce_fallback =
false;
497 planar = result.is_planar;
498 embedded = result.has_combinatorial_embedding;
499 faces = result.embedding_num_faces;
502 EXPECT_TRUE(embedded);
503 EXPECT_EQ(faces, 2u);
513 const size_t hidden = size_t(-1);
515 Const_Filter() =
default;
516 Const_Filter(
const Const_Filter & other) : hidden(other.hidden) {}
519 bool operator()(
Arc * a)
const {
return a->get_info() != hidden; }
524 struct Search_Results
526 size_t dft = 0, connected = 0,
components = 0, sub_arcs = 0,
527 list_nodes = 0,
count = 0, cuts = 0, block_nodes = 0, blocks = 0,
528 path = 0, tree_arcs = 0, tree_height = 0, matched = 0, cycle = 0;
529 bool has_cycle =
false, dag =
true, strong =
false, acyclique =
false,
530 path_has_cycle =
true, cycle_found =
false, to_end =
false;
535 constexpr size_t M = 10000;
540 Search_Results run_the_searches(SA sa)
542 Chain_Graph path, cycle;
543 auto p = build_chain(path, M,
false);
544 auto c = build_chain(cycle, M,
true);
545 Chain_Digraph dpath, dcycle;
546 build_chain(dpath, M,
false);
547 build_chain(dcycle, M,
true);
555 r.components = tarjan.num_connected_components(dpath);
556 r.dag = tarjan.is_dag(dpath);
557 r.has_cycle = tarjan.has_cycle(dcycle);
558 r.strong = tarjan.test_connectivity(dcycle);
560 if (tarjan.compute_cycle(dcycle, dc))
568 find_path(path, p[0], p[M - 1], found);
569 r.path = found.size();
578 build(path, list, p[M / 2]);
579 r.list_nodes = list.
size();
583 auto hung = cycle.insert_node(M);
584 cycle.insert_arc(c[0], hung, M);
588 r.cuts = cuts.
size();
589 cc.paint_subgraphs();
592 r.block_nodes =
block.get_num_nodes();
597 Chain_Graph cut_graph;
599 all.compute_blocks(block_list, cut_graph, cross);
600 for (
auto it = block_list.
get_it(); it.has_curr(); it.next_ne())
601 r.blocks += it.get_curr().get_num_nodes() > 0;
605 spanning(path, p[0], tree);
606 r.tree_arcs = tree.get_num_arcs();
610 r.tree_height = height_of_chain(
root);
618 std::vector<Chain_Graph::Node *> left(M + 1), right(M + 2);
619 for (
size_t i = 1; i <= M; ++i)
620 left[i] = bg.insert_node(i);
621 left[0] = bg.insert_node(
size_t(0));
622 for (
size_t i = 1; i <= M + 1; ++i)
623 right[i] = bg.insert_node(M + i);
624 bg.insert_arc(left[0], right[1],
size_t(0));
625 for (
size_t i = 1; i <= M; ++i)
627 bg.insert_arc(left[i], right[i], 2 * i - 1);
628 bg.insert_arc(left[i], right[i + 1], 2 * i);
631 hopcroft_karp_matching<Chain_Graph, SA>(bg, matching, sa);
632 r.matched = matching.
size();
636 void expect_the_results_of_the_chains(
const Search_Results &
r)
639 EXPECT_TRUE(
r.connected);
640 EXPECT_EQ(
r.components, M);
642 EXPECT_TRUE(
r.has_cycle);
643 EXPECT_TRUE(
r.strong);
644 EXPECT_EQ(
r.cycle, M + 1);
645 EXPECT_TRUE(
r.acyclique);
646 EXPECT_FALSE(
r.path_has_cycle);
647 EXPECT_EQ(
r.path, M);
648 EXPECT_EQ(
r.sub_arcs, M - 1);
649 EXPECT_EQ(
r.list_nodes, M);
650 EXPECT_EQ(
r.count, 1u);
651 EXPECT_EQ(
r.cuts, 1u);
652 EXPECT_EQ(
r.block_nodes, M - 1);
653 EXPECT_EQ(
r.blocks, 2u);
654 EXPECT_EQ(
r.tree_arcs, M - 1);
655 EXPECT_EQ(
r.tree_height, M);
656 EXPECT_TRUE(
r.cycle_found);
657 EXPECT_TRUE(
r.to_end);
658 EXPECT_EQ(
r.matched, M + 1);
667TEST(DeepChain, TheSearchesTakeAFilterThatCannotBeAssigned)
669 const size_t hidden = size_t(-1);
670 auto lambda = [hidden](
auto * a) {
return a->get_info() != hidden; };
671 static_assert(not std::is_copy_assignable_v<
decltype(lambda)>);
672 static_assert(not std::is_copy_assignable_v<Const_Filter>);
673 static_assert(not std::is_nothrow_move_constructible_v<Const_Filter>);
675 Search_Results by_lambda, by_const;
676 run_with_small_stack([&]
678 by_lambda = run_the_searches(lambda);
679 by_const = run_the_searches(Const_Filter());
682 SCOPED_TRACE(
"a lambda that captures");
683 expect_the_results_of_the_chains(by_lambda);
686 SCOPED_TRACE(
"a filter with a const member");
687 expect_the_results_of_the_chains(by_const);
Planarity testing on Aleph graphs.
Tarjan's algorithm for strongly connected components.
Build a mapped subgraph from a graph starting at a given node.
Computation of cut nodes (articulation points) of a graph.
void map_cut_graph(GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Computes the mapped cut graph of a graph.
void map_subgraph(GT &sg, typename GT::Node *gsrc, const long &color)
long paint_subgraphs()
Paints the connected components around the cut nodes.
Stateful depth-first traversal functor.
Generic directed graph (digraph) wrapper template.
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.
size_t size() const noexcept
Return the number of items of the list, in constant time.
Compute a depth-first spanning tree of a graph.
Performs a depth-first search for a path between a pair of nodes.
Functor class to convert a tree graph to Tree_Node structure.
Determines whether a graph contains cycles.
Determines whether a graph is acyclic (contains no cycles).
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Number Theoretic Transform over Z / MOD Z.
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.
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_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.
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.
Test whether a cycle is reachable from a given node.
Checks whether a path exists between two nodes.
Computes topological ordering using depth-first search.
List< typename GT::Node * > perform(const GT &g)
Compute topological ordering using DFS.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Compute the connected components of a graph.
size_t count_components(const GT &g)
Count the number of connected components in a graph.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Determines if a graph g is connected.
Pictures of depth-first trees with their df and low numbers.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Convert spanning tree graphs to Tree_Node structures.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
void kosaraju_connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Compute strongly connected components using Kosaraju's algorithm.
#define NODE_COUNTER(p)
Get the counter of a node.
DynList< GT > inconnected_components(const GT &g)
Forward declaration for inconnected_components().
bool test_for_cycle(const GT &g, typename GT::Node *src)
Search for a cycle reachable from a given node.
Planarity_Test_Result< GT > planarity_test(const GT &g, SA sa=SA(), const Planarity_Test_Options &options=Planarity_Test_Options())
Execute a comprehensive planarity test on an Aleph graph.
Df_Low_Tree_Node * build_df_low_tree(const GT &g, typename GT::Node *src, DynList< Df_Low_Non_Tree_Arc > &non_tree_arcs, Node_Label node_label=Node_Label(), SA sa=SA())
Build the depth-first spanning tree of an undirected graph, with the df and low numbers of its nodes.
void hopcroft_karp_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes a maximum cardinality matching on a bipartite graph using the Hopcroft-Karp algorithm.
long paint_subgraphs(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Paint connected blocks around articulation points.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
bool test_for_path(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Return true if there is a path between two nodes.
GT find_depth_first_spanning_tree(const GT &g, typename GT::Node *gnode)
Build a depth-first spanning tree (mapped to the original graph).
DynList< typename GT::Arc * > find_bridges(const GT &g, typename GT::Node *start, SA sa=SA())
Find all bridge edges in a connected undirected graph.
bool is_graph_acyclique(const GT &g, typename GT::Node *start_node)
Return true if an undirected graph is acyclic.
GT map_subgraph(const GT &g, const long color)
Extract a mapped subgraph containing a given color.
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Depth-first search of a path between two nodes.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Kosaraju's algorithm for finding strongly connected components.
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool between(const Point &a, const Point &b, const Point &c)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
bool run_with_small_stack(F &&f, size_t kb=256)
Runs f in a thread whose stack has kb KiB, and returns false if the thread could not run; without POS...
bool has_cycle(const Spec &s)
Whether the multigraph, not directed, has a cycle.
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
static struct argp_option options[]
Arc of graph implemented with double-linked adjacency lists.
Configuration options for planarity testing and auxiliary outputs.
bool compute_embedding
If true and the graph is found to be planar, computes a combinatorial embedding (rotation system and ...
Topological sorting algorithms for directed acyclic graphs (DAGs).
Bipartite graph detection and 2-coloring.
Graph connectivity and connected components.
Articulation points (cut nodes), bridges, and biconnected components.
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Spanning tree generation algorithms.
DAG (acyclic) graph testing.
Graph connectivity testing.
Cycle detection in graphs.