Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_deep_chain_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
43#include <gtest/gtest.h>
44
45#include <cstddef>
46#include <type_traits>
47#include <vector>
48
49#if defined(__unix__) || defined(__APPLE__)
50# include <pthread.h>
51#endif
52
53#include <tpl_graph.H>
54#include <Planarity_Test.H>
55#include <Tarjan.H>
56#include <generate_df_tree.H>
57#include <graph_to_tree.H>
58#include <kosaraju.H>
59#include <topological_sort.H>
60#include <tpl_components.H>
61#include <tpl_cut_nodes.H>
62#include <tpl_find_path.H>
63#include <tpl_graph_utils.H>
64#include <tpl_bipartite.H>
65#include <tpl_spanning_tree.H>
66#include <tpl_test_acyclique.H>
68#include <tpl_test_cycle.H>
69#include <tpl_test_path.H>
70
71using namespace Aleph;
72
73namespace
74{
75 constexpr size_t N = 100000;
76
79
80 // Runs f in a thread with a stack of 256 KiB, in which a recursion as
81 // deep as the chains of these tests overflows. Without POSIX threads,
82 // it runs f in this thread.
83 template <class F>
85 {
86# if defined(__unix__) || defined(__APPLE__)
87 pthread_attr_t attr;
88 ASSERT_EQ(pthread_attr_init(&attr), 0);
89 ASSERT_EQ(pthread_attr_setstacksize(&attr, 256 * 1024), 0);
90 auto start = [](void *arg) -> void *
91 {
92 (*static_cast<F *>(arg))();
93 return nullptr;
94 };
95 pthread_t thread;
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);
100# else
101 f();
102# endif
103 }
104
105 // Inserts in g the chain 0 -> 1 -> ... -> n - 1 and, if closed, the arc
106 // n - 1 -> 0; returns its nodes
107 template <class GT>
108 std::vector<typename GT::Node *> build_chain(GT & g, size_t n, bool closed)
109 {
110 std::vector<typename GT::Node *> nodes;
111 nodes.reserve(n);
112 for (size_t i = 0; i < n; ++i)
113 nodes.push_back(g.insert_node(i));
114 for (size_t i = 0; i + 1 < n; ++i)
115 g.insert_arc(nodes[i], nodes[i + 1], i);
116 if (closed)
117 g.insert_arc(nodes[n - 1], nodes[0], n - 1);
118 return nodes;
119 }
120}
121
122// Tarjan_Connected_Components: each node of an open chain is a component
123TEST(DeepChain, TarjanFindsTheComponentsOfAnOpenChain)
124{
125 Chain_Digraph g;
126 build_chain(g, N, false);
127 size_t num = 0;
128 bool has_cycle = true;
129 bool is_dag = false;
130 DynList<size_t> sizes;
133 run_with_small_stack([&]
134 {
136 num = tarjan.num_connected_components(g);
137 has_cycle = tarjan.has_cycle(g);
138 is_dag = tarjan.is_dag(g);
139 tarjan.connected_components(g, sizes);
140 // a block for each node, in a time that does not grow with the
141 // square of the blocks
142 tarjan.connected_components(g, blocks, between);
143 });
144 EXPECT_EQ(num, N);
145 EXPECT_FALSE(has_cycle);
146 EXPECT_TRUE(is_dag);
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; }));
151 EXPECT_EQ(between.size(), N - 1);
152}
153
154// and a closed chain is one component, and a cycle
155TEST(DeepChain, TarjanFindsTheComponentOfAClosedChain)
156{
157 Chain_Digraph g;
158 build_chain(g, N, true);
162 bool connected = false;
163 bool has_cycle = false;
164 bool found = false;
165 Path<Chain_Digraph> cycle(g);
166 run_with_small_stack([&]
167 {
169 tarjan.connected_components(g, blocks, between);
170 tarjan.connected_components(g, lists);
171 connected = tarjan.test_connectivity(g);
172 has_cycle = tarjan.has_cycle(g);
173 found = tarjan.compute_cycle(g, cycle);
174 });
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);
180 EXPECT_EQ(lists.get_first().size(), N);
181 EXPECT_TRUE(connected);
182 EXPECT_TRUE(has_cycle);
183 ASSERT_TRUE(found);
184 EXPECT_EQ(cycle.size(), N + 1); // the first node closes it
185 EXPECT_EQ(cycle.get_first_node(), cycle.get_last_node());
186}
187
188// Compute_Cut_Nodes and Compute_Bridges: the inner nodes of a path are cut
189// nodes, and its arcs bridges; a cycle has none
190TEST(DeepChain, CutNodesAndBridgesOfAPathAndOfACycle)
191{
192 Chain_Graph path;
193 build_chain(path, N, false);
194 Chain_Graph cycle;
195 build_chain(cycle, N, true);
196 DynDlist<Chain_Graph::Node *> path_cuts, cycle_cuts;
197 size_t path_bridges = 0, cycle_bridges = 1;
198 Chain_Graph cut_graph;
200 run_with_small_stack([&]
201 {
203 cc(path.get_first_node(), path_cuts);
204 cc.paint_subgraphs();
205 cc.map_cut_graph(cut_graph, cross);
206 Compute_Cut_Nodes<Chain_Graph> cycle_cc(cycle);
207 cycle_cc(cycle.get_first_node(), cycle_cuts);
208 path_bridges = find_bridges(path, path.get_first_node()).size();
209 cycle_bridges = find_bridges(cycle, cycle.get_first_node()).size();
210 });
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);
215 EXPECT_TRUE(cycle_cuts.is_empty());
216 EXPECT_EQ(path_bridges, N - 1);
217 EXPECT_EQ(cycle_bridges, 0u);
218}
219
220// The block of a cycle with a node hung from it is as deep as the cycle;
221// painting it and copying it go through it
222TEST(DeepChain, PaintsAndCopiesTheBlockOfACycle)
223{
224 for (bool utils : {false, true})
225 {
226 SCOPED_TRACE(utils ? "tpl_graph_utils.H" : "Compute_Cut_Nodes");
227 Chain_Graph g;
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([&]
233 {
234 if (utils)
235 {
236 auto list = compute_cut_nodes(g, nodes[1]);
237 cuts = list.size();
238 paint_subgraphs(g, list);
239 Chain_Graph block = map_subgraph(g, NODE_COUNTER(nodes[1]));
240 block_nodes = block.get_num_nodes();
241 block_arcs = block.get_num_arcs();
242 return;
243 }
246 cc(nodes[1], list);
247 cuts = list.size();
248 cc.paint_subgraphs();
249 Chain_Graph block;
250 cc.map_subgraph(block, NODE_COUNTER(nodes[1]));
251 block_nodes = block.get_num_nodes();
252 block_arcs = block.get_num_arcs();
253
256 all(nodes[1], again);
257 DynDlist<Chain_Graph> block_list;
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;
263 });
264 EXPECT_EQ(cuts, 1u);
265 EXPECT_EQ(block_nodes, N - 1); // the cut node is not in the block
266 EXPECT_EQ(block_arcs, N - 2);
267 if (not utils)
268 {
269 EXPECT_EQ(blocks, 2u);
270 }
271 }
272}
273
274// Build_Subgraph, Unconnected_Components and inconnected_components()
275TEST(DeepChain, CopiesTheComponentOfAPath)
276{
277 Chain_Graph g;
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([&]
281 {
282 g.reset_nodes();
283 g.reset_arcs();
284 sub_arcs = Build_Subgraph<Chain_Graph>()(g, nodes[N / 2]).get_num_arcs();
285 g.reset_nodes();
286 g.reset_arcs();
288 Build_Subgraph<Chain_Graph>()(g, list, nodes[N / 2]);
289 list_nodes = list.size();
291 auto blocks = inconnected_components(g);
292 utils_nodes = blocks.get_first().get_num_nodes();
293 });
294 EXPECT_EQ(sub_arcs, N - 1);
295 EXPECT_EQ(list_nodes, N);
296 EXPECT_EQ(components, 1u);
297 EXPECT_EQ(utils_nodes, N);
298}
299
300// Find_Path_Depth_First and the tests of a path or a cycle from one node
301TEST(DeepChain, FindsAndTestsThePathOfAChain)
302{
303 Chain_Graph path;
304 auto p = build_chain(path, N, false);
305 Chain_Graph cycle;
306 auto c = build_chain(cycle, N, true);
307 size_t found = 0, free_found = 0, dft = 0, dft_class = 0, tree_arcs = 0,
308 class_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([&]
313 {
314 Path<Chain_Graph> found_path(path);
315 Find_Path_Depth_First<Chain_Graph>()(path, p[0], p[N - 1], found_path);
316 found = found_path.size();
317 free_found = find_path_depth_first(path, p[0], p[N - 1]).size();
318 dft = depth_first_traversal<Chain_Graph>(path, p[0], nullptr);
319 dft_class = Depth_First_Traversal<Chain_Graph>()(path, p[0]);
320 to_end = test_for_path(path, p[0], p[N - 1]);
321 class_to_end = Test_For_Path<Chain_Graph>()(path, p[0], p[N - 1]);
322 cycle_found = test_for_cycle(cycle, c[0]);
323 class_cycle = Test_For_Cycle<Chain_Graph>()(cycle, c[0]);
324 path_acyclique = is_graph_acyclique(path);
325 cycle_acyclique = is_graph_acyclique(cycle, c[0]);
326 class_acyclique = Is_Graph_Acyclique<Chain_Graph>()(path);
327 class_has_cycle = Has_Cycle<Chain_Graph>()(cycle);
328 tree_arcs = find_depth_first_spanning_tree(path, p[0]).get_num_arcs();
329 Chain_Graph tree;
331 class_tree_arcs = tree.get_num_arcs();
332 });
333 EXPECT_EQ(found, N);
334 EXPECT_EQ(free_found, N);
335 EXPECT_EQ(dft, N);
336 EXPECT_EQ(dft_class, N);
337 EXPECT_TRUE(to_end);
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);
347}
348
349// Kosaraju and the topological sort of a directed chain
350TEST(DeepChain, KosarajuAndTopologicalSortOfAChain)
351{
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([&]
360 {
361 open_components = kosaraju_connected_components(open).size();
362 closed_components = kosaraju_connected_components(closed).size();
363 DynList<Chain_Digraph> blk_list;
365 kosaraju_connected_components(closed, blk_list, arcs);
366 blocks = blk_list.size();
368 Topological_Sort<Chain_Digraph>()(open, order);
369 sorted = order.size();
370 first = order.get_first();
371 // into a DynList, whose size() counts, in a time that does not
372 // grow with the square of the nodes
373 auto listed = Topological_Sort<Chain_Digraph>().perform<DynList>(open);
374 sorted_in_list = listed.size();
375 first_in_list = listed.get_first();
376 });
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]);
384}
385
386namespace
387{
388 struct Key_Of_Node
389 {
390 void operator()(Chain_Graph::Node * p, Tree_Node<size_t> * t) const
391 {
392 t->get_key() = p->get_info();
393 }
394 };
395
396 // The height of a tree that is a chain
397 template <class Key>
398 size_t height_of_chain(Tree_Node<Key> * root)
399 {
400 size_t height = 0;
401 for (auto t = root; t != nullptr; t = t->get_left_child())
402 ++height;
403 return height;
404 }
405}
406
407// graph_to_tree_node() and Graph_To_Tree_Node build a tree as deep as the
408// path from which they build it, which destroy_tree() frees
409TEST(DeepChain, BuildsTheTreeOfAPath)
410{
411 Chain_Graph g;
412 auto nodes = build_chain(g, N, false);
413 size_t height = 0, class_height = 0;
414 run_with_small_stack([&]
415 {
416 auto tree = graph_to_tree_node<Chain_Graph, size_t, Key_Of_Node>(g, nodes[0]);
417 g.reset_arcs();
419 height = height_of_chain(tree);
420 class_height = height_of_chain(class_tree);
421 destroy_tree(tree);
422 destroy_tree(class_tree);
423 });
424 EXPECT_EQ(height, N);
425 EXPECT_EQ(class_height, N);
426}
427
428// build_df_low_tree() builds the depth-first tree of a cycle, a path as
429// long as the graph with an arc back to the start
430TEST(DeepChain, BuildsTheDfLowTreeOfACycle)
431{
432 Chain_Graph g;
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([&]
437 {
439 auto root = build_df_low_tree(g, nodes[0], arcs);
440 height = height_of_chain(root);
441 non_tree_arcs = arcs.size();
442 auto last = root;
443 while (last->get_left_child() != nullptr)
444 last = last->get_left_child();
445 low_of_last = last->get_key().low;
447 });
448 EXPECT_EQ(height, N);
449 EXPECT_EQ(non_tree_arcs, 1u); // from the last node to the start
450 EXPECT_EQ(low_of_last, 0);
451}
452
453// L1 ... LN are matched with R1 ... RN in the first phase of Hopcroft-Karp,
454// and L0, whose only arc goes to R1, is left free: the augmenting path from
455// it, L0 R1 L1 R2 ... LN RN+1, goes through all the nodes
456TEST(DeepChain, HopcroftKarpFollowsALongAugmentingPath)
457{
458 Chain_Graph g;
459 std::vector<Chain_Graph::Node *> left(N + 1), right(N + 2);
460 for (size_t i = 1; i <= N; ++i) // L1 first: the coloring starts there
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)
467 {
468 g.insert_arc(left[i], right[i], 2 * i - 1); // the first phase takes it
469 g.insert_arc(left[i], right[i + 1], 2 * i);
470 }
471
472 size_t matched = 0;
473 run_with_small_stack([&]
474 {
476 hopcroft_karp_matching(g, matching);
477 matched = matching.size();
478 });
479 EXPECT_EQ(matched, N + 1);
480}
481
482// The LR planarity test orients and tests the edges in two depth-first
483// searches as deep as the path, and its embedding follows chains of
484// references as long
485TEST(DeepChain, TestsThePlanarityOfACycle)
486{
487 Chain_Graph g;
488 build_chain(g, N, true);
489 bool planar = false, embedded = false;
490 size_t faces = 0;
491 run_with_small_stack([&]
492 {
495 options.embedding_allow_bruteforce_fallback = false;
496 auto result = planarity_test(g, options);
497 planar = result.is_planar;
498 embedded = result.has_combinatorial_embedding;
499 faces = result.embedding_num_faces;
500 });
501 EXPECT_TRUE(planar);
502 EXPECT_TRUE(embedded);
503 EXPECT_EQ(faces, 2u); // inside and outside
504}
505
506namespace
507{
508 // Shows the arcs whose info is not hidden, here all of them. It has a
509 // const member, so that it cannot be assigned, and a copy that may throw,
510 // so that the stack copies the frames that keep it when it grows
511 struct Const_Filter
512 {
513 const size_t hidden = size_t(-1);
514
515 Const_Filter() = default;
516 Const_Filter(const Const_Filter & other) : hidden(other.hidden) {}
517
518 template <class Arc>
519 bool operator()(Arc * a) const { return a->get_info() != hidden; }
520 };
521
522 // What the searches give with a filter that shows every arc of the
523 // chains
524 struct Search_Results
525 {
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;
531 };
532
533 // The nodes of the chains of the searches with a filter: enough for the
534 // stack to grow, and copy its frames, several times
535 constexpr size_t M = 10000;
536
537 // Runs, with sa, the searches that keep a frame for each node of a path,
538 // with its iterator and so its filter, on the chains of M nodes
539 template <class SA>
540 Search_Results run_the_searches(SA sa)
541 {
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);
548
549 Search_Results r;
551 SA copy = sa;
552 r.connected = Test_Connectivity<Chain_Graph, SA>()(path, copy);
553
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);
559 Path<Chain_Digraph> dc(dcycle);
560 if (tarjan.compute_cycle(dcycle, dc))
561 r.cycle = dc.size();
562
563 r.acyclique = Is_Graph_Acyclique<Chain_Graph, SA>(sa)(path);
564 r.path_has_cycle = Has_Cycle<Chain_Graph, SA>(sa)(path);
565
566 Path<Chain_Graph> found(path);
568 find_path(path, p[0], p[M - 1], found);
569 r.path = found.size();
570
571 path.reset_nodes();
572 path.reset_arcs();
573 r.sub_arcs = Build_Subgraph<Chain_Graph, SA>(sa)(path, p[M / 2]).get_num_arcs();
574 path.reset_nodes();
575 path.reset_arcs();
578 build(path, list, p[M / 2]);
579 r.list_nodes = list.size();
581
582 // the block of the cycle, with a node hung from it
583 auto hung = cycle.insert_node(M);
584 cycle.insert_arc(c[0], hung, M);
587 cc(c[1], cuts);
588 r.cuts = cuts.size();
589 cc.paint_subgraphs();
590 Chain_Graph block;
591 cc.map_subgraph(block, NODE_COUNTER(c[1]));
592 r.block_nodes = block.get_num_nodes();
595 all(c[1], again);
596 DynDlist<Chain_Graph> block_list;
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;
602
603 Chain_Graph tree;
605 spanning(path, p[0], tree);
606 r.tree_arcs = tree.get_num_arcs();
607
608 path.reset_arcs();
610 r.tree_height = height_of_chain(root);
612
613 r.cycle_found = Test_For_Cycle<Chain_Graph, SA>(sa)(cycle, c[0]);
614 r.to_end = Test_For_Path<Chain_Graph, SA>(sa)(path, p[0], p[M - 1]);
615
616 // the graph of HopcroftKarpFollowsALongAugmentingPath
617 Chain_Graph bg;
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)
626 {
627 bg.insert_arc(left[i], right[i], 2 * i - 1);
628 bg.insert_arc(left[i], right[i + 1], 2 * i);
629 }
631 hopcroft_karp_matching<Chain_Graph, SA>(bg, matching, sa);
632 r.matched = matching.size();
633 return r;
634 }
635
636 void expect_the_results_of_the_chains(const Search_Results & r)
637 {
638 EXPECT_EQ(r.dft, M);
639 EXPECT_TRUE(r.connected);
640 EXPECT_EQ(r.components, M);
641 EXPECT_TRUE(r.dag);
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);
659 }
660}
661
662// The searches that keep the frame of a node, with its iterator and so its
663// filter, take a filter that cannot be assigned: a lambda that captures,
664// and a filter with a const member, whose copy may throw, so that the stack
665// copies the frames when it grows (#24 of the 7.3.2 review: they assigned
666// the frames, and did not compile with such a filter)
667TEST(DeepChain, TheSearchesTakeAFilterThatCannotBeAssigned)
668{
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>);
674
675 Search_Results by_lambda, by_const;
676 run_with_small_stack([&]
677 {
678 by_lambda = run_the_searches(lambda);
679 by_const = run_the_searches(Const_Filter());
680 });
681 {
682 SCOPED_TRACE("a lambda that captures");
683 expect_the_results_of_the_chains(by_lambda);
684 }
685 {
686 SCOPED_TRACE("a filter with a const member");
687 expect_the_results_of_the_chains(by_const);
688 }
689}
Planarity testing on Aleph graphs.
Tarjan's algorithm for strongly connected components.
WeightedDigraph::Arc Arc
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.
Definition graph-dry.H:4377
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).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
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.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Path on a graph.
Definition tpl_graph.H:3832
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:174
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
Definition Tarjan.H:547
bool test_connectivity(const GT &g)
Tests whether the digraph is strongly connected.
Definition Tarjan.H:1022
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.
Definition Tarjan.H:721
bool compute_cycle(const GT &g, Path< GT > &path)
Finds and constructs a cycle in the digraph, if one exists.
Definition Tarjan.H:972
bool is_dag(const GT &g)
Determines whether the directed graph is acyclic (a DAG).
Definition Tarjan.H:954
size_t num_connected_components(const GT &g)
Returns the number of strongly connected components in the graph.
Definition Tarjan.H:814
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.
Definition ah-dry.H:1666
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Determines if a graph g is connected.
#define TEST(name)
#define N
Definition fib.C:294
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)
Definition gmpfrxx.h:4138
Convert spanning tree graphs to Tree_Node structures.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
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.
Definition kosaraju.H:267
#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.
Definition tpl_graph.H:4695
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)
Definition SSA.H:636
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.
Definition ahAlgo.H:584
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.
Definition ahAlgo.H:127
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[]
Definition ntreepic.C:1890
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
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 ...
Gsl_Compatible_Rng r
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.
Path existence testing.