54#include <gtest/gtest.h>
81 std::vector<int>
nodes;
82 std::vector<int>
arcs;
84 [[nodiscard]] std::string str()
const
87 for (
size_t i = 0; i <
nodes.size(); ++i)
89 r += std::to_string(nodes[i]);
91 r +=
" -" + std::to_string(arcs[i]) +
"-> ";
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()));
113 std::string walk_error(
const Spec & s,
const Walk &
w,
unsigned long hidden)
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)
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);
127 return "the path " +
w.str() +
" takes the hidden arc " + std::to_string(a);
134 std::string path_error(
const Spec & s,
const Walk &
w,
int u,
int v,
135 unsigned long hidden = 0)
137 if (
auto e = walk_error(s,
w, hidden); not e.empty())
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";
149 std::string cycle_error(
const Spec & s,
const Walk &
w,
unsigned long hidden = 0)
151 if (
auto e = walk_error(s,
w, hidden); not e.empty())
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";
160 template <
class List>
161 std::vector<int> sequence(
const List &
l)
164 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
165 r.push_back(
num(it.get_curr()));
169 std::string text(
const std::vector<int> & v)
172 for (
size_t i = 0; i < v.size(); ++i)
180 bool put_block(std::vector<int> & part,
const std::vector<int> & nums)
184 const int least = *std::min_element(nums.begin(), nums.end());
187 if (x < 0 or
size_t(x) >= part.size() or part[
size_t(x)] != -1)
189 part[size_t(x)] = least;
196 template <
class Lists>
197 std::vector<int> partition_of_lists(
const Lists & blocks,
int n)
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())))
203 if (std::count(part.begin(), part.end(), -1) > 0)
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)
217 std::vector<int> part(
size_t(s.
n), -1);
218 inside.assign(s.
arcs.size(), 0);
220 for (
auto it = blocks.get_it(); it.has_curr(); it.next_ne())
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())
226 auto * bn = j.get_curr();
227 auto * gn = mapped_node<GT>(bn);
228 if (gn ==
nullptr or
num(gn) !=
num(bn))
230 nums.push_back(
num(gn));
232 if (not put_block(part, nums))
234 for (
auto j = blk.
get_arc_it(); j.has_curr(); j.next_ne())
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)
241 lost_info = lost_info or
num(ga) !=
num(ba);
242 ++inside[size_t(
num(ga))];
245 if (std::count(part.begin(), part.end(), -1) > 0)
253 template <
class GT,
class Blocks,
class Arcs>
254 std::string blocks_error(
const Spec & s,
const Blocks & blocks,
const Arcs & between,
257 std::vector<int> inside;
258 const auto part = partition_of_blocks<GT>(blocks, s, inside, lost_info);
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)
268 return "the arc " + std::to_string(a) +
" is missing";
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";
280 template <
class List>
288 decltype(std::declval<const List &>().get_it()) it;
290 Iterator(
const List &
l,
size_t from) : it(
l.get_it())
292 for (
size_t i = 0; i < from
and it.has_curr(); ++i)
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(); }
301 Iterator get_it()
const {
return Iterator(list, from); }
308 std::string contents_of(
const GT & blk)
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())
314 mine.insert(j.get_curr());
317 std::vector<std::array<int, 3>>
arcs;
318 for (
auto j = blk.
get_arc_it(); j.has_curr(); j.next_ne())
320 auto * a = j.get_curr();
323 if (mine.count(src) == 0 or mine.count(tgt) == 0)
324 return "an arc toward a node of another block";
328 std::sort(
arcs.begin(),
arcs.end());
329 std::string
r =
"nodes";
333 for (
const auto & a :
arcs)
339 std::multiset<size_t> sizes_of(
const std::vector<int> & part)
341 std::vector<size_t>
count(part.size(), 0);
344 std::multiset<size_t>
r;
345 for (
size_t c :
count)
351 int num_blocks(
const std::vector<int> & part)
353 return int(sizes_of(part).
size());
358 bool is_order_of(
const Spec & s,
const std::vector<int> & order,
359 const std::vector<bool> & keep)
361 std::vector<int> pos(
size_t(s.
n), -1);
362 for (
size_t i = 0; i < order.size(); ++i)
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)
367 pos[size_t(x)] = int(i);
369 for (
int x = 0; x < s.
n; ++x)
370 if (keep[
size_t(x)]
and pos[
size_t(x)] == -1)
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])])
380 return (got == Eulerian_Type::Cycle) == (want == oracle::Euler::Cycle)
381 and (got == Eulerian_Type::Path) == (want == oracle::Euler::Path);
386 return e == oracle::Euler::Cycle ?
"Cycle" : e == oracle::Euler::Path ?
"Path" :
"None";
391 return e == Eulerian_Type::Cycle ?
"Cycle" : e == Eulerian_Type::Path ?
"Path" :
"None";
397 bool is_euler_node_walk(
const Spec & s,
const std::vector<int> &
nodes,
bool closed)
400 return nodes.empty();
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)
408 return got == want
and (not closed or
nodes.front() ==
nodes.back());
412 template <
class Node>
413 int num_or_none(
Node * p)
415 return p ==
nullptr ? -1 :
num(p);
426 using GT = TypeParam;
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 "
447 auto check_reused = [] (
Check &
check,
const Spec & s,
auto blocks_of)
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);
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()));
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()},
473 check.fail(s,
"those of the second time: " + e);
480 const bool want_strong = s.n <= 1 or num_blocks(want) == 1;
486 const auto got = partition_of_lists(
l, s.n);
488 lists.
fail(s,
"gives " + text(got) +
", not " + text(want));
497 if (
auto e = blocks_error<GT>(s, blk,
arcs, lost); not e.empty())
501 infos.
fail(s,
"an arc of a block has not the info of its arc");
506 int(c) != num_blocks(want))
507 count.fail(s, std::to_string(c) +
" components, not " + std::to_string(num_blocks(want)));
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");
522 strong.
fail(s, want_strong ?
"says not strongly connected" :
"says strongly connected");
527 k_lists.
fail(s,
"gives " + text(got) +
", not " + text(want));
535 if (
auto e = blocks_error<GT>(s, blk,
arcs, lost); not e.empty())
539 k_infos.
fail(s,
"an arc of a block has not the info of its arc");
544 k_count.
fail(s, std::to_string(c) +
" components, not " + std::to_string(num_blocks(want)));
548 k_strong.
fail(s, want_strong ?
"says not strongly connected" :
"says strongly connected");
578 using GT = TypeParam;
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");
593 has_cycle.fail(s, dag ?
"finds a cycle" :
"finds no cycle");
597 is_dag.fail(s, dag ?
"says not a DAG" :
"says a DAG");
604 cycle.
fail(s, found ?
"finds a cycle in a DAG" :
"finds no cycle");
606 if (
auto e = cycle_error(s, walk_of(path)); not e.empty())
614 functor.
fail(s, dag ?
"finds a cycle in a DAG" :
"finds no cycle");
616 if (
auto e = cycle_error(s, walk_of(path)); not e.empty())
621 for (
int u = 0; u < s.
n; ++u)
623 bool reached =
false;
624 for (
int v = 0; v < s.
n; ++v)
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"));
635 if (
auto e = cycle_error(s, walk_of(path)); not e.empty())
636 from.
fail(s,
"from " + std::to_string(u) +
": " + e);
649 using GT = TypeParam;
651 Check dfs(
"Topological_Sort::perform()");
652 Check dfs_op(
"Topological_Sort::operator()");
653 Check bfs(
"Q_Topological_Sort::perform()");
655 Check bfs_ops(
"Q_Topological_Sort::operator()");
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;
671 dfs.fail(s,
"gives " + text(o));
677 dfs_op.
fail(s,
"gives " + text(o));
683 not is_order_of(s, o, keep))
684 bfs.
fail(s,
"gives " + text(o));
686 auto ranks_error = [&] (
const auto & lists)
688 std::vector<int> got(
size_t(s.
n), -1);
690 for (
auto it = lists.get_it(); it.has_curr(); it.next_ne(), ++i)
691 for (
int x : sequence(it.get_curr()))
693 if (x < 0 or x >= s.
n or got[
size_t(x)] != -1)
694 return std::string(
"a node in two ranks");
697 return got == rank ? std::string() :
"gives " + text(got) +
", not " + text(rank);
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));
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))
758 static std::string name(
const std::string &
search,
bool self)
760 return search + (self ?
", from a node to itself" :
"");
765 for (
const Check * c : {&
dfs, &dfs_out, &bfs, &bfs_out, &directed_dfs, &directed_bfs,
766 &test_function, &test_class, &bibfs})
777 void run_path_searches(Path_Checks & pairs, Path_Checks & self,
const Spec & s,
778 unsigned long hidden)
782 const bool digraph = b.g.is_digraph();
783 std::vector<int> orig;
787 for (
int u = 0; u < s.
n; ++u)
788 for (
int v = 0; v < s.
n; ++v)
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;
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)
802 check.fail(s, where + (found ?
"finds a path that is not" :
"finds no path"));
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");
814 auto check_answer = [&] (
Check &
check,
bool found)
818 check.fail(s, where + (want ?
"finds no path" :
"finds a path"));
827 (Filter{hidden})(b.g, src, tgt, path);
828 check_path(c.dfs, found, path,
false);
832 (Filter{hidden})(b.g, src, tgt, shortest);
833 check_path(c.bfs, found_shortest, shortest,
true);
843 (Filter{hidden})(b.g, src, tgt, path);
844 check_path(c.dfs_out, found, path,
false);
850 (Filter{hidden})(b.g, src, tgt, path);
851 check_path(c.bfs_out, found, path,
true);
856 (b.g, Filter{hidden}).
dfs(src, tgt);
857 check_path(c.directed_dfs, not path.is_empty(), path,
false);
860 (b.g, Filter{hidden}).
bfs(src, tgt);
861 check_path(c.directed_bfs, not shortest.is_empty(), shortest,
true);
867 (Filter{hidden})(b.g, src, tgt, path);
868 check_path(c.bibfs, found, path,
true);
876 void check_path_searches(
const Domain & d,
bool hiding)
878 Path_Checks pairs(
false);
879 Path_Checks self(
true);
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);
893 check_path_searches<TypeParam>(
domain(4, 3),
false);
898 check_path_searches<TypeParam>(
domain(3, 3),
true);
903 using GT = TypeParam;
904 Check warshall(
"warshall_compute_transitive_clausure()");
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)])
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));
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()");
934 Check empty(
"Find_Eulerian_Path, without arcs");
943 euler.
fail(s, std::string(
"says ") + name_of(got) +
", not " + name_of(want));
945 if (want == oracle::Euler::None)
947 const bool closed = want == oracle::Euler::Cycle;
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");
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));
966 walk.
fail(s,
"gives the arcs " + text(
arcs));
971 not is_euler_node_walk(s, seq, closed))
972 nodes.fail(s,
"gives the nodes " + text(seq));
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()");
995 auto idoms_of = [] (
const auto &
l,
int n)
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);
1005 auto tree_idoms = [] (
const GT & t,
int n)
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())
1012 auto * a = it.get_curr();
1019 auto want_idoms = [] (
const Spec & s,
int root)
1023 for (
int v = 0; v < s.
n; ++v)
1024 if (not dom[
size_t(v)][
size_t(v)])
1030 auto frontiers_text = [] (
const std::vector<std::set<int>> & f)
1033 for (
size_t x = 0; x < f.size(); ++x)
1035 r += (x == 0 ?
"" :
" ") + std::to_string(x) +
":{";
1037 r += (
r.back() ==
'{' ?
"" :
" ") + std::to_string(
y);
1043 auto frontiers_of = [] (
const auto &
m,
int n)
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())
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()));
1062 const auto want = want_idoms(s,
root);
1063 const std::string from =
"from " + std::to_string(
root) +
": ";
1067 idoms.
fail(s, from +
"gives " + text(got) +
", not " + text(want));
1071 for (
int v = 0; v < s.
n; ++v)
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)]));
1077 idom.
fail(s, from +
"the idom of " + std::to_string(v) +
" is "
1078 + std::to_string(got) +
", not " + std::to_string(
w));
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)];
1091 dominates.
fail(s, from + std::to_string(d)
1092 + (want_dom ?
" dominates " :
" does not dominate ")
1093 + std::to_string(v));
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));
1110 frontiers.
fail(s, from +
"gives " + frontiers_text(got) +
", not "
1111 + frontiers_text(f));
1116 const auto post_want = want_idoms(rs,
root);
1121 post.
fail(s,
"to " + std::to_string(
root) +
": gives " + text(got)
1122 +
", not " + text(post_want));
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));
1133 post_frontiers.
run();
1137 post_frontiers.
fail(s,
"to " + std::to_string(
root) +
": gives "
1138 + frontiers_text(got) +
", not " + frontiers_text(f));
1160 check_path_searches<TypeParam>(
domain(4, 3),
false);
1165 check_path_searches<TypeParam>(
domain(3, 3),
true);
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
Bidirectional BFS for finding shortest unweighted paths.
Bit matrix for graph connectivity.
Determines if a digraph contains a cycle and constructs it.
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.
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.
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.
bool dominates(GT &g, Node *root, Node *d, Node *v)
Test whether node d dominates node v.
Node * get_idom(GT &g, Node *root, Node *node)
Get the immediate dominator of a specific node.
Graph_Node< Node_Info > Node
The graph type.
Iterator on nodes and arcs of a path.
bool has_current_node() const noexcept
Return true if the iterator has a current node.
bool is_empty() const noexcept
Return true if the path is empty.
Computes topological ordering using breadth-first search (Kahn's algorithm).
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
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).
Tests whether a graph or digraph is Eulerian.
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.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
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)
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
DynArray< Graph::Arc * > arcs
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.
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
size_t kosaraju_scc_count(const GT &g)
Count the number of strongly connected components.
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.
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.
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.
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.
Kosaraju's algorithm for finding strongly connected components.
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).
Eulerian_Type
Enumeration for Eulerian graph classification.
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.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
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).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
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.
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)
Topological sorting algorithms for directed acyclic graphs (DAGs).
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Warshall's algorithm for transitive closure.