55#include <gtest/gtest.h>
79 std::set<int> arc_numbers(
const List &
l)
82 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
83 r.insert(it.get_curr()->get_info());
92 std::vector<int> node_order(
const List &
l)
95 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
96 r.push_back(it.get_curr()->get_info());
101 Spec cycle_through_hidden()
105 s.
arcs = {{0, 1}, {1, 2}, {2, 0}};
111 Spec path_and_shortcut()
115 s.
arcs = {{0, 1}, {1, 2}, {0, 2}};
125 for (
int u = 0; u < 5; ++u)
126 for (
int v = u + 1; v < 5; ++v)
127 s.
arcs.push_back({u, v});
133 template <
class Node,
class List>
136 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
137 if (it.get_curr().first == v)
138 return it.get_curr().second;
150 using GT = TypeParam;
155 for_each_filter<GT>([&] (
auto sa,
const char * name)
157 using SA =
decltype(sa);
159 constexpr bool h = hides<GT, SA>;
160 const size_t want =
h ? 3 : 1;
164 Tarjan_Connected_Components<GT, Out_Iterator, SA> t(sa);
167 DynList<Arc *> cross;
168 t.connected_components(g, blocks, cross);
169 EXPECT_EQ(blocks.size(), want);
170 EXPECT_EQ(arc_numbers(cross), (h ? std::set<int>{0, 1} : std::set<int>{}));
173 t.connected_components(g, lists);
174 EXPECT_EQ(lists.
size(), want);
175 EXPECT_EQ(t.connected_components(g).size(), want);
176 EXPECT_EQ(t.num_connected_components(g), want);
178 t.connected_components(g, sizes);
179 EXPECT_EQ(sizes.
size(), want);
183 t(g, blocks2, cross2);
184 EXPECT_EQ(blocks2.
size(), want);
187 EXPECT_EQ(lists2.
size(), want);
188 EXPECT_EQ(t(g).
size(), want);
191 t(g, blocks3, cross3);
192 EXPECT_EQ(blocks3.
size(), want);
195 EXPECT_EQ(lists3.
size(), want);
197 EXPECT_EQ(t.has_cycle(g), not
h);
198 EXPECT_EQ(t.is_dag(g),
h);
200 EXPECT_EQ(t.compute_cycle(g, p), not
h);
201 EXPECT_EQ(arc_numbers(p.
arcs()), (
h ? std::set<int>{} : std::set<int>{0, 1, 2}));
202 EXPECT_EQ(t.compute_cycle(g, b.
nodes[0], p), not
h);
203 EXPECT_EQ(t.test_connectivity(g), not
h);
205 EXPECT_TRUE(t.has_computation());
206 EXPECT_EQ(t.get_graph(), &b.
g);
207 static_cast<void>(t.get_filter());
208 static_cast<void>(std::as_const(t).get_filter());
214 if constexpr (std::is_move_assignable_v<SA>)
217 other = std::move(moved);
226 using GT = TypeParam;
229 for_each_filter<GT>([&] (
auto sa,
const char * name)
231 using SA =
decltype(sa);
233 constexpr bool h = hides<GT, SA>;
237 const Compute_Cycle_In_Digraph<GT, Out_Iterator, SA> cycle(sa);
239 EXPECT_EQ(cycle(g, p), not h);
242 EXPECT_EQ(arc_numbers(p.arcs()), (std::set<int>{0, 1, 2}));
244 EXPECT_EQ(cycle(g).is_empty(),
h);
245 EXPECT_EQ(cycle(g, b.
nodes[0]).is_empty(),
h);
252 using GT = TypeParam;
255 const std::vector<int> order = {0, 1, 2};
257 for_each_filter<GT>([&] (
auto sa,
const char * name)
259 using SA =
decltype(sa);
261 constexpr bool h = hides<GT, SA>;
265 Topological_Sort<GT, Out_Iterator, SA> lvalue(sa);
266 const auto l = lvalue.template perform<DynList>(g);
267 EXPECT_EQ(l.size(), 3U);
270 EXPECT_EQ(node_order(l), order);
272 EXPECT_EQ(lvalue.template perform<DynDlist>(g).size(), 3U);
277 EXPECT_EQ(dl.
size(), 3U);
280 EXPECT_EQ(node_order(dl), order);
288 using GT = TypeParam;
291 const std::vector<int> order = {0, 1, 2};
293 for_each_filter<GT>([&] (
auto sa,
const char * name)
295 using SA =
decltype(sa);
297 constexpr bool h = hides<GT, SA>;
299 const size_t want =
h ? 3 : 0;
302 Q_Topological_Sort<GT, Out_Iterator, SA> lvalue(sa);
303 const auto l = lvalue.template perform<DynList>(g);
304 EXPECT_EQ(l.size(), want);
307 EXPECT_EQ(node_order(l), order);
309 EXPECT_EQ(lvalue.template perform<DynDlist>(g).size(), want);
310 EXPECT_EQ(lvalue.template ranks<>(g).size(), want);
311 EXPECT_EQ((lvalue.template ranks<DynDlist, DynList>(g).size()), want);
316 EXPECT_EQ(r1.
size(), want);
319 EXPECT_EQ(r2.
size(), want);
322 EXPECT_EQ(r3.
size(), want);
329 using GT = TypeParam;
331 auto *
const n0 = b.
nodes[0];
332 auto *
const n1 = b.
nodes[1];
333 auto *
const n2 = b.
nodes[2];
335 for_each_filter<GT>([&] (
auto sa,
const char * name)
337 using SA =
decltype(sa);
340 constexpr bool h = hides<GT, SA>;
345 EXPECT_EQ(idoms.size(), 3U);
346 EXPECT_EQ(dominator_in(idoms, n2),
h ? n1 : n0);
347 EXPECT_EQ(d.
get_idom(b.
g, n0, n2),
h ? n1 : n0);
350 EXPECT_EQ(frontiers.find(n1).size(),
h ? 0U : 1U);
354 EXPECT_EQ(tree.get_num_nodes(), 3U);
355 EXPECT_EQ(tree.get_num_arcs(), 2U);
365 static_cast<void>(std::as_const(d).get_filter());
368 EXPECT_EQ(moved.
get_idom(b.
g, n0, n2),
h ? n1 : n0);
371 if constexpr (std::is_move_assignable_v<SA>)
374 other = std::move(moved);
375 EXPECT_EQ(other.
get_idom(b.
g, n0, n2),
h ? n1 : n0);
382 using GT = TypeParam;
384 auto *
const n0 = b.
nodes[0];
385 auto *
const n1 = b.
nodes[1];
386 auto *
const n2 = b.
nodes[2];
388 for_each_filter<GT>([&] (
auto sa,
const char * name)
390 using SA =
decltype(sa);
392 constexpr bool h = hides<GT, SA>;
397 EXPECT_EQ(dominator_in(compute_dominators(g, n0, sa), n2), h ? n1 : n0);
398 const auto frontiers = compute_dominance_frontiers(g, n0, sa);
399 EXPECT_EQ(frontiers.find(n1).size(), h ? 0U : 1U);
411 using GT = TypeParam;
413 auto *
const n0 = b.
nodes[0];
414 auto *
const n1 = b.
nodes[1];
415 auto *
const n2 = b.
nodes[2];
417 for_each_filter<GT>([&] (
auto sa,
const char * name)
419 using SA =
decltype(sa);
424 constexpr bool h = hides<GT, SA>;
427 Lengauer_Tarjan_Post_Dominators<GT, SA> d(sa);
428 const auto ipdoms = d.compute_ipdom(g, n2);
429 EXPECT_EQ(ipdoms.size(), 3U);
430 EXPECT_EQ(dominator_in(ipdoms, n0), h ? n1 : n2);
431 EXPECT_EQ(d.get_ipdom(g, n2, n0), h ? n1 : n2);
432 EXPECT_EQ(d.post_dominates(g, n2, n1, n0), h);
433 const auto frontiers = d.post_dominance_frontiers(g, n2);
434 EXPECT_EQ(frontiers.find(n1).size(), h ? 0U : 1U);
437 d.build_tree(g, n2, tree);
438 EXPECT_EQ(tree.get_num_nodes(), 3U);
439 EXPECT_EQ(tree.get_num_arcs(), 2U);
442 EXPECT_EQ(tree2.get_num_arcs(), 2U);
444 EXPECT_TRUE(d.has_computation());
445 EXPECT_EQ(d.get_graph(), &b.g);
446 EXPECT_EQ(d.get_exit(), n2);
447 static_cast<void>(d.get_filter());
448 static_cast<void>(std::as_const(d).get_filter());
450 Lengauer_Tarjan_Post_Dominators<GT, SA> moved(std::move(d));
451 EXPECT_EQ(moved.get_ipdom(g, n2, n0), h ? n1 : n2);
452 if constexpr (std::is_move_assignable_v<SA>)
454 Lengauer_Tarjan_Post_Dominators<GT, SA> other(sa);
455 other = std::move(moved);
456 EXPECT_EQ(other.get_ipdom(g, n2, n0), h ? n1 : n2);
464 using GT = TypeParam;
466 auto *
const n0 = b.
nodes[0];
467 auto *
const n1 = b.
nodes[1];
468 auto *
const n2 = b.
nodes[2];
470 for_each_filter<GT>([&] (
auto sa,
const char * name)
472 using SA =
decltype(sa);
474 constexpr bool h = hides<GT, SA>;
477 EXPECT_EQ(dominator_in(compute_post_dominators(g, n2, sa), n0), h ? n1 : n2);
479 build_post_dominator_tree(g, n2, tree, sa);
480 EXPECT_EQ(tree.get_num_arcs(), 2U);
481 const auto frontiers = compute_post_dominance_frontiers(g, n2, sa);
482 EXPECT_EQ(frontiers.find(n1).size(), h ? 0U : 1U);
489 using GT = TypeParam;
491 static_assert(not std::is_nothrow_move_constructible_v<F>);
511 using GT = TypeParam;
516 for_each_filter<GT>([&] (
auto sa,
const char * name)
518 using SA =
decltype(sa);
519 constexpr bool hide = hides<GT, SA>;
523 const long k = hide ? 1 : 2;
524 EXPECT_EQ((edge_connectivity<GT, Random_Preflow_Maximum_Flow, SA>(b.
g, sa)),
k);
525 EXPECT_EQ((vertex_connectivity<GT, Random_Preflow_Maximum_Flow, SA>(b.
g, sa)),
k);
529 EXPECT_EQ((compute_min_cut<GT, Heap_Preflow_Maximum_Flow, SA>(b.
g,
l,
r, cut, sa)),
k);
530 EXPECT_EQ(
l.
size() +
r.size(), 3U);
531 EXPECT_EQ(cut.
size(),
size_t(
k));
537 EXPECT_EQ(cut2.
size(),
size_t(
k));
540 if constexpr (std::is_default_constructible_v<SA>)
542 EXPECT_EQ((edge_connectivity<GT, Random_Preflow_Maximum_Flow, SA>(b.
g)), 2);
543 EXPECT_EQ((vertex_connectivity<GT, Random_Preflow_Maximum_Flow, SA>(b.
g)), 2);
546 EXPECT_EQ((compute_min_cut<GT, Heap_Preflow_Maximum_Flow, SA>(b.
g,
l3, r3, cut3)), 2);
554 using GT = TypeParam;
559 for_each_filter<GT>([&] (
auto sa,
const char * name)
561 using SA =
decltype(sa);
563 constexpr bool h = hides<GT, SA>;
566 const size_t want =
h ? 1 : 2;
578 EXPECT_EQ(karger(b.
g, vs, vt, cut, 2), want);
579 EXPECT_EQ(cut.
size(), want);
584 EXPECT_EQ(karger(b.
g, vs, vt, cut), want);
585 EXPECT_EQ(vs.
size() + vt.
size(), 3U);
586 EXPECT_EQ(karger.
fast(b.
g, vs, vt, cut), want);
587 EXPECT_EQ(karger.
fast(b.
g, vs, vt, cut, 2), want);
596 if constexpr (std::is_move_assignable_v<SA>)
599 other = std::move(moved);
607 using GT = TypeParam;
612 for_each_filter<GT>([&] (
auto sa,
const char * name)
614 using SA =
decltype(sa);
615 constexpr bool hide = hides<GT, SA>;
623 EXPECT_EQ(sw(b.
g, vs, vt, cut), hide ? 0 : 1);
624 EXPECT_EQ(vs.
size() + vt.
size(), 3U);
625 EXPECT_EQ(arc_numbers(cut), (hide ? std::set<int>{0} : std::set<int>{0, 1}));
629 if constexpr (std::is_default_constructible_v<SA>)
635 EXPECT_EQ(with_distance.min_cut_weight(b.
g), 1);
642 using GT = TypeParam;
648 EXPECT_FALSE(
noexcept(Karger(std::declval<Karger>())));
649 EXPECT_FALSE(
noexcept(std::declval<Karger &>() = std::declval<Karger>()));
650 EXPECT_TRUE(
noexcept(Plain(std::declval<Plain>())));
651 EXPECT_TRUE(
noexcept(std::declval<Plain &>() = std::declval<Plain>()));
661 using GT = TypeParam;
664 for_each_filter<GT>([&] (
auto sa,
const char * name)
666 using SA =
decltype(sa);
668 constexpr bool h = hides<GT, SA>;
671 const auto plain = planarity_test(g, sa);
672 EXPECT_EQ(plain.is_planar, h);
673 EXPECT_EQ(plain.num_input_arcs, h ? 9U : 10U);
675 Planarity_Test_Options options;
676 options.compute_embedding = true;
677 options.compute_nonplanar_certificate = true;
678 const auto full = planarity_test(g, SA(sa), options);
679 EXPECT_EQ(full.is_planar, h);
680 EXPECT_EQ(full.has_combinatorial_embedding, h);
681 EXPECT_EQ(full.has_nonplanar_certificate, not h);
683 EXPECT_EQ(is_planar_graph(g, sa), h);
684 EXPECT_EQ(is_planar_graph(g, SA(sa), options), h);
686 const Planarity_Test<GT, SA> test(sa);
687 EXPECT_EQ(test(g).is_planar, h);
688 const Planarity_Test<GT, SA> test_with_options(SA(sa), options);
689 EXPECT_EQ(test_with_options(g).has_combinatorial_embedding, h);
690 const Is_Planar_Graph<GT, SA> is_planar(sa);
691 EXPECT_EQ(is_planar(g), h);
692 const Is_Planar_Graph<GT, SA> is_planar_with_options(SA(sa), options);
693 EXPECT_EQ(is_planar_with_options(g), h);
700 using GT = TypeParam;
704 for_each_filter<GT>([&] (
auto sa,
const char * name)
706 using SA =
decltype(sa);
707 constexpr bool hide = hides<GT, SA>;
711 const size_t want = hide ? 2 : 3;
714 DynMapTree<Node *, size_t> colors;
715 EXPECT_EQ((greedy_coloring<GT, SA>(g, colors, sa)), want);
716 EXPECT_TRUE((is_valid_coloring<GT, SA>(g, colors, sa)));
717 EXPECT_EQ((welsh_powell_coloring<GT, SA>(g, colors, sa)), want);
718 EXPECT_TRUE((is_valid_coloring<GT, SA>(g, colors, sa)));
719 EXPECT_EQ((dsatur_coloring<GT, SA>(g, colors, sa)), want);
720 EXPECT_TRUE((is_valid_coloring<GT, SA>(g, colors, sa)));
721 EXPECT_EQ((chromatic_number<GT, SA>(g, colors, sa)), want);
722 EXPECT_TRUE((is_valid_coloring<GT, SA>(g, colors, sa)));
724 const Greedy_Coloring<GT, SA> greedy(sa);
725 EXPECT_EQ(greedy(g, colors), want);
726 const Welsh_Powell_Coloring<GT, SA> welsh_powell(sa);
727 EXPECT_EQ(welsh_powell(g, colors), want);
728 const DSatur_Coloring<GT, SA> dsatur(sa);
729 EXPECT_EQ(dsatur(g, colors), want);
732 if constexpr (std::is_default_constructible_v<SA>)
734 EXPECT_EQ((greedy_coloring<GT, SA>(g, colors)), 3U);
735 EXPECT_TRUE((is_valid_coloring<GT, SA>(g, colors)));
736 EXPECT_EQ((Greedy_Coloring<GT, SA>()(g, colors)), 3U);
737 EXPECT_EQ((Welsh_Powell_Coloring<GT, SA>()(g, colors)), 3U);
738 EXPECT_EQ((DSatur_Coloring<GT, SA>()(g, colors)), 3U);
739 EXPECT_EQ((chromatic_number<GT, SA>(g, colors)), 3U);
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Graph coloring algorithms and heuristics.
Karger's randomized min-cut algorithm.
Planarity testing on Aleph graphs.
Stoer-Wagner deterministic min-cut algorithm.
Tarjan's algorithm for strongly connected components.
WeightedDigraph::Node Node
Functor wrapper for compute_min_cut().
Default distance accessor for arc weights.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic set backed by balanced binary search trees with automatic memory management.
Karger's randomized minimum cut algorithm.
size_t compute_min_cut_size(GT &g, size_t num_iter=0)
Compute only the minimum cut size (without partition info).
unsigned long get_seed() const noexcept
Get the random seed used by this solver (useful for reproducibility).
size_t fast(GT &g, DynList< typename GT::Node * > &vs, DynList< typename GT::Node * > &vt, DynList< typename GT::Arc * > &cut, size_t num_iter=0)
Compute minimum cut using Karger-Stein fast algorithm.
void reseed(const unsigned long new_seed) noexcept
Change the random seed.
Computes dominator tree and dominance frontiers of a digraph using the Lengauer-Tarjan algorithm.
DynMapTree< Node *, DynSetTree< Node * > > dominance_frontiers(GT &g, Node *root)
Compute dominance frontiers for all reachable nodes.
void build_tree(GT &g, Node *root, GT &tree)
Build the dominator tree as a separate graph.
bool has_computation() const noexcept
Check if a computation has been performed.
SA & get_filter() noexcept
Get the arc filter.
long get_num_reachable() const noexcept
Get the number of reachable nodes in the last computation.
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.
DynList< std::pair< Node *, Node * > > compute_idom(GT &g, Node *root)
Compute immediate dominators.
Node * get_root() const noexcept
Get the root of the last computation.
GT * get_graph() const noexcept
Get the graph of the last computation.
Computes post-dominator tree and post-dominance frontiers of a digraph using the Lengauer-Tarjan algo...
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Computes topological ordering using breadth-first search (Kahn's algorithm).
Stoer-Wagner deterministic minimum cut algorithm.
Weight min_cut_weight(GT &g)
Find only the minimum cut weight (faster, no partition info).
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
bool is_dag(const GT &g)
Determines whether the directed graph is acyclic (a DAG).
Computes topological ordering using depth-first search.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST_SUITE(ContractStructureDigraph, Directed_Kinds)
TYPED_TEST(ContractStructureDigraph, TarjanConnectedComponents)
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.
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::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.
::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 > >, 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 > > > All_Kinds
The six kinds, with int infos.
Default filter for filtered iterators on arcs.
A hand-written copy that is not noexcept, which leaves the type without a move: std::move_if_noexcept...
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Topological sorting algorithms for directed acyclic graphs (DAGs).
Generic graph and digraph implementations.
K-connected graph structures and connectivity algorithms.