Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_paths_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
55#include <algorithm>
56#include <functional>
57#include <limits>
58#include <memory>
59#include <queue>
60#include <random>
61#include <set>
62#include <sstream>
63#include <stdexcept>
64#include <string>
65#include <tuple>
66#include <type_traits>
67#include <utility>
68#include <vector>
69
70#include <gtest/gtest.h>
71
72#include <tpl_graph.H>
73#include <Dijkstra.H>
74#include <AStar.H>
75#include <Zero_One_BFS.H>
76#include <Dial.H>
77#include <Floyd_Warshall.H>
78#include <Johnson.H>
79#include <K_Shortest_Paths.H>
80
82#include "graph_harness.H"
83#include "graph_oracles.H"
84
85using namespace Aleph;
86using namespace graph_harness;
87namespace oracle = graph_oracles;
90
91namespace
92{
93 using Matrix = std::vector<std::vector<long long>>;
94
95 constexpr long long Inf = oracle::Inf;
96
97 constexpr long long Int_Max = std::numeric_limits<int>::max();
98
102 template <typename T>
103 T as(long long x, bool whole = false)
104 {
105 if constexpr (std::is_floating_point_v<T>)
106 return whole ? T(x) : T(x) / 2;
107 else
108 return T(x);
109 }
110
112 template <typename T>
113 std::vector<T> weights_as(const Spec & s, bool whole = false)
114 {
115 std::vector<T> r;
116 for (long long x : s.w)
117 r.push_back(as<T>(x, whole));
118 return r;
119 }
120
121 template <typename T>
122 std::string text_of(T x)
123 {
124 std::ostringstream o;
125 o << x;
126 return o.str();
127 }
128
129 std::string route(int u, int v)
130 {
131 return "from " + std::to_string(u) + " to " + std::to_string(v) + ": ";
132 }
133
136 template <class F>
137 std::string what_throws(F && f)
138 {
139 try
140 {
141 f();
142 }
143 catch (const std::overflow_error & e)
144 {
145 return std::string("overflow_error ") + e.what();
146 }
147 catch (const std::domain_error & e)
148 {
149 return std::string("domain_error ") + e.what();
150 }
151 catch (const std::invalid_argument & e)
152 {
153 return std::string("invalid_argument ") + e.what();
154 }
155 catch (const std::exception & e)
156 {
157 return std::string("exception ") + e.what();
158 }
159 return "";
160 }
161
163 bool is(const std::string & what, const char * kind)
164 {
165 return what.rfind(kind, 0) == 0;
166 }
167
170 Matrix distances(const Spec & s, bool directed, unsigned long hidden, bool & neg)
171 {
172 std::vector<int> orig;
173 return oracle::shortest_distances(oracle::without(s, hidden, orig), directed, neg);
174 }
175
176 Matrix distances(const Spec & s, bool directed, unsigned long hidden = 0)
177 {
178 bool neg = false;
179 return distances(s, directed, hidden, neg);
180 }
181
185 std::vector<long long> potentials(const Matrix & d)
186 {
187 std::vector<long long> h(d.size(), 0);
188 for (size_t x = 0; x < d.size(); ++x)
189 for (size_t v = 0; v < d.size(); ++v)
190 if (d[x][v] != Inf)
191 h[v] = std::min(h[v], d[x][v]);
192 return h;
193 }
194
197 std::vector<long long> simple_path_costs(const Spec & s, int u, int v, bool directed)
198 {
199 std::vector<long long> costs;
200 std::vector<bool> on(size_t(s.n), false);
201 auto walk = [&] (auto & self, int x, long long cost) -> void
202 {
203 if (x == v)
204 {
205 costs.push_back(cost);
206 return;
207 }
208 on[size_t(x)] = true;
209 for (size_t i = 0; i < s.arcs.size(); ++i)
210 for (size_t e = 0; e < (directed ? 1U : 2U); ++e)
211 if (s.arcs[i][e] == x and not on[size_t(s.arcs[i][1 - e])])
212 self(self, s.arcs[i][1 - e], cost + s.w[i]);
213 on[size_t(x)] = false;
214 };
215 walk(walk, u, 0);
216 std::sort(costs.begin(), costs.end());
217 return costs;
218 }
219
225 std::vector<long long> walk_costs(const Spec & s, int u, int v, bool directed, size_t k)
226 {
227 std::vector<long long> costs;
228 std::vector<size_t> taken(size_t(s.n), 0);
229 using Item = std::pair<long long, int>;
230 std::priority_queue<Item, std::vector<Item>, std::greater<>> queue;
231 queue.push({0, u});
232 while (not queue.empty() and costs.size() < k)
233 {
234 const auto [cost, x] = queue.top();
235 queue.pop();
236 if (taken[size_t(x)]++ >= k)
237 continue;
238 if (x == v)
239 costs.push_back(cost);
240 for (size_t i = 0; i < s.arcs.size(); ++i)
241 if (s.arcs[i][0] == x)
242 queue.push({cost + s.w[i], s.arcs[i][1]});
243 else if (not directed and s.arcs[i][1] == x)
244 queue.push({cost + s.w[i], s.arcs[i][0]});
245 }
246 return costs;
247 }
248
251 bool meets_other_weight(const Spec & s, int u, bool directed)
252 {
253 std::vector<bool> seen(size_t(s.n), false);
254 seen[size_t(u)] = true;
255 for (bool grew = true; grew; )
256 {
257 grew = false;
258 for (const auto & a : s.arcs)
259 for (size_t e = 0; e < (directed ? 1U : 2U); ++e)
260 if (seen[size_t(a[e])] and not seen[size_t(a[1 - e])])
261 seen[size_t(a[1 - e])] = grew = true;
262 }
263 for (size_t i = 0; i < s.arcs.size(); ++i)
264 if (s.w[i] != 0 and s.w[i] != 1
265 and (seen[size_t(s.arcs[i][0])] or (not directed and seen[size_t(s.arcs[i][1])])))
266 return true;
267 return false;
268 }
269
275 bool sum_beyond(const Spec & s, const Matrix & d, int u, long long bound, bool directed,
276 long long max)
277 {
278 for (size_t i = 0; i < s.arcs.size(); ++i)
279 for (size_t e = 0; e < (directed ? 1U : 2U); ++e)
280 {
281 const int x = s.arcs[i][e], y = s.arcs[i][1 - e];
282 const long long dx = d[size_t(u)][size_t(x)], dy = d[size_t(u)][size_t(y)];
283 if (x != y and dx != Inf and dx <= bound and dy >= dx and dx + s.w[i] > max)
284 return true;
285 }
286 return false;
287 }
288
291 bool has_parallel_arcs(const Spec & s, bool directed)
292 {
293 std::set<std::pair<int, int>> seen;
294 for (const auto & a : s.arcs)
295 if (a[0] != a[1]
296 and not seen.insert(directed ? std::pair{a[0], a[1]}
297 : std::pair{std::min(a[0], a[1]), std::max(a[0], a[1])}).second)
298 return true;
299 return false;
300 }
301
304 Spec poisoned(const Spec & s, unsigned long hidden, long long poison)
305 {
306 Spec r = s;
307 for (size_t i = 0; i < r.w.size(); ++i)
308 if (not graph_filters::shown(hidden, int(i)))
309 r.w[i] = poison;
310 return r;
311 }
312
314 struct Walk
315 {
316 std::vector<int> nodes;
317 std::vector<int> arcs;
318
319 bool operator == (const Walk &) const = default;
320
321 [[nodiscard]] std::string str() const
322 {
323 std::string r = "[";
324 for (size_t i = 0; i < nodes.size(); ++i)
325 {
326 r += std::to_string(nodes[i]);
327 if (i < arcs.size())
328 r += " -" + std::to_string(arcs[i]) + "- ";
329 }
330 return r + "]";
331 }
332 };
333
334 template <class GT>
335 Walk walk_of(const Path<GT> & p)
336 {
337 Walk w;
338 for (typename Path<GT>::Iterator it(p); it.has_current_node(); it.next_ne())
339 {
340 w.nodes.push_back(num(it.get_current_node_ne()));
341 if (it.has_current_arc())
342 w.arcs.push_back(num(it.get_current_arc_ne()));
343 }
344 return w;
345 }
346
350 std::string walk_error(const Spec & s, const Walk & w, int u, int v, bool directed,
351 unsigned long hidden)
352 {
353 if (w.nodes.empty())
354 return "an empty path";
355 if (w.arcs.size() + 1 != w.nodes.size())
356 return "the path " + w.str() + " lacks arcs";
357 if (w.nodes.front() != u or w.nodes.back() != v)
358 return "the path " + w.str() + " does not go from " + std::to_string(u)
359 + " to " + std::to_string(v);
360 for (size_t i = 0; i < w.arcs.size(); ++i)
361 {
362 const int a = w.arcs[i];
363 if (a < 0 or size_t(a) >= s.arcs.size())
364 return "the path " + w.str() + " has an arc that is not of the graph";
365 const int x = s.arcs[size_t(a)][0], y = s.arcs[size_t(a)][1];
366 const int p = w.nodes[i], q = w.nodes[i + 1];
367 if (not (x == p and y == q) and (directed or not (x == q and y == p)))
368 return "the path " + w.str() + " does not follow the arc " + std::to_string(a);
369 if (not graph_filters::shown(hidden, a))
370 return "the path " + w.str() + " takes the hidden arc " + std::to_string(a);
371 }
372 return "";
373 }
374
376 long long cost_of(const Spec & s, const Walk & w)
377 {
378 long long c = 0;
379 for (int a : w.arcs)
380 c += s.w[size_t(a)];
381 return c;
382 }
383
384 bool is_simple(const Walk & w)
385 {
386 return std::set<int>(w.nodes.begin(), w.nodes.end()).size() == w.nodes.size();
387 }
388
391 template <class GT, typename T>
392 std::string path_error(const GT & g, const Spec & s, const Path<GT> & path, int u, int v,
393 unsigned long hidden, long long want, T got, bool whole = false)
394 {
395 if (not path.inside_graph(g))
396 return "a path of another graph";
397 const Walk w = walk_of(path);
398 if (auto e = walk_error(s, w, u, v, g.is_digraph(), hidden); not e.empty())
399 return e;
400 if (const long long c = cost_of(s, w); c != want)
401 return "the path " + w.str() + " costs " + std::to_string(c) + ", not "
402 + std::to_string(want);
403 if (got != as<T>(want, whole))
404 return "the path " + w.str() + " comes with the cost " + text_of(got) + ", not "
405 + text_of(as<T>(want, whole));
406 return "";
407 }
408
411 template <class GT, typename T>
412 bool no_path(const std::string & thrown, T got, const Path<GT> & path)
413 {
414 return is(thrown, "domain_error")
415 or (thrown.empty() and got == std::numeric_limits<T>::max() and path.is_empty());
416 }
417
427 template <class GT>
428 std::string tree_error(GT & tree, const Spec & s, int u, const std::vector<long long> & row,
429 bool directed, unsigned long hidden, std::vector<long long> & dist,
430 size_t & unreached)
431 {
432 dist.assign(size_t(s.n), Inf);
433 unreached = 0;
434 std::vector<bool> in(size_t(s.n), false);
435 for (auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
436 {
437 const int x = num(it.get_curr());
438 if (x < 0 or x >= s.n or in[size_t(x)])
439 return "the tree has a node that is not of the graph, or twice";
440 in[size_t(x)] = true;
441 }
442 if (u < 0 or u >= s.n or not in[size_t(u)])
443 return "the tree lacks its root";
444
445 // The arcs out of each node of the tree, as (arc, the other end)
446 std::vector<std::vector<std::pair<int, int>>> out(size_t(s.n));
447 for (auto it = tree.get_arc_it(); it.has_curr(); it.next_ne())
448 {
449 auto * a = it.get_curr();
450 const int i = num(a);
451 const int x = num(tree.get_src_node(a)), y = num(tree.get_tgt_node(a));
452 if (i < 0 or size_t(i) >= s.arcs.size() or not graph_filters::shown(hidden, i))
453 return "the tree has an arc that is not of the graph, or a hidden one";
454 const auto & e = s.arcs[size_t(i)];
455 if (not (e[0] == x and e[1] == y) and (directed or not (e[0] == y and e[1] == x)))
456 return "the arc " + std::to_string(i) + " joins other nodes in the tree";
457 out[size_t(x)].push_back({i, y});
458 if (not directed)
459 out[size_t(y)].push_back({i, x});
460 }
461
462 dist[size_t(u)] = 0;
463 std::vector<int> queue = {u};
464 std::vector<bool> walked(s.arcs.size(), false);
465 for (size_t h = 0; h < queue.size(); ++h)
466 for (const auto & [i, y] : out[size_t(queue[h])])
467 {
468 if (walked[size_t(i)])
469 continue;
470 walked[size_t(i)] = true;
471 if (dist[size_t(y)] != Inf)
472 return "the tree has a cycle through the arc " + std::to_string(i);
473 dist[size_t(y)] = dist[size_t(queue[h])] + s.w[size_t(i)];
474 queue.push_back(y);
475 }
476 unreached = tree.get_num_nodes() - queue.size();
477 if (tree.get_num_arcs() + 1 != queue.size())
478 return "the tree has arcs that its root does not reach";
479 for (int x = 0; x < s.n; ++x)
480 if (dist[size_t(x)] != Inf and dist[size_t(x)] != row[size_t(x)])
481 return "in the tree " + std::to_string(x) + " is at " + std::to_string(dist[size_t(x)])
482 + ", not " + std::to_string(row[size_t(x)]);
483 return "";
484 }
485
489 template <class GT>
490 std::string mapping_error(GT & tree, const Built<GT> & b)
491 {
492 for (auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
493 {
494 auto * t = it.get_curr();
495 auto * p = mapped_node<GT>(t);
496 if (p == nullptr or p != b.nodes[size_t(num(t))] or mapped_node<GT>(p) != t)
497 return "the node " + std::to_string(num(t)) + " of the tree is not mapped both ways";
498 }
499 for (auto it = tree.get_arc_it(); it.has_curr(); it.next_ne())
500 {
501 auto * t = it.get_curr();
502 auto * a = mapped_arc<GT>(t);
503 if (a == nullptr or a != b.arcs[size_t(num(t))] or mapped_arc<GT>(a) != t)
504 return "the arc " + std::to_string(num(t)) + " of the tree is not mapped both ways";
505 }
506 return "";
507 }
508
511 std::string coverage_error(const std::vector<long long> & row,
512 const std::vector<long long> & dist)
513 {
514 for (size_t x = 0; x < row.size(); ++x)
515 if (row[x] != Inf and dist[x] == Inf)
516 return "the tree lacks the node " + std::to_string(x);
517 return "";
518 }
519
522 template <class GT>
523 bool painted_hidden_arc(const Built<GT> & b, unsigned long hidden)
524 {
525 for (size_t i = 0; i < b.arcs.size(); ++i)
526 if (not graph_filters::shown(hidden, int(i))
528 return true;
529 return false;
530 }
531
536 enum class Weighting { Ones, Rising, Falling };
537
538 Spec weighted(const Spec & shape, Weighting k)
539 {
540 Spec s = shape;
541 const size_t m = shape.arcs.size();
542 for (size_t i = 0; i < m; ++i)
543 s.w.push_back(k == Weighting::Ones ? 1 : 1LL << (k == Weighting::Rising ? i : m - 1 - i));
544 return s;
545 }
546
547 constexpr Weighting All_Weightings[] = {Weighting::Ones, Weighting::Rising, Weighting::Falling};
548
550 template <class F>
551 Spec weighted(const Spec & shape, F && f)
552 {
553 Spec s = shape;
554 for (size_t i = 0; i < shape.arcs.size(); ++i)
555 s.w.push_back(f(i));
556 return s;
557 }
558
560 template <class F>
561 void for_each_weighting(const Spec & shape, const std::vector<long long> & values, F && f)
562 {
563 Spec s = shape;
564 std::vector<size_t> pick(shape.arcs.size(), 0);
565 while (true)
566 {
567 s.w.clear();
568 for (size_t i : pick)
569 s.w.push_back(values[i]);
570 f(static_cast<const Spec &>(s));
571 size_t i = 0;
572 while (i < pick.size() and ++pick[i] == values.size())
573 pick[i++] = 0;
574 if (i == pick.size())
575 return;
576 }
577 }
578
581 std::vector<Spec> random_specs(unsigned first_seed, bool directed, size_t count,
582 long long lo, long long hi)
583 {
584 std::mt19937 rng(seed(first_seed));
585 std::vector<Spec> r;
586 for (size_t i = 0, n = heavy() ? 10 * count : count; i < n; ++i)
587 {
588 const int nodes = 6 + int(rng() % 3);
589 const int arcs = nodes + int(rng() % unsigned(nodes + 1));
590 Spec s = random_spec(rng, nodes, arcs, directed);
591 random_weights(rng, s, lo, hi);
592 r.push_back(s);
593 }
594 return r;
595 }
596
600 std::vector<Spec> random_potential_specs(unsigned first_seed, size_t count)
601 {
602 std::mt19937 rng(seed(first_seed));
603 std::vector<Spec> r = random_specs(first_seed, true, count, 0, 4);
604 for (Spec & s : r)
605 {
606 std::vector<long long> p(size_t(s.n));
607 for (auto & x : p)
608 x = (long long) (rng() % 9) - 4;
609 for (size_t i = 0; i < s.arcs.size(); ++i)
610 s.w[i] += p[size_t(s.arcs[i][1])] - p[size_t(s.arcs[i][0])];
611 }
612 return r;
613 }
614
615 template <class GT, typename T>
616 using Weights = Weight_Of<GT, T>;
617
618 template <class GT, typename T, class SA = Dft_Show_Arc<GT>,
619 template <class, class, class> class Heap = ArcHeap>
620 using Dijkstra_Of = Dijkstra_Min_Paths<GT, Weights<GT, T>, Node_Arc_Iterator, SA, Heap>;
621
622 template <class GT, typename T>
623 using No_Heuristic = Zero_Heuristic<GT, Weights<GT, T>>;
624
625 template <class GT, typename T, class SA = Dft_Show_Arc<GT>,
626 template <class, class, class> class Heap = ArcHeap>
627 using AStar_Of = AStar_Min_Path<GT, Weights<GT, T>, No_Heuristic<GT, T>,
628 Node_Arc_Iterator, SA, Heap>;
629
632 std::vector<int> sources(const Spec & s, int only)
633 {
634 if (only >= 0)
635 return {only};
636 std::vector<int> r;
637 for (int u = 0; u < s.n; ++u)
638 r.push_back(u);
639 return r;
640 }
641
644 struct Painting_Checks
645 {
646 Check state;
647 Check distance;
648 Check path;
649
650 Painting_Checks(const std::string & who, const std::string & how = "",
651 bool state_known = false, bool path_known = false)
652 : state(who + "::paint_min_paths_tree()" + how, state_known),
653 distance(who + "::get_distance()" + how),
654 path(who + "::get_min_path(end, path)" + how, path_known)
655 {}
656
657 void expect() const
658 {
659 state.expect();
660 distance.expect();
661 path.expect();
662 }
663 };
664
667 template <typename T, class Solver, class GT>
668 void check_painting(Painting_Checks & c, Solver & solver, Built<GT> & b, const Spec & s,
669 const Matrix & d, unsigned long hidden, bool whole = false,
670 int only = -1)
671 {
672 for (int u : sources(s, only))
673 {
674 auto * src = b.nodes[size_t(u)];
675 const std::string from = "from " + std::to_string(u) + ": ";
676 c.state.run();
677 if (auto e = what_throws([&] { solver.paint_min_paths_tree(b.g, src); }); not e.empty())
678 {
679 c.state.fail(s, from + e);
680 continue;
681 }
682 bool computed = true;
683 if constexpr (requires { solver.has_computation(); })
684 computed = solver.has_computation();
685 if (not solver.is_painted() or solver.get_start_node() != src
686 or solver.get_graph() != &b.g or not computed)
687 c.state.fail(s, from + "the solver does not tell its painting");
688
689 for (int v = 0; v < s.n; ++v)
690 {
691 auto * tgt = b.nodes[size_t(v)];
692 const long long want = d[size_t(u)][size_t(v)];
693 const std::string where = route(u, v);
694
695 c.distance.run();
696 T got{};
697 auto e = what_throws([&] { got = solver.get_distance(tgt); });
698 if (want == Inf)
699 {
700 if (not is(e, "domain_error"))
701 c.distance.fail(s, where + "gives " + (e.empty() ? text_of(got) : e)
702 + " for a node not reached");
703 }
704 else if (not e.empty())
705 c.distance.fail(s, where + e);
706 else if (got != as<T>(want, whole))
707 c.distance.fail(s, where + "gives " + text_of(got) + ", not "
708 + text_of(as<T>(want, whole)));
709
710 c.path.run();
711 Path<GT> path(b.g);
712 e = what_throws([&] { got = solver.get_min_path(tgt, path); });
713 if (want == Inf)
714 {
715 if (not no_path(e, got, path))
716 c.path.fail(s, where + "gives the path " + walk_of(path).str()
717 + " to a node not reached");
718 }
719 else if (not e.empty())
720 c.path.fail(s, where + e);
721 else if (auto pe = path_error(b.g, s, path, u, v, hidden, want, got, whole);
722 not pe.empty())
723 c.path.fail(s, where + pe);
724 }
725 }
726 }
727
733 template <typename T, class Solver, class GT>
734 void check_copies(Check & copy, Check * nodes, Solver & solver, Built<GT> & b,
735 const Spec & s, const Matrix & d, unsigned long hidden, int only = -1)
736 {
737 for (int u : sources(s, only))
738 {
739 const std::string from = "from " + std::to_string(u) + ": ";
740 copy.run();
741 GT tree;
742 T total{};
743 if (auto e = what_throws([&]
744 {
745 solver.paint_min_paths_tree(b.g, b.nodes[size_t(u)]);
746 total = solver.copy_painted_min_paths_tree(b.g, tree);
747 }); not e.empty())
748 {
749 copy.fail(s, from + e);
750 continue;
751 }
752 std::vector<long long> dist;
753 size_t unreached = 0;
754 const auto & row = d[size_t(u)];
755 std::string e = tree_error(tree, s, u, row, b.g.is_digraph(), hidden, dist, unreached);
756 if (e.empty())
757 e = coverage_error(row, dist);
758 if (e.empty())
759 e = mapping_error(tree, b);
760 if (e.empty())
761 {
762 T weight{};
763 for (auto it = tree.get_arc_it(); it.has_curr(); it.next_ne())
764 weight += as<T>(s.w[size_t(num(it.get_curr()))]);
765 if (weight != total)
766 e = "gives the weight " + text_of(total) + " for a tree of " + text_of(weight);
767 }
768 if (e.empty() and solver.is_painted())
769 e = "the solver is still painted";
770 if (not e.empty())
771 copy.fail(s, from + e);
772
773 if (nodes == nullptr or std::count(row.begin(), row.end(), Inf) == 0)
774 continue;
775 nodes->run();
776 if (unreached > 0)
777 nodes->fail(s, from + "the tree has " + std::to_string(unreached)
778 + " nodes that the painting does not reach");
779 }
780 }
781
787 template <typename T, class GT, class Find>
788 void check_find(Check & pairs, Check * self, Built<GT> & b, const Spec & s, const Matrix & d,
789 unsigned long hidden, Find && find, bool whole = false, int only = -1)
790 {
791 for (int u : sources(s, only))
792 for (int v = 0; v < s.n; ++v)
793 {
794 if (u == v and self == nullptr)
795 continue;
796 Check & c = u == v ? *self : pairs;
797 c.run();
798 const std::string where = route(u, v);
799 const long long want = d[size_t(u)][size_t(v)];
800 Path<GT> path(b.g);
801 T got{};
802 if (auto e = what_throws([&]
803 { got = find(b.nodes[size_t(u)], b.nodes[size_t(v)], path); });
804 not e.empty())
805 c.fail(s, where + e);
806 else if (want == Inf)
807 {
808 if (got != std::numeric_limits<T>::max() or not path.is_empty())
809 c.fail(s, where + "gives " + text_of(got) + " and the path "
810 + walk_of(path).str() + " to a node not reached");
811 }
812 else if (auto pe = path_error(b.g, s, path, u, v, hidden, want, got, whole);
813 not pe.empty())
814 c.fail(s, where + pe);
815 }
816 }
817
822 template <typename T, class Solver, class GT, class Paint>
823 void check_partial_painting(Check & pairs, Check & self, Solver & solver, Built<GT> & b,
824 const Spec & s, const Matrix & d, unsigned long hidden,
825 Paint && paint)
826 {
827 for (int u = 0; u < s.n; ++u)
828 for (int v = 0; v < s.n; ++v)
829 {
830 Check & c = u == v ? self : pairs;
831 c.run();
832 auto * tgt = b.nodes[size_t(v)];
833 const std::string where = route(u, v);
834 const long long want = d[size_t(u)][size_t(v)];
835 bool found = false;
836 if (auto e = what_throws([&] { found = paint(b.nodes[size_t(u)], tgt); });
837 not e.empty())
838 {
839 c.fail(s, where + e);
840 continue;
841 }
842 if (found != (want != Inf))
843 {
844 c.fail(s, where + (found ? "reaches a node not reached" : "does not reach the end"));
845 continue;
846 }
847 if (not found)
848 continue;
849 T dist{}, cost{};
850 Path<GT> path(b.g);
851 if (auto e = what_throws([&]
852 {
853 dist = solver.get_distance(tgt);
854 cost = solver.get_min_path(tgt, path);
855 }); not e.empty())
856 c.fail(s, where + "then " + e);
857 else if (dist != as<T>(want))
858 c.fail(s, where + "then get_distance() gives " + text_of(dist) + ", not "
859 + text_of(as<T>(want)));
860 else if (auto pe = path_error(b.g, s, path, u, v, hidden, want, cost); not pe.empty())
861 c.fail(s, where + "then get_min_path(): " + pe);
862 }
863 }
864
867 struct Tree_Checks
868 {
869 Check tree;
870 Check paths;
871 Check parallel;
872 Check out;
873
874 explicit Tree_Checks(const std::string & who)
875 : tree(who),
876 paths("get_min_path(tree, end, path), after " + who),
877 parallel("get_min_path(tree, end, path), after " + who + ", with parallel arcs"),
878 out("get_min_path(tree, end, path), after " + who + ", to a node out of the tree")
879 {}
880
881 void expect() const
882 {
883 tree.expect();
884 paths.expect();
885 parallel.expect();
886 out.expect();
887 }
888 };
889
893 template <typename T, class Solver, class GT>
894 void check_tree_path(Tree_Checks & c, Solver & solver, GT & tree, Built<GT> & b,
895 const Spec & s, const Matrix & d, int u, int v)
896 {
897 const long long want = d[size_t(u)][size_t(v)];
898 Check & pc = want == Inf ? c.out : has_parallel_arcs(s, b.g.is_digraph()) ? c.parallel
899 : c.paths;
900 pc.run();
901 Path<GT> path(b.g);
902 T got{};
903 const auto e = what_throws([&]
904 { got = solver.get_min_path(tree, b.nodes[size_t(v)], path); });
905 if (want == Inf)
906 {
907 if (not no_path(e, got, path))
908 pc.fail(s, route(u, v) + "gives " + text_of(got) + " and the path "
909 + walk_of(path).str());
910 }
911 else if (not e.empty())
912 pc.fail(s, route(u, v) + e);
913 else if (auto pe = path_error(b.g, s, path, u, v, 0, want, got); not pe.empty())
914 pc.fail(s, route(u, v) + pe);
915 }
916
921 template <typename T, class Solver, class GT, class Compute>
922 void check_trees(Tree_Checks & c, Solver & solver, Built<GT> & b, const Spec & s,
923 const Matrix & d, unsigned long hidden, Compute && compute)
924 {
925 for (int u = 0; u < s.n; ++u)
926 {
927 const std::string from = "from " + std::to_string(u) + ": ";
928 const auto & row = d[size_t(u)];
929 c.tree.run();
930 GT tree;
931 typename GT::Node * root = nullptr;
932 if (auto e = what_throws([&] { root = compute(b.nodes[size_t(u)], tree); });
933 not e.empty())
934 {
935 c.tree.fail(s, from + e);
936 continue;
937 }
938 std::vector<long long> dist;
939 size_t unreached = 0;
940 std::string e = tree_error(tree, s, u, row, b.g.is_digraph(), hidden, dist, unreached);
941 if (e.empty() and unreached > 0)
942 e = "the tree has nodes that its root does not reach";
943 if (e.empty())
944 e = coverage_error(row, dist);
945 if (e.empty() and (root == nullptr or num(root) != u))
946 e = "the root is not the node of the start";
947 if (e.empty())
948 e = mapping_error(tree, b);
949 if (e.empty() and solver.is_painted())
950 e = "the solver says that it painted";
951 if (not e.empty())
952 {
953 c.tree.fail(s, from + e);
954 continue;
955 }
956
957 for (int v = 0; v < s.n; ++v)
958 check_tree_path<T>(c, solver, tree, b, s, d, u, v);
959 }
960 }
961
967 template <typename T, class Solver, class GT, class Partial>
968 void check_partial_trees(Tree_Checks & c, Solver & solver, Built<GT> & b, const Spec & s,
969 const Matrix & d, unsigned long hidden, Partial && partial)
970 {
971 for (int u = 0; u < s.n; ++u)
972 for (int v = 0; v < s.n; ++v)
973 {
974 const std::string where = route(u, v);
975 const auto & row = d[size_t(u)];
976 const long long want = row[size_t(v)];
977 c.tree.run();
978 GT tree;
979 typename GT::Node * end = nullptr;
980 if (auto e = what_throws([&]
981 { end = partial(b.nodes[size_t(u)], b.nodes[size_t(v)], tree); });
982 not e.empty())
983 {
984 c.tree.fail(s, where + e);
985 continue;
986 }
987 std::vector<long long> dist;
988 size_t unreached = 0;
989 std::string e = tree_error(tree, s, u, row, b.g.is_digraph(), hidden, dist, unreached);
990 if (e.empty() and unreached > 0)
991 e = "the tree has nodes that its root does not reach";
992 if (e.empty() and want != Inf and (end == nullptr or num(end) != v))
993 e = "the tree lacks the end";
994 if (e.empty() and want == Inf and end != nullptr)
995 e = "the tree has an end not reached";
996 if (e.empty())
997 e = mapping_error(tree, b);
998 if (not e.empty())
999 {
1000 c.tree.fail(s, where + e);
1001 continue;
1002 }
1003
1004 check_tree_path<T>(c, solver, tree, b, s, d, u, v);
1005 }
1006 }
1007
1010 template <typename T>
1011 T floyd_value(long long want)
1012 {
1013 if (want == Inf
1014 or (std::is_integral_v<T> and want >= (long long) std::numeric_limits<T>::max()))
1015 return std::numeric_limits<T>::max();
1016 return as<T>(want);
1017 }
1018
1020 struct Floyd_Checks
1021 {
1022 Check dist;
1023 Check path;
1024 Check index_path;
1025 Check next;
1026 Check nodes;
1027
1028 explicit Floyd_Checks(const std::string & how = "")
1029 : dist("Floyd_All_Shortest_Paths::get_dist_mat()" + how),
1030 path("Floyd_All_Shortest_Paths::get_min_path(src, tgt)" + how),
1031 index_path("Floyd_All_Shortest_Paths::get_min_path(i, j)" + how),
1032 next("Floyd_All_Shortest_Paths::get_path_mat()" + how),
1033 nodes("Floyd_All_Shortest_Paths::get_nodes()" + how)
1034 {}
1035
1036 void expect() const
1037 {
1038 for (const Check * c : {&dist, &path, &index_path, &next, &nodes})
1039 c->expect();
1040 }
1041 };
1042
1045 template <typename T, class Floyd, class GT>
1046 void check_floyd(Floyd_Checks & c, const Floyd & f, Built<GT> & b, const Spec & s,
1047 const Matrix & d, unsigned long hidden, bool full = true)
1048 {
1049 const bool directed = b.g.is_digraph();
1050 const auto & mat = f.get_dist_mat();
1051 const auto & next = f.get_path_mat();
1052
1053 if (full)
1054 {
1055 c.nodes.run();
1056 const auto nodes = f.get_nodes();
1057 const auto & ref = f.get_nodes_ref();
1058 const auto copy = f.get_nodes_copy();
1059 std::string e;
1060 if (nodes.size() != size_t(s.n) or ref.size() != nodes.size()
1061 or copy.size() != nodes.size())
1062 e = "the nodes are not those of the graph";
1063 for (long i = 0; e.empty() and i < s.n; ++i)
1064 if (nodes(i) != ref(i) or nodes(i) != copy(i) or f.select_node(i) != nodes(i)
1065 or f.index_node(nodes(i)) != i or (i > 0 and not (nodes(i - 1) < nodes(i))))
1066 e = "the node at " + std::to_string(i) + " is wrong, or out of order";
1067 std::set<int> seen;
1068 for (long i = 0; e.empty() and i < s.n; ++i)
1069 if (not seen.insert(num(nodes(i))).second)
1070 e = "a node twice";
1071 if (not e.empty())
1072 c.nodes.fail(s, e);
1073 }
1074
1075 c.dist.run();
1076 if (f.has_negative_cycle())
1077 {
1078 c.dist.fail(s, "finds a negative cycle");
1079 return;
1080 }
1081 for (int u = 0; u < s.n; ++u)
1082 for (int v = 0; v < s.n; ++v)
1083 {
1084 auto * nu = b.nodes[size_t(u)];
1085 auto * nv = b.nodes[size_t(v)];
1086 const long i = f.index_node(nu), j = f.index_node(nv);
1087 const long long want = d[size_t(u)][size_t(v)];
1088 const T value = floyd_value<T>(want);
1089 const bool none = value == std::numeric_limits<T>::max();
1090 if (mat(i, j) != value)
1091 c.dist.fail(s, route(u, v) + "gives " + text_of(mat(i, j)) + ", not " + text_of(value));
1092
1093 for (Check * pc : {&c.path, &c.index_path})
1094 {
1095 if (pc == &c.index_path and not full)
1096 continue;
1097 pc->run();
1098 Path<GT> path(b.g);
1099 if (auto e = what_throws([&]
1100 { path = pc == &c.path ? f.get_min_path(nu, nv) : f.get_min_path(i, j); });
1101 not e.empty())
1102 pc->fail(s, route(u, v) + e);
1103 else if (none)
1104 {
1105 if (not path.is_empty())
1106 pc->fail(s, route(u, v) + "gives the path " + walk_of(path).str()
1107 + " to a node not reached");
1108 }
1109 else if (auto pe = path_error(b.g, s, path, u, v, hidden, want, mat(i, j));
1110 not pe.empty())
1111 pc->fail(s, route(u, v) + pe);
1112 }
1113
1114 // The next node, through an arc that is the first of a shortest
1115 // path
1116 if (not full)
1117 continue;
1118 c.next.run();
1119 const long k = next(i, j);
1120 bool ok;
1121 if (u == v)
1122 ok = k == j;
1123 else if (none)
1124 ok = k == -1 or (std::is_integral_v<T> and want != Inf);
1125 else
1126 {
1127 ok = false;
1128 const int x = k >= 0 and k < s.n ? num(f.select_node(k)) : -1;
1129 for (size_t a = 0; x >= 0 and a < s.arcs.size() and not ok; ++a)
1130 {
1131 const int p = s.arcs[a][0], q = s.arcs[a][1];
1132 ok = graph_filters::shown(hidden, int(a))
1133 and ((p == u and q == x) or (not directed and q == u and p == x))
1134 and d[size_t(x)][size_t(v)] != Inf
1135 and s.w[a] + d[size_t(x)][size_t(v)] == want;
1136 }
1137 }
1138 if (not ok)
1139 c.next.fail(s, route(u, v) + "the next node is at " + std::to_string(k));
1140 }
1141 }
1142
1145 template <class GT, typename T>
1146 struct Johnson_Weight
1147 {
1148 using Distance_Type = T;
1149
1150 const std::vector<T> * weights = nullptr;
1151
1152 T operator () (typename GT::Arc * a) const
1153 {
1154 return (*weights)[size_t(a->get_info())];
1155 }
1156 };
1157
1159 template <typename T>
1160 T johnson_none()
1161 {
1162 if constexpr (std::numeric_limits<T>::has_infinity)
1163 return std::numeric_limits<T>::infinity();
1164 else
1165 return std::numeric_limits<T>::max();
1166 }
1167
1169 struct Johnson_Checks
1170 {
1171 Check build;
1172 Check distance;
1173 Check self;
1174 Check path;
1175 Check all_pairs;
1176 Check potential;
1177 Check copy;
1178 Check reweighted;
1179
1180 explicit Johnson_Checks(const std::string & how = "", bool answers_known = false,
1181 bool reweighted_known = false)
1182 : build("Johnson::Johnson()" + how),
1183 distance("Johnson::get_distance()" + how, answers_known),
1184 self("Johnson::get_distance(), from a node to itself" + how),
1185 path("Johnson::find_path()" + how, answers_known),
1186 all_pairs("Johnson::compute_all_pairs_distances()" + how, answers_known),
1187 potential("Johnson::get_potential()" + how),
1188 copy("Johnson::get_copy_node()" + how),
1189 reweighted("Johnson::get_reweighted_graph()" + how, reweighted_known)
1190 {}
1191
1192 void expect() const
1193 {
1194 for (const Check * c : {&build, &distance, &self, &path, &all_pairs, &potential,
1195 &copy, &reweighted})
1196 c->expect();
1197 }
1198 };
1199
1203 template <typename T, class GT, class SA>
1204 void check_johnson(Johnson_Checks & c, Built<GT> & b, const Spec & s, const Matrix & d,
1205 unsigned long hidden, const std::vector<T> & w, SA sa)
1206 {
1208 c.build.run();
1209 std::unique_ptr<Solver> j;
1210 if (auto e = what_throws([&]
1211 { j = std::make_unique<Solver>(b.g, Johnson_Weight<GT, T>{&w}, sa); });
1212 not e.empty())
1213 {
1214 c.build.fail(s, e);
1215 return;
1216 }
1217 if (not j->is_initialized())
1218 c.build.fail(s, "is not initialized");
1219
1220 const auto h = potentials(d);
1221 auto value = [&] (long long want) { return want == Inf ? johnson_none<T>() : as<T>(want); };
1222
1223 for (int u = 0; u < s.n; ++u)
1224 for (int v = 0; v < s.n; ++v)
1225 {
1226 auto * nu = b.nodes[size_t(u)];
1227 auto * nv = b.nodes[size_t(v)];
1228 const long long want = d[size_t(u)][size_t(v)];
1229 Check & dc = u == v ? c.self : c.distance;
1230 dc.run();
1231 T got{};
1232 if (auto e = what_throws([&] { got = j->get_distance(nu, nv); }); not e.empty())
1233 dc.fail(s, route(u, v) + e);
1234 else if (got != value(want))
1235 dc.fail(s, route(u, v) + "gives " + text_of(got) + ", not " + text_of(value(want)));
1236
1237 c.path.run();
1238 Path<GT> path(b.g);
1239 if (auto e = what_throws([&] { path = j->find_path(nu, nv); }); not e.empty())
1240 c.path.fail(s, route(u, v) + e);
1241 else if (want == Inf)
1242 {
1243 if (not path.is_empty())
1244 c.path.fail(s, route(u, v) + "gives the path " + walk_of(path).str()
1245 + " to a node not reached");
1246 }
1247 else if (auto pe = path_error(b.g, s, path, u, v, hidden, want, as<T>(want));
1248 not pe.empty())
1249 c.path.fail(s, route(u, v) + pe);
1250 }
1251
1252 c.all_pairs.run();
1253 if (auto e = what_throws([&]
1254 {
1255 const auto all = j->compute_all_pairs_distances();
1256 std::string wrong = all.size() == size_t(s.n * s.n) ? "" : "lacks pairs";
1257 for (int u = 0; wrong.empty() and u < s.n; ++u)
1258 for (int v = 0; wrong.empty() and v < s.n; ++v)
1259 {
1260 const auto * p = all.search({b.nodes[size_t(u)], b.nodes[size_t(v)]});
1261 const T want = value(d[size_t(u)][size_t(v)]);
1262 if (p == nullptr or p->second != want)
1263 wrong = route(u, v) + "gives "
1264 + (p == nullptr ? std::string("nothing") : text_of(p->second))
1265 + ", not " + text_of(want);
1266 }
1267 if (not wrong.empty())
1268 c.all_pairs.fail(s, wrong);
1269 }); not e.empty())
1270 c.all_pairs.fail(s, e);
1271
1272 // Again, with another source at each query, after the tree of the last
1273 // source that compute_all_pairs_distances() painted
1274 for (int v = 0; v < s.n; ++v)
1275 for (int u = s.n - 1; u >= 0; --u)
1276 {
1277 c.distance.run();
1278 T got{};
1279 const long long want = d[size_t(u)][size_t(v)];
1280 if (auto e = what_throws([&]
1281 { got = j->get_distance(b.nodes[size_t(u)], b.nodes[size_t(v)]); });
1282 not e.empty())
1283 c.distance.fail(s, route(u, v) + "again: " + e);
1284 else if (got != value(want))
1285 c.distance.fail(s, route(u, v) + "again: gives " + text_of(got) + ", not "
1286 + text_of(value(want)));
1287 }
1288
1289 for (int v = 0; v < s.n; ++v)
1290 {
1291 c.potential.run();
1292 T got{};
1293 if (auto e = what_throws([&] { got = j->get_potential(b.nodes[size_t(v)]); });
1294 not e.empty())
1295 c.potential.fail(s, "of " + std::to_string(v) + ": " + e);
1296 else if (got != as<T>(h[size_t(v)]))
1297 c.potential.fail(s, "of " + std::to_string(v) + ": gives " + text_of(got) + ", not "
1298 + text_of(as<T>(h[size_t(v)])));
1299
1300 c.copy.run();
1301 auto * p = j->get_copy_node(b.nodes[size_t(v)]);
1302 if (p == nullptr or num(p) != v)
1303 c.copy.fail(s, "of " + std::to_string(v) + ": a node of another number");
1304 }
1305
1306 // The arcs of the copy weigh w + h(x) - h(y) >= 0, as many between two
1307 // nodes as in the graph; the hidden ones are not reweighted
1308 if (hidden != 0)
1309 return;
1310 // The info of each arc of the copy is its new weight w + h(u) - h(v),
1311 // which is not negative, converted to the type of the info: an int
1312 // info keeps the integer part of a double new weight
1313 c.reweighted.run();
1314 using Info = typename GT::Arc_Type;
1315 std::multiset<std::tuple<int, int, Info>> want, got;
1316 for (size_t i = 0; i < s.arcs.size(); ++i)
1317 {
1318 const int x = s.arcs[i][0], y = s.arcs[i][1];
1319 want.insert({x, y, static_cast<Info>(w[i] + as<T>(h[size_t(x)]) - as<T>(h[size_t(y)]))});
1320 }
1321 std::string e;
1322 const GT & copy = j->get_reweighted_graph();
1323 for (auto it = copy.get_arc_it(); it.has_curr(); it.next_ne())
1324 {
1325 auto * a = it.get_curr();
1326 const int x = num(copy.get_src_node(a)), y = num(copy.get_tgt_node(a));
1327 if (a->get_info() < Info(0))
1328 e = "the arc from " + std::to_string(x) + " to " + std::to_string(y)
1329 + " weighs " + std::to_string(a->get_info());
1330 got.insert({x, y, a->get_info()});
1331 }
1332 if (e.empty() and got != want)
1333 e = "the infos of the reweighted arcs are not their new weights";
1334 if (not e.empty())
1335 c.reweighted.fail(s, e);
1336 }
1337
1343 template <typename T, class GT, class Items>
1344 std::string items_error(const GT & g, const Spec & s, const Items & items, int u, int v,
1345 unsigned long hidden, const std::vector<long long> & want, bool simple)
1346 {
1347 std::vector<Walk> seen;
1348 size_t i = 0;
1349 for (auto it = items.get_it(); it.has_curr(); it.next_ne(), ++i)
1350 {
1351 const auto & item = it.get_curr();
1352 if (i >= want.size())
1353 return "gives more than " + std::to_string(want.size()) + " paths";
1354 if (not item.path.inside_graph(g))
1355 return "a path of another graph";
1356 const Walk w = walk_of(item.path);
1357 if (auto e = walk_error(s, w, u, v, g.is_digraph(), hidden); not e.empty())
1358 return e;
1359 if (simple and not is_simple(w))
1360 return "the path " + w.str() + " repeats a node";
1361 const long long c = cost_of(s, w);
1362 if (item.total_cost != as<T>(c))
1363 return "the path " + w.str() + " comes with the cost " + text_of(item.total_cost)
1364 + ", not " + text_of(as<T>(c));
1365 if (c != want[i])
1366 return "the path " + std::to_string(i) + ", " + w.str() + ", costs "
1367 + std::to_string(c) + ", not " + std::to_string(want[i]);
1368 if (std::find(seen.begin(), seen.end(), w) != seen.end())
1369 return "gives the path " + w.str() + " twice";
1370 seen.push_back(w);
1371 }
1372 if (i != want.size())
1373 return "gives " + std::to_string(i) + " paths, not " + std::to_string(want.size());
1374 return "";
1375 }
1376
1381 struct K_Paths_Checks
1382 {
1383 Check yen;
1384 Check yen_class;
1385 Check eppstein;
1386 Check through;
1387 Check parallel;
1388 Check none;
1389
1390 explicit K_Paths_Checks(const std::string & how = "")
1391 : yen("yen_k_shortest_paths()" + how),
1392 yen_class("Yen_K_Shortest_Paths" + how),
1393 eppstein("eppstein_k_shortest_paths() and Eppstein_K_Shortest_Paths" + how),
1394 through("eppstein_k_shortest_paths() and Eppstein_K_Shortest_Paths" + how
1395 + ", to a node on a cycle"),
1396 parallel("eppstein_k_shortest_paths() and Eppstein_K_Shortest_Paths" + how
1397 + ", with parallel arcs"),
1398 none("yen_k_shortest_paths() and eppstein_k_shortest_paths(), with k = 0" + how)
1399 {}
1400
1401 void expect() const
1402 {
1403 for (const Check * c : {&yen, &yen_class, &eppstein, &through, &parallel, &none})
1404 c->expect();
1405 }
1406 };
1407
1414 template <typename T, class GT, class SA>
1415 void check_k_shortest(K_Paths_Checks & c, Built<GT> & b, const Spec & s,
1416 unsigned long hidden, const std::vector<T> & w, SA sa,
1417 bool yen = true, bool eppstein = true, bool light = false)
1418 {
1419 using W = Weights<GT, T>;
1420 std::vector<int> orig;
1421 const Spec shown = oracle::without(s, hidden, orig);
1422 const bool directed = b.g.is_digraph();
1423 const auto r = oracle::closure(shown, directed);
1424 const bool parallel = directed and has_parallel_arcs(shown, true);
1425
1426 for (int u = 0; u < s.n; ++u)
1427 for (int v = 0; v < s.n; ++v)
1428 {
1429 auto * nu = b.nodes[size_t(u)];
1430 auto * nv = b.nodes[size_t(v)];
1431 const auto simple = simple_path_costs(shown, u, v, directed);
1432 for (size_t k : {size_t(1), size_t(2), simple.size() + 1})
1433 for (Check * kc : {&c.yen, &c.yen_class})
1434 {
1435 if (not yen)
1436 break;
1437 if ((kc == &c.yen_class or light) and k != 2)
1438 continue;
1439 kc->run();
1440 const std::vector<long long>
1441 want(simple.begin(), simple.begin() + long(std::min(k, simple.size())));
1443 if (auto e = what_throws([&]
1444 {
1445 if (kc == &c.yen)
1446 items = yen_k_shortest_paths<GT, W, SA>(b.g, nu, nv, k, W{&w}, sa);
1447 else
1448 items = Yen_K_Shortest_Paths<GT, W, SA>(W{&w}, sa)(b.g, nu, nv, k);
1449 }); not e.empty())
1450 kc->fail(s, route(u, v) + "k = " + std::to_string(k) + ": " + e);
1451 else if (auto ie = items_error<T>(b.g, s, items, u, v, hidden, want, true);
1452 not ie.empty())
1453 kc->fail(s, route(u, v) + "k = " + std::to_string(k) + ": " + ie);
1454 }
1455
1456 // A walk that may go on from the end, or a choice among parallel
1457 // arcs, which are classes of their own
1458 const bool on_cycle = u != v and r[size_t(u)][size_t(v)] and r[size_t(v)][size_t(v)];
1459 Check & split = on_cycle ? c.through : parallel ? c.parallel : c.eppstein;
1460 for (size_t k : {size_t(1), size_t(3), size_t(6)})
1461 for (bool by_class : {false, true})
1462 {
1463 if (not eppstein)
1464 break;
1465 if ((by_class or light) and k != 3)
1466 continue;
1467 Check * kc = &split;
1468 kc->run();
1469 const std::vector<long long> want
1470 = u == v ? std::vector<long long>{0} : walk_costs(shown, u, v, directed, k);
1472 if (auto e = what_throws([&]
1473 {
1474 if (not by_class)
1475 items = eppstein_k_shortest_paths<GT, W, SA>(b.g, nu, nv, k, W{&w}, sa);
1476 else
1477 items = Eppstein_K_Shortest_Paths<GT, W, SA>(W{&w}, sa)(b.g, nu, nv, k);
1478 }); not e.empty())
1479 kc->fail(s, route(u, v) + "k = " + std::to_string(k) + ": " + e);
1480 else if (auto ie = items_error<T>(b.g, s, items, u, v, hidden, want, false);
1481 not ie.empty())
1482 kc->fail(s, route(u, v) + "k = " + std::to_string(k) + ": " + ie);
1483 }
1484 }
1485
1486 if (s.n == 0 or not yen or not eppstein)
1487 return;
1488 c.none.run();
1489 if (auto e = what_throws([&]
1490 {
1491 if (not yen_k_shortest_paths<GT, W, SA>(b.g, b.nodes[0], b.nodes[0], 0, W{&w}, sa)
1492 .is_empty()
1493 or not eppstein_k_shortest_paths<GT, W, SA>(b.g, b.nodes[0], b.nodes[0], 0,
1494 W{&w}, sa).is_empty())
1495 c.none.fail(s, "gives paths");
1496 }); not e.empty())
1497 c.none.fail(s, e);
1498 }
1499
1502 template <class GT, typename T>
1503 struct Static_Weight
1504 {
1505 using Distance_Type = T;
1506
1507 inline static const std::vector<T> * weights = nullptr;
1508
1509 T operator () (typename GT::Arc * a) const { return (*weights)[size_t(a->get_info())]; }
1510 };
1511
1513 Spec alternating(const Spec & shape, long long first)
1514 {
1515 return weighted(shape, [first] (size_t i) { return (first + (long long) i) % 2; });
1516 }
1517
1521 Spec with_negatives(const Spec & shape, bool directed)
1522 {
1523 if (not directed)
1524 return weighted(shape, Weighting::Falling);
1525 return weighted(shape, [] (size_t i) { return (long long) (i % 3) - 1; });
1526 }
1527}
1528
1529namespace
1530{
1533 template <typename T, class GT>
1534 void check_dijkstra_and_astar_with(const std::string & type)
1535 {
1536 using Node = typename GT::Node;
1537 Painting_Checks paint("Dijkstra_Min_Paths", ", " + type);
1538 Check copy("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), " + type);
1539 Check find("Dijkstra_Min_Paths::find_min_path(), " + type);
1540 Tree_Checks tree("Dijkstra_Min_Paths::compute_min_paths_tree(), " + type);
1541 Check astar("AStar_Min_Path::find_path(), " + type);
1542 Check astar_self("AStar_Min_Path::find_path(), " + type + ", from a node to itself");
1543
1544 std::vector<T> w;
1545 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1546 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1547
1548 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1549 {
1550 for (Weighting k : {Weighting::Ones, Weighting::Falling})
1551 {
1552 const Spec s = weighted(shape, k);
1553 Built<GT> b(s);
1554 w = weights_as<T>(s);
1555 const Matrix d = distances(s, b.g.is_digraph());
1556 check_painting<T>(paint, dij, b, s, d, 0);
1557 check_copies<T>(copy, nullptr, dij, b, s, d, 0);
1558 check_find<T>(find, nullptr, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
1559 { return dij.find_min_path(b.g, x, y, p); });
1560 check_trees<T>(tree, dij, b, s, d, 0, [&] (Node * x, GT & t)
1561 { return dij.compute_min_paths_tree(b.g, x, t); });
1562 check_find<T>(astar, &astar_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
1563 { return ast.find_path(b.g, x, y, p); });
1564 }
1565 });
1566
1567 paint.expect();
1568 copy.expect();
1569 find.expect();
1570 tree.expect();
1571 astar.expect();
1572 astar_self.expect();
1573 }
1574}
1575
1576template <class GT>
1577class ExhaustivePaths : public ::testing::Test {};
1578
1580
1581TYPED_TEST(ExhaustivePaths, DijkstraPaintingsMatchTheOracle)
1582{
1583 using GT = TypeParam;
1584 using T = long long;
1585 Painting_Checks paint("Dijkstra_Min_Paths");
1586
1587 // The same solver for every graph
1588 std::vector<T> w;
1589 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1590
1591 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1592 {
1593 for (Weighting k : {Weighting::Ones, Weighting::Falling})
1594 {
1595 const Spec s = weighted(shape, k);
1596 Built<GT> b(s);
1597 w = weights_as<T>(s);
1598 check_painting<T>(paint, dij, b, s, distances(s, b.g.is_digraph()), 0);
1599 }
1600 });
1601
1602 paint.expect();
1603}
1604
1605TYPED_TEST(ExhaustivePaths, AStarPaintingsMatchTheOracle)
1606{
1607 using GT = TypeParam;
1608 using T = long long;
1609 Painting_Checks paint("AStar_Min_Path");
1610
1611 std::vector<T> w;
1612 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1613
1614 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1615 {
1616 for (Weighting k : {Weighting::Ones, Weighting::Falling})
1617 {
1618 const Spec s = weighted(shape, k);
1619 Built<GT> b(s);
1620 w = weights_as<T>(s);
1621 check_painting<T>(paint, ast, b, s, distances(s, b.g.is_digraph()), 0);
1622 }
1623 });
1624
1625 paint.expect();
1626}
1627
1628TYPED_TEST(ExhaustivePaths, PaintedTreesAreCopied)
1629{
1630 using GT = TypeParam;
1631 using T = long long;
1632 Check dijkstra("Dijkstra_Min_Paths::copy_painted_min_paths_tree()");
1633 Check astar("AStar_Min_Path::copy_painted_min_paths_tree()");
1634 // The copy took every node of the graph, also those that the painting
1635 // does not reach, which compute_min_paths_tree() leaves out
1636 Check d_nodes("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), the nodes not reached");
1637 Check a_nodes("AStar_Min_Path::copy_painted_min_paths_tree(), the nodes not reached");
1638
1639 std::vector<T> w;
1640 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1641 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1642
1643 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1644 {
1645 for (Weighting k : All_Weightings)
1646 {
1647 const Spec s = weighted(shape, k);
1648 Built<GT> b(s);
1649 w = weights_as<T>(s);
1650 const Matrix d = distances(s, b.g.is_digraph());
1651 check_copies<T>(dijkstra, &d_nodes, dij, b, s, d, 0);
1652 if (k == Weighting::Falling)
1653 check_copies<T>(astar, &a_nodes, ast, b, s, d, 0);
1654 }
1655 });
1656
1657 dijkstra.expect();
1658 astar.expect();
1659 d_nodes.expect();
1660 a_nodes.expect();
1661}
1662
1663TYPED_TEST(ExhaustivePaths, DijkstraFindsTheShortestPaths)
1664{
1665 using GT = TypeParam;
1666 using Node = typename GT::Node;
1667 using T = long long;
1668 Check find("Dijkstra_Min_Paths::find_min_path()");
1669 Check op("Dijkstra_Min_Paths::operator()(g, start, end, path)");
1670 Check partial("Dijkstra_Min_Paths::paint_partial_min_paths_tree()");
1671 // The partial painting did not take its start for its end, so that from
1672 // a node to itself there was no path; there is the path of no arc
1673 Check find_self("Dijkstra_Min_Paths::find_min_path(), from a node to itself");
1674 Check op_self("Dijkstra_Min_Paths::operator()(g, start, end, path), from a node to itself");
1675 Check partial_self("Dijkstra_Min_Paths::paint_partial_min_paths_tree(), from a node to itself");
1676
1677 std::vector<T> w;
1678 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1679
1680 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1681 {
1682 for (Weighting k : All_Weightings)
1683 {
1684 const Spec s = weighted(shape, k);
1685 Built<GT> b(s);
1686 w = weights_as<T>(s);
1687 const Matrix d = distances(s, b.g.is_digraph());
1688 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
1689 { return dij.find_min_path(b.g, x, y, p); });
1690 if (k == Weighting::Falling)
1691 check_partial_painting<T>(partial, partial_self, dij, b, s, d, 0,
1692 [&] (Node * x, Node * y)
1693 { return dij.paint_partial_min_paths_tree(b.g, x, y); });
1694 }
1695 });
1696
1697 // operator() gives find_min_path(): the smaller domain suffices
1698 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1699 {
1700 const Spec s = weighted(shape, Weighting::Rising);
1701 Built<GT> b(s);
1702 w = weights_as<T>(s);
1703 check_find<T>(op, &op_self, b, s, distances(s, b.g.is_digraph()), 0,
1704 [&] (Node * x, Node * y, Path<GT> & p)
1705 { return dij(b.g, x, y, p); });
1706 });
1707
1708 for (const Check * c : {&find, &op, &partial, &find_self, &op_self, &partial_self})
1709 c->expect();
1710}
1711
1712TYPED_TEST(ExhaustivePaths, AStarFindsTheShortestPaths)
1713{
1714 using GT = TypeParam;
1715 using Node = typename GT::Node;
1716 using T = long long;
1717 Check find_path("AStar_Min_Path::find_path()");
1718 Check find_path_self("AStar_Min_Path::find_path(), from a node to itself");
1719 Check op("AStar_Min_Path::operator()(g, start, end, path)");
1720 Check op_self("AStar_Min_Path::operator()(g, start, end, path), from a node to itself");
1721 Check paint_path("AStar_Min_Path::paint_partial_path()");
1722 Check paint_path_self("AStar_Min_Path::paint_partial_path(), from a node to itself");
1723 Check alias("AStar_Min_Path::paint_path()");
1724 Check alias_self("AStar_Min_Path::paint_path(), from a node to itself");
1725
1726 std::vector<T> w;
1727 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1728
1729 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1730 {
1731 for (Weighting k : All_Weightings)
1732 {
1733 const Spec s = weighted(shape, k);
1734 Built<GT> b(s);
1735 w = weights_as<T>(s);
1736 const Matrix d = distances(s, b.g.is_digraph());
1737 if (k != Weighting::Rising)
1738 check_find<T>(find_path, &find_path_self, b, s, d, 0,
1739 [&] (Node * x, Node * y, Path<GT> & p)
1740 { return ast.find_path(b.g, x, y, p); });
1741 if (k == Weighting::Falling)
1742 check_partial_painting<T>(paint_path, paint_path_self, ast, b, s, d, 0,
1743 [&] (Node * x, Node * y)
1744 { return ast.paint_partial_path(b.g, x, y); });
1745 }
1746 });
1747
1748 // operator() gives find_path(), and paint_path() paint_partial_path():
1749 // the smaller domain suffices
1750 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1751 {
1752 for (Weighting k : {Weighting::Ones, Weighting::Rising})
1753 {
1754 const Spec s = weighted(shape, k);
1755 Built<GT> b(s);
1756 w = weights_as<T>(s);
1757 const Matrix d = distances(s, b.g.is_digraph());
1758 if (k == Weighting::Rising)
1759 check_find<T>(op, &op_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
1760 { return ast(b.g, x, y, p); });
1761 else
1762 check_partial_painting<T>(alias, alias_self, ast, b, s, d, 0, [&] (Node * x, Node * y)
1763 { return ast.paint_path(b.g, x, y); });
1764 }
1765 });
1766
1767 for (const Check * c : {&find_path, &find_path_self, &op, &op_self, &paint_path,
1768 &paint_path_self, &alias, &alias_self})
1769 c->expect();
1770}
1771
1772TYPED_TEST(ExhaustivePaths, AStarWithoutAHeuristicFindsTheShortestPaths)
1773{
1774 using GT = TypeParam;
1775 using Node = typename GT::Node;
1776 using T = long long;
1777 Check find("AStar_Min_Path::find_min_path()");
1778 Check find_self("AStar_Min_Path::find_min_path(), from a node to itself");
1779 Check partial("AStar_Min_Path::paint_partial_min_paths_tree()");
1780 Check partial_self("AStar_Min_Path::paint_partial_min_paths_tree(), from a node to itself");
1781
1782 std::vector<T> w;
1783 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1784
1785 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1786 {
1787 for (Weighting k : All_Weightings)
1788 {
1789 const Spec s = weighted(shape, k);
1790 Built<GT> b(s);
1791 w = weights_as<T>(s);
1792 const Matrix d = distances(s, b.g.is_digraph());
1793 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
1794 { return ast.find_min_path(b.g, x, y, p); });
1795 if (k == Weighting::Falling)
1796 check_partial_painting<T>(partial, partial_self, ast, b, s, d, 0,
1797 [&] (Node * x, Node * y)
1798 { return ast.paint_partial_min_paths_tree(b.g, x, y); });
1799 }
1800 });
1801
1802 for (const Check * c : {&find, &find_self, &partial, &partial_self})
1803 c->expect();
1804}
1805
1806TYPED_TEST(ExhaustivePaths, DijkstraTreesMatchTheOracle)
1807{
1808 using GT = TypeParam;
1809 using Node = typename GT::Node;
1810 using T = long long;
1811 Tree_Checks tree("Dijkstra_Min_Paths::compute_min_paths_tree()");
1812 Tree_Checks op("Dijkstra_Min_Paths::operator()(g, start, tree)");
1813 Tree_Checks partial("Dijkstra_Min_Paths::compute_partial_min_paths_tree()");
1814
1815 std::vector<T> w;
1816 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1817
1818 // The falling powers of 2 make the first of two parallel arcs the heavier
1819 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1820 {
1821 const Spec s = weighted(shape, Weighting::Falling);
1822 Built<GT> b(s);
1823 w = weights_as<T>(s);
1824 check_trees<T>(tree, dij, b, s, distances(s, b.g.is_digraph()), 0,
1825 [&] (Node * x, GT & t)
1826 { return dij.compute_min_paths_tree(b.g, x, t); });
1827 });
1828
1829 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1830 {
1831 for (Weighting k : All_Weightings)
1832 {
1833 const Spec s = weighted(shape, k);
1834 Built<GT> b(s);
1835 w = weights_as<T>(s);
1836 const Matrix d = distances(s, b.g.is_digraph());
1837 if (k == Weighting::Rising)
1838 continue;
1839 if (k == Weighting::Falling)
1840 check_trees<T>(op, dij, b, s, d, 0, [&] (Node * x, GT & t)
1841 {
1842 dij(b.g, x, t);
1843 return node_of(t, num(x));
1844 });
1845 check_partial_trees<T>(partial, dij, b, s, d, 0, [&] (Node * x, Node * y, GT & t)
1846 {
1847 dij.compute_partial_min_paths_tree(b.g, x, y, t);
1848 return node_of(t, num(y));
1849 });
1850 }
1851 });
1852
1853 tree.expect();
1854 op.expect();
1855 partial.expect();
1856}
1857
1858TYPED_TEST(ExhaustivePaths, AStarTreesMatchTheOracle)
1859{
1860 using GT = TypeParam;
1861 using Node = typename GT::Node;
1862 using T = long long;
1863 Tree_Checks tree("AStar_Min_Path::compute_min_paths_tree()");
1864 Tree_Checks op("AStar_Min_Path::operator()(g, start, tree)");
1865 Tree_Checks partial("AStar_Min_Path::compute_partial_min_paths_tree()");
1866 Tree_Checks path("AStar_Min_Path::compute_partial_path()");
1867 Tree_Checks alias("AStar_Min_Path::compute_path()");
1868
1869 std::vector<T> w;
1870 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1871
1872 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
1873 {
1874 const Spec s = weighted(shape, Weighting::Falling);
1875 Built<GT> b(s);
1876 w = weights_as<T>(s);
1877 check_trees<T>(tree, ast, b, s, distances(s, b.g.is_digraph()), 0,
1878 [&] (Node * x, GT & t)
1879 { return ast.compute_min_paths_tree(b.g, x, t); });
1880 });
1881
1882 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1883 {
1884 for (Weighting k : All_Weightings)
1885 {
1886 const Spec s = weighted(shape, k);
1887 Built<GT> b(s);
1888 w = weights_as<T>(s);
1889 const Matrix d = distances(s, b.g.is_digraph());
1890 if (k == Weighting::Rising)
1891 continue;
1892 check_partial_trees<T>(partial, ast, b, s, d, 0, [&] (Node * x, Node * y, GT & t)
1893 {
1894 ast.compute_partial_min_paths_tree(b.g, x, y, t);
1895 return node_of(t, num(y));
1896 });
1897 if (k != Weighting::Falling)
1898 continue;
1899 check_trees<T>(op, ast, b, s, d, 0, [&] (Node * x, GT & t)
1900 {
1901 ast(b.g, x, t);
1902 return node_of(t, num(x));
1903 });
1904 check_partial_trees<T>(path, ast, b, s, d, 0, [&] (Node * x, Node * y, GT & t)
1905 { return ast.compute_partial_path(b.g, x, y, t); });
1906 check_partial_trees<T>(alias, ast, b, s, d, 0, [&] (Node * x, Node * y, GT & t)
1907 { return ast.compute_path(b.g, x, y, t); });
1908 }
1909 });
1910
1911 tree.expect();
1912 op.expect();
1913 partial.expect();
1914 path.expect();
1915 alias.expect();
1916}
1917
1918TYPED_TEST(ExhaustivePaths, DijkstraPaintsWithEveryWeighting)
1919{
1920 using GT = TypeParam;
1921 using T = long long;
1922 Painting_Checks paint("Dijkstra_Min_Paths", ", every weighting");
1923
1924 std::vector<T> w;
1925 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1926
1927 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1928 {
1929 for_each_weighting(shape, {0, 1, 2}, [&] (const Spec & s)
1930 {
1931 Built<GT> b(s);
1932 w = weights_as<T>(s);
1933 check_painting<T>(paint, dij, b, s, distances(s, b.g.is_digraph()), 0);
1934 });
1935 });
1936
1937 paint.expect();
1938}
1939
1940TYPED_TEST(ExhaustivePaths, DijkstraFindsWithEveryWeighting)
1941{
1942 using GT = TypeParam;
1943 using Node = typename GT::Node;
1944 using T = long long;
1945 Check find("Dijkstra_Min_Paths::find_min_path(), every weighting");
1946
1947 std::vector<T> w;
1948 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
1949
1950 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1951 {
1952 for_each_weighting(shape, {0, 1, 2}, [&] (const Spec & s)
1953 {
1954 Built<GT> b(s);
1955 w = weights_as<T>(s);
1956 check_find<T>(find, nullptr, b, s, distances(s, b.g.is_digraph()), 0,
1957 [&] (Node * x, Node * y, Path<GT> & p)
1958 { return dij.find_min_path(b.g, x, y, p); });
1959 });
1960 });
1961
1962 find.expect();
1963}
1964
1965TYPED_TEST(ExhaustivePaths, AStarFindsWithEveryWeighting)
1966{
1967 using GT = TypeParam;
1968 using Node = typename GT::Node;
1969 using T = long long;
1970 Check find("AStar_Min_Path::find_path(), every weighting");
1971 Check find_self("AStar_Min_Path::find_path(), every weighting, from a node to itself");
1972
1973 std::vector<T> w;
1974 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
1975
1976 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
1977 {
1978 for_each_weighting(shape, {0, 1, 2}, [&] (const Spec & s)
1979 {
1980 Built<GT> b(s);
1981 w = weights_as<T>(s);
1982 check_find<T>(find, &find_self, b, s, distances(s, b.g.is_digraph()), 0,
1983 [&] (Node * x, Node * y, Path<GT> & p)
1984 { return ast.find_path(b.g, x, y, p); });
1985 });
1986 });
1987
1988 find.expect();
1989 find_self.expect();
1990}
1991
1992TYPED_TEST(ExhaustivePaths, DijkstraAndAStarOnRandomGraphs)
1993{
1994 using GT = TypeParam;
1995 using Node = typename GT::Node;
1996 using T = long long;
1997 Painting_Checks paint("Dijkstra_Min_Paths", ", random graphs");
1998 Check copy("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), random graphs");
1999 Check find("Dijkstra_Min_Paths::find_min_path(), random graphs");
2000 Check astar("AStar_Min_Path::find_path(), random graphs");
2001 Check astar_self("AStar_Min_Path::find_path(), random graphs, from a node to itself");
2002 Tree_Checks tree("Dijkstra_Min_Paths::compute_min_paths_tree(), random graphs");
2003
2004 std::vector<T> w;
2005 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
2006 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
2007
2008 for (const Spec & s : random_specs(3301, GT().is_digraph(), 60, 0, 9))
2009 {
2010 Built<GT> b(s);
2011 w = weights_as<T>(s);
2012 const Matrix d = distances(s, b.g.is_digraph());
2013 check_painting<T>(paint, dij, b, s, d, 0);
2014 check_copies<T>(copy, nullptr, dij, b, s, d, 0);
2015 check_find<T>(find, nullptr, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2016 { return dij.find_min_path(b.g, x, y, p); });
2017 check_find<T>(astar, &astar_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2018 { return ast.find_path(b.g, x, y, p); });
2019 check_trees<T>(tree, dij, b, s, d, 0, [&] (Node * x, GT & t)
2020 { return dij.compute_min_paths_tree(b.g, x, t); });
2021 }
2022
2023 paint.expect();
2024 copy.expect();
2025 find.expect();
2026 astar.expect();
2027 astar_self.expect();
2028 tree.expect();
2029}
2030
2031TYPED_TEST(ExhaustivePaths, DijkstraRespectsTheFilter)
2032{
2033 using GT = TypeParam;
2034 using Node = typename GT::Node;
2035 using T = long long;
2036 using F = Hide_Mask<GT>;
2037 Painting_Checks paint("Dijkstra_Min_Paths", ", hiding arcs");
2038 Check copy("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), hiding arcs");
2039 Check find("Dijkstra_Min_Paths::find_min_path(), hiding arcs");
2040 Tree_Checks tree("Dijkstra_Min_Paths::compute_min_paths_tree(), hiding arcs");
2041
2042 // The hidden arcs weigh -1, which a search that took them would notice
2043 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2044 {
2045 const Spec base = weighted(shape, Weighting::Falling);
2046 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
2047 {
2048 const Spec s = poisoned(base, hidden, -1);
2049 Built<GT> b(s);
2050 const std::vector<T> w = weights_as<T>(s);
2051 const Matrix d = distances(s, b.g.is_digraph(), hidden);
2052 Dijkstra_Of<GT, T, F> dij(Weights<GT, T>{&w}, F{hidden});
2053 check_painting<T>(paint, dij, b, s, d, hidden);
2054 check_copies<T>(copy, nullptr, dij, b, s, d, hidden);
2055 check_find<T>(find, nullptr, b, s, d, hidden, [&] (Node * x, Node * y, Path<GT> & p)
2056 { return dij.find_min_path(b.g, x, y, p); });
2057 check_trees<T>(tree, dij, b, s, d, hidden, [&] (Node * x, GT & t)
2058 { return dij.compute_min_paths_tree(b.g, x, t); });
2059 }
2060 });
2061
2062 paint.expect();
2063 copy.expect();
2064 find.expect();
2065 tree.expect();
2066}
2067
2068TYPED_TEST(ExhaustivePaths, AStarRespectsTheFilter)
2069{
2070 using GT = TypeParam;
2071 using Node = typename GT::Node;
2072 using T = long long;
2073 using F = Hide_Mask<GT>;
2074 Painting_Checks paint("AStar_Min_Path", ", hiding arcs");
2075 Check find_path("AStar_Min_Path::find_path(), hiding arcs");
2076 Check find_path_self("AStar_Min_Path::find_path(), hiding arcs, from a node to itself");
2077 Check find("AStar_Min_Path::find_min_path(), hiding arcs");
2078 Tree_Checks path("AStar_Min_Path::compute_partial_path(), hiding arcs");
2079
2080 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2081 {
2082 const Spec base = weighted(shape, Weighting::Falling);
2083 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
2084 {
2085 const Spec s = poisoned(base, hidden, -1);
2086 Built<GT> b(s);
2087 const std::vector<T> w = weights_as<T>(s);
2088 const Matrix d = distances(s, b.g.is_digraph(), hidden);
2089 AStar_Of<GT, T, F> ast(Weights<GT, T>{&w}, No_Heuristic<GT, T>(), F{hidden});
2090 check_painting<T>(paint, ast, b, s, d, hidden);
2091 check_find<T>(find_path, &find_path_self, b, s, d, hidden,
2092 [&] (Node * x, Node * y, Path<GT> & p)
2093 { return ast.find_path(b.g, x, y, p); });
2094 check_find<T>(find, nullptr, b, s, d, hidden, [&] (Node * x, Node * y, Path<GT> & p)
2095 { return ast.find_min_path(b.g, x, y, p); });
2096 check_partial_trees<T>(path, ast, b, s, d, hidden, [&] (Node * x, Node * y, GT & t)
2097 { return ast.compute_partial_path(b.g, x, y, t); });
2098 }
2099 });
2100
2101 paint.expect();
2102 find_path.expect();
2103 find_path_self.expect();
2104 find.expect();
2105 path.expect();
2106}
2107
2108TYPED_TEST(ExhaustivePaths, DijkstraAndAStarAfterAnotherPainting)
2109{
2110 using GT = TypeParam;
2111 using Node = typename GT::Node;
2112 using T = long long;
2113 using F = Hide_Mask<GT>;
2114 Painting_Checks paint("Dijkstra_Min_Paths", ", hiding arcs after another painting");
2115 Check copy("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), hiding arcs after another "
2116 "painting");
2117 Check find("Dijkstra_Min_Paths::find_min_path(), hiding arcs after another painting");
2118 Check astar("AStar_Min_Path::find_path(), hiding arcs after another painting");
2119 // Dial_Min_Paths resets every arc before it paints (Dial.H:219-220)
2120 Check dial_find("Dial_Min_Paths::find_min_path(), hiding arcs after another painting");
2121 Painting_Checks paint_left("Dijkstra_Min_Paths",
2122 ", hiding arcs after a painting that left one painted");
2123 Check copy_left("Dijkstra_Min_Paths::copy_painted_min_paths_tree(), hiding arcs after a "
2124 "painting that left one painted");
2125 Check find_left("Dijkstra_Min_Paths::find_min_path(), hiding arcs after a painting that "
2126 "left one painted");
2127 Check astar_left("AStar_Min_Path::find_path(), hiding arcs after a painting that left one "
2128 "painted");
2129
2130 // The rising weights make the first of two parallel arcs the lighter, the
2131 // one that the painting without the filter takes
2132 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2133 {
2134 const Spec s = weighted(shape, Weighting::Rising);
2135 const std::vector<T> w = weights_as<T>(s);
2136 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
2137 {
2138 Built<GT> b(s);
2139 const Matrix d = distances(s, b.g.is_digraph(), hidden);
2140 Dijkstra_Of<GT, T> plain(Weights<GT, T>{&w});
2141 Dijkstra_Of<GT, T, F> dij(Weights<GT, T>{&w}, F{hidden});
2142 AStar_Of<GT, T, F> ast(Weights<GT, T>{&w}, No_Heuristic<GT, T>(), F{hidden});
2143 Dial_Min_Paths<GT, Weights<GT, T>, Node_Arc_Iterator, F> dial(Weights<GT, T>{&w},
2144 F{hidden});
2145 for (int u = 0; u < s.n; ++u)
2146 {
2147 // What an algorithm without the filter leaves on the graph
2148 plain.paint_min_paths_tree(b.g, b.nodes[size_t(u)]);
2149 const bool left = painted_hidden_arc(b, hidden);
2150 check_find<T>(left ? find_left : find, nullptr, b, s, d, hidden,
2151 [&] (Node * x, Node * y, Path<GT> & p)
2152 { return dij.find_min_path(b.g, x, y, p); }, false, u);
2153 check_find<T>(left ? astar_left : astar, nullptr, b, s, d, hidden,
2154 [&] (Node * x, Node * y, Path<GT> & p)
2155 { return ast.find_path(b.g, x, y, p); }, false, u);
2156 check_painting<T>(left ? paint_left : paint, dij, b, s, d, hidden, false, u);
2157 check_copies<T>(left ? copy_left : copy, nullptr, dij, b, s, d, hidden, u);
2158 plain.paint_min_paths_tree(b.g, b.nodes[size_t(u)]);
2159 check_find<T>(dial_find, nullptr, b, s, d, hidden,
2160 [&] (Node * x, Node * y, Path<GT> & p)
2161 { return dial.find_min_path(b.g, x, y, p); }, false, u);
2162 }
2163 }
2164 });
2165
2166 paint.expect();
2167 copy.expect();
2168 find.expect();
2169 astar.expect();
2170 dial_find.expect();
2171 paint_left.expect();
2172 copy_left.expect();
2173 find_left.expect();
2174 astar_left.expect();
2175}
2176
2177TYPED_TEST(ExhaustivePaths, DijkstraAndAStarTakeIntAndDouble)
2178{
2179 check_dijkstra_and_astar_with<int, TypeParam>("int");
2180 check_dijkstra_and_astar_with<double, TypeParam>("double");
2181}
2182
2183namespace
2184{
2189 template <class GT, typename T>
2190 struct Exact_Heuristic
2191 {
2192 using Distance_Type = T;
2193
2194 const Matrix * d = nullptr;
2195
2196 T operator () (typename GT::Node * from, typename GT::Node * to) const
2197 {
2198 const long long x = (*d)[size_t(from->get_info())][size_t(to->get_info())];
2199 return x > (long long) std::numeric_limits<T>::max()
2200 ? std::numeric_limits<T>::max() : T(x);
2201 }
2202 };
2203
2209 template <class GT>
2210 void check_near_int_limits(bool astar)
2211 {
2212 using T = int;
2213 constexpr long long M = Int_Max;
2214 const std::string who = astar ? "AStar_Min_Path::find_path()"
2215 : "Dijkstra_Min_Paths::find_min_path()";
2216 Check paint("Dijkstra_Min_Paths::paint_min_paths_tree(), near the limits of int");
2217 Check paint_beyond("Dijkstra_Min_Paths::paint_min_paths_tree(), a distance beyond max()");
2218 Check find(who + ", near the limits of int");
2219 Check find_beyond(who + ", a distance beyond max()");
2220 // Each relaxation added with checked_add(), which threw for a sum beyond
2221 // max() also when the node is reached by a lighter path, or is not the
2222 // end; such a sum no longer throws by itself
2223 Check paint_sum("Dijkstra_Min_Paths::paint_min_paths_tree(), near the limits of int, "
2224 "a sum beyond max()");
2225 Check find_sum(who + ", near the limits of int, a sum beyond max()");
2226 // With a heuristic, a sum g + h beyond max() gave the arc the priority
2227 // max(), which tied with a path of exactly max(): the heap could keep
2228 // the other arc to the node, and then no path fitted
2229 Check exact("AStar_Min_Path::find_path() and compute_partial_path(), with the distance "
2230 "to the end as heuristic, near the limits of int");
2231 Check exact_beyond("AStar_Min_Path::find_path() and compute_partial_path(), with the "
2232 "distance to the end as heuristic, a distance beyond max()");
2233
2234 std::vector<T> w;
2235 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
2236 AStar_Of<GT, T> ast(Weights<GT, T>{&w});
2237 Matrix exact_d;
2238 AStar_Min_Path<GT, Weights<GT, T>, Exact_Heuristic<GT, T>>
2239 exact_ast(Weights<GT, T>{&w}, Exact_Heuristic<GT, T>{&exact_d});
2240 const bool directed = GT().is_digraph();
2241
2242 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
2243 {
2244 for_each_weighting(shape, {1, M / 2, M - 1}, [&] (const Spec & s)
2245 {
2246 Built<GT> b(s);
2247 w = weights_as<T>(s);
2248 const Matrix d = distances(s, directed);
2249 exact_d = d;
2250 for (int u = 0; u < s.n; ++u)
2251 {
2252 const auto & row = d[size_t(u)];
2253 if (not astar)
2254 {
2255 const bool fits = std::all_of(row.begin(), row.end(), [] (long long x)
2256 { return x == Inf or x <= Int_Max; });
2257 Check & pc = not fits ? paint_beyond
2258 : sum_beyond(s, d, u, Inf, directed, M) ? paint_sum : paint;
2259 pc.run();
2260 const auto e = what_throws([&]
2261 { dij.paint_min_paths_tree(b.g, b.nodes[size_t(u)]); });
2262 if (not fits)
2263 {
2264 if (not is(e, "overflow_error"))
2265 pc.fail(s, "from " + std::to_string(u) + ": gives no overflow_error");
2266 }
2267 else if (not e.empty())
2268 pc.fail(s, "from " + std::to_string(u) + ": " + e);
2269 else
2270 for (int v = 0; v < s.n; ++v)
2271 {
2272 if (row[size_t(v)] == Inf)
2273 continue;
2274 T got{};
2275 const auto ge = what_throws([&]
2276 { got = dij.get_distance(b.nodes[size_t(v)]); });
2277 if (not ge.empty() or got != T(row[size_t(v)]))
2278 {
2279 pc.fail(s, route(u, v) + (ge.empty() ? "gives " + text_of(got) : ge));
2280 break;
2281 }
2282 }
2283 }
2284
2285 for (int v = 0; v < s.n; ++v)
2286 {
2287 if (u == v)
2288 continue;
2289 const long long want = row[size_t(v)];
2290 const bool beyond = want != Inf and want > M;
2291 Check & c = beyond ? find_beyond
2292 : sum_beyond(s, d, u, want, directed, M) ? find_sum : find;
2293 c.run();
2294 Path<GT> path(b.g);
2295 T got{};
2296 const auto fe = what_throws([&]
2297 {
2298 auto * x = b.nodes[size_t(u)];
2299 auto * y = b.nodes[size_t(v)];
2300 got = astar ? ast.find_path(b.g, x, y, path)
2301 : dij.find_min_path(b.g, x, y, path);
2302 });
2303 if (beyond)
2304 {
2305 if (not is(fe, "overflow_error"))
2306 c.fail(s, route(u, v) + "gives no overflow_error");
2307 }
2308 else if (not fe.empty())
2309 c.fail(s, route(u, v) + fe);
2310 else if (want == Inf)
2311 {
2312 if (got != std::numeric_limits<T>::max() or not path.is_empty())
2313 c.fail(s, route(u, v) + "gives a path to a node not reached");
2314 }
2315 else if (auto pe = path_error(b.g, s, path, u, v, 0, want, got); not pe.empty())
2316 c.fail(s, route(u, v) + pe);
2317
2318 if (not astar)
2319 continue;
2320 Check & ec = beyond ? exact_beyond : exact;
2321 ec.run();
2322 auto * x = b.nodes[size_t(u)];
2323 auto * y = b.nodes[size_t(v)];
2324 Path<GT> found(b.g);
2325 T cost{};
2326 const auto ee = what_throws([&] { cost = exact_ast.find_path(b.g, x, y, found); });
2327 GT tree;
2328 typename GT::Node * reached = nullptr;
2329 const auto te = what_throws([&]
2330 { reached = exact_ast.compute_partial_path(b.g, x, y, tree); });
2331 if (beyond)
2332 {
2333 if (not is(ee, "overflow_error") or not is(te, "overflow_error"))
2334 ec.fail(s, route(u, v) + "gives no overflow_error");
2335 continue;
2336 }
2337 if (not ee.empty() or not te.empty())
2338 {
2339 ec.fail(s, route(u, v) + ee + te);
2340 continue;
2341 }
2342 if (want == Inf)
2343 {
2344 if (cost != std::numeric_limits<T>::max() or not found.is_empty()
2345 or reached != nullptr)
2346 ec.fail(s, route(u, v) + "gives a path to a node not reached");
2347 continue;
2348 }
2349 if (auto pe = path_error(b.g, s, found, u, v, 0, want, cost); not pe.empty())
2350 {
2351 ec.fail(s, route(u, v) + "find_path(): " + pe);
2352 continue;
2353 }
2354 Path<GT> in_tree(b.g);
2355 T tree_cost{};
2356 const auto ge = what_throws([&]
2357 { tree_cost = exact_ast.get_min_path(tree, y, in_tree); });
2358 if (reached == nullptr or not ge.empty())
2359 ec.fail(s, route(u, v) + "compute_partial_path() gives no path " + ge);
2360 else if (auto tpe = path_error(b.g, s, in_tree, u, v, 0, want, tree_cost);
2361 not tpe.empty())
2362 ec.fail(s, route(u, v) + "compute_partial_path(): " + tpe);
2363 }
2364 }
2365 });
2366 });
2367
2368 for (const Check * c : {&paint, &paint_beyond, &find, &find_beyond, &paint_sum, &find_sum,
2369 &exact, &exact_beyond})
2370 c->expect();
2371 }
2372}
2373
2374namespace
2375{
2386 template <class GT>
2387 void check_k_near_int_limits()
2388 {
2389 using T = int;
2390 using W = Weights<GT, T>;
2391 constexpr long long M = Int_Max;
2392 Check yen("yen_k_shortest_paths(), near the limits of int");
2393 Check yen_beyond("yen_k_shortest_paths(), near the limits of int, a path beyond max()");
2394 Check epp("eppstein_k_shortest_paths(), near the limits of int");
2395 Check epp_beyond("eppstein_k_shortest_paths(), near the limits of int, a walk beyond "
2396 "max()");
2397
2398 // Three weights on each arc, and every pair and k: up to 3 nodes and 3
2399 // arcs also in the heavy mode, whose node and arc more would make more
2400 // than a hundred times as many queries
2401 Domain dom = domain(3, 3);
2402 dom.max_nodes = std::min(dom.max_nodes, 3);
2403 dom.max_arcs = std::min(dom.max_arcs, 3);
2404
2405 std::vector<T> w;
2406 const bool directed = GT().is_digraph();
2407 for_each_spec(dom, directed, [&] (const Spec & shape)
2408 {
2409 for_each_weighting(shape, {1, M / 2, M - 1}, [&] (const Spec & s)
2410 {
2411 Built<GT> b(s);
2412 w = weights_as<T>(s);
2413 for (int u = 0; u < s.n; ++u)
2414 for (int v = 0; v < s.n; ++v)
2415 {
2416 if (u == v)
2417 continue;
2418 auto * nu = b.nodes[size_t(u)];
2419 auto * nv = b.nodes[size_t(v)];
2420 const auto simple = simple_path_costs(s, u, v, directed);
2421 for (size_t k : {size_t(1), size_t(2), size_t(3)})
2422 for (bool by_yen : {true, false})
2423 {
2424 std::vector<long long> want;
2425 if (by_yen)
2426 want.assign(simple.begin(),
2427 simple.begin() + long(std::min(k, simple.size())));
2428 else
2429 want = walk_costs(s, u, v, directed, k);
2430 const bool beyond = std::any_of(want.begin(), want.end(),
2431 [] (long long c)
2432 { return c > Int_Max; });
2433 Check & c = by_yen ? (beyond ? yen_beyond : yen)
2434 : (beyond ? epp_beyond : epp);
2435 c.run();
2436 const std::string at = route(u, v) + "k = " + std::to_string(k) + ": ";
2438 const auto e = what_throws([&]
2439 {
2440 items = by_yen ? yen_k_shortest_paths<GT, W>(b.g, nu, nv, k, W{&w})
2441 : eppstein_k_shortest_paths<GT, W>(b.g, nu, nv, k, W{&w});
2442 });
2443 if (beyond)
2444 {
2445 if (not is(e, "overflow_error"))
2446 c.fail(s, at + "gives no overflow_error " + e);
2447 }
2448 else if (not e.empty())
2449 c.fail(s, at + e);
2450 else if (auto ie = items_error<T>(b.g, s, items, u, v, 0, want, by_yen);
2451 not ie.empty())
2452 c.fail(s, at + ie);
2453 }
2454 }
2455 });
2456 });
2457
2458 for (const Check * c : {&yen, &yen_beyond, &epp, &epp_beyond})
2459 c->expect();
2460 }
2461}
2462
2463TYPED_TEST(ExhaustivePaths, KShortestPathsNearTheLimitsOfInt)
2464{
2465 check_k_near_int_limits<TypeParam>();
2466}
2467
2468TYPED_TEST(ExhaustivePaths, DijkstraNearTheLimitsOfInt)
2469{
2470 check_near_int_limits<TypeParam>(false);
2471}
2472
2473TYPED_TEST(ExhaustivePaths, AStarNearTheLimitsOfInt)
2474{
2475 check_near_int_limits<TypeParam>(true);
2476}
2477
2478TYPED_TEST(ExhaustivePaths, DijkstraAndAStarWithAFibonacciHeap)
2479{
2480 using GT = TypeParam;
2481 using Node = typename GT::Node;
2482 using T = long long;
2483 Painting_Checks paint("Dijkstra_Min_Paths<ArcFibonacciHeap>");
2484 Check find("Dijkstra_Min_Paths<ArcFibonacciHeap>::find_min_path()");
2485 Check astar("AStar_Min_Path<ArcFibonacciHeap>::find_path()");
2486 Check astar_self("AStar_Min_Path<ArcFibonacciHeap>::find_path(), from a node to itself");
2487
2488 std::vector<T> w;
2489 Dijkstra_Of<GT, T, Dft_Show_Arc<GT>, ArcFibonacciHeap> dij(Weights<GT, T>{&w});
2490 AStar_Of<GT, T, Dft_Show_Arc<GT>, ArcFibonacciHeap> ast(Weights<GT, T>{&w});
2491
2492 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2493 {
2494 for (Weighting k : All_Weightings)
2495 {
2496 const Spec s = weighted(shape, k);
2497 Built<GT> b(s);
2498 w = weights_as<T>(s);
2499 const Matrix d = distances(s, b.g.is_digraph());
2500 check_painting<T>(paint, dij, b, s, d, 0);
2501 check_find<T>(find, nullptr, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2502 { return dij.find_min_path(b.g, x, y, p); });
2503 check_find<T>(astar, &astar_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2504 { return ast.find_path(b.g, x, y, p); });
2505 }
2506 });
2507
2508 paint.expect();
2509 find.expect();
2510 astar.expect();
2511 astar_self.expect();
2512}
2513
2514namespace
2515{
2519 template <typename T, class GT>
2520 void check_zero_one_with(const std::string & type)
2521 {
2522 using Node = typename GT::Node;
2523 Painting_Checks paint("Zero_One_BFS", ", every weighting, " + type);
2524 Check find("Zero_One_BFS::find_min_path(), every weighting, " + type);
2525 Check find_self("Zero_One_BFS::find_min_path(), every weighting, " + type
2526 + ", from a node to itself");
2527 std::vector<T> w;
2528
2529 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2530 {
2531 for_each_weighting(shape, {0, 1}, [&] (const Spec & s)
2532 {
2533 Built<GT> b(s);
2534 w = weights_as<T>(s, true);
2535 const Matrix d = distances(s, b.g.is_digraph());
2536 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2537 check_painting<T>(paint, bfs, b, s, d, 0, true);
2538 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2539 { return bfs.find_min_path(b.g, x, y, p); }, true);
2540 });
2541 });
2542
2543 paint.expect();
2544 find.expect();
2545 find_self.expect();
2546 }
2547}
2548
2549TYPED_TEST(ExhaustivePaths, ZeroOneBFSMatchesTheOracle)
2550{
2551 using GT = TypeParam;
2552 using Node = typename GT::Node;
2553 using T = long long;
2554 Painting_Checks paint("Zero_One_BFS");
2555 Check find("Zero_One_BFS::find_min_path()");
2556 Check find_self("Zero_One_BFS::find_min_path(), from a node to itself");
2557 Check op("Zero_One_BFS::operator()");
2558 Check op_self("Zero_One_BFS::operator(), from a node to itself");
2559 std::vector<T> w;
2560
2561 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
2562 {
2563 for (long long first : {0, 1})
2564 {
2565 const Spec s = alternating(shape, first);
2566 Built<GT> b(s);
2567 w = weights_as<T>(s);
2568 const Matrix d = distances(s, b.g.is_digraph());
2569 // Declared after the graph, which outlives it
2570 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2571 if (first == 0)
2572 {
2573 check_painting<T>(paint, bfs, b, s, d, 0);
2574 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2575 { return bfs.find_min_path(b.g, x, y, p); });
2576 }
2577 else
2578 check_find<T>(op, &op_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2579 { return bfs(b.g, x, y, p); });
2580 }
2581 });
2582
2583 paint.expect();
2584 for (const Check * c : {&find, &find_self, &op, &op_self})
2585 c->expect();
2586}
2587
2588TYPED_TEST(ExhaustivePaths, ZeroOneBFSTakesEveryWeighting)
2589{
2590 check_zero_one_with<int, TypeParam>("int");
2591 check_zero_one_with<double, TypeParam>("double");
2592}
2593
2594TYPED_TEST(ExhaustivePaths, ZeroOneBFSRejectsOtherWeights)
2595{
2596 using GT = TypeParam;
2597 using T = long long;
2598 Check rejects("Zero_One_BFS::paint_min_paths_tree(), meeting a weight that is not 0 or 1");
2599 Check takes("Zero_One_BFS::paint_min_paths_tree(), meeting only weights 0 and 1");
2600 std::vector<T> w;
2601 const bool directed = GT().is_digraph();
2602
2603 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
2604 {
2605 for_each_weighting(shape, {0, 1, 2}, [&] (const Spec & s)
2606 {
2607 Built<GT> b(s);
2608 w = weights_as<T>(s);
2609 const Matrix d = distances(s, directed);
2610 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2611 for (int u = 0; u < s.n; ++u)
2612 {
2613 const bool meets = meets_other_weight(s, u, directed);
2614 Check & c = meets ? rejects : takes;
2615 c.run();
2616 const std::string from = "from " + std::to_string(u) + ": ";
2617 auto * src = b.nodes[size_t(u)];
2618 const auto e = what_throws([&] { bfs.paint_min_paths_tree(b.g, src); });
2619 if (meets)
2620 {
2621 if (not is(e, "domain_error") or bfs.is_painted())
2622 c.fail(s, from + "gives no domain_error, or says painted");
2623 continue;
2624 }
2625 if (not e.empty())
2626 {
2627 c.fail(s, from + e);
2628 continue;
2629 }
2630 for (int v = 0; v < s.n; ++v)
2631 {
2632 if (d[size_t(u)][size_t(v)] == Inf)
2633 continue;
2634 T got{};
2635 const auto ge = what_throws([&] { got = bfs.get_distance(b.nodes[size_t(v)]); });
2636 if (not ge.empty() or got != d[size_t(u)][size_t(v)])
2637 {
2638 c.fail(s, route(u, v) + (ge.empty() ? "gives " + text_of(got) : ge));
2639 break;
2640 }
2641 }
2642 }
2643 });
2644 });
2645
2646 rejects.expect();
2647 takes.expect();
2648}
2649
2650TYPED_TEST(ExhaustivePaths, ZeroOneBFSRespectsTheFilter)
2651{
2652 using GT = TypeParam;
2653 using Node = typename GT::Node;
2654 using T = long long;
2655 using F = Hide_Mask<GT>;
2656 Painting_Checks paint("Zero_One_BFS", ", hiding arcs");
2657 Check find("Zero_One_BFS::find_min_path(), hiding arcs");
2658 Check find_self("Zero_One_BFS::find_min_path(), hiding arcs, from a node to itself");
2659
2660 // The hidden arcs weigh 2, which the search would reject if it met them
2661 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2662 {
2663 const Spec base = alternating(shape, 0);
2664 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
2665 {
2666 const Spec s = poisoned(base, hidden, 2);
2667 Built<GT> b(s);
2668 const std::vector<T> w = weights_as<T>(s);
2669 const Matrix d = distances(s, b.g.is_digraph(), hidden);
2670 Zero_One_BFS<GT, Weights<GT, T>, Node_Arc_Iterator, F> bfs(Weights<GT, T>{&w}, F{hidden});
2671 check_painting<T>(paint, bfs, b, s, d, hidden);
2672 check_find<T>(find, &find_self, b, s, d, hidden, [&] (Node * x, Node * y, Path<GT> & p)
2673 { return bfs.find_min_path(b.g, x, y, p); });
2674 }
2675 });
2676
2677 paint.expect();
2678 find.expect();
2679 find_self.expect();
2680}
2681
2682TYPED_TEST(ExhaustivePaths, ZeroOneBFSLeavesOtherPaintingsAlone)
2683{
2684 using GT = TypeParam;
2685 using T = long long;
2686 Check others("~Zero_One_BFS, after another algorithm painted its graph");
2687 Check alone("~Zero_One_BFS, after another algorithm painted its graph, without a node reached");
2688 // A read of the freed graph shows under AddressSanitizer only
2689 Check dead("~Zero_One_BFS, after its graph was destroyed");
2690 std::vector<T> w;
2691
2692 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2693 {
2694 const Spec s = alternating(shape, 1);
2695 Built<GT> b(s);
2696 w = weights_as<T>(s);
2697 const Matrix d = distances(s, b.g.is_digraph());
2698 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&w});
2699 for (int u = 0; u < s.n; ++u)
2700 {
2701 const auto & row = d[size_t(u)];
2702 const bool reaches = std::count(row.begin(), row.end(), Inf) + 1 < long(row.size());
2703 Check & c = reaches ? others : alone;
2704 c.run();
2705 if (auto e = what_throws([&]
2706 {
2707 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2708 bfs.paint_min_paths_tree(b.g, b.nodes[size_t(u)]);
2709 dij.paint_min_paths_tree(b.g, b.nodes[size_t(u)]);
2710 }); not e.empty())
2711 {
2712 c.fail(s, "from " + std::to_string(u) + ": " + e);
2713 continue;
2714 }
2715 for (int v = 0; v < s.n; ++v)
2716 {
2717 T got{};
2718 const auto e = what_throws([&] { got = dij.get_distance(b.nodes[size_t(v)]); });
2719 if (row[size_t(v)] == Inf ? not is(e, "domain_error")
2720 : not e.empty() or got != row[size_t(v)])
2721 {
2722 c.fail(s, route(u, v) + "the painting of Dijkstra_Min_Paths is lost: "
2723 + (e.empty() ? text_of(got) : e));
2724 break;
2725 }
2726 }
2727 }
2728 // The search outlives the graph that it painted: the graph, made
2729 // after it, is destroyed before it
2730 dead.run();
2731 if (auto e = what_throws([&]
2732 {
2733 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2734 Built<GT> gone(s);
2735 if (s.n > 0)
2736 bfs.paint_min_paths_tree(gone.g, gone.nodes[0]);
2737 }); not e.empty())
2738 dead.fail(s, e);
2739 });
2740
2741 others.expect();
2742 alone.expect();
2743 dead.expect();
2744}
2745
2746TYPED_TEST(ExhaustivePaths, ZeroOneBFSAndDialOnRandomGraphs)
2747{
2748 using GT = TypeParam;
2749 using Node = typename GT::Node;
2750 using T = long long;
2751 Painting_Checks bfs_paint("Zero_One_BFS", ", random graphs");
2752 Check bfs_find("Zero_One_BFS::find_min_path(), random graphs");
2753 Painting_Checks dial_paint("Dial_Min_Paths", ", random graphs");
2754 Check dial_find("Dial_Min_Paths::find_min_path(), random graphs");
2755 std::vector<T> w;
2756 Dial_Min_Paths<GT, Weights<GT, T>> dial(Weights<GT, T>{&w});
2757
2758 for (const Spec & s : random_specs(3305, GT().is_digraph(), 40, 0, 1))
2759 {
2760 Built<GT> b(s);
2761 w = weights_as<T>(s);
2762 const Matrix d = distances(s, b.g.is_digraph());
2763 Zero_One_BFS<GT, Weights<GT, T>> bfs(Weights<GT, T>{&w});
2764 check_painting<T>(bfs_paint, bfs, b, s, d, 0);
2765 check_find<T>(bfs_find, nullptr, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2766 { return bfs.find_min_path(b.g, x, y, p); });
2767 }
2768
2769 for (const Spec & s : random_specs(3306, GT().is_digraph(), 40, 0, 5))
2770 {
2771 Built<GT> b(s);
2772 w = weights_as<T>(s);
2773 const Matrix d = distances(s, b.g.is_digraph());
2774 check_painting<T>(dial_paint, dial, b, s, d, 0);
2775 check_find<T>(dial_find, nullptr, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2776 { return dial.find_min_path(b.g, x, y, p); });
2777 }
2778
2779 bfs_paint.expect();
2780 bfs_find.expect();
2781 dial_paint.expect();
2782 dial_find.expect();
2783}
2784
2785namespace
2786{
2790 template <typename T, class GT>
2791 void check_dial_with(const std::string & type, bool painting)
2792 {
2793 using Node = typename GT::Node;
2794 Painting_Checks paint("Dial_Min_Paths", ", every weighting, " + type);
2795 Check find("Dial_Min_Paths::find_min_path(), every weighting, " + type);
2796 Check find_self("Dial_Min_Paths::find_min_path(), every weighting, " + type
2797 + ", from a node to itself");
2798
2799 std::vector<T> w;
2800 Dial_Min_Paths<GT, Weights<GT, T>> dial(Weights<GT, T>{&w});
2801
2802 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2803 {
2804 for_each_weighting(shape, {0, 1, 3}, [&] (const Spec & s)
2805 {
2806 Built<GT> b(s);
2807 w = weights_as<T>(s);
2808 const Matrix d = distances(s, b.g.is_digraph());
2809 if (painting)
2810 check_painting<T>(paint, dial, b, s, d, 0);
2811 else
2812 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2813 { return dial.find_min_path(b.g, x, y, p); });
2814 });
2815 });
2816
2817 paint.expect();
2818 find.expect();
2819 find_self.expect();
2820 }
2821}
2822
2823TYPED_TEST(ExhaustivePaths, DialMatchesTheOracle)
2824{
2825 using GT = TypeParam;
2826 using Node = typename GT::Node;
2827 using T = long long;
2828 Painting_Checks paint("Dial_Min_Paths");
2829 Check find("Dial_Min_Paths::find_min_path()");
2830 Check find_self("Dial_Min_Paths::find_min_path(), from a node to itself");
2831 Check op("Dial_Min_Paths::operator()");
2832 Check op_self("Dial_Min_Paths::operator(), from a node to itself");
2833
2834 std::vector<T> w;
2835 Dial_Min_Paths<GT, Weights<GT, T>> dial(Weights<GT, T>{&w});
2836
2837 for_each_spec(domain(4, 3), GT().is_digraph(), [&] (const Spec & shape)
2838 {
2839 for (Weighting k : {Weighting::Ones, Weighting::Falling})
2840 {
2841 const Spec s = weighted(shape, k);
2842 Built<GT> b(s);
2843 w = weights_as<T>(s);
2844 const Matrix d = distances(s, b.g.is_digraph());
2845 if (k == Weighting::Falling)
2846 {
2847 check_painting<T>(paint, dial, b, s, d, 0);
2848 check_find<T>(find, &find_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2849 { return dial.find_min_path(b.g, x, y, p); });
2850 }
2851 else
2852 check_find<T>(op, &op_self, b, s, d, 0, [&] (Node * x, Node * y, Path<GT> & p)
2853 { return dial(b.g, x, y, p); });
2854 }
2855 });
2856
2857 paint.expect();
2858 for (const Check * c : {&find, &find_self, &op, &op_self})
2859 c->expect();
2860}
2861
2862TYPED_TEST(ExhaustivePaths, DialPaintsWithEveryWeighting)
2863{
2864 check_dial_with<int, TypeParam>("int", true);
2865}
2866
2867TYPED_TEST(ExhaustivePaths, DialFindsWithEveryWeighting)
2868{
2869 check_dial_with<long long, TypeParam>("long long", false);
2870}
2871
2872TYPED_TEST(ExhaustivePaths, DialRespectsTheFilter)
2873{
2874 using GT = TypeParam;
2875 using Node = typename GT::Node;
2876 using T = long long;
2877 using F = Hide_Mask<GT>;
2878 Painting_Checks paint("Dial_Min_Paths", ", hiding arcs");
2879 Check find("Dial_Min_Paths::find_min_path(), hiding arcs");
2880 Check find_self("Dial_Min_Paths::find_min_path(), hiding arcs, from a node to itself");
2881 Painting_Checks negative("Dial_Min_Paths", ", hiding a negative arc", false, false);
2882
2883 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
2884 {
2885 const Spec base = weighted(shape, Weighting::Falling);
2886 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
2887 {
2888 Built<GT> b(base);
2889 const Matrix d = distances(base, b.g.is_digraph(), hidden);
2890 const std::vector<T> w = weights_as<T>(base);
2891 Dial_Min_Paths<GT, Weights<GT, T>, Node_Arc_Iterator, F> dial(Weights<GT, T>{&w},
2892 F{hidden});
2893 check_painting<T>(paint, dial, b, base, d, hidden);
2894 check_find<T>(find, &find_self, b, base, d, hidden, [&] (Node * x, Node * y, Path<GT> & p)
2895 { return dial.find_min_path(b.g, x, y, p); });
2896
2897 const Spec s = poisoned(base, hidden, -1);
2898 const std::vector<T> wn = weights_as<T>(s);
2900 with_negative(Weights<GT, T>{&wn}, F{hidden});
2901 check_painting<T>(negative, with_negative, b, s, d, hidden);
2902 }
2903 });
2904
2905 paint.expect();
2906 find.expect();
2907 find_self.expect();
2908 negative.expect();
2909}
2910
2911namespace
2912{
2916 template <typename T, class GT, class SA = Dft_Show_Arc<GT>>
2917 void check_floyd_of(Floyd_Checks & c, Built<GT> & b, const Spec & s, const Matrix & d,
2918 unsigned long hidden, const std::vector<T> & w, SA sa = SA(),
2919 bool full = true)
2920 {
2921 std::string e = what_throws([&]
2922 {
2923 const Floyd_All_Shortest_Paths<GT, Weights<GT, T>, SA> f(b.g, Weights<GT, T>{&w}, sa);
2924 check_floyd<T>(c, f, b, s, d, hidden, full);
2925 });
2926 if (not e.empty())
2927 {
2928 c.dist.run();
2929 c.dist.fail(s, "the solver " + e);
2930 }
2931 }
2932
2935 template <typename T, class GT>
2936 void check_floyd_with(const std::string & type)
2937 {
2938 Floyd_Checks c(", " + type);
2939 std::vector<T> w;
2940 const bool directed = GT().is_digraph();
2941 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
2942 {
2943 if (shape.n == 0)
2944 return;
2945 for (const Spec & s : {weighted(shape, Weighting::Falling),
2946 with_negatives(shape, directed)})
2947 {
2948 bool neg = false;
2949 const Matrix d = distances(s, directed, 0, neg);
2950 if (neg)
2951 continue;
2952 Built<GT> b(s);
2953 w = weights_as<T>(s);
2954 check_floyd_of<T>(c, b, s, d, 0, w);
2955 }
2956 });
2957 c.expect();
2958 }
2959}
2960
2961TYPED_TEST(ExhaustivePaths, FloydMatchesTheOracle)
2962{
2963 using GT = TypeParam;
2964 using T = long long;
2965 Floyd_Checks c;
2966 Check empty("Floyd_All_Shortest_Paths, a graph without nodes");
2967 std::vector<T> w;
2968 const bool directed = GT().is_digraph();
2969
2970 for_each_spec(domain(4, 3), directed, [&] (const Spec & shape)
2971 {
2972 if (shape.n == 0)
2973 {
2974 empty.run();
2975 Built<GT> b(shape);
2976 if (not is(what_throws([&]
2977 { Floyd_All_Shortest_Paths<GT, Weights<GT, T>> f(b.g, Weights<GT, T>{&w}); }),
2978 "invalid_argument"))
2979 empty.fail(shape, "gives no invalid_argument");
2980 return;
2981 }
2982 // A negative cycle is left out: it is a matter of the tests of
2983 // negative cycles
2984 const Spec s = with_negatives(shape, directed);
2985 bool neg = false;
2986 const Matrix d = distances(s, directed, 0, neg);
2987 if (neg)
2988 return;
2989 Built<GT> b(s);
2990 w = weights_as<T>(s);
2991 check_floyd_of<T>(c, b, s, d, 0, w);
2992 });
2993
2994 c.expect();
2995 empty.expect();
2996}
2997
2998TYPED_TEST(ExhaustivePaths, FloydTakesEveryWeighting)
2999{
3000 using GT = TypeParam;
3001 using T = long long;
3002 Floyd_Checks c(", every weighting");
3003 std::vector<T> w;
3004 const bool directed = GT().is_digraph();
3005 // In a graph that is not directed a negative arc is a negative cycle
3006 const std::vector<long long> values = directed ? std::vector<long long>{-1, 0, 2}
3007 : std::vector<long long>{0, 1, 2};
3008
3009 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
3010 {
3011 if (shape.n > 0)
3012 for_each_weighting(shape, values, [&] (const Spec & s)
3013 {
3014 bool neg = false;
3015 const Matrix d = distances(s, directed, 0, neg);
3016 if (neg)
3017 return;
3018 Built<GT> b(s);
3019 w = weights_as<T>(s);
3020 check_floyd_of<T>(c, b, s, d, 0, w, Dft_Show_Arc<GT>(), false);
3021 });
3022 });
3023
3024 c.expect();
3025}
3026
3027TYPED_TEST(ExhaustivePaths, FloydTakesNegativeWeights)
3028{
3029 using GT = TypeParam;
3030 using T = long long;
3031 Floyd_Checks c(", from -2 to 2");
3032 std::vector<T> w;
3033 const bool directed = GT().is_digraph();
3034
3035 // In a graph that is not directed, only the weightings without a
3036 // negative arc are left
3037 for_each_spec(domain(3, 2), directed, [&] (const Spec & shape)
3038 {
3039 if (shape.n > 0)
3040 for_each_weighting(shape, {-2, -1, 0, 1, 2}, [&] (const Spec & s)
3041 {
3042 bool neg = false;
3043 const Matrix d = distances(s, directed, 0, neg);
3044 if (neg)
3045 return;
3046 Built<GT> b(s);
3047 w = weights_as<T>(s);
3048 check_floyd_of<T>(c, b, s, d, 0, w);
3049 });
3050 });
3051
3052 c.expect();
3053}
3054
3055TYPED_TEST(ExhaustivePaths, FloydOnRandomGraphs)
3056{
3057 using GT = TypeParam;
3058 using T = long long;
3059 Floyd_Checks c(", random graphs");
3060 std::vector<T> w;
3061 const bool directed = GT().is_digraph();
3062
3063 for (const Spec & s : directed ? random_potential_specs(3302, 60)
3064 : random_specs(3302, false, 60, 0, 9))
3065 {
3066 Built<GT> b(s);
3067 w = weights_as<T>(s);
3068 check_floyd_of<T>(c, b, s, distances(s, directed), 0, w);
3069 }
3070
3071 c.expect();
3072}
3073
3074TYPED_TEST(ExhaustivePaths, FloydRespectsTheFilter)
3075{
3076 using GT = TypeParam;
3077 using T = long long;
3078 using F = Hide_Mask<GT>;
3079 Floyd_Checks c(", hiding arcs");
3080 Check lvalue("Floyd_All_Shortest_Paths(g, sa), hiding arcs");
3081 Check rvalue("Floyd_All_Shortest_Paths(g, SA(...)), hiding arcs");
3082 const bool directed = GT().is_digraph();
3083
3084 // The hidden arcs weigh -5, a negative cycle if a solver took them
3085 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
3086 {
3087 if (shape.n == 0)
3088 return;
3089 const Spec base = with_negatives(shape, directed);
3090 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
3091 {
3092 bool neg = false;
3093 const Matrix d = distances(base, directed, hidden, neg);
3094 if (neg)
3095 continue;
3096 const Spec s = poisoned(base, hidden, -5);
3097 Built<GT> b(s);
3098 const std::vector<T> w = weights_as<T>(s);
3099 check_floyd_of<T>(c, b, s, d, hidden, w, F{hidden}, false);
3100
3101 // The constructors that build the distance with Distance()
3102 Static_Weight<GT, T>::weights = &w;
3103 for (Check * k : {&lvalue, &rvalue})
3104 {
3105 k->run();
3106 F sa{hidden};
3107 std::string wrong;
3108 const auto e = what_throws([&]
3109 {
3111 const auto f = k == &lvalue ? Floyd(b.g, sa) : Floyd(b.g, F{hidden});
3112 for (int u = 0; wrong.empty() and u < s.n; ++u)
3113 for (int v = 0; wrong.empty() and v < s.n; ++v)
3114 if (f.get_dist_mat()(f.index_node(b.nodes[size_t(u)]),
3115 f.index_node(b.nodes[size_t(v)]))
3116 != floyd_value<T>(d[size_t(u)][size_t(v)]))
3117 wrong = route(u, v) + "a wrong distance";
3118 });
3119 if (not e.empty() or not wrong.empty())
3120 k->fail(s, e + wrong);
3121 }
3122 }
3123 });
3124
3125 c.expect();
3126 lvalue.expect();
3127 rvalue.expect();
3128}
3129
3130TYPED_TEST(ExhaustivePaths, FloydNearTheLimitsOfInt)
3131{
3132 using GT = TypeParam;
3133 using T = int;
3134 constexpr long long M = Int_Max;
3135 // A sum above max() does not count, as documented, so that a distance
3136 // beyond max() is no path
3137 Floyd_Checks c(", near the limits of int");
3138 std::vector<T> w;
3139 const bool directed = GT().is_digraph();
3140
3141 auto check = [&] (const Spec & s)
3142 {
3143 Built<GT> b(s);
3144 w = weights_as<T>(s);
3145 check_floyd_of<T>(c, b, s, distances(s, directed), 0, w, Dft_Show_Arc<GT>(), false);
3146 };
3147
3148 // The sums of two halves fit, and with a 1 reach max(), which means no
3149 // path; the others do not fit
3150 for_each_spec(domain(3, 3), directed, [&] (const Spec & shape)
3151 {
3152 if (shape.n > 0)
3153 for_each_weighting(shape, {M / 2, M - 1}, check);
3154 });
3155 for_each_spec(domain(3, 2), directed, [&] (const Spec & shape)
3156 {
3157 if (shape.n > 0)
3158 for_each_weighting(shape, {1, M / 2, M - 1}, check);
3159 });
3160
3161 c.expect();
3162}
3163
3164TYPED_TEST(ExhaustivePaths, FloydTakesIntAndDouble)
3165{
3166 check_floyd_with<int, TypeParam>("int");
3167 check_floyd_with<double, TypeParam>("double");
3168}
3169
3170namespace
3171{
3176 template <typename T, class GT, class Pick>
3177 void check_johnson_with(Pick && pick)
3178 {
3179 std::vector<T> w;
3180 for_each_spec(domain(3, 3), true, [&] (const Spec & shape)
3181 {
3182 for (const Spec & s : {weighted(shape, Weighting::Falling), with_negatives(shape, true)})
3183 {
3184 bool neg = false;
3185 const Matrix d = distances(s, true, 0, neg);
3186 if (neg)
3187 continue;
3188 Built<GT> b(s);
3189 w = weights_as<T>(s);
3190 check_johnson<T>(pick(s, d), b, s, d, 0, w, Dft_Show_Arc<GT>());
3191 }
3192 });
3193 }
3194
3197 std::vector<long long> reweighted(const Spec & s, const Matrix & d, unsigned long hidden)
3198 {
3199 const auto h = potentials(d);
3200 std::vector<long long> r;
3201 for (size_t i = 0; i < s.arcs.size(); ++i)
3202 if (graph_filters::shown(hidden, int(i)))
3203 r.push_back(s.w[i] + h[size_t(s.arcs[i][0])] - h[size_t(s.arcs[i][1])]);
3204 return r;
3205 }
3206
3209 bool reweighting_hides(const Spec & s, const Matrix & d, unsigned long hidden)
3210 {
3211 for (long long x : reweighted(s, d, hidden))
3212 if (not graph_filters::shown(hidden, int(x)))
3213 return true;
3214 return false;
3215 }
3216
3218 bool hides_a_parallel_arc(const Spec & s, unsigned long hidden)
3219 {
3220 for (size_t i = 0; i < s.arcs.size(); ++i)
3221 for (size_t j = 0; j < s.arcs.size(); ++j)
3222 if (s.arcs[i] == s.arcs[j] and not graph_filters::shown(hidden, int(i))
3223 and graph_filters::shown(hidden, int(j)))
3224 return true;
3225 return false;
3226 }
3227
3230 bool reweighting_halves(const Spec & s, const Matrix & d)
3231 {
3232 for (long long x : reweighted(s, d, 0))
3233 if (x % 2 != 0)
3234 return true;
3235 return false;
3236 }
3237}
3238
3239template <class GT>
3240class ExhaustiveJohnson : public ::testing::Test {};
3241
3243
3244TYPED_TEST(ExhaustiveJohnson, JohnsonMatchesTheOracle)
3245{
3246 using GT = TypeParam;
3247 using T = long long;
3248 Johnson_Checks c;
3249 Check none("Johnson(), without a graph");
3250 std::vector<T> w;
3251
3252 for_each_spec(domain(4, 3), true, [&] (const Spec & shape)
3253 {
3254 // A negative cycle is left out: it is a matter of the tests of
3255 // negative cycles
3256 for (const Spec & s : {weighted(shape, Weighting::Falling), with_negatives(shape, true)})
3257 {
3258 bool neg = false;
3259 const Matrix d = distances(s, true, 0, neg);
3260 if (neg)
3261 continue;
3262 Built<GT> b(s);
3263 w = weights_as<T>(s);
3264 check_johnson<T>(c, b, s, d, 0, w, Dft_Show_Arc<GT>());
3265 }
3266
3267 // Without a graph, the queries throw
3268 if (shape.n == 0)
3269 return;
3270 none.run();
3271 Built<GT> b(shape);
3273 auto * p = b.nodes[0];
3274 if (j.is_initialized()
3275 or not is(what_throws([&] { (void) j.get_distance(p, p); }), "domain_error")
3276 or not is(what_throws([&] { (void) j.find_path(p, p); }), "domain_error")
3277 or not is(what_throws([&] { (void) j.compute_all_pairs_distances(); }), "domain_error")
3278 or not is(what_throws([&] { (void) j.get_potential(p); }), "domain_error"))
3279 none.fail(shape, "answers without a graph");
3280 });
3281
3282 c.expect();
3283 none.expect();
3284}
3285
3286TYPED_TEST(ExhaustiveJohnson, JohnsonTakesEveryWeighting)
3287{
3288 using GT = TypeParam;
3289 using T = long long;
3290 Johnson_Checks c(", every weighting");
3291 std::vector<T> w;
3292
3293 for_each_spec(domain(3, 3), true, [&] (const Spec & shape)
3294 {
3295 for_each_weighting(shape, {-1, 0, 2}, [&] (const Spec & s)
3296 {
3297 bool neg = false;
3298 const Matrix d = distances(s, true, 0, neg);
3299 if (neg)
3300 return;
3301 Built<GT> b(s);
3302 w = weights_as<T>(s);
3303 check_johnson<T>(c, b, s, d, 0, w, Dft_Show_Arc<GT>());
3304 });
3305 });
3306
3307 c.expect();
3308}
3309
3310TYPED_TEST(ExhaustiveJohnson, JohnsonOnRandomGraphs)
3311{
3312 using GT = TypeParam;
3313 using T = long long;
3314 Johnson_Checks c(", random graphs");
3315 std::vector<T> w;
3316
3317 for (const Spec & s : random_potential_specs(3303, 40))
3318 {
3319 Built<GT> b(s);
3320 w = weights_as<T>(s);
3321 check_johnson<T>(c, b, s, distances(s, true), 0, w, Dft_Show_Arc<GT>());
3322 }
3323
3324 c.expect();
3325}
3326
3327TYPED_TEST(ExhaustiveJohnson, JohnsonRespectsTheFilter)
3328{
3329 using GT = TypeParam;
3330 using T = long long;
3331 using F = Hide_Mask<GT>;
3332 Johnson_Checks c(", hiding arcs");
3333 // Two classes of cases of their own, which the harness found: an arc
3334 // whose new weight w + h(u) - h(v) is the number of a hidden one, which
3335 // the filter hid when it read the infos of the copies of the arcs, where
3336 // those weights are; and one of two parallel arcs hidden, whose copies
3337 // were paired with the arcs by their ends, so that in List_SDigraph,
3338 // which gives its arcs in the order of their addresses, the shown one
3339 // could take the weight of the hidden one, and the path the hidden arc
3340 Johnson_Checks hides(", hiding arcs, a new weight that the filter hides");
3341 Johnson_Checks paired(", hiding one of two parallel arcs");
3342
3343 // The hidden arcs weigh -5, a negative cycle if the solver took them
3344 for_each_spec(domain(3, 3), true, [&] (const Spec & shape)
3345 {
3346 const Spec base = with_negatives(shape, true);
3347 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
3348 {
3349 bool neg = false;
3350 const Matrix d = distances(base, true, hidden, neg);
3351 if (neg)
3352 continue;
3353 const Spec s = poisoned(base, hidden, -5);
3354 Built<GT> b(s);
3355 const std::vector<T> w = weights_as<T>(s);
3356 Johnson_Checks & which = reweighting_hides(base, d, hidden) ? hides
3357 : hides_a_parallel_arc(base, hidden) ? paired : c;
3358 check_johnson<T>(which, b, s, d, hidden, w, F{hidden});
3359 }
3360 });
3361
3362 c.expect();
3363 hides.expect();
3364 paired.expect();
3365}
3366
3367TYPED_TEST(ExhaustiveJohnson, JohnsonNearTheLimitsOfInt)
3368{
3369 using GT = TypeParam;
3370 using T = int;
3371 constexpr long long M = Int_Max;
3372 Check fits("Johnson, near the limits of int");
3373 // Two classes of cases of their own, which the harness found: a new
3374 // weight w + h(u) - h(v), or a sum of Dijkstra on those weights, beyond
3375 // max() although every distance fits, which threw overflow_error while
3376 // they were kept in int; and a distance beyond max(), which made every
3377 // query from its source throw while the whole tree was painted in int
3378 Check too_big("Johnson, near the limits of int, a reweighted value beyond max()");
3379 Check beyond("Johnson, near the limits of int, a distance beyond max()");
3380 std::vector<T> w;
3381
3382 for_each_spec(domain(3, 3), true, [&] (const Spec & shape)
3383 {
3384 for_each_weighting(shape, {-2, 1, M - 1}, [&] (const Spec & s)
3385 {
3386 bool neg = false;
3387 const Matrix d = distances(s, true, 0, neg);
3388 if (neg)
3389 return;
3390 const auto h = potentials(d);
3391 bool all_fit = true;
3392 for (const auto & row : d)
3393 for (long long x : row)
3394 all_fit = all_fit and (x == Inf or x <= M);
3395 bool over = false;
3396 for (size_t i = 0; i < s.arcs.size() and not over; ++i)
3397 {
3398 const size_t x = size_t(s.arcs[i][0]), y = size_t(s.arcs[i][1]);
3399 const long long wp = s.w[i] + h[x] - h[y];
3400 over = wp > M;
3401 for (int src = 0; src < s.n and not over; ++src)
3402 over = x != y and d[size_t(src)][x] != Inf
3403 and d[size_t(src)][x] + h[size_t(src)] - h[x] + wp > M;
3404 }
3405 Check & c = not all_fit ? beyond : over ? too_big : fits;
3406 c.run();
3407
3408 Built<GT> b(s);
3409 w = weights_as<T>(s);
3410 using Solver = Johnson<GT, Johnson_Weight<GT, T>>;
3411 std::unique_ptr<Solver> j;
3412 if (auto e = what_throws([&]
3413 { j = std::make_unique<Solver>(b.g, Johnson_Weight<GT, T>{&w}); });
3414 not e.empty())
3415 {
3416 c.fail(s, "the constructor " + e);
3417 return;
3418 }
3419 for (int u = 0; u < s.n; ++u)
3420 for (int v = 0; v < s.n; ++v)
3421 {
3422 const long long want = d[size_t(u)][size_t(v)];
3423 T got{};
3424 const auto e = what_throws([&]
3425 { got = j->get_distance(b.nodes[size_t(u)], b.nodes[size_t(v)]); });
3426 const bool ok = want != Inf and want > M ? is(e, "overflow_error")
3427 : e.empty() and got == (want == Inf ? std::numeric_limits<T>::max() : T(want));
3428 if (not ok)
3429 {
3430 c.fail(s, route(u, v) + (e.empty() ? "gives " + text_of(got) : e));
3431 return;
3432 }
3433 }
3434 });
3435 });
3436
3437 fits.expect();
3438 beyond.expect();
3439 too_big.expect();
3440}
3441
3442TYPED_TEST(ExhaustiveJohnson, JohnsonNearTheLowestOfInt)
3443{
3444 using GT = TypeParam;
3445 using T = int;
3446 constexpr long long M = Int_Max;
3447 constexpr long long L = std::numeric_limits<T>::lowest();
3448 // Classes of their own: a distance below lowest(), and so a potential,
3449 // which is a distance from some node, which made the constructor throw
3450 // overflow_error and lost every query, and throws as a distance above
3451 // max() does; and a negative cycle whose sums go below lowest(), which
3452 // threw overflow_error and is a domain_error
3453 Check low("Johnson, near the lowest of int");
3454 Check below("Johnson, near the lowest of int, a distance below lowest()");
3455 Check cycle("Johnson, near the lowest of int, a negative cycle");
3456 size_t runs_low = 0, runs_below = 0, runs_cycle = 0;
3457 std::vector<T> w;
3458 auto fits = [] (long long x) { return L <= x and x <= M; };
3459 auto said = [] (const std::string & e) { return e.empty() ? std::string("nothing") : e; };
3460
3461 for_each_spec(domain(3, 3), true, [&] (const Spec & shape)
3462 {
3463 for_each_weighting(shape, {L, -1, 1, M}, [&] (const Spec & s)
3464 {
3465 bool neg = false;
3466 const Matrix d = distances(s, true, 0, neg);
3467 Built<GT> b(s);
3468 w = weights_as<T>(s);
3469 using Solver = Johnson<GT, Johnson_Weight<GT, T>>;
3470 std::unique_ptr<Solver> j;
3471 const auto e = what_throws([&]
3472 { j = std::make_unique<Solver>(b.g, Johnson_Weight<GT, T>{&w}); });
3473 if (neg)
3474 {
3475 cycle.run();
3476 ++runs_cycle;
3477 if (not is(e, "domain_error"))
3478 cycle.fail(s, "the constructor throws " + said(e));
3479 return;
3480 }
3481
3482 const auto h = potentials(d);
3483 bool low_d = false, all_fit = true;
3484 for (const auto & row : d)
3485 for (long long x : row)
3486 if (x != Inf)
3487 {
3488 low_d = low_d or x < L;
3489 all_fit = all_fit and fits(x);
3490 }
3491 Check & c = low_d ? below : low;
3492 ++(low_d ? runs_below : runs_low);
3493 c.run();
3494 if (not e.empty())
3495 {
3496 c.fail(s, "the constructor " + e);
3497 return;
3498 }
3499
3500 for (int u = 0; u < s.n; ++u)
3501 for (int v = 0; v < s.n; ++v)
3502 {
3503 auto * nu = b.nodes[size_t(u)];
3504 auto * nv = b.nodes[size_t(v)];
3505 const long long want = d[size_t(u)][size_t(v)];
3506 const bool throws = want != Inf and not fits(want);
3507
3508 T got{};
3509 const auto de = what_throws([&] { got = j->get_distance(nu, nv); });
3510 if (throws ? not is(de, "overflow_error")
3511 : not de.empty() or got != (want == Inf ? johnson_none<T>() : T(want)))
3512 {
3513 c.fail(s, route(u, v) + (de.empty() ? "gives " + text_of(got) : de));
3514 return;
3515 }
3516
3517 Path<GT> path(b.g);
3518 const auto pe = what_throws([&] { path = j->find_path(nu, nv); });
3519 std::string why;
3520 if (throws)
3521 why = is(pe, "overflow_error") ? "" : "find_path() throws " + said(pe);
3522 else if (not pe.empty())
3523 why = "find_path() " + pe;
3524 else if (want == Inf)
3525 why = path.is_empty() ? "" : "a path to a node not reached";
3526 else
3527 why = path_error(b.g, s, path, u, v, 0, want, T(want));
3528 if (not why.empty())
3529 {
3530 c.fail(s, route(u, v) + why);
3531 return;
3532 }
3533 }
3534
3535 for (int v = 0; v < s.n; ++v)
3536 {
3537 T got{};
3538 const auto pe = what_throws([&] { got = j->get_potential(b.nodes[size_t(v)]); });
3539 const long long want = h[size_t(v)];
3540 if (want < L ? not is(pe, "overflow_error") : not pe.empty() or got != T(want))
3541 {
3542 c.fail(s, "the potential of " + std::to_string(v) + ": "
3543 + (pe.empty() ? "gives " + text_of(got) : pe));
3544 return;
3545 }
3546 }
3547
3548 const auto ae = what_throws([&] { (void) j->compute_all_pairs_distances(); });
3549 if (all_fit ? not ae.empty() : not is(ae, "overflow_error"))
3550 c.fail(s, "compute_all_pairs_distances() throws " + said(ae));
3551 });
3552 });
3553
3554 // Each class has cases
3555 EXPECT_GT(runs_low, 0u);
3556 EXPECT_GT(runs_below, 0u);
3557 EXPECT_GT(runs_cycle, 0u);
3558 low.expect();
3559 below.expect();
3560 cycle.expect();
3561}
3562
3563TYPED_TEST(ExhaustiveJohnson, JohnsonTakesIntAndDouble)
3564{
3565 Johnson_Checks with_int(", int");
3566 check_johnson_with<int, TypeParam>([&] (const Spec &, const Matrix &) -> Johnson_Checks &
3567 { return with_int; });
3568 with_int.expect();
3569
3570 Johnson_Checks with_double(", double");
3571 // A class of its own, which the harness found: a new weight w + h(u) -
3572 // h(v) with a fraction, which the answers once lost; the infos of the
3573 // copy, int here, keep its integer part, as get_reweighted_graph() says
3574 Johnson_Checks halves(", double, a new weight with a fraction");
3575 check_johnson_with<double, TypeParam>([&] (const Spec & s, const Matrix & d) -> Johnson_Checks &
3576 { return reweighting_halves(s, d) ? halves : with_double; });
3577 with_double.expect();
3578 halves.expect();
3579}
3580
3581TYPED_TEST(ExhaustivePaths, YenGivesTheSimplePathsInCostOrder)
3582{
3583 using GT = TypeParam;
3584 using T = long long;
3585 K_Paths_Checks c;
3586
3587 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
3588 {
3589 for (Weighting k : {Weighting::Ones, Weighting::Falling})
3590 {
3591 const Spec s = weighted(shape, k);
3592 Built<GT> b(s);
3593 const std::vector<T> w = weights_as<T>(s);
3594 check_k_shortest<T>(c, b, s, 0, w, Dft_Show_Arc<GT>(), true, false);
3595 }
3596 });
3597
3598 c.expect();
3599}
3600
3601TYPED_TEST(ExhaustivePaths, EppsteinGivesTheWalksInCostOrder)
3602{
3603 using GT = TypeParam;
3604 using T = long long;
3605 K_Paths_Checks c;
3606
3607 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
3608 {
3609 for (Weighting k : {Weighting::Ones, Weighting::Falling})
3610 {
3611 const Spec s = weighted(shape, k);
3612 Built<GT> b(s);
3613 const std::vector<T> w = weights_as<T>(s);
3614 check_k_shortest<T>(c, b, s, 0, w, Dft_Show_Arc<GT>(), false, true,
3615 k == Weighting::Ones);
3616 }
3617 });
3618
3619 c.expect();
3620}
3621
3622TYPED_TEST(ExhaustivePaths, KShortestPathsTakeEveryWeighting)
3623{
3624 using GT = TypeParam;
3625 using T = long long;
3626 K_Paths_Checks c(", every weighting");
3627
3628 for_each_spec(domain(3, 2), GT().is_digraph(), [&] (const Spec & shape)
3629 {
3630 for_each_weighting(shape, {0, 1, 2}, [&] (const Spec & s)
3631 {
3632 Built<GT> b(s);
3633 const std::vector<T> w = weights_as<T>(s);
3634 check_k_shortest<T>(c, b, s, 0, w, Dft_Show_Arc<GT>(), true, true, true);
3635 });
3636 });
3637
3638 c.expect();
3639}
3640
3641namespace
3642{
3646 template <class GT>
3647 void check_k_shortest_hiding(bool yen)
3648 {
3649 using T = long long;
3650 using F = Hide_Mask<GT>;
3651 K_Paths_Checks c(", hiding arcs");
3652 for_each_spec(domain(3, 3), GT().is_digraph(), [&] (const Spec & shape)
3653 {
3654 const Spec base = weighted(shape, Weighting::Falling);
3655 for (unsigned long hidden = 1; hidden < (1UL << shape.num_arcs()); ++hidden)
3656 {
3657 const Spec s = poisoned(base, hidden, -1);
3658 Built<GT> b(s);
3659 const std::vector<T> w = weights_as<T>(s);
3660 check_k_shortest<T>(c, b, s, hidden, w, F{hidden}, yen, not yen, true);
3661 }
3662 });
3663 c.expect();
3664 }
3665}
3666
3667TYPED_TEST(ExhaustivePaths, YenRespectsTheFilter)
3668{
3669 check_k_shortest_hiding<TypeParam>(true);
3670}
3671
3672TYPED_TEST(ExhaustivePaths, EppsteinRespectsTheFilter)
3673{
3674 check_k_shortest_hiding<TypeParam>(false);
3675}
3676
3677TYPED_TEST(ExhaustivePaths, KShortestPathsOnRandomGraphs)
3678{
3679 using GT = TypeParam;
3680 using T = long long;
3681 // Without the cases of Eppstein that fail by now, which the domains above
3682 // cover
3683 K_Paths_Checks c(", random graphs");
3684
3685 for (const Spec & s : random_specs(3304, GT().is_digraph(), 6, 0, 9))
3686 {
3687 Built<GT> b(s);
3688 const std::vector<T> w = weights_as<T>(s);
3689 check_k_shortest<T>(c, b, s, 0, w, Dft_Show_Arc<GT>());
3690 }
3691
3692 c.expect();
3693}
3694
3695TYPED_TEST(ExhaustivePaths, KShortestPathsTakeIntAndDouble)
3696{
3697 using GT = TypeParam;
3698 K_Paths_Checks with_int(", int");
3699 K_Paths_Checks with_double(", double");
3700
3701 for_each_spec(domain(3, 2), GT().is_digraph(), [&] (const Spec & shape)
3702 {
3703 const Spec s = weighted(shape, Weighting::Falling);
3704 Built<GT> b(s);
3705 const std::vector<int> wi = weights_as<int>(s);
3706 check_k_shortest<int>(with_int, b, s, 0, wi, Dft_Show_Arc<GT>());
3707 const std::vector<double> wd = weights_as<double>(s);
3708 check_k_shortest<double>(with_double, b, s, 0, wd, Dft_Show_Arc<GT>());
3709 });
3710
3711 with_int.expect();
3712 with_double.expect();
3713}
A* shortest path algorithm.
Dial's algorithm for shortest paths with small integer weights.
Dijkstra's shortest path algorithm.
Floyd-Warshall algorithm for all-pairs shortest paths.
Johnson's algorithm for all-pairs shortest paths.
K-shortest path algorithms (Yen and Eppstein-style API).
0-1 BFS shortest path algorithm.
WeightedDigraph::Node Node
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t row
Definition ca-c-api.h:115
A* algorithm for finding the shortest path between two nodes.
Definition AStar.H:176
Dial's algorithm for shortest paths with integer weights.
Definition Dial.H:128
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:117
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Functor wrapper for Eppstein-style k-shortest paths API.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
Johnson's algorithm for all-pairs shortest paths.
Definition Johnson.H:216
Path< GT > find_path(Node *src, Node *tgt)
Find the shortest path from source to target.
Definition Johnson.H:913
bool is_initialized() const noexcept
Check if the algorithm has been initialized.
Definition Johnson.H:867
DynMapTree< std::pair< Node *, Node * >, Distance_Type > compute_all_pairs_distances()
Compute the shortest distance for all pairs.
Definition Johnson.H:958
Distance_Type get_potential(Node *node) const
Get the node potential (h value) for a node.
Definition Johnson.H:992
Distance_Type get_distance(Node *src, Node *tgt)
Get the shortest distance from source to target.
Definition Johnson.H:885
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Sparse matrix with generic row and column domains.
Definition al-matrix.H:70
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
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
Definition tpl_graph.H:3927
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition tpl_graph.H:4565
Functor wrapper for Yen's k-shortest loopless paths algorithm.
0-1 BFS algorithm for shortest paths in graphs with 0/1 weights.
void paint_min_paths_tree(const GT &g, Node *start)
Paints the shortest paths tree on the graph from start.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
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.
static mt19937 rng
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
__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(ExhaustivePaths, DijkstraPaintingsMatchTheOracle)
TYPED_TEST_SUITE(ExhaustivePaths, All_Kinds)
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
DynList< K_Shortest_Path_Item< GT, typename Distance::Distance_Type > > eppstein_k_shortest_paths(const GT &g, typename GT::Node *source, typename GT::Node *target, const size_t k, Distance distance=Distance(), SA sa=SA())
Compute k shortest general (loopy) paths.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
@ Spanning_Tree
Definition aleph-graph.H:79
static mpfr_t y
Definition mpfr_mul_d.c:3
void poison(const void *p, size_t bytes) noexcept
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
void expect(std::ifstream &input_stream, const std::string &expected)
Expect and consume a specific string/keyword.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
Distance::Distance_Type get_min_path(typename GT::Node *s, typename GT::Node *end, Path< GT > &path, Distance distance=Distance())
Extracts a shortest path from a graph painted by a shortest paths algorithm.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
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.
GT::Node * node_of(const GT &g, int i)
The node of a graph whose number is i.
::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.
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.
Spec random_spec(std::mt19937 &rng, int n, int m, bool directed, bool loops=true, bool parallel=true)
A random spec of n nodes and m arcs, with loops and parallel arcs if asked; parallel is ignored when ...
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 heavy()
Whether the mode is heavy, which asks for the larger domains.
void random_weights(std::mt19937 &rng, Spec &s, long long lo, long long hi)
Gives each arc of s a weight from lo to hi.
::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.
constexpr long long Inf
No distance, in the matrices of the oracles.
std::vector< std::vector< long long > > shortest_distances(const Spec &s, bool directed, bool &neg)
Floyd on a matrix of long long: the shortest distances, Inf where there is no path,...
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...
bool on_cycle(const Spec &s, int x)
Whether the node x lies on a cycle of the multigraph, not directed.
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...
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Definition AStar.H:79
Hides the arcs of a mask; copyable, assignable, noexcept.
The weight of an arc, read by its number from a vector.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
std::vector< typename GT::Arc * > arcs
Which multigraphs for_each_spec() gives.
A small multigraph: its nodes are 0 .
std::vector< long long > w
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)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r
Generic graph and digraph implementations.