Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_contract_matrix_structure_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
49#include <set>
50#include <string>
51#include <type_traits>
52#include <utility>
53#include <vector>
54
55#include <gtest/gtest.h>
56
57#include <tpl_graph.H>
58#include <Tarjan.H>
59#include <topological_sort.H>
60#include <Dominators.H>
61#include <tpl_kgraph.H>
62#include <Karger.H>
63#include <Stoer_Wagner.H>
64#include <Planarity_Test.H>
65#include <Graph_Coloring.H>
66
67#include "graph_harness.H"
70
71using namespace Aleph;
72using namespace graph_harness;
73using namespace graph_contract_matrix;
74
75namespace
76{
78 template <class List>
79 std::set<int> arc_numbers(const List & l)
80 {
81 std::set<int> r;
82 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
83 r.insert(it.get_curr()->get_info());
84 return r;
85 }
86} // namespace
87
88namespace
89{
91 template <class List>
92 std::vector<int> node_order(const List & l)
93 {
94 std::vector<int> r;
95 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
96 r.push_back(it.get_curr()->get_info());
97 return r;
98 }
99
101 Spec cycle_through_hidden()
102 {
103 Spec s;
104 s.n = 3;
105 s.arcs = {{0, 1}, {1, 2}, {2, 0}};
106 return s;
107 }
108
111 Spec path_and_shortcut()
112 {
113 Spec s;
114 s.n = 3;
115 s.arcs = {{0, 1}, {1, 2}, {0, 2}};
116 return s;
117 }
118
121 Spec k5()
122 {
123 Spec s;
124 s.n = 5;
125 for (int u = 0; u < 5; ++u)
126 for (int v = u + 1; v < 5; ++v)
127 s.arcs.push_back({u, v});
128 return s;
129 }
130
133 template <class Node, class List>
134 Node * dominator_in(const List & l, Node * v)
135 {
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;
139 return nullptr;
140 }
141} // namespace
142
143template <class GT>
144class ContractStructureDigraph : public ::testing::Test {};
145
147
148TYPED_TEST(ContractStructureDigraph, TarjanConnectedComponents)
149{
150 using GT = TypeParam;
151 using Node = typename GT::Node;
152 using Arc = typename GT::Arc;
153 Built<GT> b(cycle_through_hidden());
154
155 for_each_filter<GT>([&] (auto sa, const char * name)
156 {
157 using SA = decltype(sa);
158 SCOPED_TRACE(name);
159 constexpr bool h = hides<GT, SA>;
160 const size_t want = h ? 3 : 1; // the hidden arc closes the only cycle
161
162 for_each_constness(b.g, [&] (auto & g)
163 {
164 Tarjan_Connected_Components<GT, Out_Iterator, SA> t(sa);
165
166 DynList<GT> blocks;
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>{}));
171
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);
177 DynList<size_t> sizes;
178 t.connected_components(g, sizes);
179 EXPECT_EQ(sizes.size(), want);
180
181 DynList<GT> blocks2;
182 DynList<Arc *> cross2;
183 t(g, blocks2, cross2);
184 EXPECT_EQ(blocks2.size(), want);
186 t(g, lists2);
187 EXPECT_EQ(lists2.size(), want);
188 EXPECT_EQ(t(g).size(), want);
189 DynDlist<GT> blocks3;
190 DynDlist<Arc *> cross3;
191 t(g, blocks3, cross3);
192 EXPECT_EQ(blocks3.size(), want);
194 t(g, lists3);
195 EXPECT_EQ(lists3.size(), want);
196
197 EXPECT_EQ(t.has_cycle(g), not h);
198 EXPECT_EQ(t.is_dag(g), h);
199 Path<GT> p(g);
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);
204
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());
209
211 EXPECT_EQ(moved.has_cycle(g), not h);
212 // The defaulted move assignment is deleted with a filter that
213 // cannot be assigned
214 if constexpr (std::is_move_assignable_v<SA>)
215 {
217 other = std::move(moved);
218 EXPECT_EQ(other.is_dag(g), h);
219 }
220 });
221 });
222}
223
225{
226 using GT = TypeParam;
227 Built<GT> b(cycle_through_hidden());
228
229 for_each_filter<GT>([&] (auto sa, const char * name)
230 {
231 using SA = decltype(sa);
232 SCOPED_TRACE(name);
233 constexpr bool h = hides<GT, SA>;
234
235 for_each_constness(b.g, [&] (auto & g)
236 {
237 const Compute_Cycle_In_Digraph<GT, Out_Iterator, SA> cycle(sa);
238 Path<GT> p(g);
239 EXPECT_EQ(cycle(g, p), not h);
240 if (not h)
241 {
242 EXPECT_EQ(arc_numbers(p.arcs()), (std::set<int>{0, 1, 2}));
243 }
244 EXPECT_EQ(cycle(g).is_empty(), h);
245 EXPECT_EQ(cycle(g, b.nodes[0]).is_empty(), h);
246 });
247 });
248}
249
251{
252 using GT = TypeParam;
253 using Node = typename GT::Node;
254 Built<GT> b(cycle_through_hidden());
255 const std::vector<int> order = {0, 1, 2};
256
257 for_each_filter<GT>([&] (auto sa, const char * name)
258 {
259 using SA = decltype(sa);
260 SCOPED_TRACE(name);
261 constexpr bool h = hides<GT, SA>;
262 for_each_constness(b.g, [&] (auto & g)
263 {
264 // With the cycle the order is not defined, but has every node
265 Topological_Sort<GT, Out_Iterator, SA> lvalue(sa);
266 const auto l = lvalue.template perform<DynList>(g);
267 EXPECT_EQ(l.size(), 3U);
268 if (h)
269 {
270 EXPECT_EQ(node_order(l), order);
271 }
272 EXPECT_EQ(lvalue.template perform<DynDlist>(g).size(), 3U);
273
276 rvalue(g, dl);
277 EXPECT_EQ(dl.size(), 3U);
278 if (h)
279 {
280 EXPECT_EQ(node_order(dl), order);
281 }
282 });
283 });
284}
285
287{
288 using GT = TypeParam;
289 using Node = typename GT::Node;
290 Built<GT> b(cycle_through_hidden());
291 const std::vector<int> order = {0, 1, 2};
292
293 for_each_filter<GT>([&] (auto sa, const char * name)
294 {
295 using SA = decltype(sa);
296 SCOPED_TRACE(name);
297 constexpr bool h = hides<GT, SA>;
298 // On the cycle no node is a source: Kahn's algorithm gives none
299 const size_t want = h ? 3 : 0;
300 for_each_constness(b.g, [&] (auto & g)
301 {
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);
305 if (h)
306 {
307 EXPECT_EQ(node_order(l), order);
308 }
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);
312
315 rvalue(g, r1);
316 EXPECT_EQ(r1.size(), want);
318 rvalue(g, r2);
319 EXPECT_EQ(r2.size(), want);
321 rvalue(g, r3);
322 EXPECT_EQ(r3.size(), want);
323 });
324 });
325}
326
327TYPED_TEST(ContractStructureDigraph, LengauerTarjanDominators)
328{
329 using GT = TypeParam;
330 Built<GT> b(path_and_shortcut());
331 auto * const n0 = b.nodes[0];
332 auto * const n1 = b.nodes[1];
333 auto * const n2 = b.nodes[2];
334
335 for_each_filter<GT>([&] (auto sa, const char * name)
336 {
337 using SA = decltype(sa);
338 SCOPED_TRACE(name);
339 // Without the shortcut 0 -> 2, every path to 2 goes through 1
340 constexpr bool h = hides<GT, SA>;
341
342 // It takes GT &, not a const graph
344 const auto idoms = d.compute_idom(b.g, n0);
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);
348 EXPECT_EQ(d.dominates(b.g, n0, n1, n2), h);
349 const auto frontiers = d.dominance_frontiers(b.g, n0);
350 EXPECT_EQ(frontiers.find(n1).size(), h ? 0U : 1U);
351
352 GT tree;
353 d.build_tree(b.g, n0, tree);
354 EXPECT_EQ(tree.get_num_nodes(), 3U);
355 EXPECT_EQ(tree.get_num_arcs(), 2U);
356 GT tree2;
357 d(b.g, n0, tree2);
358 EXPECT_EQ(tree2.get_num_arcs(), 2U);
359
360 EXPECT_TRUE(d.has_computation());
361 EXPECT_EQ(d.get_graph(), &b.g);
362 EXPECT_EQ(d.get_root(), n0);
363 EXPECT_EQ(d.get_num_reachable(), 3);
364 static_cast<void>(d.get_filter());
365 static_cast<void>(std::as_const(d).get_filter());
366
367 Lengauer_Tarjan_Dominators<GT, SA> moved(std::move(d));
368 EXPECT_EQ(moved.get_idom(b.g, n0, n2), h ? n1 : n0);
369 // The defaulted move assignment is deleted with a filter that cannot
370 // be assigned
371 if constexpr (std::is_move_assignable_v<SA>)
372 {
374 other = std::move(moved);
375 EXPECT_EQ(other.get_idom(b.g, n0, n2), h ? n1 : n0);
376 }
377 });
378}
379
381{
382 using GT = TypeParam;
383 Built<GT> b(path_and_shortcut());
384 auto * const n0 = b.nodes[0];
385 auto * const n1 = b.nodes[1];
386 auto * const n2 = b.nodes[2];
387
388 for_each_filter<GT>([&] (auto sa, const char * name)
389 {
390 using SA = decltype(sa);
391 SCOPED_TRACE(name);
392 constexpr bool h = hides<GT, SA>;
393
394 // They take GT &; on a const graph, these two deduce a const GT
395 for_each_constness(b.g, [&] (auto & g)
396 {
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);
400 });
401
402 // The tree is a GT & too, which a const GT cannot be
403 GT tree;
404 build_dominator_tree(b.g, n0, tree, sa);
405 EXPECT_EQ(tree.get_num_arcs(), 2U);
406 });
407}
408
409TYPED_TEST(ContractStructureDigraph, LengauerTarjanPostDominators)
410{
411 using GT = TypeParam;
412 Built<GT> b(path_and_shortcut());
413 auto * const n0 = b.nodes[0];
414 auto * const n1 = b.nodes[1];
415 auto * const n2 = b.nodes[2];
416
417 for_each_filter<GT>([&] (auto sa, const char * name)
418 {
419 using SA = decltype(sa);
420 SCOPED_TRACE(name);
421 // Without the shortcut, every path from 0 to the exit 2 goes through
422 // 1. The filter sees the arcs of the inverted digraph, which keep the
423 // info, and so the number, of theirs.
424 constexpr bool h = hides<GT, SA>;
425 for_each_constness(b.g, [&] (auto & g)
426 {
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);
435
436 GT tree;
437 d.build_tree(g, n2, tree);
438 EXPECT_EQ(tree.get_num_nodes(), 3U);
439 EXPECT_EQ(tree.get_num_arcs(), 2U);
440 GT tree2;
441 d(g, n2, tree2);
442 EXPECT_EQ(tree2.get_num_arcs(), 2U);
443
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());
449
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>)
453 {
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);
457 }
458 });
459 });
460}
461
462TYPED_TEST(ContractStructureDigraph, PostDominatorFunctions)
463{
464 using GT = TypeParam;
465 Built<GT> b(path_and_shortcut());
466 auto * const n0 = b.nodes[0];
467 auto * const n1 = b.nodes[1];
468 auto * const n2 = b.nodes[2];
469
470 for_each_filter<GT>([&] (auto sa, const char * name)
471 {
472 using SA = decltype(sa);
473 SCOPED_TRACE(name);
474 constexpr bool h = hides<GT, SA>;
475 for_each_constness(b.g, [&] (auto & g)
476 {
477 EXPECT_EQ(dominator_in(compute_post_dominators(g, n2, sa), n0), h ? n1 : n2);
478 GT tree;
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);
483 });
484 });
485}
486
487TYPED_TEST(ContractStructureDigraph, NoexceptOnlyIfTheFilterIs)
488{
489 using GT = TypeParam;
491 static_assert(not std::is_nothrow_move_constructible_v<F>);
492
493 using Plain = Dft_Show_Arc<GT>;
494
495 // A prvalue initializes the parameter without a move, so that noexcept()
496 // sees only the constructor, which moves it into the member. Tarjan's is
497 // noexcept if the move of the filter is; the dominators reserve arrays.
498 EXPECT_FALSE(noexcept(Tarjan_Connected_Components<GT, Out_Iterator, F>(F())));
499 EXPECT_TRUE(noexcept(Tarjan_Connected_Components<GT, Out_Iterator, Plain>(Plain())));
500 EXPECT_FALSE(noexcept(Lengauer_Tarjan_Dominators<GT, F>(F())));
501 EXPECT_FALSE(noexcept(Lengauer_Tarjan_Post_Dominators<GT, F>(F())));
502}
503
504template <class GT>
505class ContractStructureGraph : public ::testing::Test {};
506
508
509TYPED_TEST(ContractStructureGraph, EdgeAndVertexConnectivity)
510{
511 using GT = TypeParam;
512 using Node = typename GT::Node;
513 using Arc = typename GT::Arc;
514 Built<GT> b(path_and_shortcut());
515
516 for_each_filter<GT>([&] (auto sa, const char * name)
517 {
518 using SA = decltype(sa);
519 constexpr bool hide = hides<GT, SA>;
520 SCOPED_TRACE(name);
521 // They take GT &. The triangle is 2-connected, and without the arc
522 // Hidden it is a path, 1-connected
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);
526
528 DynDlist<Arc *> cut;
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));
532
535 DynDlist<Arc *> cut2;
536 EXPECT_EQ(min_cut(b.g, l2, r2, cut2), k);
537 EXPECT_EQ(cut2.size(), size_t(k));
538
539 // Without the object they build SA(), which hides nothing
540 if constexpr (std::is_default_constructible_v<SA>)
541 {
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);
545 DynDlist<Arc *> cut3;
546 EXPECT_EQ((compute_min_cut<GT, Heap_Preflow_Maximum_Flow, SA>(b.g, l3, r3, cut3)), 2);
547 EXPECT_EQ((Compute_Min_Cut<GT, Heap_Preflow_Maximum_Flow, SA>()(b.g, l3, r3, cut3)), 2);
548 }
549 });
550}
551
553{
554 using GT = TypeParam;
555 using Node = typename GT::Node;
556 using Arc = typename GT::Arc;
557 Built<GT> b(path_and_shortcut());
558
559 for_each_filter<GT>([&] (auto sa, const char * name)
560 {
561 using SA = decltype(sa);
562 SCOPED_TRACE(name);
563 constexpr bool h = hides<GT, SA>;
564 // The triangle has cuts of 2 arcs; without the arc 0 - 2, a path of
565 // cuts of 1. Every contraction finds them.
566 const size_t want = h ? 1 : 2;
567
568 // It takes GT &, not a const graph
569 Karger_Min_Cut<GT, SA> karger(17, sa);
570 EXPECT_EQ(karger.get_seed(), 17U);
571 karger.reseed(3);
572 EXPECT_EQ(karger.get_seed(), 3U);
573 EXPECT_EQ(karger.compute_min_cut_size(b.g), want);
574 EXPECT_EQ(karger.compute_min_cut_size(b.g, 2), want);
575
576 DynList<Node *> vs, vt;
577 DynList<Arc *> cut;
578 EXPECT_EQ(karger(b.g, vs, vt, cut, 2), want);
579 EXPECT_EQ(cut.size(), want);
580 if (h)
581 {
582 EXPECT_EQ(arc_numbers(cut).count(Hidden), 0U);
583 }
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);
588 if (h)
589 {
590 EXPECT_EQ(arc_numbers(cut).count(Hidden), 0U);
591 }
592
593 Karger_Min_Cut<GT, SA> moved(std::move(karger));
594 EXPECT_EQ(moved.compute_min_cut_size(b.g), want);
595 // The move assignment is only for a filter that can be assigned
596 if constexpr (std::is_move_assignable_v<SA>)
597 {
598 Karger_Min_Cut<GT, SA> other(19, sa);
599 other = std::move(moved);
600 EXPECT_EQ(other.compute_min_cut_size(b.g), want);
601 }
602 });
603}
604
606{
607 using GT = TypeParam;
608 using Node = typename GT::Node;
609 using Arc = typename GT::Arc;
610 Built<GT> b(path_and_shortcut());
611
612 for_each_filter<GT>([&] (auto sa, const char * name)
613 {
614 using SA = decltype(sa);
615 constexpr bool hide = hides<GT, SA>;
616 SCOPED_TRACE(name);
617 // It takes GT &. The weights are the arc numbers; the cut around
618 // node 1 weighs 0 + 1, and without the arc Hidden, the cut around
619 // node 0 weighs 0
621 DynList<Node *> vs, vt;
622 DynList<Arc *> cut;
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}));
626 EXPECT_EQ(sw.min_cut_weight(b.g), hide ? 0 : 1);
627
628 // Without the object it builds SA(), which hides nothing
629 if constexpr (std::is_default_constructible_v<SA>)
630 {
632 EXPECT_EQ(by_default.min_cut_weight(b.g), 1);
633
635 EXPECT_EQ(with_distance.min_cut_weight(b.g), 1);
636 }
637 });
638}
639
640TYPED_TEST(ContractStructureGraph, NoexceptOnlyIfTheFilterIs)
641{
642 using GT = TypeParam;
644 using Karger = Karger_Min_Cut<GT, F>;
645 using Plain = Karger_Min_Cut<GT>;
646
647 // The moves are noexcept if those of the filter are
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>()));
652}
653
654template <class GT>
655class ContractStructureAllKinds : public ::testing::Test {};
656
658
659TYPED_TEST(ContractStructureAllKinds, PlanarityTestAndIsPlanarGraph)
660{
661 using GT = TypeParam;
662 Built<GT> b(k5());
663
664 for_each_filter<GT>([&] (auto sa, const char * name)
665 {
666 using SA = decltype(sa);
667 SCOPED_TRACE(name);
668 constexpr bool h = hides<GT, SA>;
669 for_each_constness(b.g, [&] (auto & g)
670 {
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);
674
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);
682
683 EXPECT_EQ(is_planar_graph(g, sa), h);
684 EXPECT_EQ(is_planar_graph(g, SA(sa), options), h);
685
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);
694 });
695 });
696}
697
698TYPED_TEST(ContractStructureAllKinds, ColoringFunctionsAndClasses)
699{
700 using GT = TypeParam;
701 using Node = typename GT::Node;
702 Built<GT> b(path_and_shortcut());
703
704 for_each_filter<GT>([&] (auto sa, const char * name)
705 {
706 using SA = decltype(sa);
707 constexpr bool hide = hides<GT, SA>;
708 SCOPED_TRACE(name);
709 // The triangle takes 3 colors, and without the arc Hidden it is a
710 // path, which takes 2
711 const size_t want = hide ? 2 : 3;
712 for_each_constness(b.g, [&] (auto & g)
713 {
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)));
723
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);
730
731 // Without the object they build SA(), which hides nothing
732 if constexpr (std::is_default_constructible_v<SA>)
733 {
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);
740 }
741 });
742 });
743}
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
WeightedDigraph::Arc Arc
long double h
Definition btreepic.C:154
Functor wrapper for compute_min_cut().
Definition tpl_kgraph.H:458
Default distance accessor for arc weights.
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.
const size_t & size() const noexcept
Return the number of elements (constant time)
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
Dynamic set backed by balanced binary search trees with automatic memory management.
Karger's randomized minimum cut algorithm.
Definition Karger.H:165
size_t compute_min_cut_size(GT &g, size_t num_iter=0)
Compute only the minimum cut size (without partition info).
Definition Karger.H:547
unsigned long get_seed() const noexcept
Get the random seed used by this solver (useful for reproducibility).
Definition Karger.H:526
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.
Definition Karger.H:639
void reseed(const unsigned long new_seed) noexcept
Change the random seed.
Definition Karger.H:530
Computes dominator tree and dominance frontiers of a digraph using the Lengauer-Tarjan algorithm.
Definition Dominators.H:156
DynMapTree< Node *, DynSetTree< Node * > > dominance_frontiers(GT &g, Node *root)
Compute dominance frontiers for all reachable nodes.
Definition Dominators.H:553
void build_tree(GT &g, Node *root, GT &tree)
Build the dominator tree as a separate graph.
Definition Dominators.H:452
bool has_computation() const noexcept
Check if a computation has been performed.
Definition Dominators.H:610
SA & get_filter() noexcept
Get the arc filter.
Definition Dominators.H:604
long get_num_reachable() const noexcept
Get the number of reachable nodes in the last computation.
Definition Dominators.H:628
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
DynList< std::pair< Node *, Node * > > compute_idom(GT &g, Node *root)
Compute immediate dominators.
Definition Dominators.H:427
Node * get_root() const noexcept
Get the root of the last computation.
Definition Dominators.H:625
GT * get_graph() const noexcept
Get the graph of the last computation.
Definition Dominators.H:622
Computes post-dominator tree and post-dominance frontiers of a digraph using the Lengauer-Tarjan algo...
Definition Dominators.H:749
Path on a graph.
Definition tpl_graph.H:3832
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Definition tpl_graph.H:4576
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.
Definition Tarjan.H:174
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
Definition Tarjan.H:547
bool is_dag(const GT &g)
Determines whether the directed graph is acyclic (a DAG).
Definition Tarjan.H:954
Computes topological ordering using depth-first search.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
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.
Definition Dominators.H:678
FFT< double > F
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.
Definition tpl_net.H:2790
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
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.
Definition tpl_graph.H:1024
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
DynList< char > l3
DynList< int > l2
static int * k
Gsl_Compatible_Rng r
Topological sorting algorithms for directed acyclic graphs (DAGs).
Generic graph and digraph implementations.
K-connected graph structures and connectivity algorithms.
DynList< int > l