70#include <gtest/gtest.h>
93 using Matrix = std::vector<std::vector<long long>>;
97 constexpr long long Int_Max = std::numeric_limits<int>::max();
102 template <
typename T>
103 T as(
long long x,
bool whole =
false)
105 if constexpr (std::is_floating_point_v<T>)
106 return whole ?
T(x) :
T(x) / 2;
112 template <
typename T>
113 std::vector<T> weights_as(
const Spec & s,
bool whole =
false)
116 for (
long long x : s.
w)
117 r.push_back(as<
T>(x, whole));
121 template <
typename T>
122 std::string text_of(
T x)
124 std::ostringstream o;
129 std::string route(
int u,
int v)
131 return "from " + std::to_string(u) +
" to " + std::to_string(v) +
": ";
137 std::string what_throws(
F && f)
143 catch (
const std::overflow_error & e)
145 return std::string(
"overflow_error ") + e.what();
147 catch (
const std::domain_error & e)
149 return std::string(
"domain_error ") + e.what();
151 catch (
const std::invalid_argument & e)
153 return std::string(
"invalid_argument ") + e.what();
155 catch (
const std::exception & e)
157 return std::string(
"exception ") + e.what();
163 bool is(
const std::string & what,
const char * kind)
165 return what.rfind(kind, 0) == 0;
170 Matrix distances(
const Spec & s,
bool directed,
unsigned long hidden,
bool & neg)
172 std::vector<int> orig;
176 Matrix distances(
const Spec & s,
bool directed,
unsigned long hidden = 0)
179 return distances(s, directed, hidden, neg);
185 std::vector<long long> potentials(
const Matrix & d)
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)
191 h[v] = std::min(
h[v], d[x][v]);
197 std::vector<long long> simple_path_costs(
const Spec & s,
int u,
int v,
bool directed)
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
205 costs.push_back(cost);
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;
216 std::sort(costs.begin(), costs.end());
225 std::vector<long long> walk_costs(
const Spec & s,
int u,
int v,
bool directed,
size_t k)
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;
232 while (not queue.empty()
and costs.size() <
k)
234 const auto [cost, x] = queue.top();
236 if (taken[
size_t(x)]++ >=
k)
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]});
251 bool meets_other_weight(
const Spec & s,
int u,
bool directed)
253 std::vector<bool> seen(
size_t(s.
n),
false);
254 seen[size_t(u)] =
true;
255 for (
bool grew =
true; grew; )
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;
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])])))
275 bool sum_beyond(
const Spec & s,
const Matrix & d,
int u,
long long bound,
bool directed,
278 for (
size_t i = 0; i < s.
arcs.size(); ++i)
279 for (
size_t e = 0; e < (directed ? 1U : 2U); ++e)
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)
291 bool has_parallel_arcs(
const Spec & s,
bool directed)
293 std::set<std::pair<int, int>> seen;
294 for (
const auto & a : s.
arcs)
304 Spec poisoned(
const Spec & s,
unsigned long hidden,
long long poison)
307 for (
size_t i = 0; i <
r.w.size(); ++i)
316 std::vector<int>
nodes;
317 std::vector<int>
arcs;
321 [[nodiscard]] std::string str()
const
324 for (
size_t i = 0; i <
nodes.size(); ++i)
326 r += std::to_string(nodes[i]);
328 r +=
" -" + std::to_string(arcs[i]) +
"- ";
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()));
350 std::string walk_error(
const Spec & s,
const Walk &
w,
int u,
int v,
bool directed,
351 unsigned long hidden)
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)
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);
370 return "the path " +
w.str() +
" takes the hidden arc " + std::to_string(a);
376 long long cost_of(
const Spec & s,
const Walk &
w)
384 bool is_simple(
const Walk &
w)
386 return std::set<int>(
w.nodes.begin(),
w.nodes.end()).size() ==
w.nodes.size();
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)
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())
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));
411 template <
class GT,
typename T>
412 bool no_path(
const std::string & thrown,
T got,
const Path<GT> & path)
414 return is(thrown,
"domain_error")
415 or (thrown.empty()
and got ==
std::numeric_limits<
T>::
max()
and path.is_empty());
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,
432 dist.assign(
size_t(s.n), Inf);
434 std::vector<bool> in(
size_t(s.n),
false);
435 for (
auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
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;
442 if (u < 0 or u >= s.n or not in[
size_t(u)])
443 return "the tree lacks its root";
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())
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));
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});
459 out[size_t(
y)].push_back({i, x});
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])])
468 if (walked[
size_t(i)])
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)];
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)]);
490 std::string mapping_error(
GT & tree,
const Built<GT> & b)
492 for (
auto it = tree.
get_node_it(); it.has_curr(); it.next_ne())
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";
499 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
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";
511 std::string coverage_error(
const std::vector<long long> &
row,
512 const std::vector<long long> & dist)
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);
523 bool painted_hidden_arc(
const Built<GT> & b,
unsigned long hidden)
525 for (
size_t i = 0; i < b.
arcs.size(); ++i)
536 enum class Weighting { Ones, Rising, Falling };
538 Spec weighted(
const Spec & shape, Weighting
k)
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));
547 constexpr Weighting All_Weightings[] = {Weighting::Ones, Weighting::Rising, Weighting::Falling};
551 Spec weighted(
const Spec & shape,
F && f)
554 for (
size_t i = 0; i < shape.
arcs.size(); ++i)
561 void for_each_weighting(
const Spec & shape,
const std::vector<long long> & values,
F && f)
564 std::vector<size_t> pick(shape.
arcs.size(), 0);
568 for (
size_t i : pick)
569 s.
w.push_back(values[i]);
570 f(
static_cast<const Spec &
>(s));
572 while (i < pick.size()
and ++pick[i] == values.size())
574 if (i == pick.size())
581 std::vector<Spec> random_specs(
unsigned first_seed,
bool directed,
size_t count,
582 long long lo,
long long hi)
584 std::mt19937
rng(
seed(first_seed));
588 const int nodes = 6 + int(
rng() % 3);
600 std::vector<Spec> random_potential_specs(
unsigned first_seed,
size_t count)
602 std::mt19937
rng(
seed(first_seed));
603 std::vector<Spec>
r = random_specs(first_seed,
true,
count, 0, 4);
606 std::vector<long long> p(
size_t(s.
n));
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])];
615 template <
class GT,
typename T>
618 template <
class GT,
typename T,
class SA = Dft_Show_Arc<GT>,
619 template <
class,
class,
class>
class Heap =
ArcHeap>
622 template <
class GT,
typename T>
625 template <
class GT,
typename T,
class SA = Dft_Show_Arc<GT>,
626 template <
class,
class,
class>
class Heap =
ArcHeap>
632 std::vector<int> sources(
const Spec & s,
int only)
637 for (
int u = 0; u < s.
n; ++u)
644 struct Painting_Checks
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)
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,
672 for (
int u : sources(s, only))
674 auto * src = b.
nodes[size_t(u)];
675 const std::string from =
"from " + std::to_string(u) +
": ";
677 if (
auto e = what_throws([&] { solver.paint_min_paths_tree(b.
g, src); }); not e.empty())
679 c.state.fail(s, from + e);
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");
689 for (
int v = 0; v < s.
n; ++v)
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);
697 auto e = what_throws([&] { got = solver.get_distance(tgt); });
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");
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)));
712 e = what_throws([&] { got = solver.get_min_path(tgt, path); });
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");
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);
723 c.path.fail(s, where + pe);
733 template <
typename T,
class Solver,
class GT>
735 const Spec & s,
const Matrix & d,
unsigned long hidden,
int only = -1)
737 for (
int u : sources(s, only))
739 const std::string from =
"from " + std::to_string(u) +
": ";
743 if (
auto e = what_throws([&]
745 solver.paint_min_paths_tree(b.
g, b.
nodes[
size_t(u)]);
746 total = solver.copy_painted_min_paths_tree(b.
g, tree);
749 copy.fail(s, from + e);
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);
757 e = coverage_error(
row, dist);
759 e = mapping_error(tree, b);
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()))]);
766 e =
"gives the weight " + text_of(total) +
" for a tree of " + text_of(weight);
768 if (e.empty()
and solver.is_painted())
769 e =
"the solver is still painted";
771 copy.fail(s, from + e);
773 if (
nodes ==
nullptr or std::count(
row.begin(),
row.end(), Inf) == 0)
777 nodes->fail(s, from +
"the tree has " + std::to_string(unreached)
778 +
" nodes that the painting does not reach");
787 template <
typename T,
class GT,
class Find>
789 unsigned long hidden, Find &&
find,
bool whole =
false,
int only = -1)
791 for (
int u : sources(s, only))
792 for (int v = 0; v < s.
n; ++v)
794 if (u == v
and self ==
nullptr)
796 Check & c = u == v ? *self : pairs;
798 const std::string where = route(u, v);
799 const long long want = d[size_t(u)][size_t(v)];
802 if (
auto e = what_throws([&]
805 c.
fail(s, where + e);
806 else if (want == Inf)
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");
812 else if (
auto pe = path_error(b.
g, s, path, u, v, hidden, want, got, whole);
814 c.
fail(s, where + pe);
822 template <
typename T,
class Solver,
class GT,
class Pa
int>
824 const Spec & s,
const Matrix & d,
unsigned long hidden,
827 for (
int u = 0; u < s.
n; ++u)
828 for (
int v = 0; v < s.
n; ++v)
830 Check & c = u == v ? self : pairs;
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)];
836 if (
auto e = what_throws([&] { found = paint(b.
nodes[
size_t(u)], tgt); });
839 c.
fail(s, where + e);
842 if (found != (want != Inf))
844 c.
fail(s, where + (found ?
"reaches a node not reached" :
"does not reach the end"));
851 if (
auto e = what_throws([&]
853 dist = solver.get_distance(tgt);
854 cost = solver.get_min_path(tgt, path);
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);
874 explicit Tree_Checks(
const std::string & 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")
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)
897 const long long want = d[size_t(u)][size_t(v)];
903 const auto e = what_throws([&]
904 { got = solver.get_min_path(tree, b.
nodes[
size_t(v)], path); });
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());
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);
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)
925 for (
int u = 0; u < s.
n; ++u)
927 const std::string from =
"from " + std::to_string(u) +
": ";
928 const auto &
row = d[size_t(u)];
932 if (
auto e = what_throws([&] {
root = compute(b.
nodes[
size_t(u)], tree); });
935 c.tree.fail(s, from + e);
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";
944 e = coverage_error(
row, dist);
946 e =
"the root is not the node of the start";
948 e = mapping_error(tree, b);
949 if (e.empty()
and solver.is_painted())
950 e =
"the solver says that it painted";
953 c.tree.fail(s, from + e);
957 for (
int v = 0; v < s.
n; ++v)
958 check_tree_path<T>(c, solver, tree, b, s, d, u, v);
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)
971 for (
int u = 0; u < s.
n; ++u)
972 for (
int v = 0; v < s.
n; ++v)
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)];
980 if (
auto e = what_throws([&]
981 { end = partial(b.
nodes[
size_t(u)], b.
nodes[
size_t(v)], tree); });
984 c.tree.fail(s, where + e);
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";
997 e = mapping_error(tree, b);
1000 c.tree.fail(s, where + e);
1004 check_tree_path<T>(c, solver, tree, b, s, d, u, v);
1010 template <
typename T>
1011 T floyd_value(
long long want)
1014 or (std::is_integral_v<T>
and want >= (
long long) std::numeric_limits<T>::max()))
1015 return std::numeric_limits<T>::max();
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)
1038 for (
const Check * c : {&dist, &path, &index_path, &
next, &
nodes})
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)
1050 const auto & mat = f.get_dist_mat();
1051 const auto &
next = f.get_path_mat();
1056 const auto nodes = f.get_nodes();
1057 const auto & ref = f.get_nodes_ref();
1058 const auto copy = f.get_nodes_copy();
1060 if (
nodes.size() !=
size_t(s.
n) or ref.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)
1066 e =
"the node at " + std::to_string(i) +
" is wrong, or out of order";
1068 for (
long i = 0; e.empty()
and i < s.
n; ++i)
1069 if (not seen.insert(
num(
nodes(i))).second)
1076 if (f.has_negative_cycle())
1078 c.dist.fail(s,
"finds a negative cycle");
1081 for (
int u = 0; u < s.
n; ++u)
1082 for (
int v = 0; v < s.
n; ++v)
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));
1093 for (
Check * pc : {&c.path, &c.index_path})
1095 if (pc == &c.index_path
and not full)
1099 if (
auto e = what_throws([&]
1100 { path = pc == &c.path ? f.get_min_path(nu, nv) : f.
get_min_path(i, j); });
1102 pc->
fail(s, route(u, v) + e);
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");
1109 else if (
auto pe = path_error(b.
g, s, path, u, v, hidden, want, mat(i, j));
1111 pc->
fail(s, route(u, v) + pe);
1119 const long k =
next(i, j);
1124 ok =
k == -1 or (std::is_integral_v<T>
and want != Inf);
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)
1131 const int p = s.
arcs[a][0], q = s.
arcs[a][1];
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;
1139 c.next.fail(s, route(u, v) +
"the next node is at " + std::to_string(
k));
1145 template <
class GT,
typename T>
1146 struct Johnson_Weight
1148 using Distance_Type =
T;
1150 const std::vector<T> * weights =
nullptr;
1152 T operator () (
typename GT::Arc * a)
const
1154 return (*weights)[size_t(a->
get_info())];
1159 template <
typename T>
1162 if constexpr (std::numeric_limits<T>::has_infinity)
1163 return std::numeric_limits<T>::infinity();
1165 return std::numeric_limits<T>::max();
1169 struct Johnson_Checks
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)
1194 for (
const Check * c : {&
build, &distance, &self, &path, &all_pairs, &potential,
1195 &
copy, &reweighted})
1203 template <
typename T,
class GT,
class SA>
1205 unsigned long hidden,
const std::vector<T> &
w, SA sa)
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); });
1217 if (not j->is_initialized())
1218 c.build.fail(s,
"is not initialized");
1220 const auto h = potentials(d);
1221 auto value = [&] (
long long want) {
return want ==
Inf ? johnson_none<T>() : as<
T>(want); };
1223 for (
int u = 0; u < s.
n; ++u)
1224 for (
int v = 0; v < s.
n; ++v)
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;
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)));
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)
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");
1247 else if (
auto pe = path_error(b.
g, s, path, u, v, hidden, want, as<T>(want));
1249 c.path.fail(s, route(u, v) + pe);
1253 if (
auto e = what_throws([&]
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)
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);
1267 if (not wrong.empty())
1268 c.all_pairs.fail(s, wrong);
1270 c.all_pairs.fail(s, e);
1274 for (
int v = 0; v < s.
n; ++v)
1275 for (
int u = s.
n - 1; u >= 0; --u)
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)]); });
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)));
1289 for (
int v = 0; v < s.
n; ++v)
1293 if (
auto e = what_throws([&] { got = j->get_potential(b.
nodes[
size_t(v)]); });
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)])));
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");
1315 std::multiset<std::tuple<int, int, Info>> want, got;
1316 for (
size_t i = 0; i < s.
arcs.size(); ++i)
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)]))});
1322 const GT &
copy = j->get_reweighted_graph();
1323 for (
auto it =
copy.get_arc_it(); it.has_curr(); it.next_ne())
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()});
1332 if (e.empty()
and got != want)
1333 e =
"the infos of the reweighted arcs are not their new weights";
1335 c.reweighted.fail(s, e);
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)
1347 std::vector<Walk> seen;
1349 for (
auto it = items.get_it(); it.has_curr(); it.next_ne(), ++i)
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())
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));
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";
1372 if (i != want.size())
1373 return "gives " + std::to_string(i) +
" paths, not " + std::to_string(want.size());
1381 struct K_Paths_Checks
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)
1403 for (
const Check * c : {¥, ¥_class, &eppstein, &through, ¶llel, &
none})
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)
1419 using W = Weights<GT, T>;
1420 std::vector<int> orig;
1424 const bool parallel = directed
and has_parallel_arcs(shown,
true);
1426 for (
int u = 0; u < s.
n; ++u)
1427 for (
int v = 0; v < s.
n; ++v)
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})
1437 if ((kc == &c.yen_class or
light)
and k != 2)
1440 const std::vector<long long>
1441 want(simple.begin(), simple.begin() +
long(std::min(
k, simple.size())));
1443 if (
auto e = what_throws([&]
1446 items = yen_k_shortest_paths<GT, W, SA>(b.
g, nu, nv,
k, W{&w}, sa);
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);
1453 kc->fail(s, route(u, v) +
"k = " + std::to_string(
k) +
": " + ie);
1458 const bool on_cycle = u != v
and r[size_t(u)][size_t(v)]
and r[size_t(v)][size_t(v)];
1460 for (
size_t k : {size_t(1), size_t(3), size_t(6)})
1461 for (
bool by_class : {
false,
true})
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([&]
1475 items = eppstein_k_shortest_paths<GT, W, SA>(b.
g, nu, nv,
k, W{&w}, sa);
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);
1482 kc->
fail(s, route(u, v) +
"k = " + std::to_string(
k) +
": " + ie);
1486 if (s.
n == 0 or not yen or not eppstein)
1489 if (
auto e = what_throws([&]
1491 if (not yen_k_shortest_paths<GT, W, SA>(b.
g, b.
nodes[0], b.
nodes[0], 0, W{&w}, sa)
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");
1502 template <
class GT,
typename T>
1503 struct Static_Weight
1505 using Distance_Type =
T;
1507 inline static const std::vector<T> * weights =
nullptr;
1509 T operator () (
typename GT::Arc * a)
const {
return (*weights)[size_t(a->
get_info())]; }
1513 Spec alternating(
const Spec & shape,
long long first)
1515 return weighted(shape, [first] (
size_t i) {
return (first + (
long long) i) % 2; });
1521 Spec with_negatives(
const Spec & shape,
bool directed)
1524 return weighted(shape, Weighting::Falling);
1525 return weighted(shape, [] (
size_t i) {
return (
long long) (i % 3) - 1; });
1533 template <
typename T,
class GT>
1534 void check_dijkstra_and_astar_with(
const std::string & type)
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");
1545 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1546 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1550 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
1552 const Spec s = weighted(shape,
k);
1554 w = weights_as<T>(s);
1556 check_painting<T>(paint, dij, b, s, d, 0);
1557 check_copies<T>(
copy,
nullptr, dij, b, s, d, 0);
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); });
1572 astar_self.expect();
1583 using GT = TypeParam;
1584 using T =
long long;
1585 Painting_Checks paint(
"Dijkstra_Min_Paths");
1589 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1593 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
1595 const Spec s = weighted(shape,
k);
1597 w = weights_as<T>(s);
1598 check_painting<T>(paint, dij, b, s, distances(s, b.
g.
is_digraph()), 0);
1607 using GT = TypeParam;
1608 using T =
long long;
1609 Painting_Checks paint(
"AStar_Min_Path");
1612 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1616 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
1618 const Spec s = weighted(shape,
k);
1620 w = weights_as<T>(s);
1621 check_painting<T>(paint, ast, b, s, distances(s, b.
g.
is_digraph()), 0);
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()");
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");
1640 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1641 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1645 for (Weighting
k : All_Weightings)
1647 const Spec s = weighted(shape,
k);
1649 w = weights_as<T>(s);
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);
1665 using GT = TypeParam;
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()");
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");
1678 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1682 for (Weighting
k : All_Weightings)
1684 const Spec s = weighted(shape,
k);
1686 w = weights_as<T>(s);
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,
1693 {
return dij.paint_partial_min_paths_tree(b.
g, x,
y); });
1700 const Spec s = weighted(shape, Weighting::Rising);
1702 w = weights_as<T>(s);
1703 check_find<T>(op, &op_self, b, s, distances(s, b.
g.
is_digraph()), 0,
1705 { return dij(b.g, x, y, p); });
1708 for (
const Check * c : {&
find, &op, &partial, &find_self, &op_self, &partial_self})
1714 using GT = TypeParam;
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");
1727 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1731 for (Weighting
k : All_Weightings)
1733 const Spec s = weighted(shape,
k);
1735 w = weights_as<T>(s);
1737 if (
k != Weighting::Rising)
1738 check_find<T>(find_path, &find_path_self, b, s, d, 0,
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,
1744 {
return ast.paint_partial_path(b.
g, x,
y); });
1752 for (Weighting
k : {Weighting::Ones, Weighting::Rising})
1754 const Spec s = weighted(shape,
k);
1756 w = weights_as<T>(s);
1758 if (
k == Weighting::Rising)
1760 {
return ast(b.
g, x,
y, p); });
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); });
1767 for (
const Check * c : {&find_path, &find_path_self, &op, &op_self, &paint_path,
1768 &paint_path_self, &alias, &alias_self})
1774 using GT = TypeParam;
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");
1783 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1787 for (Weighting
k : All_Weightings)
1789 const Spec s = weighted(shape,
k);
1791 w = weights_as<T>(s);
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,
1798 {
return ast.paint_partial_min_paths_tree(b.
g, x,
y); });
1802 for (
const Check * c : {&
find, &find_self, &partial, &partial_self})
1808 using GT = TypeParam;
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()");
1816 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1821 const Spec s = weighted(shape, Weighting::Falling);
1823 w = weights_as<T>(s);
1824 check_trees<T>(tree, dij, b, s, distances(s, b.
g.
is_digraph()), 0,
1826 { return dij.compute_min_paths_tree(b.g, x, t); });
1831 for (Weighting
k : All_Weightings)
1833 const Spec s = weighted(shape,
k);
1835 w = weights_as<T>(s);
1837 if (
k == Weighting::Rising)
1839 if (
k == Weighting::Falling)
1840 check_trees<T>(op, dij, b, s, d, 0, [&] (
Node * x,
GT & t)
1845 check_partial_trees<T>(partial, dij, b, s, d, 0, [&] (
Node * x,
Node *
y,
GT & t)
1847 dij.compute_partial_min_paths_tree(b.
g, x,
y, t);
1860 using GT = TypeParam;
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()");
1870 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1874 const Spec s = weighted(shape, Weighting::Falling);
1876 w = weights_as<T>(s);
1877 check_trees<T>(tree, ast, b, s, distances(s, b.
g.
is_digraph()), 0,
1879 { return ast.compute_min_paths_tree(b.g, x, t); });
1884 for (Weighting
k : All_Weightings)
1886 const Spec s = weighted(shape,
k);
1888 w = weights_as<T>(s);
1890 if (
k == Weighting::Rising)
1892 check_partial_trees<T>(partial, ast, b, s, d, 0, [&] (
Node * x,
Node *
y,
GT & t)
1894 ast.compute_partial_min_paths_tree(b.
g, x,
y, t);
1897 if (
k != Weighting::Falling)
1899 check_trees<T>(op, ast, b, s, d, 0, [&] (
Node * x,
GT & t)
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); });
1920 using GT = TypeParam;
1921 using T =
long long;
1922 Painting_Checks paint(
"Dijkstra_Min_Paths",
", every weighting");
1925 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1929 for_each_weighting(shape, {0, 1, 2}, [&] (
const Spec & s)
1932 w = weights_as<T>(s);
1933 check_painting<T>(paint, dij, b, s, distances(s, b.
g.
is_digraph()), 0);
1942 using GT = TypeParam;
1944 using T =
long long;
1945 Check find(
"Dijkstra_Min_Paths::find_min_path(), every weighting");
1948 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
1952 for_each_weighting(shape, {0, 1, 2}, [&] (
const Spec & s)
1955 w = weights_as<T>(s);
1958 { return dij.find_min_path(b.g, x, y, p); });
1967 using GT = TypeParam;
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");
1974 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
1978 for_each_weighting(shape, {0, 1, 2}, [&] (
const Spec & s)
1981 w = weights_as<T>(s);
1982 check_find<T>(
find, &find_self, b, s, distances(s, b.
g.
is_digraph()), 0,
1984 { return ast.find_path(b.g, x, y, p); });
1994 using GT = TypeParam;
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");
2005 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
2006 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
2008 for (
const Spec & s : random_specs(3301,
GT().is_digraph(), 60, 0, 9))
2011 w = weights_as<T>(s);
2013 check_painting<T>(paint, dij, b, s, d, 0);
2014 check_copies<T>(
copy,
nullptr, dij, b, s, d, 0);
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); });
2033 using GT = TypeParam;
2035 using T =
long long;
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");
2045 const Spec base = weighted(shape, Weighting::Falling);
2046 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
2048 const Spec s = poisoned(base, hidden, -1);
2050 const std::vector<T>
w = weights_as<T>(s);
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);
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); });
2070 using GT = TypeParam;
2072 using T =
long long;
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");
2082 const Spec base = weighted(shape, Weighting::Falling);
2083 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
2085 const Spec s = poisoned(base, hidden, -1);
2087 const std::vector<T>
w = weights_as<T>(s);
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,
2093 {
return ast.find_path(b.
g, x,
y, 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); });
2110 using GT = TypeParam;
2112 using T =
long long;
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 "
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");
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 "
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)
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});
2145 for (
int u = 0; u < s.
n; ++u)
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,
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,
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,
2161 {
return dial.find_min_path(b.g, x,
y, p); },
false, u);
2171 paint_left.expect();
2179 check_dijkstra_and_astar_with<int, TypeParam>(
"int");
2180 check_dijkstra_and_astar_with<double, TypeParam>(
"double");
2189 template <
class GT,
typename T>
2190 struct Exact_Heuristic
2192 using Distance_Type =
T;
2194 const Matrix * d =
nullptr;
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);
2210 void check_near_int_limits(
bool astar)
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()");
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()");
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()");
2235 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
2236 AStar_Of<GT, T> ast(Weights<GT, T>{&
w});
2239 exact_ast(Weights<GT, T>{&
w}, Exact_Heuristic<GT, T>{&exact_d});
2244 for_each_weighting(shape, {1, M / 2, M - 1}, [&] (
const Spec & s)
2247 w = weights_as<T>(s);
2248 const Matrix d = distances(s, directed);
2250 for (
int u = 0; u < s.
n; ++u)
2252 const auto &
row = d[size_t(u)];
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;
2260 const auto e = what_throws([&]
2261 { dij.paint_min_paths_tree(b.
g, b.
nodes[
size_t(u)]); });
2264 if (not is(e,
"overflow_error"))
2265 pc.
fail(s,
"from " + std::to_string(u) +
": gives no overflow_error");
2267 else if (not e.empty())
2268 pc.
fail(s,
"from " + std::to_string(u) +
": " + e);
2270 for (
int v = 0; v < s.
n; ++v)
2272 if (
row[
size_t(v)] ==
Inf)
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)]))
2279 pc.
fail(s, route(u, v) + (ge.empty() ?
"gives " + text_of(got) : ge));
2285 for (
int v = 0; v < s.
n; ++v)
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;
2296 const auto fe = what_throws([&]
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);
2305 if (not is(fe,
"overflow_error"))
2306 c.
fail(s, route(u, v) +
"gives no overflow_error");
2308 else if (not fe.empty())
2309 c.
fail(s, route(u, v) + fe);
2310 else if (want == Inf)
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");
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);
2320 Check & ec = beyond ? exact_beyond : exact;
2322 auto * x = b.
nodes[size_t(u)];
2323 auto *
y = b.
nodes[size_t(v)];
2326 const auto ee = what_throws([&] { cost = exact_ast.find_path(b.
g, x,
y, found); });
2328 typename GT::Node * reached =
nullptr;
2329 const auto te = what_throws([&]
2330 { reached = exact_ast.compute_partial_path(b.
g, x,
y, tree); });
2333 if (not is(ee,
"overflow_error") or not is(te,
"overflow_error"))
2334 ec.
fail(s, route(u, v) +
"gives no overflow_error");
2337 if (not ee.empty() or not te.empty())
2339 ec.
fail(s, route(u, v) + ee + te);
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");
2349 if (
auto pe = path_error(b.
g, s, found, u, v, 0, want, cost); not pe.empty())
2351 ec.
fail(s, route(u, v) +
"find_path(): " + pe);
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);
2362 ec.
fail(s, route(u, v) +
"compute_partial_path(): " + tpe);
2368 for (
const Check * c : {&paint, &paint_beyond, &
find, &find_beyond, &paint_sum, &find_sum,
2369 &exact, &exact_beyond})
2387 void check_k_near_int_limits()
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 "
2409 for_each_weighting(shape, {1, M / 2, M - 1}, [&] (
const Spec & 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)
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})
2424 std::vector<long long> want;
2426 want.assign(simple.begin(),
2427 simple.begin() + long(std::min(
k, simple.size())));
2429 want = walk_costs(s, u, v, directed,
k);
2430 const bool beyond = std::any_of(want.begin(), want.end(),
2432 { return c > Int_Max; });
2433 Check & c = by_yen ? (beyond ? yen_beyond : yen)
2434 : (beyond ? epp_beyond : epp);
2436 const std::string at = route(u, v) +
"k = " + std::to_string(
k) +
": ";
2438 const auto e = what_throws([&]
2440 items = by_yen ? yen_k_shortest_paths<GT, W>(b.
g, nu, nv,
k, W{&w})
2445 if (not is(e,
"overflow_error"))
2446 c.
fail(s, at +
"gives no overflow_error " + e);
2448 else if (not e.empty())
2450 else if (
auto ie = items_error<T>(b.
g, s, items, u, v, 0, want, by_yen);
2458 for (
const Check * c : {¥, ¥_beyond, &epp, &epp_beyond})
2465 check_k_near_int_limits<TypeParam>();
2470 check_near_int_limits<TypeParam>(
false);
2475 check_near_int_limits<TypeParam>(
true);
2480 using GT = TypeParam;
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");
2494 for (Weighting
k : All_Weightings)
2496 const Spec s = weighted(shape,
k);
2498 w = weights_as<T>(s);
2500 check_painting<T>(paint, dij, b, s, d, 0);
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); });
2519 template <
typename T,
class GT>
2520 void check_zero_one_with(
const std::string & type)
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");
2531 for_each_weighting(shape, {0, 1}, [&] (
const Spec & s)
2534 w = weights_as<T>(s,
true);
2537 check_painting<T>(paint, bfs, b, s, d, 0,
true);
2539 {
return bfs.find_min_path(b.
g, x,
y, p); },
true);
2551 using GT = TypeParam;
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");
2563 for (
long long first : {0, 1})
2565 const Spec s = alternating(shape, first);
2567 w = weights_as<T>(s);
2573 check_painting<T>(paint, bfs, b, s, d, 0);
2575 {
return bfs.find_min_path(b.
g, x,
y, p); });
2579 {
return bfs(b.
g, x,
y, p); });
2584 for (
const Check * c : {&
find, &find_self, &op, &op_self})
2590 check_zero_one_with<int, TypeParam>(
"int");
2591 check_zero_one_with<double, TypeParam>(
"double");
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");
2605 for_each_weighting(shape, {0, 1, 2}, [&] (
const Spec & s)
2608 w = weights_as<T>(s);
2609 const Matrix d = distances(s, directed);
2611 for (
int u = 0; u < s.
n; ++u)
2613 const bool meets = meets_other_weight(s, u, directed);
2614 Check & c = meets ? rejects : takes;
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); });
2621 if (not is(e,
"domain_error") or bfs.is_painted())
2622 c.
fail(s, from +
"gives no domain_error, or says painted");
2627 c.
fail(s, from + e);
2630 for (
int v = 0; v < s.
n; ++v)
2632 if (d[
size_t(u)][size_t(v)] == Inf)
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)])
2638 c.
fail(s, route(u, v) + (ge.empty() ?
"gives " + text_of(got) : ge));
2652 using GT = TypeParam;
2654 using T =
long long;
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");
2663 const Spec base = alternating(shape, 0);
2664 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
2666 const Spec s = poisoned(base, hidden, 2);
2668 const std::vector<T>
w = weights_as<T>(s);
2671 check_painting<T>(paint, bfs, b, s, d, hidden);
2673 {
return bfs.find_min_path(b.
g, x,
y, p); });
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");
2689 Check dead(
"~Zero_One_BFS, after its graph was destroyed");
2694 const Spec s = alternating(shape, 1);
2696 w = weights_as<T>(s);
2698 Dijkstra_Of<GT, T> dij(Weights<GT, T>{&
w});
2699 for (
int u = 0; u < s.
n; ++u)
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;
2705 if (
auto e = what_throws([&]
2709 dij.paint_min_paths_tree(b.
g, b.
nodes[
size_t(u)]);
2712 c.
fail(s,
"from " + std::to_string(u) +
": " + e);
2715 for (
int v = 0; v < s.
n; ++v)
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)])
2722 c.
fail(s, route(u, v) +
"the painting of Dijkstra_Min_Paths is lost: "
2723 + (e.empty() ? text_of(got) : e));
2731 if (
auto e = what_throws([&]
2736 bfs.paint_min_paths_tree(gone.g, gone.nodes[0]);
2748 using GT = TypeParam;
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");
2758 for (
const Spec & s : random_specs(3305,
GT().is_digraph(), 40, 0, 1))
2761 w = weights_as<T>(s);
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); });
2769 for (
const Spec & s : random_specs(3306,
GT().is_digraph(), 40, 0, 5))
2772 w = weights_as<T>(s);
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); });
2781 dial_paint.expect();
2790 template <
typename T,
class GT>
2791 void check_dial_with(
const std::string & type,
bool painting)
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");
2804 for_each_weighting(shape, {0, 1, 3}, [&] (
const Spec & s)
2807 w = weights_as<T>(s);
2810 check_painting<T>(paint, dial, b, s, d, 0);
2813 {
return dial.find_min_path(b.
g, x,
y, p); });
2825 using GT = TypeParam;
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");
2839 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
2841 const Spec s = weighted(shape,
k);
2843 w = weights_as<T>(s);
2845 if (
k == Weighting::Falling)
2847 check_painting<T>(paint, dial, b, s, d, 0);
2849 {
return dial.find_min_path(b.
g, x,
y, p); });
2853 {
return dial(b.
g, x,
y, p); });
2858 for (
const Check * c : {&
find, &find_self, &op, &op_self})
2864 check_dial_with<int, TypeParam>(
"int",
true);
2869 check_dial_with<long long, TypeParam>(
"long long",
false);
2874 using GT = TypeParam;
2876 using T =
long long;
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);
2885 const Spec base = weighted(shape, Weighting::Falling);
2886 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
2890 const std::vector<T>
w = weights_as<T>(base);
2893 check_painting<T>(paint, dial, b, base, d, hidden);
2895 {
return dial.find_min_path(b.
g, x,
y, p); });
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);
2916 template <
typename T,
class GT,
class SA = Dft_Show_Arc<GT>>
2918 unsigned long hidden,
const std::vector<T> &
w, SA sa = SA(),
2921 std::string e = what_throws([&]
2924 check_floyd<T>(c, f, b, s, d, hidden, full);
2929 c.dist.fail(s,
"the solver " + e);
2935 template <
typename T,
class GT>
2936 void check_floyd_with(
const std::string & type)
2938 Floyd_Checks c(
", " + type);
2945 for (
const Spec & s : {weighted(shape, Weighting::Falling),
2946 with_negatives(shape, directed)})
2949 const Matrix d = distances(s, directed, 0, neg);
2953 w = weights_as<T>(s);
2954 check_floyd_of<T>(c, b, s, d, 0,
w);
2963 using GT = TypeParam;
2964 using T =
long long;
2966 Check empty(
"Floyd_All_Shortest_Paths, a graph without nodes");
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");
2984 const Spec s = with_negatives(shape, directed);
2986 const Matrix d = distances(s, directed, 0, neg);
2990 w = weights_as<T>(s);
2991 check_floyd_of<T>(c, b, s, d, 0,
w);
3000 using GT = TypeParam;
3001 using T =
long long;
3002 Floyd_Checks c(
", every weighting");
3006 const std::vector<long long> values = directed ? std::vector<long long>{-1, 0, 2}
3007 : std::vector<long long>{0, 1, 2};
3012 for_each_weighting(shape, values, [&] (
const Spec & s)
3015 const Matrix d = distances(s, directed, 0, neg);
3019 w = weights_as<T>(s);
3029 using GT = TypeParam;
3030 using T =
long long;
3031 Floyd_Checks c(
", from -2 to 2");
3040 for_each_weighting(shape, {-2, -1, 0, 1, 2}, [&] (
const Spec & s)
3043 const Matrix d = distances(s, directed, 0, neg);
3047 w = weights_as<T>(s);
3048 check_floyd_of<T>(c, b, s, d, 0,
w);
3057 using GT = TypeParam;
3058 using T =
long long;
3059 Floyd_Checks c(
", random graphs");
3063 for (
const Spec & s : directed ? random_potential_specs(3302, 60)
3064 : random_specs(3302,
false, 60, 0, 9))
3067 w = weights_as<T>(s);
3068 check_floyd_of<T>(c, b, s, distances(s, directed), 0,
w);
3076 using GT = TypeParam;
3077 using T =
long long;
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");
3089 const Spec base = with_negatives(shape, directed);
3090 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
3093 const Matrix d = distances(base, directed, hidden, neg);
3096 const Spec s = poisoned(base, hidden, -5);
3098 const std::vector<T>
w = weights_as<T>(s);
3099 check_floyd_of<T>(c, b, s, d, hidden,
w,
F{hidden},
false);
3102 Static_Weight<GT, T>::weights = &
w;
3103 for (
Check *
k : {&lvalue, &rvalue})
3108 const auto e = what_throws([&]
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";
3119 if (not e.empty() or not wrong.empty())
3120 k->fail(s, e + wrong);
3132 using GT = TypeParam;
3134 constexpr long long M = Int_Max;
3137 Floyd_Checks c(
", near the limits of int");
3144 w = weights_as<T>(s);
3145 check_floyd_of<T>(c, b, s, distances(s, directed), 0,
w,
Dft_Show_Arc<GT>(),
false);
3153 for_each_weighting(shape, {M / 2, M - 1},
check);
3158 for_each_weighting(shape, {1, M / 2, M - 1},
check);
3166 check_floyd_with<int, TypeParam>(
"int");
3167 check_floyd_with<double, TypeParam>(
"double");
3176 template <
typename T,
class GT,
class Pick>
3177 void check_johnson_with(Pick && pick)
3182 for (
const Spec & s : {weighted(shape, Weighting::Falling), with_negatives(shape,
true)})
3185 const Matrix d = distances(s,
true, 0, neg);
3189 w = weights_as<T>(s);
3197 std::vector<long long> reweighted(
const Spec & s,
const Matrix & d,
unsigned long hidden)
3199 const auto h = potentials(d);
3200 std::vector<long long>
r;
3201 for (
size_t i = 0; i < s.
arcs.size(); ++i)
3203 r.push_back(s.
w[i] +
h[
size_t(s.
arcs[i][0])] -
h[
size_t(s.
arcs[i][1])]);
3209 bool reweighting_hides(
const Spec & s,
const Matrix & d,
unsigned long hidden)
3211 for (
long long x : reweighted(s, d, hidden))
3218 bool hides_a_parallel_arc(
const Spec & s,
unsigned long hidden)
3220 for (
size_t i = 0; i < s.
arcs.size(); ++i)
3221 for (
size_t j = 0; j < s.
arcs.size(); ++j)
3230 bool reweighting_halves(
const Spec & s,
const Matrix & d)
3232 for (
long long x : reweighted(s, d, 0))
3246 using GT = TypeParam;
3247 using T =
long long;
3249 Check none(
"Johnson(), without a graph");
3256 for (
const Spec & s : {weighted(shape, Weighting::Falling), with_negatives(shape,
true)})
3259 const Matrix d = distances(s,
true, 0, neg);
3263 w = weights_as<T>(s);
3273 auto * p = b.
nodes[0];
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")
3278 or not is(what_throws([&] { (void) j.
get_potential(p); }),
"domain_error"))
3279 none.fail(shape,
"answers without a graph");
3288 using GT = TypeParam;
3289 using T =
long long;
3290 Johnson_Checks c(
", every weighting");
3295 for_each_weighting(shape, {-1, 0, 2}, [&] (
const Spec & s)
3298 const Matrix d = distances(s,
true, 0, neg);
3302 w = weights_as<T>(s);
3312 using GT = TypeParam;
3313 using T =
long long;
3314 Johnson_Checks c(
", random graphs");
3317 for (
const Spec & s : random_potential_specs(3303, 40))
3320 w = weights_as<T>(s);
3329 using GT = TypeParam;
3330 using T =
long long;
3332 Johnson_Checks c(
", hiding arcs");
3340 Johnson_Checks hides(
", hiding arcs, a new weight that the filter hides");
3341 Johnson_Checks paired(
", hiding one of two parallel arcs");
3346 const Spec base = with_negatives(shape,
true);
3347 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
3350 const Matrix d = distances(base,
true, hidden, neg);
3353 const Spec s = poisoned(base, hidden, -5);
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});
3369 using GT = TypeParam;
3371 constexpr long long M = Int_Max;
3372 Check fits(
"Johnson, near the limits of 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()");
3384 for_each_weighting(shape, {-2, 1, M - 1}, [&] (
const Spec & s)
3387 const Matrix d = distances(s,
true, 0, neg);
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);
3396 for (
size_t i = 0; i < s.
arcs.size()
and not over; ++i)
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];
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;
3405 Check & c = not all_fit ? beyond : over ? too_big : fits;
3409 w = weights_as<T>(s);
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}); });
3416 c.
fail(s,
"the constructor " + e);
3419 for (
int u = 0; u < s.
n; ++u)
3420 for (
int v = 0; v < s.
n; ++v)
3422 const long long want = d[size_t(u)][size_t(v)];
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));
3430 c.
fail(s, route(u, v) + (e.empty() ?
"gives " + text_of(got) : e));
3444 using GT = TypeParam;
3446 constexpr long long M = Int_Max;
3447 constexpr long long L = std::numeric_limits<T>::lowest();
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;
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; };
3463 for_each_weighting(shape, {L, -1, 1, M}, [&] (
const Spec & s)
3466 const Matrix d = distances(s,
true, 0, neg);
3468 w = weights_as<T>(s);
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}); });
3477 if (not is(e,
"domain_error"))
3478 cycle.
fail(s,
"the constructor throws " + said(e));
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)
3488 low_d = low_d or x < L;
3489 all_fit = all_fit
and fits(x);
3492 ++(low_d ? runs_below : runs_low);
3496 c.
fail(s,
"the constructor " + e);
3500 for (
int u = 0; u < s.
n; ++u)
3501 for (
int v = 0; v < s.
n; ++v)
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);
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)))
3513 c.
fail(s, route(u, v) + (de.empty() ?
"gives " + text_of(got) : de));
3518 const auto pe = what_throws([&] { path = j->find_path(nu, nv); });
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";
3527 why = path_error(b.
g, s, path, u, v, 0, want,
T(want));
3528 if (not why.empty())
3530 c.
fail(s, route(u, v) + why);
3535 for (
int v = 0; v < s.
n; ++v)
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))
3542 c.
fail(s,
"the potential of " + std::to_string(v) +
": "
3543 + (pe.empty() ?
"gives " + text_of(got) : pe));
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));
3555 EXPECT_GT(runs_low, 0u);
3556 EXPECT_GT(runs_below, 0u);
3557 EXPECT_GT(runs_cycle, 0u);
3565 Johnson_Checks with_int(
", int");
3566 check_johnson_with<int, TypeParam>([&] (
const Spec &,
const Matrix &) -> Johnson_Checks &
3567 {
return with_int; });
3570 Johnson_Checks with_double(
", double");
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();
3583 using GT = TypeParam;
3584 using T =
long long;
3589 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
3591 const Spec s = weighted(shape,
k);
3593 const std::vector<T>
w = weights_as<T>(s);
3603 using GT = TypeParam;
3604 using T =
long long;
3609 for (Weighting
k : {Weighting::Ones, Weighting::Falling})
3611 const Spec s = weighted(shape,
k);
3613 const std::vector<T>
w = weights_as<T>(s);
3615 k == Weighting::Ones);
3624 using GT = TypeParam;
3625 using T =
long long;
3626 K_Paths_Checks c(
", every weighting");
3630 for_each_weighting(shape, {0, 1, 2}, [&] (
const Spec & s)
3633 const std::vector<T>
w = weights_as<T>(s);
3647 void check_k_shortest_hiding(
bool yen)
3649 using T =
long long;
3651 K_Paths_Checks c(
", hiding arcs");
3654 const Spec base = weighted(shape, Weighting::Falling);
3655 for (
unsigned long hidden = 1; hidden < (1UL << shape.
num_arcs()); ++hidden)
3657 const Spec s = poisoned(base, hidden, -1);
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);
3669 check_k_shortest_hiding<TypeParam>(
true);
3674 check_k_shortest_hiding<TypeParam>(
false);
3679 using GT = TypeParam;
3680 using T =
long long;
3683 K_Paths_Checks c(
", random graphs");
3685 for (
const Spec & s : random_specs(3304,
GT().is_digraph(), 6, 0, 9))
3688 const std::vector<T>
w = weights_as<T>(s);
3697 using GT = TypeParam;
3698 K_Paths_Checks with_int(
", int");
3699 K_Paths_Checks with_double(
", double");
3703 const Spec s = weighted(shape, Weighting::Falling);
3705 const std::vector<int> wi = weights_as<int>(s);
3707 const std::vector<double> wd = weights_as<double>(s);
3712 with_double.expect();
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
size_t size_t int32_t value
size_t size_t int32_t * out
A* algorithm for finding the shortest path between two nodes.
Dial's algorithm for shortest paths with integer weights.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Doubly-linked list (defined in tpl_dynList.H).
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.
Path< GT > find_path(Node *src, Node *tgt)
Find the shortest path from source to target.
bool is_initialized() const noexcept
Check if the algorithm has been initialized.
DynMapTree< std::pair< Node *, Node * >, Distance_Type > compute_all_pairs_distances()
Compute the shortest distance for all pairs.
Distance_Type get_potential(Node *node) const
Get the node potential (h value) for a node.
Distance_Type get_distance(Node *src, Node *tgt)
Get the shortest distance from source to target.
Graph_Node< Node_Info > Node
The graph type.
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Sparse matrix with generic row and column domains.
Iterator on nodes and arcs of a path.
bool has_current_node() const noexcept
Return true if the iterator has a current node.
bool is_empty() const noexcept
Return true if the path is empty.
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
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.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
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.
__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)
__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)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
TYPED_TEST(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
DynArray< Graph::Arc * > arcs
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.
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.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
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.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
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).
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.
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...
Filtered iterator on all the arcs of a graph.
Default filter for filtered iterators on arcs.
Filtered iterator of adjacent arcs of a node.
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
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)
Generic graph and digraph implementations.