Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_digraph_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
47#include <algorithm>
48#include <array>
49#include <set>
50#include <string>
51#include <utility>
52#include <vector>
53
54#include <gtest/gtest.h>
55
56#include <tpl_graph.H>
57#include <Tarjan.H>
58#include <kosaraju.H>
59#include <topological_sort.H>
60#include <tpl_find_path.H>
61#include <tpl_test_path.H>
62#include <Bidirectional_BFS.H>
63#include <warshall.H>
64#include <eulerian.H>
65#include <Dominators.H>
66
68#include "graph_harness.H"
69#include "graph_oracles.H"
70
71using namespace Aleph;
72using namespace graph_harness;
73namespace oracle = graph_oracles;
75
76namespace
77{
79 struct Walk
80 {
81 std::vector<int> nodes;
82 std::vector<int> arcs;
83
84 [[nodiscard]] std::string str() const
85 {
86 std::string r = "[";
87 for (size_t i = 0; i < nodes.size(); ++i)
88 {
89 r += std::to_string(nodes[i]);
90 if (i < arcs.size())
91 r += " -" + std::to_string(arcs[i]) + "-> ";
92 }
93 return r + "]";
94 }
95 };
96
97 template <class GT>
98 Walk walk_of(const Path<GT> & p)
99 {
100 Walk w;
101 for (typename Path<GT>::Iterator it(p); it.has_current_node(); it.next_ne())
102 {
103 w.nodes.push_back(num(it.get_current_node_ne()));
104 if (it.has_current_arc())
105 w.arcs.push_back(num(it.get_current_arc_ne()));
106 }
107 return w;
108 }
109
113 std::string walk_error(const Spec & s, const Walk & w, unsigned long hidden)
114 {
115 if (w.nodes.empty())
116 return "an empty path";
117 if (w.arcs.size() + 1 != w.nodes.size())
118 return "the path " + w.str() + " lacks arcs";
119 for (size_t i = 0; i < w.arcs.size(); ++i)
120 {
121 const int a = w.arcs[i];
122 if (a < 0 or size_t(a) >= s.arcs.size())
123 return "the path " + w.str() + " has an arc that is not of the graph";
124 if (s.arcs[size_t(a)][0] != w.nodes[i] or s.arcs[size_t(a)][1] != w.nodes[i + 1])
125 return "the path " + w.str() + " does not follow the arc " + std::to_string(a);
126 if (not graph_filters::shown(hidden, a))
127 return "the path " + w.str() + " takes the hidden arc " + std::to_string(a);
128 }
129 return "";
130 }
131
134 std::string path_error(const Spec & s, const Walk & w, int u, int v,
135 unsigned long hidden = 0)
136 {
137 if (auto e = walk_error(s, w, hidden); not e.empty())
138 return e;
139 if (w.nodes.front() != u or w.nodes.back() != v)
140 return "the path " + w.str() + " does not go from " + std::to_string(u)
141 + " to " + std::to_string(v);
142 if (std::set<int>(w.nodes.begin(), w.nodes.end()).size() != w.nodes.size())
143 return "the path " + w.str() + " repeats a node";
144 return "";
145 }
146
149 std::string cycle_error(const Spec & s, const Walk & w, unsigned long hidden = 0)
150 {
151 if (auto e = walk_error(s, w, hidden); not e.empty())
152 return e;
153 if (w.arcs.empty() or w.nodes.front() != w.nodes.back())
154 return "the cycle " + w.str() + " is not closed";
155 if (std::set<int>(w.nodes.begin() + 1, w.nodes.end()).size() != w.arcs.size())
156 return "the cycle " + w.str() + " repeats a node";
157 return "";
158 }
159
160 template <class List>
161 std::vector<int> sequence(const List & l)
162 {
163 std::vector<int> r;
164 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
165 r.push_back(num(it.get_curr()));
166 return r;
167 }
168
169 std::string text(const std::vector<int> & v)
170 {
171 std::string r = "(";
172 for (size_t i = 0; i < v.size(); ++i)
173 r += (i == 0 ? "" : " ") + std::to_string(v[i]);
174 return r + ")";
175 }
176
180 bool put_block(std::vector<int> & part, const std::vector<int> & nums)
181 {
182 if (nums.empty())
183 return false;
184 const int least = *std::min_element(nums.begin(), nums.end());
185 for (int x : nums)
186 {
187 if (x < 0 or size_t(x) >= part.size() or part[size_t(x)] != -1)
188 return false;
189 part[size_t(x)] = least;
190 }
191 return true;
192 }
193
196 template <class Lists>
197 std::vector<int> partition_of_lists(const Lists & blocks, int n)
198 {
199 std::vector<int> part(size_t(n), -1);
200 for (auto it = blocks.get_it(); it.has_curr(); it.next_ne())
201 if (not put_block(part, sequence(it.get_curr())))
202 return {};
203 if (std::count(part.begin(), part.end(), -1) > 0)
204 return {};
205 return part;
206 }
207
213 template <class GT, class Blocks>
214 std::vector<int> partition_of_blocks(const Blocks & blocks, const Spec & s,
215 std::vector<int> & inside, bool & lost_info)
216 {
217 std::vector<int> part(size_t(s.n), -1);
218 inside.assign(s.arcs.size(), 0);
219 lost_info = false;
220 for (auto it = blocks.get_it(); it.has_curr(); it.next_ne())
221 {
222 const GT & blk = it.get_curr();
223 std::vector<int> nums;
224 for (auto j = blk.get_node_it(); j.has_curr(); j.next_ne())
225 {
226 auto * bn = j.get_curr();
227 auto * gn = mapped_node<GT>(bn);
228 if (gn == nullptr or num(gn) != num(bn))
229 return {};
230 nums.push_back(num(gn));
231 }
232 if (not put_block(part, nums))
233 return {};
234 for (auto j = blk.get_arc_it(); j.has_curr(); j.next_ne())
235 {
236 auto * ba = j.get_curr();
237 auto * ga = mapped_arc<GT>(ba);
238 if (ga == nullptr or num(ga) < 0 or size_t(num(ga)) >= s.arcs.size()
239 or mapped_arc<GT>(ga) != ba)
240 return {};
241 lost_info = lost_info or num(ga) != num(ba);
242 ++inside[size_t(num(ga))];
243 }
244 }
245 if (std::count(part.begin(), part.end(), -1) > 0)
246 return {};
247 return part;
248 }
249
253 template <class GT, class Blocks, class Arcs>
254 std::string blocks_error(const Spec & s, const Blocks & blocks, const Arcs & between,
255 bool & lost_info)
256 {
257 std::vector<int> inside;
258 const auto part = partition_of_blocks<GT>(blocks, s, inside, lost_info);
259 const auto want = oracle::scc_partition(s);
260 if (part != want)
261 return "the blocks give " + text(part) + ", not " + text(want);
262 for (int a : sequence(between))
263 if (a < 0 or size_t(a) >= s.arcs.size() or inside[size_t(a)]++ != 0)
264 return "the arc " + std::to_string(a) + " is in two places";
265 for (size_t a = 0; a < s.arcs.size(); ++a)
266 {
267 if (inside[a] != 1)
268 return "the arc " + std::to_string(a) + " is missing";
269 // An arc of a block joins two nodes of that block
270 const bool same = want[size_t(s.arcs[a][0])] == want[size_t(s.arcs[a][1])];
271 const auto b = sequence(between);
272 if (same == (std::find(b.begin(), b.end(), int(a)) != b.end()))
273 return "the arc " + std::to_string(a) + " is on the wrong side";
274 }
275 return "";
276 }
277
280 template <class List>
281 struct Tail_Of
282 {
283 const List & list;
284 size_t from = 0;
285
286 struct Iterator
287 {
288 decltype(std::declval<const List &>().get_it()) it;
289
290 Iterator(const List & l, size_t from) : it(l.get_it())
291 {
292 for (size_t i = 0; i < from and it.has_curr(); ++i)
293 it.next_ne();
294 }
295
296 bool has_curr() const { return it.has_curr(); }
297 void next_ne() { it.next_ne(); }
298 decltype(auto) get_curr() const { return it.get_curr(); }
299 };
300
301 Iterator get_it() const { return Iterator(list, from); }
302 };
303
307 template <class GT>
308 std::string contents_of(const GT & blk)
309 {
310 std::set<const void *> mine;
311 std::vector<int> nodes;
312 for (auto j = blk.get_node_it(); j.has_curr(); j.next_ne())
313 {
314 mine.insert(j.get_curr());
315 nodes.push_back(num(j.get_curr()));
316 }
317 std::vector<std::array<int, 3>> arcs;
318 for (auto j = blk.get_arc_it(); j.has_curr(); j.next_ne())
319 {
320 auto * a = j.get_curr();
321 auto * src = blk.get_src_node(a);
322 auto * tgt = blk.get_tgt_node(a);
323 if (mine.count(src) == 0 or mine.count(tgt) == 0)
324 return "an arc toward a node of another block";
325 arcs.push_back({num(src), num(tgt), num(a)});
326 }
327 std::sort(nodes.begin(), nodes.end());
328 std::sort(arcs.begin(), arcs.end());
329 std::string r = "nodes";
330 for (int x : nodes)
331 r += " " + std::to_string(x);
332 r += "; arcs";
333 for (const auto & a : arcs)
334 r += " " + std::to_string(a[0]) + "->" + std::to_string(a[1]) + " #" + std::to_string(a[2]);
335 return r;
336 }
337
339 std::multiset<size_t> sizes_of(const std::vector<int> & part)
340 {
341 std::vector<size_t> count(part.size(), 0);
342 for (int x : part)
343 ++count[size_t(x)];
344 std::multiset<size_t> r;
345 for (size_t c : count)
346 if (c > 0)
347 r.insert(c);
348 return r;
349 }
350
351 int num_blocks(const std::vector<int> & part)
352 {
353 return int(sizes_of(part).size());
354 }
355
358 bool is_order_of(const Spec & s, const std::vector<int> & order,
359 const std::vector<bool> & keep)
360 {
361 std::vector<int> pos(size_t(s.n), -1);
362 for (size_t i = 0; i < order.size(); ++i)
363 {
364 const int x = order[i];
365 if (x < 0 or x >= s.n or not keep[size_t(x)] or pos[size_t(x)] != -1)
366 return false;
367 pos[size_t(x)] = int(i);
368 }
369 for (int x = 0; x < s.n; ++x)
370 if (keep[size_t(x)] and pos[size_t(x)] == -1)
371 return false;
372 for (const auto & a : s.arcs)
373 if (keep[size_t(a[0])] and keep[size_t(a[1])] and pos[size_t(a[0])] >= pos[size_t(a[1])])
374 return false;
375 return true;
376 }
377
378 bool same_class(Eulerian_Type got, oracle::Euler want)
379 {
380 return (got == Eulerian_Type::Cycle) == (want == oracle::Euler::Cycle)
381 and (got == Eulerian_Type::Path) == (want == oracle::Euler::Path);
382 }
383
384 const char * name_of(oracle::Euler e)
385 {
386 return e == oracle::Euler::Cycle ? "Cycle" : e == oracle::Euler::Path ? "Path" : "None";
387 }
388
389 const char * name_of(Eulerian_Type e)
390 {
391 return e == Eulerian_Type::Cycle ? "Cycle" : e == Eulerian_Type::Path ? "Path" : "None";
392 }
393
397 bool is_euler_node_walk(const Spec & s, const std::vector<int> & nodes, bool closed)
398 {
399 if (s.arcs.empty())
400 return nodes.empty();
401 if (nodes.size() != s.arcs.size() + 1)
402 return false;
403 std::multiset<std::pair<int, int>> want, got;
404 for (const auto & a : s.arcs)
405 want.insert({a[0], a[1]});
406 for (size_t i = 0; i + 1 < nodes.size(); ++i)
407 got.insert({nodes[i], nodes[i + 1]});
408 return got == want and (not closed or nodes.front() == nodes.back());
409 }
410
412 template <class Node>
413 int num_or_none(Node * p)
414 {
415 return p == nullptr ? -1 : num(p);
416 }
417}
418
419template <class GT>
420class ExhaustiveDigraph : public ::testing::Test {};
421
423
424TYPED_TEST(ExhaustiveDigraph, StrongComponentsMatchTheOracle)
425{
426 using GT = TypeParam;
427 using Node = typename GT::Node;
428 Check lists("Tarjan_Connected_Components::connected_components(g, lists)");
429 Check blocks("Tarjan_Connected_Components::connected_components(g, blocks, arcs)");
430 Check count("Tarjan_Connected_Components::num_connected_components()");
431 Check sizes("Tarjan_Connected_Components::connected_components(g, sizes)");
432 Check strong("Tarjan_Connected_Components::test_connectivity()");
433 Check k_lists("kosaraju_connected_components(g)");
434 Check k_blocks("kosaraju_connected_components(g, blocks, arcs)");
435 Check k_infos("kosaraju_connected_components(g, blocks, arcs), the infos");
436 Check infos("Tarjan_Connected_Components::connected_components(g, blocks, arcs), the infos");
437 Check k_count("kosaraju_scc_count()");
438 Check k_strong("is_strongly_connected()");
439 Check reused("Tarjan_Connected_Components::connected_components(g, blocks, arcs), "
440 "into lists that hold the blocks of g");
441 Check k_reused("kosaraju_connected_components(g, blocks, arcs), into lists that hold "
442 "the blocks of g");
443
444 // Runs blocks_of twice into the same lists: the blocks and arcs of the
445 // first time do not change, and those of the second go after them, with
446 // g mapped to them
447 auto check_reused = [] (Check & check, const Spec & s, auto blocks_of)
448 {
449 check.run();
450 DynList<GT> blk;
452 blocks_of(blk, arcs);
453 std::vector<std::string> before;
454 for (auto it = blk.get_it(); it.has_curr(); it.next_ne())
455 before.push_back(contents_of(it.get_curr()));
456 const auto arcs_before = sequence(arcs);
457
458 blocks_of(blk, arcs);
459 std::vector<std::string> after;
460 for (auto it = blk.get_it(); it.has_curr() and after.size() < before.size(); it.next_ne())
461 after.push_back(contents_of(it.get_curr()));
462 auto arcs_after = sequence(arcs);
463 arcs_after.resize(std::min(arcs_after.size(), arcs_before.size()));
464 bool lost = false;
465 if (after != before)
466 check.fail(s, "the blocks of the first time changed");
467 else if (arcs_after != arcs_before)
468 check.fail(s, "the arcs between the blocks of the first time changed");
469 else if (auto e = blocks_error<GT>(s, Tail_Of<DynList<GT>>{blk, before.size()},
470 Tail_Of<DynList<typename GT::Arc *>>{arcs, arcs_before.size()},
471 lost);
472 not e.empty())
473 check.fail(s, "those of the second time: " + e);
474 };
475
476 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
477 {
478 Built<GT> b(s);
479 const auto want = oracle::scc_partition(s);
480 const bool want_strong = s.n <= 1 or num_blocks(want) == 1;
481
482 lists.run();
483 {
486 const auto got = partition_of_lists(l, s.n);
487 if (got != want)
488 lists.fail(s, "gives " + text(got) + ", not " + text(want));
489 }
490
491 blocks.run();
492 {
493 DynList<GT> blk;
496 bool lost = false;
497 if (auto e = blocks_error<GT>(s, blk, arcs, lost); not e.empty())
498 blocks.fail(s, e);
499 infos.run();
500 if (lost)
501 infos.fail(s, "an arc of a block has not the info of its arc");
502 }
503
504 count.run();
505 if (const size_t c = Tarjan_Connected_Components<GT>().num_connected_components(b.g);
506 int(c) != num_blocks(want))
507 count.fail(s, std::to_string(c) + " components, not " + std::to_string(num_blocks(want)));
508
509 sizes.run();
510 {
513 std::multiset<size_t> got;
514 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
515 got.insert(it.get_curr());
516 if (got != sizes_of(want))
517 sizes.fail(s, "the sizes are wrong");
518 }
519
520 strong.run();
522 strong.fail(s, want_strong ? "says not strongly connected" : "says strongly connected");
523
524 k_lists.run();
525 if (const auto got = partition_of_lists(kosaraju_connected_components(b.g), s.n);
526 got != want)
527 k_lists.fail(s, "gives " + text(got) + ", not " + text(want));
528
529 k_blocks.run();
530 {
531 DynList<GT> blk;
534 bool lost = false;
535 if (auto e = blocks_error<GT>(s, blk, arcs, lost); not e.empty())
536 k_blocks.fail(s, e);
537 k_infos.run();
538 if (lost)
539 k_infos.fail(s, "an arc of a block has not the info of its arc");
540 }
541
542 k_count.run();
543 if (const size_t c = kosaraju_scc_count(b.g); int(c) != num_blocks(want))
544 k_count.fail(s, std::to_string(c) + " components, not " + std::to_string(num_blocks(want)));
545
546 k_strong.run();
547 if (is_strongly_connected(b.g) != want_strong)
548 k_strong.fail(s, want_strong ? "says not strongly connected" : "says strongly connected");
549
551 check_reused(reused, s, [&] (DynList<GT> & blk, DynList<typename GT::Arc *> & arcs)
552 {
553 tarjan.connected_components(b.g, blk, arcs);
554 });
555 check_reused(k_reused, s, [&] (DynList<GT> & blk, DynList<typename GT::Arc *> & arcs)
556 {
558 });
559 });
560
561 lists.expect();
562 blocks.expect();
563 infos.expect();
564 count.expect();
565 sizes.expect();
566 strong.expect();
567 k_lists.expect();
568 k_blocks.expect();
569 k_infos.expect();
570 k_count.expect();
571 k_strong.expect();
572 reused.expect();
573 k_reused.expect();
574}
575
576TYPED_TEST(ExhaustiveDigraph, CyclesMatchTheOracle)
577{
578 using GT = TypeParam;
579 Check has_cycle("Tarjan_Connected_Components::has_cycle()");
580 Check is_dag("Tarjan_Connected_Components::is_dag()");
581 Check cycle("Tarjan_Connected_Components::compute_cycle(g, path)");
582 Check from("Tarjan_Connected_Components::compute_cycle(g, src, path)");
583 Check functor("Compute_Cycle_In_Digraph");
584
585 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
586 {
587 Built<GT> b(s);
588 const bool dag = oracle::is_dag(s);
589 const auto r = oracle::closure(s, true);
590
591 has_cycle.run();
593 has_cycle.fail(s, dag ? "finds a cycle" : "finds no cycle");
594
595 is_dag.run();
597 is_dag.fail(s, dag ? "says not a DAG" : "says a DAG");
598
599 cycle.run();
600 {
601 Path<GT> path(b.g);
602 const bool found = Tarjan_Connected_Components<GT>().compute_cycle(b.g, path);
603 if (found == dag)
604 cycle.fail(s, found ? "finds a cycle in a DAG" : "finds no cycle");
605 else if (found)
606 if (auto e = cycle_error(s, walk_of(path)); not e.empty())
607 cycle.fail(s, e);
608 }
609
610 functor.run();
611 {
612 const Path<GT> path = Compute_Cycle_In_Digraph<GT>()(b.g);
613 if (path.is_empty() != dag)
614 functor.fail(s, dag ? "finds a cycle in a DAG" : "finds no cycle");
615 else if (not dag)
616 if (auto e = cycle_error(s, walk_of(path)); not e.empty())
617 functor.fail(s, e);
618 }
619
620 // From each node, a cycle is found if one is reached
621 for (int u = 0; u < s.n; ++u)
622 {
623 bool reached = false;
624 for (int v = 0; v < s.n; ++v)
625 reached = reached or (oracle::reaches(r, u, v) and r[size_t(v)][size_t(v)]);
626
627 from.run();
628 Path<GT> path(b.g);
629 const bool found
630 = Tarjan_Connected_Components<GT>().compute_cycle(b.g, b.nodes[size_t(u)], path);
631 if (found != reached)
632 from.fail(s, "from " + std::to_string(u)
633 + (found ? " finds a cycle that it does not reach" : " finds no cycle"));
634 else if (found)
635 if (auto e = cycle_error(s, walk_of(path)); not e.empty())
636 from.fail(s, "from " + std::to_string(u) + ": " + e);
637 }
638 });
639
640 has_cycle.expect();
641 is_dag.expect();
642 cycle.expect();
643 from.expect();
644 functor.expect();
645}
646
647TYPED_TEST(ExhaustiveDigraph, TopologicalOrdersMatchTheOracle)
648{
649 using GT = TypeParam;
650 using Node = typename GT::Node;
651 Check dfs("Topological_Sort::perform()");
652 Check dfs_op("Topological_Sort::operator()");
653 Check bfs("Q_Topological_Sort::perform()");
654 Check ranks("Q_Topological_Sort::ranks()");
655 Check bfs_ops("Q_Topological_Sort::operator()");
656
657 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
658 {
659 Built<GT> b(s);
660 const auto rank = oracle::kahn_ranks(s);
661 std::vector<bool> keep(size_t(s.n));
662 for (int x = 0; x < s.n; ++x)
663 keep[size_t(x)] = rank[size_t(x)] != -1;
664
665 // The search in depth is defined on a DAG only
666 if (oracle::is_dag(s))
667 {
668 dfs.run();
669 if (const auto o = sequence(Topological_Sort<GT>().template perform<DynList>(b.g));
671 dfs.fail(s, "gives " + text(o));
672
673 dfs_op.run();
676 if (const auto o = sequence(l); not oracle::is_topological_order(s, o))
677 dfs_op.fail(s, "gives " + text(o));
678 }
679
680 // Kahn's algorithm leaves out the nodes that a cycle reaches
681 bfs.run();
682 if (const auto o = sequence(Q_Topological_Sort<GT>().template perform<DynList>(b.g));
683 not is_order_of(s, o, keep))
684 bfs.fail(s, "gives " + text(o));
685
686 auto ranks_error = [&] (const auto & lists)
687 {
688 std::vector<int> got(size_t(s.n), -1);
689 int i = 0;
690 for (auto it = lists.get_it(); it.has_curr(); it.next_ne(), ++i)
691 for (int x : sequence(it.get_curr()))
692 {
693 if (x < 0 or x >= s.n or got[size_t(x)] != -1)
694 return std::string("a node in two ranks");
695 got[size_t(x)] = i;
696 }
697 return got == rank ? std::string() : "gives " + text(got) + ", not " + text(rank);
698 };
699
700 ranks.run();
701 if (auto e = ranks_error(Q_Topological_Sort<GT>().ranks(b.g)); not e.empty())
702 ranks.fail(s, e);
703
704 bfs_ops.run();
705 {
707 Q_Topological_Sort<GT>()(b.g, dl);
709 Q_Topological_Sort<GT>()(b.g, ll);
710 DynDlist<Node *> flat;
711 Q_Topological_Sort<GT>()(b.g, flat);
712 if (auto e = ranks_error(dl); not e.empty())
713 bfs_ops.fail(s, "into a DynDlist of ranks: " + e);
714 else if (auto e2 = ranks_error(ll); not e2.empty())
715 bfs_ops.fail(s, "into a DynList of ranks: " + e2);
716 else if (const auto o = sequence(flat); not is_order_of(s, o, keep))
717 bfs_ops.fail(s, "into a list: gives " + text(o));
718 }
719 });
720
721 dfs.expect();
722 dfs_op.expect();
723 bfs.expect();
724 ranks.expect();
725 bfs_ops.expect();
726}
727
728namespace
729{
734 struct Path_Checks
735 {
736 Check dfs;
737 Check dfs_out;
738 Check bfs;
739 Check bfs_out;
740 Check directed_dfs;
741 Check directed_bfs;
742 Check test_function;
743 Check test_class;
744 Check bibfs;
745
746 explicit Path_Checks(bool self)
747 : dfs(name("Find_Path_Depth_First", self)),
748 dfs_out(name("Find_Path_Depth_First<Out_Iterator>", self)),
749 bfs(name("Find_Path_Breadth_First", self)),
750 bfs_out(name("Find_Path_Breadth_First<Out_Iterator>", self)),
751 directed_dfs(name("Directed_Find_Path::dfs()", self)),
752 directed_bfs(name("Directed_Find_Path::bfs()", self)),
753 test_function(name("test_for_path()", self)),
754 test_class(name("Test_For_Path", self)),
755 bibfs(name("Bidirectional_BFS<Out_Iterator, In_Iterator>", self))
756 {}
757
758 static std::string name(const std::string & search, bool self)
759 {
760 return search + (self ? ", from a node to itself" : "");
761 }
762
763 void expect() const
764 {
765 for (const Check * c : {&dfs, &dfs_out, &bfs, &bfs_out, &directed_dfs, &directed_bfs,
766 &test_function, &test_class, &bibfs})
767 c->expect();
768 }
769 };
770
776 template <class GT>
777 void run_path_searches(Path_Checks & pairs, Path_Checks & self, const Spec & s,
778 unsigned long hidden)
779 {
780 using Filter = Hide_Mask<GT>;
781 Built<GT> b(s);
782 const bool digraph = b.g.is_digraph();
783 std::vector<int> orig;
784 const Spec shown = oracle::without(s, hidden, orig);
785 const auto h = oracle::hops(shown, true);
786
787 for (int u = 0; u < s.n; ++u)
788 for (int v = 0; v < s.n; ++v)
789 {
790 auto * src = b.nodes[size_t(u)];
791 auto * tgt = b.nodes[size_t(v)];
792 const std::string where = std::to_string(u) + " to " + std::to_string(v)
793 + (hidden == 0 ? "" : " hiding " + std::to_string(hidden)) + ": ";
794 Path_Checks & c = u == v ? self : pairs;
795
796 const bool want = h[size_t(u)][size_t(v)] != -1;
797 auto check_path = [&] (Check & check, bool found, const Path<GT> & path, bool shortest)
798 {
799 check.run();
800 if (found != want)
801 {
802 check.fail(s, where + (found ? "finds a path that is not" : "finds no path"));
803 return;
804 }
805 if (not found)
806 return;
807 const Walk w = walk_of(path);
808 if (auto e = path_error(s, w, u, v, hidden); not e.empty())
809 check.fail(s, where + e);
810 else if (shortest and int(w.arcs.size()) != h[size_t(u)][size_t(v)])
811 check.fail(s, where + "the path " + w.str() + " is not a shortest one");
812 };
813
814 auto check_answer = [&] (Check & check, bool found)
815 {
816 check.run();
817 if (found != want)
818 check.fail(s, where + (want ? "finds no path" : "finds a path"));
819 };
820
821 // Node_Arc_Iterator gives the arcs out of a node in a digraph
822 // only
823 if (digraph)
824 {
825 Path<GT> path(b.g);
827 (Filter{hidden})(b.g, src, tgt, path);
828 check_path(c.dfs, found, path, false);
829
830 Path<GT> shortest(b.g);
832 (Filter{hidden})(b.g, src, tgt, shortest);
833 check_path(c.bfs, found_shortest, shortest, true);
834
835 if (hidden == 0)
836 check_answer(c.test_function, test_for_path(b.g, src, tgt));
837 check_answer(c.test_class, Test_For_Path<GT, Filter>(Filter{hidden})(b.g, src, tgt));
838 }
839
840 {
841 Path<GT> path(b.g);
843 (Filter{hidden})(b.g, src, tgt, path);
844 check_path(c.dfs_out, found, path, false);
845 }
846
847 {
848 Path<GT> path(b.g);
850 (Filter{hidden})(b.g, src, tgt, path);
851 check_path(c.bfs_out, found, path, true);
852 }
853
854 {
856 (b.g, Filter{hidden}).dfs(src, tgt);
857 check_path(c.directed_dfs, not path.is_empty(), path, false);
858
860 (b.g, Filter{hidden}).bfs(src, tgt);
861 check_path(c.directed_bfs, not shortest.is_empty(), shortest, true);
862 }
863
864 {
865 Path<GT> path(b.g);
867 (Filter{hidden})(b.g, src, tgt, path);
868 check_path(c.bibfs, found, path, true);
869 }
870 }
871 }
872
875 template <class GT>
876 void check_path_searches(const Domain & d, bool hiding)
877 {
878 Path_Checks pairs(false);
879 Path_Checks self(true);
880 for_each_spec(d, true, [&] (const Spec & s)
881 {
882 for (unsigned long hidden = hiding ? 1 : 0;
883 hidden < (hiding ? 1UL << s.num_arcs() : 1UL); ++hidden)
884 run_path_searches<GT>(pairs, self, s, hidden);
885 });
886 pairs.expect();
887 self.expect();
888 }
889}
890
891TYPED_TEST(ExhaustiveDigraph, PathsMatchTheReachabilityOfTheOracle)
892{
893 check_path_searches<TypeParam>(domain(4, 3), false);
894}
895
896TYPED_TEST(ExhaustiveDigraph, PathsRespectTheFilter)
897{
898 check_path_searches<TypeParam>(domain(3, 3), true);
899}
900
901TYPED_TEST(ExhaustiveDigraph, WarshallMatchesTheClosureOfTheOracle)
902{
903 using GT = TypeParam;
904 Check warshall("warshall_compute_transitive_clausure()");
905
906 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
907 {
908 warshall.run();
909 Built<GT> b(s);
912 const auto r = oracle::closure(s, true);
913 for (int u = 0; u < s.n; ++u)
914 for (int v = 0; v < s.n; ++v)
915 if (bool(int(mat(b.nodes[size_t(u)], b.nodes[size_t(v)]))) != r[size_t(u)][size_t(v)])
916 {
917 warshall.fail(s, "says " + std::string(r[size_t(u)][size_t(v)] ? "no " : "")
918 + "path of one arc or more from " + std::to_string(u)
919 + " to " + std::to_string(v));
920 return;
921 }
922 });
923
924 warshall.expect();
925}
926
927TYPED_TEST(ExhaustiveDigraph, EulerMatchesTheOracle)
928{
929 using GT = TypeParam;
930 Check euler("Test_Eulerian::compute()");
931 Check walk("Find_Eulerian_Path");
932 Check nodes("Find_Eulerian_Path::find_node_sequence()");
933 // Without arcs, Find_Eulerian_Path said None, and Test_Eulerian Cycle
934 Check empty("Find_Eulerian_Path, without arcs");
935
936 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
937 {
938 Built<GT> b(s);
939 const auto want = oracle::euler_class(s, true);
940
941 euler.run();
942 if (const auto got = Test_Eulerian<GT>().compute(b.g); not same_class(got, want))
943 euler.fail(s, std::string("says ") + name_of(got) + ", not " + name_of(want));
944
945 if (want == oracle::Euler::None)
946 return;
947 const bool closed = want == oracle::Euler::Cycle;
948
949 if (s.arcs.empty())
950 {
951 empty.run();
952 const auto r = Find_Eulerian_Path<GT>()(b.g);
953 if (r.type != Eulerian_Type::Cycle or not r.path.is_empty())
954 empty.fail(s, std::string("says ") + name_of(r.type) + " with "
955 + std::to_string(r.path.size()) + " arcs");
956 return;
957 }
958
959 walk.run();
960 {
961 const auto r = Find_Eulerian_Path<GT>()(b.g);
962 const auto arcs = sequence(r.path);
963 if (not same_class(r.type, want))
964 walk.fail(s, std::string("says ") + name_of(r.type) + ", not " + name_of(want));
965 else if (not oracle::is_euler_walk(s, arcs, true, closed))
966 walk.fail(s, "gives the arcs " + text(arcs));
967 }
968
969 nodes.run();
970 if (const auto seq = sequence(Find_Eulerian_Path<GT>().find_node_sequence(b.g));
971 not is_euler_node_walk(s, seq, closed))
972 nodes.fail(s, "gives the nodes " + text(seq));
973 });
974
975 euler.expect();
976 walk.expect();
977 nodes.expect();
978 empty.expect();
979}
980
981TYPED_TEST(ExhaustiveDigraph, DominatorsMatchTheDefinition)
982{
983 using GT = TypeParam;
984 Check idoms("compute_dominators()");
985 Check idom("Lengauer_Tarjan_Dominators::get_idom()");
986 Check dominates("Lengauer_Tarjan_Dominators::dominates()");
987 Check tree("build_dominator_tree()");
988 Check frontiers("compute_dominance_frontiers()");
989 Check post("compute_post_dominators()");
990 Check post_tree("build_post_dominator_tree()");
991 Check post_frontiers("compute_post_dominance_frontiers()");
992
993 // The idoms as numbers, -1 for none, from a list of (node, idom); the
994 // nodes not in the list have -2
995 auto idoms_of = [] (const auto & l, int n)
996 {
997 std::vector<int> r(size_t(n), -2);
998 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
999 r[size_t(num(it.get_curr().first))] = num_or_none(it.get_curr().second);
1000 return r;
1001 };
1002
1003 // The idoms that a tree of arcs from idom to node gives, -2 for the
1004 // nodes out of the tree; the infos of the tree are the numbers
1005 auto tree_idoms = [] (const GT & t, int n)
1006 {
1007 std::vector<int> r(size_t(n), -2);
1008 for (auto it = t.get_node_it(); it.has_curr(); it.next_ne())
1009 r[size_t(num(it.get_curr()))] = -1;
1010 for (auto it = t.get_arc_it(); it.has_curr(); it.next_ne())
1011 {
1012 auto * a = it.get_curr();
1013 r[size_t(num(t.get_tgt_node(a)))] = num(t.get_src_node(a));
1014 }
1015 return r;
1016 };
1017
1018 // The idoms of the oracle in the same form: -2 for the nodes not reached
1019 auto want_idoms = [] (const Spec & s, int root)
1020 {
1021 const auto dom = oracle::dominators(s, root);
1023 for (int v = 0; v < s.n; ++v)
1024 if (not dom[size_t(v)][size_t(v)])
1025 r[size_t(v)] = -2;
1026 return r;
1027 };
1028
1029 // The frontiers as sets of numbers, from a map of node to set of nodes
1030 auto frontiers_text = [] (const std::vector<std::set<int>> & f)
1031 {
1032 std::string r;
1033 for (size_t x = 0; x < f.size(); ++x)
1034 {
1035 r += (x == 0 ? "" : " ") + std::to_string(x) + ":{";
1036 for (int y : f[x])
1037 r += (r.back() == '{' ? "" : " ") + std::to_string(y);
1038 r += "}";
1039 }
1040 return r;
1041 };
1042
1043 auto frontiers_of = [] (const auto & m, int n)
1044 {
1045 std::vector<std::set<int>> r(static_cast<size_t>(n));
1046 for (auto it = m.get_it(); it.has_curr(); it.next_ne())
1047 {
1048 const auto & [node, set] = it.get_curr();
1049 for (auto j = set.get_it(); j.has_curr(); j.next_ne())
1050 r[size_t(num(node))].insert(num(j.get_curr()));
1051 }
1052 return r;
1053 };
1054
1055 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
1056 {
1057 Built<GT> b(s);
1058 for (int root = 0; root < s.n; ++root)
1059 {
1060 auto * rp = b.nodes[size_t(root)];
1061 const auto dom = oracle::dominators(s, root);
1062 const auto want = want_idoms(s, root);
1063 const std::string from = "from " + std::to_string(root) + ": ";
1064
1065 idoms.run();
1066 if (const auto got = idoms_of(compute_dominators(b.g, rp), s.n); got != want)
1067 idoms.fail(s, from + "gives " + text(got) + ", not " + text(want));
1068
1070 idom.run();
1071 for (int v = 0; v < s.n; ++v)
1072 {
1073 const int w = want[size_t(v)] == -2 ? -1 : want[size_t(v)];
1074 if (const int got = num_or_none(lt.get_idom(b.g, rp, b.nodes[size_t(v)]));
1075 got != w)
1076 {
1077 idom.fail(s, from + "the idom of " + std::to_string(v) + " is "
1078 + std::to_string(got) + ", not " + std::to_string(w));
1079 break;
1080 }
1081 }
1082
1083 // Every node dominates itself, as dominates() documents, even if
1084 // it is not reached
1085 dominates.run();
1086 for (int d = 0; d < s.n; ++d)
1087 for (int v = 0; v < s.n; ++v)
1088 if (const bool want_dom = d == v or dom[size_t(d)][size_t(v)];
1089 lt.dominates(b.g, rp, b.nodes[size_t(d)], b.nodes[size_t(v)]) != want_dom)
1090 {
1091 dominates.fail(s, from + std::to_string(d)
1092 + (want_dom ? " dominates " : " does not dominate ")
1093 + std::to_string(v));
1094 d = s.n;
1095 break;
1096 }
1097
1098 tree.run();
1099 {
1100 GT t;
1101 build_dominator_tree(b.g, rp, t);
1102 if (const auto got = tree_idoms(t, s.n); got != want)
1103 tree.fail(s, from + "the tree gives " + text(got) + ", not " + text(want));
1104 }
1105
1106 frontiers.run();
1107 {
1108 const auto got = frontiers_of(compute_dominance_frontiers(b.g, rp), s.n);
1109 if (const auto f = oracle::dominance_frontiers(s, root); got != f)
1110 frontiers.fail(s, from + "gives " + frontiers_text(got) + ", not "
1111 + frontiers_text(f));
1112 }
1113
1114 // The post-dominators are the dominators of the reversed digraph
1115 const Spec rs = oracle::reversed(s);
1116 const auto post_want = want_idoms(rs, root);
1117
1118 post.run();
1119 if (const auto got = idoms_of(compute_post_dominators(b.g, rp), s.n);
1120 got != post_want)
1121 post.fail(s, "to " + std::to_string(root) + ": gives " + text(got)
1122 + ", not " + text(post_want));
1123
1124 post_tree.run();
1125 {
1126 GT t;
1127 build_post_dominator_tree(b.g, rp, t);
1128 if (const auto got = tree_idoms(t, s.n); got != post_want)
1129 post_tree.fail(s, "to " + std::to_string(root) + ": the tree gives "
1130 + text(got) + ", not " + text(post_want));
1131 }
1132
1133 post_frontiers.run();
1134 {
1135 const auto got = frontiers_of(compute_post_dominance_frontiers(b.g, rp), s.n);
1136 if (const auto f = oracle::dominance_frontiers(rs, root); got != f)
1137 post_frontiers.fail(s, "to " + std::to_string(root) + ": gives "
1138 + frontiers_text(got) + ", not " + frontiers_text(f));
1139 }
1140 }
1141 });
1142
1143 idoms.expect();
1144 idom.expect();
1145 dominates.expect();
1146 tree.expect();
1147 frontiers.expect();
1148 post.expect();
1149 post_tree.expect();
1150 post_frontiers.expect();
1151}
1152
1153template <class GT>
1154class ExhaustiveDirectedUse : public ::testing::Test {};
1155
1157
1158TYPED_TEST(ExhaustiveDirectedUse, PathsMatchTheReachabilityOfTheOracle)
1159{
1160 check_path_searches<TypeParam>(domain(4, 3), false);
1161}
1162
1163TYPED_TEST(ExhaustiveDirectedUse, PathsRespectTheFilter)
1164{
1165 check_path_searches<TypeParam>(domain(3, 3), true);
1166}
Bidirectional BFS for shortest unweighted path.
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Tarjan's algorithm for strongly connected components.
WeightedDigraph::Node Node
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Bidirectional BFS for finding shortest unweighted paths.
Bit matrix for graph connectivity.
Determines if a digraph contains a cycle and constructs it.
Definition Tarjan.H:1105
Path search on directed graphs defined through a graph class (not a digraph).
Path< GT > dfs(typename GT::Node *start, Op &op)
Path< GT > bfs(typename GT::Node *start, Op &op)
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.
Definition eulerian.H:477
Performs a breadth-first search for a path between a pair of nodes.
Performs a depth-first search for a path between a pair of nodes.
Computes dominator tree and dominance frontiers of a digraph using the Lengauer-Tarjan algorithm.
Definition Dominators.H:156
bool dominates(GT &g, Node *root, Node *d, Node *v)
Test whether node d dominates node v.
Definition Dominators.H:510
Node * get_idom(GT &g, Node *root, Node *node)
Get the immediate dominator of a specific node.
Definition Dominators.H:486
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:4512
Path on a graph.
Definition tpl_graph.H:3832
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
Computes topological ordering using breadth-first search (Kahn's algorithm).
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
Definition Tarjan.H:174
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
Tests whether a graph or digraph is Eulerian.
Definition eulerian.H:241
Checks whether a path exists between two nodes.
Computes topological ordering using depth-first search.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
Eulerian graph detection and path/cycle finding.
__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
TYPED_TEST_SUITE(ExhaustiveDigraph, Directed_Kinds)
TYPED_TEST(ExhaustiveDigraph, StrongComponentsMatchTheOracle)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
void build_post_dominator_tree(const GT &g, typename GT::Node *exit_node, GT &tree, SA sa=SA())
Build the post-dominator tree of a digraph.
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
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
Definition Dominators.H:678
size_t kosaraju_scc_count(const GT &g)
Count the number of strongly connected components.
Definition kosaraju.H:445
void warshall_compute_transitive_clausure(GT &g, Bit_Mat_Graph< GT, SA > &mat, SA sa=SA())
Computes the transitive closure of a graph using Warshall's algorithm.
Definition warshall.H:84
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_dominators(GT &g, typename GT::Node *root, SA sa=SA())
Compute immediate dominators of a digraph from a root node.
Definition Dominators.H:657
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.
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_post_dominators(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute immediate post-dominators of a digraph from an exit node.
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_post_dominance_frontiers(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute post-dominance frontiers of a digraph.
bool is_strongly_connected(const GT &g)
Check if a directed graph is strongly connected.
Definition kosaraju.H:466
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_dominance_frontiers(GT &g, typename GT::Node *root, SA sa=SA())
Compute dominance frontiers of a digraph.
Definition Dominators.H:700
Kosaraju's algorithm for finding strongly connected components.
static mpfr_t y
Definition mpfr_mul_d.c:3
DFSResult< typename Domain::Distance > dfs(Domain &domain, typename Domain::State &state, SearchPath< typename Domain::Move > &path, const typename Domain::Distance g, const typename Domain::Distance threshold, const size_t depth, IDAStarResult< Solution, typename Domain::Distance > &result, OnSolution &on_solution)
Core recursive DFS used by IDA* for a single threshold pass.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
void expect(std::ifstream &input_stream, const std::string &expected)
Expect and consume a specific string/keyword.
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
Eulerian_Type
Enumeration for Eulerian graph classification.
Definition eulerian.H:114
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Definition ahSort.H:696
bool between(const Point &a, const Point &b, const Point &c)
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
Definition ah-zip.H:252
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
constexpr bool shown(unsigned long hidden, int i) noexcept
Whether the arc number i is shown by the mask hidden.
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
bool is_dag(const Spec &s)
Whether the digraph has no directed cycle (a loop is one)
std::vector< std::vector< int > > hops(const Spec &s, bool directed)
hops[u][v]: the least number of arcs of a path from u to v, 0 from u to itself and -1 if there is no ...
Euler euler_class(const Spec &s, bool directed)
The Eulerian class of s: the arcs must be in one component (weakly, in a digraph),...
std::vector< std::vector< bool > > dominators(const Spec &s, int root)
dom[d][v]: d dominates v from root in a digraph, that is, v is reached from root and every path from ...
std::vector< std::vector< bool > > closure(const Spec &s, bool directed)
reach[u][v]: v is reached from u by a directed path of one arc or more (in a graph that is not direct...
std::vector< std::set< int > > dominance_frontiers(const Spec &s, int root)
The dominance frontier of each node from root: y is in that of x if x dominates a predecessor of y an...
bool reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
std::vector< int > kahn_ranks(const Spec &s)
The rank that Kahn's algorithm gives each node of a digraph: 0 for a node without arcs in,...
bool is_topological_order(const Spec &s, const std::vector< int > &order)
Whether order, a sequence of all the nodes, is a topological order.
Spec without(const Spec &s, unsigned long hidden, std::vector< int > &orig)
The spec without the arcs whose numbers are set in hidden, with orig[i] the number in s of its i-th a...
Euler
What kind of Eulerian walk the multigraph has.
bool is_euler_walk(const Spec &s, const std::vector< int > &arcs, bool directed, bool closed)
Whether arcs, by number, walk every arc of s once, each from the node where the one before ended (fro...
Spec reversed(const Spec &s)
The spec with each arc reversed; the arcs keep their numbers.
std::vector< int > immediate_dominators(const Spec &s, int root)
The immediate dominator of each node from root: the strict dominator that all the others dominate; -1...
std::vector< int > scc_partition(const Spec &s)
The strongly connected components, as the smallest node of the component of each node.
STL namespace.
Hides the arcs of a mask; copyable, assignable, noexcept.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
Which multigraphs for_each_spec() gives.
A small multigraph: its nodes are 0 .
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
bool check(GT &g)
Gsl_Compatible_Rng r
Topological sorting algorithms for directed acyclic graphs (DAGs).
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Path existence testing.
DynList< int > l
Warshall's algorithm for transitive closure.