90#include <gtest/gtest.h>
104using namespace Aleph;
112 template <
typename T>
113 std::string text_of(
const T & x)
115 std::ostringstream s;
122 template <
class GT,
typename T>
125 using Distance_Type =
T;
127 const std::vector<T> * weights =
nullptr;
129 T operator () (
typename GT::Arc * a)
const
131 return (*weights)[size_t(a->
get_info())];
144 unsigned long shown = 0;
146 bool operator () (
typename GT::Arc * a)
const noexcept
157 const std::set<typename GT::Arc *> *
arcs =
nullptr;
159 bool operator () (
typename GT::Arc * a)
const
161 return arcs->count(a) != 0;
166 std::vector<bool> shown_but(
const Spec & s,
unsigned long hidden)
169 for (
size_t i = 0; i <
r.size(); ++i)
175 std::vector<bool> shown_only(
const Spec & s,
unsigned long shown)
178 for (
size_t i = 0; i <
r.size(); ++i)
179 r[i] = ((shown >> i) & 1UL) != 0;
184 const std::vector<long long> Small_Weights = {-2, -1, 0, 1, 2};
188 const std::vector<long long> Sign_Weights = {-2, 1};
192 const std::vector<long long> Unit_Weights = {-1, 0, 1};
196 Domain small_weights_domain()
214 void for_each_weighting(
const Spec & s,
const std::vector<long long> & values,
F && f)
221 f(
static_cast<const Spec &
>(t));
223 for (; i <
k.size(); ++i)
225 if (++
k[i] < values.size())
227 t.
w[i] = values[
k[i]];
243 void for_each_weighted_spec(
F && f,
const std::vector<long long> & small_weights = Small_Weights)
245 const Domain small = small_weights_domain();
248 for_each_weighting(s, small_weights, f);
253 for_each_weighting(s, Sign_Weights, f);
260 std::vector<int>
nodes;
261 std::vector<int>
arcs;
263 [[nodiscard]] std::string str()
const
266 for (
size_t i = 0; i <
nodes.size(); ++i)
268 r += std::to_string(nodes[i]);
270 r +=
" -" + std::to_string(arcs[i]) +
"-> ";
277 template <
class GT,
class Number>
278 Walk walk_of(
const Path<GT> & p, Number && number)
283 w.nodes.push_back(
num(it.get_current_node_ne()));
284 if (it.has_current_arc())
285 w.arcs.push_back(number(it.get_current_arc_ne()));
294 return walk_of(p, [] (
typename GT::Arc * a) {
return num(a); });
298 template <
class Nodes,
class Arcs>
299 Walk walk_of(
const Nodes &
nodes,
const Arcs &
arcs)
302 for (
auto it =
nodes.get_it(); it.has_curr(); it.next_ne())
303 w.nodes.push_back(
num(it.get_curr()));
304 for (
auto it =
arcs.get_it(); it.has_curr(); it.next_ne())
305 w.arcs.push_back(
num(it.get_curr()));
312 std::string walk_error(
const Spec & s,
const Walk &
w,
const std::vector<bool> & show)
314 if (
w.nodes.empty() or
w.nodes.size() !=
w.arcs.size() + 1)
315 return "the walk " +
w.str() +
" is empty or lacks arcs";
316 for (
size_t i = 0; i <
w.arcs.size(); ++i)
318 const int a =
w.arcs[i];
319 if (a < 0 or
size_t(a) >= s.
arcs.size())
320 return "the walk " +
w.str() +
" has an arc that is not of the graph";
321 if (s.
arcs[
size_t(a)][0] !=
w.nodes[i] or s.
arcs[
size_t(a)][1] !=
w.nodes[i + 1])
322 return "the walk " +
w.str() +
" does not follow the arc " + std::to_string(a);
323 if (not show[
size_t(a)])
324 return "the walk " +
w.str() +
" takes the hidden arc " + std::to_string(a);
332 std::string closed_walk_error(
const Spec & s,
const Walk &
w,
333 const std::vector<bool> & show,
bool simple)
335 if (
auto e = walk_error(s,
w, show); not e.empty())
337 if (
w.arcs.empty() or
w.nodes.front() !=
w.nodes.back())
338 return "the walk " +
w.str() +
" is not a closed one";
339 if (simple
and std::set<int>(
w.nodes.begin() + 1,
w.nodes.end()).size() !=
w.arcs.size())
340 return "the cycle " +
w.str() +
" repeats a node";
345 long long cost_of(
const Spec & s,
const Walk &
w)
357 std::vector<int>
arcs;
364 std::vector<Cycle> simple_cycles(
const Spec & s,
const std::vector<bool> & show)
366 std::vector<Cycle>
r;
367 std::vector<bool> on(
size_t(s.
n),
false);
368 std::vector<int>
arcs;
372 auto extend = [&] (
auto & self,
int root,
int x,
long long c) ->
void
374 for (
size_t i = 0; i < s.
arcs.size(); ++i)
376 if (not show[i] or s.
arcs[i][0] != x)
378 const int y = s.
arcs[i][1];
379 arcs.push_back(
int(i));
381 r.push_back({
arcs, c + s.
w[i]});
384 on[size_t(
y)] =
true;
385 self(self,
root,
y, c + s.
w[i]);
386 on[size_t(
y)] =
false;
397 bool has_negative_cycle(
const Spec & s,
const std::vector<bool> & show)
399 for (
const auto & c : simple_cycles(s, show))
406 Spec shown_spec(
const Spec & s,
const std::vector<bool> & show)
408 unsigned long hidden = 0;
409 for (
size_t i = 0; i < show.size(); ++i)
412 std::vector<int> orig;
420 std::vector<bool> show;
421 std::vector<Cycle> cycles;
422 std::vector<std::vector<bool>> reach;
423 std::vector<std::vector<long long>> dist;
424 bool negative =
false;
425 std::vector<bool> negative_from;
427 Truth(
const Spec & s, std::vector<bool> shown) : show(
std::move(
shown))
429 cycles = simple_cycles(s, show);
430 const Spec t = shown_spec(s, show);
434 negative_from.assign(
size_t(s.
n),
false);
435 for (
const auto & c : cycles)
439 const int x = s.
arcs[size_t(c.arcs[0])][0];
440 for (
int u = 0; u < s.
n; ++u)
442 negative_from[size_t(u)] =
true;
452 std::vector<long long> potentials(
const Truth & t,
int n)
454 std::vector<long long>
h(
size_t(n), 0);
455 for (
int u = 0; u < n; ++u)
456 for (
int v = 0; v < n; ++v)
458 h[size_t(v)] = std::min(
h[
size_t(v)], t.dist[
size_t(u)][
size_t(v)]);
464 bool least_mean(
const Truth & t,
long long &
num,
long long & den)
467 for (
const auto & c : t.cycles)
469 const long long len = (
long long) c.arcs.size();
470 if (not found or c.cost * den <
num * len)
482 bool least_cost(
const Truth & t,
size_t len,
long long & cost,
size_t &
arcs)
485 for (
const auto & c : t.cycles)
487 (not found or c.cost < cost or (c.cost == cost
and c.
arcs.
size() <
arcs)))
490 arcs = c.arcs.size();
500 bool least_closed_walk(
const Spec & s,
const std::vector<bool> & show,
size_t len,
504 const size_t n = size_t(s.
n);
505 for (
int v = 0; v < s.
n; ++v)
509 for (
size_t i = 0; i < s.
arcs.size(); ++i)
510 if (show[i]
and s.
arcs[i][0] == v)
511 cur[size_t(s.
arcs[i][1])] = std::min(cur[
size_t(s.
arcs[i][1])], s.
w[i]);
512 for (
size_t k = 1;
k <= len; ++
k)
514 if (cur[
size_t(v)] !=
oracle::Inf and (not found or cur[
size_t(v)] < cost))
516 cost = cur[size_t(v)];
520 for (
size_t i = 0; i < s.
arcs.size(); ++i)
522 const int x = s.
arcs[i][0],
y = s.
arcs[i][1];
524 next[size_t(
y)] = std::min(
next[
size_t(
y)], cur[
size_t(x)] + s.
w[i]);
526 cur = std::move(
next);
542 bool sums_may_overflow(
const Spec & s,
const std::vector<bool> & show,
543 long long lo,
long long hi)
545 const size_t n = size_t(s.
n),
m = s.
num_arcs();
547 std::vector<bool> on(n,
false);
548 auto extend = [&] (
auto & self,
int x,
long long c) ->
void
550 most = std::max(most, c);
551 for (
size_t i = 0; i <
m; ++i)
552 if (show[i]
and s.
arcs[i][0] == x)
554 const int y = s.
arcs[i][1];
555 most = std::max(most, c + s.
w[i]);
556 if (not on[
size_t(
y)])
558 on[size_t(
y)] =
true;
559 self(self,
y, c + s.
w[i]);
560 on[size_t(
y)] =
false;
564 for (
int x = 0; x < s.
n; ++x)
566 on[size_t(x)] =
true;
567 extend(extend, x, 0);
568 on[size_t(x)] =
false;
571 const size_t k = (n + 4) * (
m + n) + 1;
573 std::vector<long long> cur(n, 0);
574 for (
size_t step = 0; step <
k; ++step)
577 for (
size_t i = 0; i <
m; ++i)
580 cur[
size_t(s.
arcs[i][0])] + s.
w[i]);
581 for (
long long x :
next)
583 least =
std::
min(least, x);
584 cur = std::move(
next);
586 return most > hi or least < lo;
593 bool walks_may_overflow(
const Spec & s,
const std::vector<bool> & show,
size_t len,
594 long long lo,
long long hi)
596 const size_t n = size_t(s.
n);
598 std::vector<long long> most(n, 0), least(n, 0);
599 std::vector<bool> ends(n,
true);
600 for (
size_t k = 1;
k <= len; ++
k)
602 std::vector<long long> next_most(n, std::numeric_limits<long long>::lowest());
603 std::vector<long long> next_least(n, std::numeric_limits<long long>::max());
604 std::vector<bool> next_ends(n,
false);
605 for (
size_t i = 0; i < s.
num_arcs(); ++i)
607 const size_t x = size_t(s.
arcs[i][0]),
y = size_t(s.
arcs[i][1]);
608 if (not show[i] or not ends[x])
611 next_most[
y] = std::max(next_most[
y], most[x] + s.
w[i]);
612 next_least[
y] = std::min(next_least[
y], least[x] + s.
w[i]);
614 for (
size_t v = 0; v < n; ++v)
615 if (next_ends[v]
and (next_most[v] > hi or next_least[v] < lo))
617 most = std::move(next_most);
618 least = std::move(next_least);
619 ends = std::move(next_ends);
626 template <
class GT,
typename T>
632 Spec_Weight<GT, T> d;
639 bool may_overflow =
false;
642 bool may_reject =
false;
645 std::vector<bool> filtered;
648 std::optional<Truth> signs;
650 Case(
const Spec & spec, std::vector<bool> show)
651 : s(spec), b(spec),
w(spec.
w.begin(), spec.
w.end()), t(spec,
std::move(show)),
657 Case(
const Case &) =
delete;
658 Case & operator = (
const Case &) =
delete;
660 [[nodiscard]]
const std::vector<bool> & show() const noexcept {
return t.show; }
663 template <
class GT,
typename T,
class SA = Dft_Show_Arc<GT>>
673 for (
size_t i = 0; i < b.
arcs.size(); ++i)
684 w =
w < 0 ? -1 :
w > 0 ? 1 : 0;
691 template <
class GT,
typename T>
692 std::string data_error(
const Case<GT, T> & c)
694 for (
auto * p : c.b.
nodes)
697 return
"the node " +
std::
to_string(
num(p)) +
" keeps its data or a bit";
698 for (
auto * a : c.b.
arcs)
706 template <
class GT,
typename T,
class SA>
707 std::string state_error(
const BF<GT, T, SA> & bf,
const Case<GT, T> & c)
709 if (bf.has_computation() or bf.is_painted())
710 return "the object keeps a computation";
711 return data_error(c);
717 template <
class GT,
typename T,
class SA>
718 std::string after_throw_error(BF<GT, T, SA> & bf, Case<GT, T> & c)
720 if (
auto e = state_error(bf, c); not e.empty() or c.s.n == 0)
722 const Spec t = with_signs(c.s);
724 c.signs.emplace(t, c.filtered);
725 const std::vector<T> weights = c.w;
726 for (
size_t i = 0; i < c.w.size(); ++i)
728 const bool negative = bf.paint_spanning_tree(c.b.nodes[0]);
730 return negative == c.signs->negative_from[0] ?
"" :
"it does not run again";
738 template <
class GT,
typename T,
class SA,
class Call>
739 bool returns(
Check &
k, BF<GT, T, SA> & bf, Case<GT, T> & c,
const std::string & where,
747 catch (
const std::overflow_error & e)
750 if (not c.may_overflow)
751 k.fail(c.s, c.label + where +
"throws " + e.what());
752 else if (
auto err = after_throw_error(bf, c); not err.empty())
753 k.fail(c.s, c.label + where +
"after overflow_error, " + err);
756 catch (
const std::domain_error &)
758 if (not c.may_reject)
761 if (
auto err = after_throw_error(bf, c); not err.empty())
762 k.fail(c.s, c.label + where +
"after domain_error, " + err);
767 const char * says(
bool found)
769 return found ?
"finds a negative cycle where there is none" :
"finds no negative cycle";
772 std::string from_text(
int u)
774 return "from " + std::to_string(u) +
": ";
778 struct Detection_Checks
787 explicit Detection_Checks(
const std::string & how =
"")
788 : paint(
"Bellman_Ford::paint_spanning_tree()" + how),
789 faster(
"Bellman_Ford::faster_paint_spanning_tree()" + how),
790 from(
"Bellman_Ford::has_negative_cycle(start)" + how),
791 global(
"Bellman_Ford::has_negative_cycle()" + how),
792 weights(
"Bellman_Ford::compute_nodes_weights()" + how)
797 for (
const Check * c : {&paint, &faster, &from, &global, &weights})
804 template <
class GT,
typename T,
class Map>
805 std::string potentials_error(
const Case<GT, T> & c,
const Map &
m)
807 const auto h = potentials(c.t, c.s.n);
808 if (
m.
size() !=
size_t(c.s.n))
809 return "gives " + std::to_string(
m.
size()) +
" potentials";
810 for (
int v = 0; v < c.s.n; ++v)
812 const auto * p =
m.
search(c.b.nodes[
size_t(v)]);
814 return "gives no potential to " + std::to_string(v);
815 if ((
long double) p->second != (
long double)
h[
size_t(v)])
816 return "gives " + std::to_string((
long double) p->second) +
" to "
817 + std::to_string(v) +
", not " + std::to_string(
h[
size_t(v)]);
825 template <
class GT,
typename T,
class SA>
826 void run_detection(Detection_Checks &
k, Case<GT, T> & c,
const SA & sa)
828 const Spec & s = c.s;
829 const bool faster =
k.turn++ % 2 == 1;
830 Check & paint = faster ?
k.faster :
k.paint;
831 for (
int u = 0; u < s.
n; ++u)
833 auto * p = c.b.nodes[size_t(u)];
834 const bool want = c.t.negative_from[size_t(u)];
835 const std::string from = from_text(u);
837 BF<GT, T, SA> bf(c.b.g, c.d, sa);
838 returns(paint, bf, c, from, [&]
840 const bool got = faster ? bf.faster_paint_spanning_tree(p)
841 : bf.paint_spanning_tree(p);
844 paint.
fail(s, c.label + from + says(got));
847 BF<GT, T, SA> bf(c.b.g, c.d, sa);
848 returns(
k.from, bf, c, from, [&]
850 const bool got = bf.has_negative_cycle(p);
853 k.from.fail(s, c.label + from + says(got));
858 BF<GT, T, SA> bf(c.b.g, c.d, sa);
859 returns(
k.global, bf, c,
"", [&]
861 const bool got = bf.has_negative_cycle();
863 if (got != c.t.negative)
864 k.global.fail(s, c.label + says(got));
865 else if (not intact(c.b, s))
866 k.global.fail(s, c.label +
"leaves the graph changed");
870 BF<GT, T, SA> bf(c.b.g, c.d, sa);
871 returns(
k.weights, bf, c,
"", [&]
875 const auto m = bf.compute_nodes_weights();
878 k.weights.fail(s, c.label +
"does not throw on a negative cycle");
879 else if (auto e = potentials_error(c, m); not e.empty())
880 k.weights.fail(s, c.label + e);
882 catch (
const std::domain_error &)
885 if (not c.t.negative
and not c.may_reject)
886 k.weights.fail(s, c.label +
"throws domain_error without a negative cycle");
887 else if (
auto e = state_error(bf, c); not e.empty())
888 k.weights.fail(s, c.label +
"after domain_error, " + e);
891 if (not intact(c.b, s))
892 k.weights.fail(s, c.label +
"leaves the graph changed");
900 template <
class GT,
typename T>
901 void check_witness(
Check &
k,
const Case<GT, T> & c,
const Path<GT> & path,
bool want,
902 const std::string & where,
const bool * found =
nullptr)
905 if (found !=
nullptr and *found == path.
is_empty())
907 k.fail(c.s, c.label + where +
"returns " + (*found ?
"true" :
"false") +
" with a path of "
914 k.fail(c.s, c.label + where +
"finds no negative cycle");
917 const Walk
w = walk_of(path);
918 if (
auto e = closed_walk_error(c.s,
w, c.show(),
true); not e.empty())
919 k.fail(c.s, c.label + where + e);
920 else if (
const long long cost = cost_of(c.s,
w); cost >= 0)
921 k.fail(c.s, c.label + where +
"the cycle " +
w.str() +
" costs " + std::to_string(cost));
923 k.fail(c.s, c.label + where +
"finds the negative cycle " +
w.str()
924 +
", that the oracle lacks");
930 const std::pair<double, size_t> Steps[] = {{0.0, 1}, {0.5, 1}, {1.0, 2}, {4.0, 1}};
934 struct Witness_Checks
942 Witness_Checks(
bool global,
const std::string & how)
943 :
test(
std::string(
"Bellman_Ford::test_negative_cycle(")
944 + (global ?
"" :
"start") +
")" + how),
945 test_path(
std::string(
"Bellman_Ford::test_negative_cycle(")
946 + (global ?
"path" :
"start, path") +
")" + how),
947 search(
std::string(
"Bellman_Ford::search_negative_cycle(")
948 + (global ?
"" :
"start") +
")" + how),
949 steps(
std::string(
"Bellman_Ford::search_negative_cycle(")
950 + (global ?
"" :
"start, ") +
"it_factor, step)" + how)
965 template <
class GT,
typename T,
class SA>
966 void run_witnesses(Witness_Checks &
k, Case<GT, T> & c,
const SA & sa,
bool global)
968 const size_t turn =
k.turn++;
970 const double factor = Steps[(turn / 4) % std::size(Steps)].first;
971 const size_t step = Steps[(turn / 4) % std::size(Steps)].second;
972 const bool with_path = turn % 2 == 1;
973 const bool with_steps = (turn / 2) % 2 == 1;
974 const std::string with =
"with " + text_of(factor) +
" and " + text_of(step) +
": ";
977 auto searches = [&] (
int u)
979 typename GT::Node * p = u < 0 ? nullptr : c.b.nodes[size_t(u)];
980 const bool want = u < 0 ? c.t.negative : c.t.negative_from[size_t(u)];
981 const std::string from = u < 0 ? std::string() : from_text(u);
984 BF<GT, T, SA> bf(c.b.g, c.d, sa);
985 returns(
test, bf, c, from, [&]
988 check_witness(
test, c, u < 0 ? bf.test_negative_cycle() : bf.test_negative_cycle(p),
993 const bool found = u < 0 ? bf.test_negative_cycle(path)
994 : bf.test_negative_cycle(p, path);
995 check_witness(
test, c, path, want, from, &found);
1000 BF<GT, T, SA> bf(c.b.g, c.d, sa);
1001 returns(
search, bf, c, from, [&]
1005 u < 0 ? bf.search_negative_cycle() : bf.search_negative_cycle(p),
1009 std::get<0>(u < 0 ? bf.search_negative_cycle(factor, step)
1010 : bf.search_negative_cycle(p, factor, step)),
1016 for (
int u = 0; u < c.s.n; ++u)
1021 if (not intact(c.b, c.s))
1022 k.test.fail(c.s, c.label +
"a search leaves the graph changed");
1028 struct Functor_Checks
1036 explicit Functor_Checks(
const std::string & how)
1037 : path(
"Bellman_Ford_Negative_Cycle(g, path, d, sa)" + how),
1038 start_path(
"Bellman_Ford_Negative_Cycle(g, start, path, d, sa)" + how),
1039 start(
"Bellman_Ford_Negative_Cycle(g, start, d, sa)" + how),
1040 graph(
"Bellman_Ford_Negative_Cycle(g, d, sa)" + how)
1045 for (
const Check * c : {&path, &start_path, &start, &graph})
1052 std::vector<int> ends_of(
const Spec & s)
1058 return {0, s.
n - 1};
1065 template <
template <
class,
class>
class NAit,
class GT,
typename T,
class SA>
1066 void run_functors(Functor_Checks &
k, Case<GT, T> & c,
const SA & sa)
1068 using D = Spec_Weight<GT, T>;
1070 const size_t turn =
k.turn++;
1071 const bool lvalues = turn % 2 == 0;
1072 const bool with_path = (turn / 2) % 2 == 0;
1073 const std::string how = lvalues ?
"lvalues: " :
"rvalues: ";
1077 auto guarded = [&] (
Check &
k,
const std::string & where,
auto && call)
1083 catch (
const std::exception & e)
1086 k.fail(c.s, c.label + where +
"throws " + e.what());
1090 const bool want = c.t.negative;
1092 guarded(
k.path, how, [&]
1094 Path<GT> path(c.b.g);
1095 const bool found = lvalues ? F()(c.b.g, path, d, a) : F()(c.b.g, path, D(d), SA(a));
1096 check_witness(k.path, c, path, want, how, &found);
1099 guarded(
k.graph, how, [&]
1101 check_witness(k.graph, c, lvalues ? F()(c.b.g, d, a) : F()(c.b.g, D(d), SA(a)), want,
1104 if (not intact(c.b, c.s))
1105 k.graph.fail(c.s, c.label +
"a search leaves the graph changed");
1107 for (
int u : ends_of(c.s))
1109 auto * p = c.b.nodes[size_t(u)];
1110 const bool want = c.t.negative_from[size_t(u)];
1111 const std::string where = how + from_text(u);
1113 guarded(
k.start_path, where, [&]
1115 Path<GT> path(c.b.g);
1116 const bool found = lvalues ? F()(c.b.g, p, path, d, a)
1117 : F()(c.b.g, p, path, D(d), SA(a));
1118 check_witness(k.start_path, c, path, want, where, &found);
1121 guarded(
k.start, where, [&]
1123 check_witness(k.start, c,
1124 lvalues ? F()(c.b.g, p, d, a) : F()(c.b.g, p, D(d), SA(a)), want,
1132 std::set<int> painted_arcs(
const Built<GT> & b)
1135 for (
size_t i = 0; i < b.
arcs.size(); ++i)
1145 template <
class GT,
typename T>
1146 std::string painting_error(
const Case<GT, T> & c,
int u,
const std::set<int> & painted)
1148 const Spec & s = c.s;
1149 std::vector<int> into(
size_t(s.
n), 0);
1150 for (
int i : painted)
1152 const int x = s.
arcs[size_t(i)][0],
y = s.
arcs[size_t(i)][1];
1153 if (not c.show()[
size_t(i)])
1154 return "paints the hidden arc " + std::to_string(i);
1155 if (not c.t.reaches(u, x) or
y == u)
1156 return "paints the arc " + std::to_string(i) +
", out of the tree";
1157 if (c.t.dist[
size_t(u)][
size_t(x)] + s.
w[
size_t(i)] != c.t.dist[
size_t(u)][
size_t(
y)])
1158 return "paints the arc " + std::to_string(i) +
", which is not on a shortest path";
1161 for (
int v = 0; v < s.
n; ++v)
1164 return "paints the node " + std::to_string(v) +
" wrongly";
1165 if (v != u
and c.t.reaches(u, v)
and into[
size_t(v)] != 1)
1166 return "paints " + std::to_string(into[
size_t(v)]) +
" arcs into " + std::to_string(v);
1175 template <
class GT,
typename T,
class SA>
1176 std::string distances_error(BF<GT, T, SA> & bf,
const Case<GT, T> & c,
int u,
1177 bool domain_error_is_right =
false)
1179 for (
int v = 0; v < c.s.n; ++v)
1181 const bool reached = c.t.reaches(u, v);
1184 const T got = bf.get_distance(c.b.nodes[
size_t(v)]);
1186 return "gives a distance to " + std::to_string(v) +
", which is not reached";
1187 if ((
long double) got != (
long double) c.t.dist[
size_t(u)][
size_t(v)])
1188 return "gives " + text_of(got) +
" to " + std::to_string(v) +
", not "
1189 + std::to_string(c.t.dist[
size_t(u)][
size_t(v)]);
1191 catch (
const std::domain_error &)
1193 if (reached
and not domain_error_is_right)
1194 return "throws domain_error for " + std::to_string(v) +
", which is reached";
1203 template <
class GT,
typename T>
1204 std::string min_path_error(
const Case<GT, T> & c,
const Path<GT> & path,
int u,
int v,
T got)
1206 const Walk
w = walk_of(path);
1207 if (
auto e = walk_error(c.s,
w, c.show()); not e.empty())
1209 if (
w.nodes.front() != u or
w.nodes.back() != v)
1210 return "the path " +
w.str() +
" does not go from " + std::to_string(u) +
" to "
1211 + std::to_string(v);
1212 if (std::set<int>(
w.nodes.begin(),
w.nodes.end()).size() !=
w.nodes.size())
1213 return "the path " +
w.str() +
" repeats a node";
1214 const long long want = c.t.dist[size_t(u)][size_t(v)];
1215 if (cost_of(c.s,
w) != want or (
long double) got != (
long double) want)
1216 return "the path " +
w.str() +
" costs " + std::to_string(cost_of(c.s,
w)) +
" and "
1217 + text_of(got) +
" is returned, not " + std::to_string(want);
1224 template <
class GT,
typename T,
class SA>
1225 std::string min_paths_error(BF<GT, T, SA> & bf,
const Case<GT, T> & c,
int u,
1226 bool domain_error_is_right =
false)
1228 for (
int v = 0; v < c.s.n; ++v)
1230 const bool reached = c.t.reaches(u, v);
1234 const T got = bf.get_min_path(c.b.nodes[
size_t(v)], path);
1236 return "gives a path to " + std::to_string(v) +
", which is not reached";
1237 if (
auto e = min_path_error(c, path, u, v, got); not e.empty())
1240 catch (
const std::domain_error &)
1242 if (reached
and not domain_error_is_right)
1243 return "throws domain_error for " + std::to_string(v) +
", which is reached";
1254 std::string tree_error(
const Spec & s,
const GT & tree,
const std::set<int> & painted)
1256 std::multiset<int>
nodes, want_nodes;
1257 for (
auto it = tree.
get_node_it(); it.has_curr(); it.next_ne())
1259 for (
int v = 0; v < s.
n; ++v)
1260 want_nodes.insert(v);
1261 if (
nodes != want_nodes)
1262 return "the tree has " + std::to_string(
nodes.size()) +
" nodes, not those of the graph";
1263 std::multiset<std::array<int, 3>>
arcs, want_arcs;
1264 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
1266 auto * a = it.get_curr();
1269 for (
int i : painted)
1270 want_arcs.
insert({i, s.
arcs[size_t(i)][0], s.
arcs[size_t(i)][1]});
1271 if (
arcs != want_arcs)
1272 return "the tree has " + std::to_string(
arcs.size()) +
" arcs, not the "
1273 + std::to_string(painted.size()) +
" painted ones";
1281 std::string mapping_error(
const Built<GT> & b,
const GT & tree,
const std::set<int> & painted)
1283 std::set<typename GT::Node *>
nodes;
1284 for (
auto it = tree.
get_node_it(); it.has_curr(); it.next_ne())
1285 nodes.insert(it.get_curr());
1286 std::set<typename GT::Arc *>
arcs;
1287 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
1288 arcs.insert(it.get_curr());
1289 for (
size_t v = 0; v < b.
nodes.size(); ++v)
1291 auto * p = b.
nodes[v];
1294 return "the node " + std::to_string(v) +
" is not mapped to its image";
1296 for (
int i : painted)
1298 auto * a = b.
arcs[size_t(i)];
1301 return "the arc " + std::to_string(i) +
" is not mapped to its image";
1318 Check mapped_distance;
1321 Check unpainted_build;
1324 explicit Tree_Checks(
const std::string & how =
"")
1325 : painted(
"Bellman_Ford, the painted tree" + how),
1326 state(
"Bellman_Ford, the state after painting" + how),
1327 distance(
"Bellman_Ford::get_distance()" + how),
1328 path(
"Bellman_Ford::get_min_path()" + how),
1329 extract(
"Bellman_Ford::extract_min_spanning_tree()" + how),
1330 tree(
"Bellman_Ford::build_tree(tree, false)" + how),
1331 mapped(
"Bellman_Ford::build_tree(tree)" + how),
1332 mapped_path(
"Bellman_Ford::get_min_path(), after build_tree(tree)" + how),
1333 mapped_distance(
"Bellman_Ford::get_distance(), after build_tree(tree)" + how),
1334 mapped_build(
"Bellman_Ford::build_tree(tree), after build_tree(tree)" + how),
1335 unpainted(
"Bellman_Ford, the queries without a painted tree" + how),
1336 unpainted_build(
"Bellman_Ford::build_tree(tree, false), without a painted tree" + how)
1341 for (
const Check * c : {&painted, &state, &distance, &path, &extract, &tree, &mapped,
1342 &mapped_path, &mapped_distance, &mapped_build, &unpainted,
1350 template <
class GT,
typename T,
class SA>
1351 void check_unpainted_build(
Check &
k, BF<GT, T, SA> & bf,
const Spec & s,
1352 const std::string & where)
1358 bf.build_tree(tree,
false);
1359 k.fail(s, where +
"builds a tree of " + std::to_string(tree.
get_num_arcs()) +
" arcs");
1361 catch (
const std::domain_error &)
1365 catch (
const std::exception & e)
1367 k.fail(s, where +
"throws " + e.what());
1373 template <
class GT,
typename T,
class SA>
1374 void check_unpainted(Tree_Checks &
k, BF<GT, T, SA> & bf,
const Case<GT, T> & c,
int u,
1375 const std::string & where)
1377 auto * p = c.b.nodes[size_t(u)];
1380 auto count = [&] (
auto && call)
1386 catch (
const std::domain_error &)
1391 count([&] { bf.get_distance(p); });
1392 count([&] {
Path<GT> path(c.b.g); bf.get_min_path(p, path); });
1393 count([&] { bf.extract_min_spanning_tree(); });
1394 count([&] {
GT tree; bf.build_tree(tree); });
1395 if (bf.is_painted() or thrown != 4)
1396 k.unpainted.fail(c.s, c.label + where + std::to_string(4 - thrown)
1397 +
" of 4 queries do not throw domain_error");
1398 check_unpainted_build(
k.unpainted_build, bf, c.s, c.label + where);
1406 template <
class GT,
typename T,
class SA>
1407 void run_tree(Tree_Checks &
k, Case<GT, T> & c,
const SA & sa)
1409 const Spec & s = c.s;
1412 const size_t turn =
k.turn++;
1413 const bool faster = turn % 2 == 1;
1414 const int u = (turn / 2) % 2 == 0 ? 0 : s.n - 1;
1415 const std::string where = from_text(u) + (faster ?
"SPFA: " :
"");
1416 auto * p = c.b.nodes[size_t(u)];
1417 BF<GT, T, SA> bf(c.b.g, c.d, sa);
1418 bool negative =
false;
1419 if (not returns(
k.painted, bf, c, where, [&]
1421 negative = faster ? bf.faster_paint_spanning_tree(p) : bf.paint_spanning_tree(p);
1425 if (negative != c.t.negative_from[
size_t(u)])
1427 k.painted.fail(s, c.label + where + says(negative));
1432 check_unpainted(
k, bf, c, u, where);
1436 const auto painted = painted_arcs(c.b);
1437 if (
auto e = painting_error(c, u, painted); not e.empty())
1438 k.painted.fail(s, c.label + where + e);
1441 if (not bf.is_painted() or not bf.has_computation() or bf.get_start_node() != p
1442 or &bf.get_graph() != &c.b.g)
1443 k.state.fail(s, c.label + where +
"the getters do not tell the painting");
1446 if (
auto e = distances_error(bf, c, u); not e.empty())
1447 k.distance.fail(s, c.label + where + e);
1450 if (
auto e = min_paths_error(bf, c, u); not e.empty())
1451 k.path.fail(s, c.label + where + e);
1455 const auto arcs = bf.extract_min_spanning_tree();
1457 for (
size_t i = 0; i <
arcs.size(); ++i)
1458 if (
arcs(i) !=
nullptr)
1461 k.extract.fail(s, c.label + where +
"gives " + std::to_string(got.size())
1462 +
" arcs, not the " + std::to_string(painted.size()) +
" painted ones");
1469 bf.build_tree(tree,
false);
1470 if (
auto e = tree_error(s, tree, painted); not e.empty())
1471 k.tree.fail(s, c.label + where + e);
1472 else if (
auto e2 = distances_error(bf, c, u); not e2.empty())
1473 k.tree.fail(s, c.label + where +
"after it, get_distance() " + e2);
1478 bf.build_tree(tree);
1479 if (
auto e = tree_error(s, tree, painted); not e.empty())
1480 k.mapped.fail(s, c.label + where + e);
1481 else if (
auto e2 = mapping_error(c.b, tree, painted); not e2.empty())
1482 k.mapped.fail(s, c.label + where + e2);
1486 k.mapped_path.run();
1487 if (
auto e = min_paths_error(bf, c, u,
true); not e.empty())
1488 k.mapped_path.fail(s, c.label + where + e);
1490 k.mapped_distance.run();
1491 if (
auto e = distances_error(bf, c, u,
true); not e.empty())
1492 k.mapped_distance.fail(s, c.label + where + e);
1494 k.mapped_build.run();
1498 bf.build_tree(again);
1499 if (
auto e = tree_error(s, again, painted); not e.empty())
1500 k.mapped_build.fail(s, c.label + where + e);
1502 catch (
const std::domain_error &)
1506 catch (
const std::exception & e)
1508 k.mapped_build.fail(s, c.label + where +
"throws " + e.what());
1513 struct Search_Checks
1517 Check disjoint_none;
1523 explicit Search_Checks(
const std::string & how =
"")
1524 : disjoint(
"find_disjoint_negative_cycles(), Min_Weight_Arc" + how),
1525 disjoint_all(
"find_disjoint_negative_cycles(), All_Arcs" + how),
1526 disjoint_none(
"find_disjoint_negative_cycles(), max_cycles 0" + how),
1527 bounded(
"most_negative_cycle_bounded()" + how),
1528 bounded_none(
"most_negative_cycle_bounded(), max_length 0" + how),
1529 short_cycle(
"most_negative_cycle_up_to_3()" + how),
1530 short_bounds(
"most_negative_cycle_up_to_3(), max_length 0 and 4" + how)
1535 for (
const Check * c : {&disjoint, &disjoint_all, &disjoint_none, &
bounded, &bounded_none,
1536 &short_cycle, &short_bounds})
1548 template <
class GT,
typename T,
class Items>
1549 std::string disjoint_error(
const Case<GT, T> & c,
const Items & items,
size_t max_cycles,
1552 const Spec & s = c.s;
1553 std::vector<std::vector<int>> cycles;
1554 std::vector<bool> left = c.show();
1555 for (
auto it = items.get_it(); it.has_curr(); it.next_ne())
1557 const auto & item = it.get_curr();
1558 const Walk
w = walk_of(item.cycle);
1559 if (
auto e = closed_walk_error(s,
w, c.show(),
true); not e.empty())
1561 if (not item.cycle.is_cycle() or item.length !=
w.arcs.size())
1562 return "the cycle " +
w.str() +
" has the length " + std::to_string(item.length);
1563 const long long cost = cost_of(s,
w);
1564 if (cost >= 0 or (
long double) item.total_cost != (
long double) cost)
1565 return "the cycle " +
w.str() +
" costs " + std::to_string(cost) +
", and "
1566 + text_of(item.total_cost) +
" is given";
1567 std::vector<int>
arcs =
w.arcs;
1568 std::sort(
arcs.begin(),
arcs.end());
1569 for (
const auto & other : cycles)
1572 return "the cycle " +
w.str() +
" is given twice";
1573 if (exclusion == Negative_Cycle_Exclusion::All_Arcs)
1576 return
"the cycle " +
w.str() +
" shares the arc " +
std::
to_string(a);
1578 cycles.push_back(
arcs);
1582 long long least = s.
w[size_t(
arcs[0])];
1584 least =
std::
min(least, s.
w[size_t(a)]);
1587 left[size_t(a)] = false;
1589 if (cycles.size() > max_cycles)
1590 return "gives " + std::to_string(cycles.size()) +
" cycles";
1591 if (cycles.empty() != not c.t.negative)
1592 return cycles.empty() ?
"finds no negative cycle" :
"finds a negative cycle";
1593 if (cycles.size() < max_cycles
and has_negative_cycle(s, left))
1594 return "stops after " + std::to_string(cycles.size()) +
" cycles, with a negative one left";
1603 template <
class GT,
typename T,
class Result>
1604 std::string bounded_error(
const Case<GT, T> & c,
const Result &
r,
size_t len)
1606 const Spec & s = c.s;
1607 const size_t bound = std::min(len,
size_t(s.
n));
1608 long long least = 0;
1610 const bool has = least_cost(c.t, bound, least, fewest);
1611 if (
r.has_cycle != has)
1612 return has ?
"finds no cycle" :
"finds a cycle where there is none";
1614 return r.is_negative() ?
"is negative without a cycle" :
"";
1615 const Walk
w = walk_of(
r.cycle_nodes,
r.cycle_arcs);
1616 if (
auto e = closed_walk_error(s,
w, c.show(),
true); not e.empty())
1618 const long long cost = cost_of(s,
w);
1619 if (
w.arcs.size() > bound or
r.length !=
w.arcs.size())
1620 return "the cycle " +
w.str() +
" has the length " + std::to_string(
r.length);
1621 if ((
long double)
r.total_cost != (
long double) cost)
1622 return "the cycle " +
w.str() +
" costs " + std::to_string(cost) +
", and "
1623 + text_of(
r.total_cost) +
" is given";
1624 if (
r.is_negative() != (least < 0))
1625 return "is_negative() is wrong: the least cost is " + std::to_string(least);
1626 const long double gap = (
long double)
r.optimality_gap;
1627 if (gap < 0 or
r.is_exact != (gap == 0) or (
long double) cost - gap > (
long double) least
1628 or (
r.is_exact
and cost != least))
1629 return "the cycle costs " + std::to_string(cost) +
" with the gap "
1630 + text_of(
r.optimality_gap) + (
r.is_exact ?
", exact" :
"") +
", and the least cost is "
1631 + std::to_string(least);
1632 if constexpr (std::is_integral_v<T>)
1634 long long relaxed = 0;
1635 least_closed_walk(s, c.show(), bound, relaxed);
1637 const long long gap_want =
1638 std::min<long long>(cost - relaxed, std::numeric_limits<T>::max());
1639 if (
r.numeric_quality != Cycle_Numeric_Quality::Exact
1640 or
r.matches_relaxed_bound !=
r.is_exact or (
long long)
r.optimality_gap != gap_want)
1641 return "the gap " + text_of(
r.optimality_gap) +
" is not the distance from "
1642 + std::to_string(cost) +
" to the relaxed bound " + std::to_string(relaxed);
1650 template <
class GT,
typename T,
class Result>
1651 std::string short_error(
const Case<GT, T> & c,
const Result &
r,
size_t len)
1653 const Spec & s = c.s;
1654 long long least = 0;
1656 const bool has = least_cost(c.t, len, least, fewest);
1657 if (
r.has_cycle != has)
1658 return has ?
"finds no cycle" :
"finds a cycle where there is none";
1660 return r.is_negative() or not
r.cycle_arcs.is_empty() ?
"gives a cycle" :
"";
1661 const Walk
w = walk_of(
r.cycle_nodes,
r.cycle_arcs);
1662 if (
auto e = closed_walk_error(s,
w, c.show(),
true); not e.empty())
1664 const long long cost = cost_of(s,
w);
1665 if (cost != least or (
long double)
r.total_cost != (
long double) least)
1666 return "the cycle " +
w.str() +
" costs " + std::to_string(cost) +
", and "
1667 + text_of(
r.total_cost) +
" is given, not " + std::to_string(least);
1668 if (
r.length !=
w.arcs.size() or
r.length != fewest)
1669 return "the cycle " +
w.str() +
" has the length " + std::to_string(
r.length) +
", not "
1670 + std::to_string(fewest);
1671 if (
r.is_negative() != (least < 0))
1672 return "is_negative() is wrong";
1678 template <
class GT,
typename T,
class SA>
1679 void run_searches(Search_Checks &
k, Case<GT, T> & c,
const SA & sa)
1681 const GT & g = c.b.g;
1682 const Spec & s = c.s;
1683 for (
auto exclusion : {Negative_Cycle_Exclusion::Min_Weight_Arc,
1684 Negative_Cycle_Exclusion::All_Arcs})
1685 for (
size_t max_cycles : {size_t(1), size_t(64)})
1687 Check &
check = exclusion == Negative_Cycle_Exclusion::All_Arcs ?
k.disjoint_all
1691 if (
auto e = disjoint_error(c, items, max_cycles, exclusion); not e.empty())
1692 check.fail(s, c.label +
"up to " + std::to_string(max_cycles) +
": " + e);
1695 k.disjoint_none.run();
1698 k.disjoint_none.fail(s, c.label +
"gives cycles");
1701 for (
size_t len : {size_t(1), size_t(2), size_t(s.
n + 1)})
1705 if (
auto e = bounded_error(c,
r, len); not e.empty())
1706 k.bounded.fail(s, c.label +
"up to " + std::to_string(len) +
" arcs: " + e);
1709 k.bounded_none.run();
1711 r.has_cycle or
r.is_negative() or not
r.cycle_arcs.is_empty())
1712 k.bounded_none.fail(s, c.label +
"gives a cycle");
1714 for (
size_t len : {size_t(1), size_t(2), size_t(3)})
1716 k.short_cycle.run();
1718 if (
auto e = short_error(c,
r, len); not e.empty())
1719 k.short_cycle.fail(s, c.label +
"up to " + std::to_string(len) +
" arcs: " + e);
1722 k.short_bounds.run();
1724 k.short_bounds.fail(s, c.label +
"gives a cycle of no arc");
1728 k.short_bounds.fail(s, c.label +
"takes 4 arcs");
1730 catch (
const std::invalid_argument &)
1747 Check fallback_limit;
1750 explicit Mean_Checks(
const std::string & how =
"")
1751 : karp(
"karp_minimum_mean_cycle()" + how),
1752 karp_value(
"karp_minimum_mean_cycle_value()" + how),
1753 aliases(
"minimum_mean_cycle() and minimum_mean_cycle_value()" + how),
1754 functors(
"Karp_Minimum_Mean_Cycle and Karp_Minimum_Mean_Cycle_Value" + how),
1755 howard(
"howard_minimum_mean_cycle()" + how),
1756 howard_value(
"howard_minimum_mean_cycle_value()" + how),
1757 howard_limit(
"howard_minimum_mean_cycle() and its value, without bytes" + how),
1758 fallback(
"howard_detail::minimum_mean_cycle(), without iterations" + how),
1759 fallback_limit(
"howard_detail::minimum_mean_cycle(), without iterations nor bytes" + how)
1764 for (
const Check * c : {&karp, &karp_value, &aliases, &functors, &howard, &howard_value,
1765 &howard_limit, &fallback, &fallback_limit})
1773 template <
class GT,
typename T>
1774 long double least_mean_value(
const Case<GT, T> & c,
bool & has,
long long &
num,
1779 has = least_mean(c.t,
num, den);
1780 return has ? (
long double)
num / (
long double) den
1781 : std::numeric_limits<long double>::infinity();
1788 template <
class GT,
typename T,
class Result>
1789 std::string mean_error(
const Case<GT, T> & c,
const Result &
r)
1792 long long num = 0, den = 1;
1793 const long double mean = least_mean_value(c, has,
num, den);
1794 if (
r.has_cycle != has)
1795 return has ?
"finds no cycle" :
"finds a cycle where there is none";
1796 if (
r.minimum_mean != mean)
1797 return "gives the mean " + text_of(
r.minimum_mean) +
", not " + std::to_string(
num) +
"/"
1798 + std::to_string(den);
1800 return r.cycle_arcs.is_empty()
and r.cycle_length == 0 ?
"" :
"gives a cycle";
1801 const Walk
w = walk_of(
r.cycle_nodes,
r.cycle_arcs);
1802 if (
auto e = closed_walk_error(c.s,
w, c.show(),
true); not e.empty())
1804 const long long cost = cost_of(c.s,
w);
1805 if (
r.cycle_length !=
w.arcs.size() or (
long double)
r.cycle_total_cost != (
long double) cost)
1806 return "the cycle " +
w.str() +
" has the length " + std::to_string(
r.cycle_length)
1807 +
" and the cost " + text_of(
r.cycle_total_cost);
1808 if (cost * den !=
num * (
long long)
w.arcs.size())
1809 return "the cycle " +
w.str() +
" has not the least mean " + std::to_string(
num) +
"/"
1810 + std::to_string(den);
1811 if (
r.witness_node ==
nullptr)
1812 return "gives no witness node";
1819 template <
class Result>
1820 std::string howard_error(
const Result &
r)
1822 if (
r.has_cycle
and r.witness_node !=
r.cycle_nodes.get_first())
1823 return "the witness node does not start the cycle";
1824 if (
r.used_karp != (
r.fallback_reason != Howard_Fallback_Reason::None))
1825 return "used_karp and fallback_reason disagree";
1826 if (
r.has_cycle
and (
r.numeric_quality == Cycle_Numeric_Quality::Exact) ==
r.used_karp)
1827 return std::string(
"the quality is ")
1828 + (
r.numeric_quality == Cycle_Numeric_Quality::Exact ?
"Exact" :
"Rounded")
1829 + (
r.used_karp ?
" after" :
" without") +
" the fallback";
1835 template <
class Result>
1836 bool same_cycle(
const Result & a,
const Result & b)
1838 if (a.has_cycle != b.has_cycle or a.minimum_mean != b.minimum_mean
1839 or a.cycle_length != b.cycle_length or a.cycle_total_cost != b.cycle_total_cost)
1841 auto i = a.cycle_arcs.get_it();
1842 auto j = b.cycle_arcs.get_it();
1843 for (; i.has_curr()
and j.has_curr(); i.next_ne(), j.next_ne())
1844 if (i.get_curr() != j.get_curr())
1846 return not i.has_curr()
and not j.has_curr();
1851 template <
class GT,
typename T,
class SA>
1852 void run_means(Mean_Checks &
k, Case<GT, T> & c,
const SA & sa)
1854 using D = Spec_Weight<GT, T>;
1855 const GT & g = c.b.g;
1856 const Spec & s = c.s;
1860 if (
auto e = mean_error(c,
r); not e.empty())
1861 k.karp.fail(s, c.label + e);
1865 if (v.has_cycle !=
r.has_cycle or v.minimum_mean !=
r.minimum_mean)
1866 k.karp_value.fail(s, c.label +
"gives " + text_of(v.minimum_mean) +
", and the witness "
1867 + text_of(
r.minimum_mean));
1870 if (
k.turn++ % 2 == 0)
1875 if (not same_cycle(a,
r) or b.has_cycle != v.has_cycle or b.minimum_mean != v.minimum_mean)
1876 k.aliases.fail(s, c.label +
"they do not give what karp_minimum_mean_cycle() gives");
1884 const auto a = f(g), b = f(g);
1885 const auto x = fv(g),
y = fv(g);
1886 if (not same_cycle(a,
r) or not same_cycle(b,
r) or x.has_cycle != v.has_cycle
1887 or x.minimum_mean != v.minimum_mean or
y.minimum_mean != v.minimum_mean)
1888 k.functors.fail(s, c.label +
"they do not give what the functions give");
1893 if (
auto e = mean_error(c,
h); not e.empty())
1894 k.howard.fail(s, c.label + e);
1895 else if (
auto e2 = howard_error(
h); not e2.empty())
1896 k.howard.fail(s, c.label + e2);
1898 k.howard_value.run();
1900 hv.has_cycle !=
h.has_cycle or hv.minimum_mean !=
h.minimum_mean)
1901 k.howard_value.fail(s, c.label +
"gives " + text_of(hv.minimum_mean) +
", and the witness "
1902 + text_of(
h.minimum_mean));
1905 k.howard_limit.run();
1911 k.howard_limit.fail(s, c.label +
"does not throw length_error");
1912 else if (not same_cycle(z,
h) or zv.minimum_mean !=
h.minimum_mean)
1913 k.howard_limit.fail(s, c.label +
"gives another answer");
1915 catch (
const std::length_error &)
1917 if (not
h.used_karp)
1918 k.howard_limit.fail(s, c.label +
"throws length_error without the fallback");
1923 const auto f = howard_detail::minimum_mean_cycle<GT, D, SA>(g, c.d, sa, 0);
1924 if (
auto e = mean_error(c, f); not e.empty())
1925 k.fallback.fail(s, c.label + e);
1926 else if (
auto e2 = howard_error(f); not e2.empty())
1927 k.fallback.fail(s, c.label + e2);
1928 else if (f.used_karp
and f.fallback_reason != Howard_Fallback_Reason::Iteration_Limit)
1929 k.fallback.fail(s, c.label +
"the reason of the fallback is not the iteration limit");
1930 else if (
const auto fv = howard_detail::minimum_mean_cycle<GT, D, SA, false>(g, c.d, sa, 0);
1931 fv.has_cycle != f.has_cycle or fv.minimum_mean != f.minimum_mean)
1932 k.fallback.fail(s, c.label +
"without the witness it gives " + text_of(fv.minimum_mean));
1935 k.fallback_limit.run();
1938 const auto z = howard_detail::minimum_mean_cycle<GT, D, SA>(g, c.d, sa, 0, 0);
1940 k.fallback_limit.fail(s, c.label +
"does not throw length_error");
1941 else if (not same_cycle(z, f))
1942 k.fallback_limit.fail(s, c.label +
"gives another cycle");
1944 catch (
const std::length_error &)
1946 if (not f.used_karp)
1947 k.fallback_limit.fail(s, c.label +
"throws length_error without the fallback");
1953 struct All_Pairs_Checks
1958 explicit All_Pairs_Checks(
const std::string & how =
"")
1959 : floyd(
"Floyd_All_Shortest_Paths::has_negative_cycle()" + how),
1960 johnson(
"Johnson, its constructor" + how)
1974 template <
class GT,
typename T,
class SA>
1975 void run_floyd(
Check &
k, Case<GT, T> & c,
const SA & sa)
1977 using D = Spec_Weight<GT, T>;
1978 const Spec & s = c.s;
1979 bool negative = c.t.negative;
1980 if (not c.b.g.is_digraph())
1987 k.fail(s, c.label +
"takes a graph without nodes");
1988 else if (f.has_negative_cycle() != negative)
1989 k.fail(s, c.label + says(f.has_negative_cycle()));
1993 const long zero = 0;
1994 f.get_min_path(zero, zero);
1995 k.fail(s, c.label +
"get_min_path() gives a path with a negative cycle");
1997 catch (
const std::domain_error &)
2002 catch (
const std::invalid_argument &)
2005 k.fail(s, c.label +
"throws invalid_argument");
2012 template <
class GT,
typename T,
class SA>
2013 void run_johnson(
Check &
k, Case<GT, T> & c,
const SA & sa)
2015 using D = Spec_Weight<GT, T>;
2021 k.fail(c.s, c.label +
"does not throw on a negative cycle");
2022 else if (not j.is_initialized())
2023 k.fail(c.s, c.label +
"is not initialized");
2025 catch (
const std::domain_error &)
2027 if (not c.t.negative)
2028 k.fail(c.s, c.label +
"throws domain_error without a negative cycle");
2030 catch (
const std::overflow_error & e)
2032 if (not c.may_overflow)
2033 k.fail(c.s, c.label + std::string(
"throws ") + e.what());
2035 catch (
const std::exception & e)
2037 k.fail(c.s, c.label + std::string(
"throws ") + e.what());
2043 const std::vector<long long> Limit_Weights = {std::numeric_limits<int>::lowest() / 2, -1,
2044 std::numeric_limits<int>::max() / 2 - 1,
2045 std::numeric_limits<int>::max() - 5};
2049 const std::vector<long long> Johnson_Limit_Weights = {std::numeric_limits<int>::lowest() / 2,
2051 std::numeric_limits<int>::max() - 5};
2056 const std::vector<long long> Johnson_Query_Weights = {std::numeric_limits<int>::lowest() / 2,
2058 std::numeric_limits<int>::max() - 5};
2063 constexpr long long Minus_Infinity = -(1LL << 56);
2064 constexpr long long Not_A_Number = 1LL << 56;
2067 const std::vector<long long> Infinite_Weights = {Minus_Infinity, Not_A_Number, -1, 1};
2070 unsigned long not_finite(
const Spec & s)
2072 unsigned long mask = 0;
2073 for (
size_t i = 0; i < s.
num_arcs(); ++i)
2074 if (s.
w[i] == Minus_Infinity or s.
w[i] == Not_A_Number)
2080 unsigned long not_a_number(
const Spec & s)
2082 unsigned long mask = 0;
2083 for (
size_t i = 0; i < s.
num_arcs(); ++i)
2084 if (s.
w[i] == Not_A_Number)
2092 template <
class GT,
typename T>
2093 bool reweighting_overflows(
const Case<GT, T> & c)
2095 const auto h = potentials(c.t, c.s.n);
2096 constexpr long long lo = std::numeric_limits<int>::lowest();
2097 constexpr long long hi = std::numeric_limits<int>::max();
2098 for (
size_t i = 0; i < c.s.num_arcs(); ++i)
2100 if (not c.show()[i])
2102 const long long first = c.s.w[i] +
h[size_t(c.s.arcs[i][0])];
2103 const long long last = first -
h[size_t(c.s.arcs[i][1])];
2104 if (first < lo or first > hi or last < lo or last > hi)
2113 std::map<typename GT::Arc *, int> weights_as_infos(
Built<GT> & b,
const Spec & s)
2115 std::map<typename GT::Arc *, int> numbers;
2116 for (
size_t i = 0; i < b.
arcs.size(); ++i)
2118 b.
arcs[i]->get_info() = int(s.
w[i]);
2119 numbers[b.
arcs[i]] = int(i);
2132 using GT = TypeParam;
2134 Check null_start(
"Bellman_Ford, the searches from a null start");
2136 for_each_weighted_spec([&] (
const Spec & s)
2138 Case<GT, int> c(s, shown_but(s, 0));
2147 Case<GT, int> c(t, shown_but(t, 0));
2148 BF<GT, int> bf(c.b.g, c.d);
2150 auto count = [&] (
auto && call)
2156 catch (
const std::domain_error &)
2162 count([&] { bf.paint_spanning_tree(
nullptr); });
2163 count([&] { bf.faster_paint_spanning_tree(
nullptr); });
2164 count([&] { bf.has_negative_cycle(
nullptr); });
2165 count([&] { bf.test_negative_cycle(
nullptr); });
2166 count([&] { bf.search_negative_cycle(
nullptr); });
2167 count([&] { bf.search_negative_cycle(
nullptr, 0.5, 1); });
2168 count([&] { bf.get_distance(
nullptr); });
2170 null_start.
fail(t, std::to_string(7 - thrown) +
" of 7 do not throw domain_error");
2179 using GT = TypeParam;
2180 Witness_Checks
k(
false,
"");
2182 for_each_weighted_spec([&] (
const Spec & s)
2184 Case<GT, int> c(s, shown_but(s, 0));
2193 using GT = TypeParam;
2194 Witness_Checks
k(
true,
"");
2196 for_each_weighted_spec([&] (
const Spec & s)
2198 Case<GT, int> c(s, shown_but(s, 0));
2207 using GT = TypeParam;
2208 Functor_Checks
k(
"");
2210 for_each_weighted_spec([&] (
const Spec & s)
2212 Case<GT, int> c(s, shown_but(s, 0));
2221 using GT = TypeParam;
2224 for_each_weighted_spec([&] (
const Spec & s)
2226 Case<GT, int> c(s, shown_but(s, 0));
2235 using GT = TypeParam;
2236 Check repaint(
"Bellman_Ford, a painting after another, from another start");
2237 Check after_search(
"Bellman_Ford, a painting after each search");
2238 Check after_tree(
"Bellman_Ford, a painting after build_tree(tree)");
2239 Check after_clear(
"Bellman_Ford::clear()");
2240 Check fresh_build(
"Bellman_Ford::build_tree(tree, false), on a new object");
2241 Check bits(
"Bellman_Ford, the bits Depth_First that a search leaves");
2242 Check cookies(
"Bellman_Ford, the cookies that a search leaves");
2249 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2251 Case<GT, int> c(s, shown_but(s, 0));
2252 BF<GT, int> bf(c.b.g, c.d);
2253 const int first = 0, last = s.
n - 1;
2255 check_unpainted_build(fresh_build, bf, s,
"");
2258 auto painting = [&] (
int u,
bool faster) -> std::string
2260 auto * p = c.b.nodes[size_t(u)];
2261 const bool negative =
2262 faster ? bf.faster_paint_spanning_tree(p) : bf.paint_spanning_tree(p);
2263 if (negative != c.t.negative_from[
size_t(u)])
2264 return from_text(u) + says(negative);
2266 return bf.is_painted() ? from_text(u) +
"is painted" :
"";
2267 if (bf.get_start_node() != p)
2268 return from_text(u) +
"has another start";
2269 if (
auto e = distances_error(bf, c, u); not e.empty())
2270 return from_text(u) + e;
2275 for (
auto [u, faster] : {std::pair{first,
false}, {last,
true}, {first,
true},
2277 if (
auto e = painting(u, faster); not e.empty())
2286 auto * p = c.b.nodes[size_t(first)];
2290 std::function<void()> call;
2292 const Search searches[] = {
2293 {
"has_negative_cycle(start)", [&] { bf.has_negative_cycle(p); }},
2294 {
"test_negative_cycle(start)", [&] { bf.test_negative_cycle(p); }},
2295 {
"search_negative_cycle(start)", [&] { bf.search_negative_cycle(p); }},
2296 {
"search_negative_cycle(start, 0.5, 1)", [&] { bf.search_negative_cycle(p, 0.5, 1); }},
2297 {
"has_negative_cycle()", [&] { bf.has_negative_cycle(); }},
2298 {
"test_negative_cycle()", [&] { bf.test_negative_cycle(); }},
2299 {
"search_negative_cycle()", [&] { bf.search_negative_cycle(); }},
2300 {
"search_negative_cycle(0.5, 1)", [&] { bf.search_negative_cycle(0.5, 1); }},
2301 {
"compute_nodes_weights()", [&]
2305 bf.compute_nodes_weights();
2307 catch (
const std::domain_error &)
2313 const Search &
search = searches[turn++ % std::size(searches)];
2315 const std::set<void *>
nodes(c.b.nodes.begin(), c.b.nodes.end());
2316 bool marked =
false, outside =
false;
2317 for (
auto * q : c.b.nodes)
2325 bits.fail(s, std::string(
"after ") +
search.name +
", a node keeps the bit");
2328 cookies.
fail(s, std::string(
"after ") +
search.name
2329 +
", a cookie points out of the graph");
2330 if (
auto e = painting(last,
false); not e.empty())
2331 after_search.
fail(s, std::string(
"after ") +
search.name +
", " + e);
2337 if (not c.t.negative_from[
size_t(first)]
and not c.t.negative_from[
size_t(last)])
2339 bf.paint_spanning_tree(p);
2341 bf.build_tree(tree);
2342 if (
auto e = painting(last,
false); not e.empty())
2343 after_tree.
fail(s, e);
2347 bf.build_tree(again,
false);
2348 if (
auto e2 = tree_error(s, again, painted_arcs(c.b)); not e2.empty())
2349 after_tree.
fail(s,
"build_tree(tree, false): " + e2);
2355 bf.paint_spanning_tree(p);
2357 bool painted =
false;
2358 for (
auto * q : c.b.nodes)
2360 for (
auto * a : c.b.arcs)
2365 bf.extract_min_spanning_tree();
2367 catch (
const std::domain_error &)
2375 catch (
const std::domain_error &)
2379 if (painted or bf.is_painted() or bf.has_computation()
2380 or bf.get_start_node() !=
nullptr or thrown != 2)
2381 after_clear.
fail(s,
"keeps some of the painting");
2396 using GT = TypeParam;
2397 Detection_Checks
k(
", hiding arcs");
2401 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2403 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
2405 Case<GT, int> c(s, shown_but(s, hidden));
2406 c.label =
"hiding " + std::to_string(hidden) +
": ";
2417 using GT = TypeParam;
2418 Witness_Checks from(
false,
", hiding arcs");
2419 Witness_Checks global(
true,
", hiding arcs");
2423 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2425 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
2427 Case<GT, int> c(s, shown_but(s, hidden));
2428 c.label =
"hiding " + std::to_string(hidden) +
": ";
2441 using GT = TypeParam;
2442 const std::string how =
", with a whitelist of numbers";
2443 Detection_Checks
k(how);
2444 Witness_Checks from(
false, how);
2445 Witness_Checks global(
true, how);
2449 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2451 for (
unsigned long shown = 0; shown < (1UL << s.
num_arcs()); ++shown)
2453 Case<GT, int> c(s, shown_only(s, shown));
2454 c.label =
"showing " + std::to_string(shown) +
": ";
2455 const Show_Mask<GT> sa{shown};
2456 run_detection(
k, c, sa);
2457 run_witnesses(from, c, sa,
false);
2458 run_witnesses(global, c, sa,
true);
2470 using GT = TypeParam;
2471 const std::string how =
", with a whitelist of pointers";
2472 Detection_Checks
k(how);
2473 Witness_Checks from(
false, how);
2474 Witness_Checks global(
true, how);
2478 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2480 for (
unsigned long shown = 0; shown < (1UL << s.
num_arcs()); ++shown)
2482 Case<GT, int> c(s, shown_only(s, shown));
2483 c.label =
"showing " + std::to_string(shown) +
": ";
2484 std::set<typename GT::Arc *>
arcs;
2485 for (
size_t i = 0; i < s.
num_arcs(); ++i)
2487 arcs.insert(c.b.arcs[i]);
2488 const Show_Arcs<GT> sa{&
arcs};
2489 run_detection(
k, c, sa);
2490 run_witnesses(from, c, sa,
false);
2491 run_witnesses(global, c, sa,
true);
2503 using GT = TypeParam;
2504 const std::string how =
", with double distances";
2505 Detection_Checks
k(how);
2506 Witness_Checks from(
false, how);
2507 Witness_Checks global(
true, how);
2508 Tree_Checks tree(how);
2512 for_each_weighting(shape, Unit_Weights, [&] (
const Spec & s)
2514 Case<GT, double> c(s, shown_but(s, 0));
2535 void for_each_spec_near_the_limits(
F && f)
2539 for_each_weighting(shape, Limit_Weights, [&] (
const Spec & s)
2541 f(s, sums_may_overflow(s, shown_but(s, 0), std::numeric_limits<int>::lowest(),
2542 std::numeric_limits<int>::max() - 1));
2550 using GT = TypeParam;
2551 const std::string how =
", near the limits of int";
2552 Detection_Checks
k(how);
2553 Witness_Checks from(
false, how);
2554 Witness_Checks global(
true, how);
2557 for_each_spec_near_the_limits([&] (
const Spec & s,
bool overflow)
2559 Case<GT, int> c(s, shown_but(s, 0));
2560 c.may_overflow = overflow;
2573 using GT = TypeParam;
2574 Tree_Checks
k(
", near the limits of int");
2577 for_each_spec_near_the_limits([&] (
const Spec & s,
bool overflow)
2579 Case<GT, int> c(s, shown_but(s, 0));
2580 c.may_overflow = overflow;
2589 using GT = TypeParam;
2590 const std::string how =
", on a path near the limits of int";
2591 Check painted(
"Bellman_Ford::paint_spanning_tree()" + how);
2595 Check distance(
"Bellman_Ford::get_distance()" + how);
2596 Check path(
"Bellman_Ford::get_min_path()" + how);
2597 size_t sums_from_the_end_leave_int = 0;
2598 constexpr long long lo = std::numeric_limits<int>::lowest();
2599 constexpr long long hi = std::numeric_limits<int>::max();
2602 for (
bool reversed : {
false,
true})
2606 shape.
arcs = {{0, 1}, {1, 2}, {2, 3}};
2608 std::reverse(shape.
arcs.begin(), shape.
arcs.end());
2609 for_each_weighting(shape, Limit_Weights, [&] (
const Spec & s)
2612 Case<GT, int> c(s, shown_but(s, 0));
2613 c.may_overflow =
true;
2614 BF<GT, int> bf(c.b.g, c.d);
2615 bool negative =
false;
2616 if (not returns(painted, bf, c,
"", [&]
2618 negative = bf.paint_spanning_tree(c.b.nodes[0]);
2624 painted.
fail(s,
"finds a negative cycle");
2629 auto weight = [&] (
int v)
2631 for (
size_t i = 0; i < s.
num_arcs(); ++i)
2632 if (s.
arcs[i][1] == v)
2636 for (
int v = 1; v < s.
n; ++v)
2641 for (
int x = v; x > 0; --x)
2644 back = back or sum < lo or sum > hi;
2646 const long long want = c.t.dist[0][size_t(v)];
2647 sums_from_the_end_leave_int += back;
2651 if (
const int got = bf.get_distance(c.b.nodes[
size_t(v)]); got != want)
2652 distance.
fail(s,
"gives " + std::to_string(got) +
" to " + std::to_string(v)
2653 +
", not " + std::to_string(want));
2655 catch (
const std::overflow_error & e)
2657 distance.
fail(s,
"to " + std::to_string(v) +
": throws " + e.what());
2661 const int got = bf.get_min_path(c.b.nodes[
size_t(v)], p);
2662 if (
auto e = min_path_error(c, p, 0, v, got); not e.empty())
2671 EXPECT_GT(sums_from_the_end_leave_int, 0u);
2680 template <
class GT,
class Show,
class F>
2681 void for_each_infinite_case(Show && show,
F && f)
2685 for_each_weighting(shape, Infinite_Weights, [&] (
const Spec & s)
2687 Case<GT, double> c(s, show(s));
2688 for (
size_t i = 0; i < s.
num_arcs(); ++i)
2689 if (s.
w[i] == Minus_Infinity)
2690 c.w[i] = -std::numeric_limits<double>::infinity();
2691 else if (s.
w[i] == Not_A_Number)
2692 c.w[i] = std::numeric_limits<double>::quiet_NaN();
2701 using GT = TypeParam;
2702 const std::string how =
", with weights of minus infinity or NaN";
2703 Detection_Checks
k(how);
2704 Witness_Checks from(
false, how);
2705 Witness_Checks global(
true, how);
2706 Tree_Checks tree(how);
2712 for_each_infinite_case<GT>([] (
const Spec & s) {
return shown_but(s, not_a_number(s)); },
2713 [&] (
const Spec & s, Case<GT, double> & c)
2715 c.may_reject = not_finite(s) != 0;
2716 c.filtered = shown_but(s, 0);
2731 using GT = TypeParam;
2732 const std::string how =
", with weights of minus infinity or NaN hidden by the filter";
2733 Detection_Checks
k(how);
2734 Witness_Checks from(
false, how);
2735 Witness_Checks global(
true, how);
2737 for_each_infinite_case<GT>([] (
const Spec & s) {
return shown_but(s, not_finite(s)); },
2738 [&] (
const Spec & s, Case<GT, double> & c)
2740 const unsigned long bad = not_finite(s);
2743 c.label =
"hiding " + std::to_string(bad) +
": ";
2745 run_detection(
k, c, sa);
2746 run_witnesses(from, c, sa,
false);
2747 run_witnesses(global, c, sa,
true);
2757 using GT = TypeParam;
2758 const std::string how =
", on random digraphs";
2759 Detection_Checks
k(how);
2760 Witness_Checks from(
false, how);
2761 Witness_Checks global(
true, how);
2762 Tree_Checks tree(how);
2765 for (
int round = 0; round < (
heavy() ? 400 : 40); ++round)
2767 const int n = 6 + int(
rng() % 3);
2770 Case<GT, int> c(s, shown_but(s, 0));
2790 using GT = TypeParam;
2793 for_each_weighted_spec([&] (
const Spec & s)
2795 Case<GT, int> c(s, shown_but(s, 0));
2804 using GT = TypeParam;
2805 Search_Checks
k(
", hiding arcs");
2809 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2811 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
2813 Case<GT, int> c(s, shown_but(s, hidden));
2814 c.label =
"hiding " + std::to_string(hidden) +
": ";
2825 using GT = TypeParam;
2826 Search_Checks
k(
", with double costs");
2828 for_each_weighted_spec([&] (
const Spec & s)
2830 Case<GT, double> c(s, shown_but(s, 0));
2839 using GT = TypeParam;
2842 for_each_weighted_spec([&] (
const Spec & s)
2844 Case<GT, int> c(s, shown_but(s, 0));
2853 using GT = TypeParam;
2854 Mean_Checks
k(
", hiding arcs");
2858 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2860 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
2862 Case<GT, int> c(s, shown_but(s, hidden));
2863 c.label =
"hiding " + std::to_string(hidden) +
": ";
2874 using GT = TypeParam;
2875 Mean_Checks
k(
", with double costs");
2877 for_each_weighted_spec([&] (
const Spec & s)
2879 Case<GT, double> c(s, shown_but(s, 0));
2891 template <
class GT,
class Run>
2892 void check_all_pairs_detection(
Check & ints,
Check & doubles,
Check & hiding,
Run && run)
2894 for_each_weighted_spec([&] (
const Spec & s)
2896 Case<GT, int> c(s, shown_but(s, 0));
2902 for_each_weighting(shape, Unit_Weights, [&] (
const Spec & s)
2904 Case<GT, double> c(s, shown_but(s, 0));
2907 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2909 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
2911 Case<GT, int> c(s, shown_but(s, hidden));
2912 c.label =
"hiding " + std::to_string(hidden) +
": ";
2922 using GT = TypeParam;
2923 const std::string name =
"Floyd_All_Shortest_Paths::has_negative_cycle()";
2925 Check doubles(name +
", with double costs");
2926 Check hiding(name +
", hiding arcs");
2928 check_all_pairs_detection<GT>(ints, doubles, hiding, [] (
Check &
k,
auto & c,
const auto & sa)
2930 run_floyd(
k, c, sa);
2940 using GT = TypeParam;
2941 const std::string name =
"Johnson, its constructor";
2943 Check doubles(name +
", with double costs");
2944 Check hiding(name +
", hiding arcs");
2946 check_all_pairs_detection<GT>(ints, doubles, hiding, [] (
Check &
k,
auto & c,
const auto & sa)
2948 run_johnson(
k, c, sa);
2958 using GT = TypeParam;
2959 const std::string how =
", with a whitelist of numbers";
2960 Check floyd(
"Floyd_All_Shortest_Paths::has_negative_cycle()" + how);
2961 Check johnson(
"Johnson, its constructor" + how);
2965 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
2967 for (
unsigned long shown = 0; shown < (1UL << s.
num_arcs()); ++shown)
2969 Case<GT, int> c(s, shown_only(s, shown));
2970 c.label =
"showing " + std::to_string(shown) +
": ";
2971 const Show_Mask<GT> sa{shown};
2972 run_floyd(floyd, c, sa);
2973 run_johnson(johnson, c, sa);
2986 template <
class GT,
typename T>
2987 bool least_means_fit(
const Case<GT, T> & c)
2989 long long num = 0, den = 1;
2990 if (not least_mean(c.t,
num, den))
2992 for (
const auto & cy : c.t.cycles)
2993 if (cy.cost * den ==
num * (long long) cy.
arcs.
size()
2994 and (cy.cost <
std::numeric_limits<int>::lowest()
2995 or cy.cost >
std::numeric_limits<int>::
max()))
3003 void unless_it_overflows(
Check &
k,
const Spec & s,
bool allowed,
const std::string & what,
3010 catch (
const std::overflow_error & e)
3013 k.fail(s, what +
": throws " + e.what());
3020 using GT = TypeParam;
3021 const std::string how =
", near the limits of int";
3022 Check disjoint(
"find_disjoint_negative_cycles()" + how);
3023 Check bounded(
"most_negative_cycle_bounded()" + how);
3024 Check short_cycle(
"most_negative_cycle_up_to_3()" + how);
3025 Check karp(
"karp_minimum_mean_cycle() and its value" + how);
3026 Check howard(
"howard_minimum_mean_cycle() and its value" + how);
3030 for_each_weighting(shape, Johnson_Limit_Weights, [&] (
const Spec & s)
3032 Case<GT, int> c(s, shown_but(s, 0));
3033 const GT & g = c.b.g;
3036 const bool may_overflow = sums_may_overflow(s, c.show(),
3037 std::numeric_limits<int>::lowest(),
3038 std::numeric_limits<int>::max());
3039 unless_it_overflows(disjoint, s, may_overflow,
"up to 64", [&]
3044 if (
auto e = disjoint_error(c, items, 64, Negative_Cycle_Exclusion::Min_Weight_Arc);
3046 disjoint.
fail(s, e);
3049 for (
size_t len : {size_t(1), size_t(2), size_t(3)})
3051 const std::string up_to =
"up to " + std::to_string(len) +
" arcs";
3052 const bool walks_overflow =
3053 walks_may_overflow(s, c.show(), len, std::numeric_limits<int>::lowest(),
3054 std::numeric_limits<int>::max());
3055 unless_it_overflows(bounded, s, walks_overflow, up_to, [&]
3060 bounded.fail(s, up_to +
": " + e);
3064 long long least = 0;
3066 const bool has = least_cost(c.t, len, least, fewest);
3067 const bool out = has
and (least < std::numeric_limits<int>::lowest()
3068 or least > std::numeric_limits<int>::max());
3069 unless_it_overflows(short_cycle, s,
out, up_to, [&]
3074 short_cycle.
fail(s, up_to +
": gives a total out of int");
3075 else if (
auto e = short_error(c,
r, len); not e.empty())
3076 short_cycle.
fail(s, up_to +
": " + e);
3081 const bool fit = least_means_fit(c);
3082 unless_it_overflows(karp, s, not fit,
"karp_minimum_mean_cycle()", [&]
3088 unless_it_overflows(karp, s,
false,
"karp_minimum_mean_cycle_value()", [&]
3093 long long num = 0, den = 1;
3094 const long double mean = least_mean_value(c, has,
num, den);
3095 if (
r.has_cycle != has or
r.minimum_mean !=
mean)
3096 karp.
fail(s,
"the value gives " + text_of(
r.minimum_mean));
3098 unless_it_overflows(howard, s, not fit,
"howard_minimum_mean_cycle()", [&]
3102 if (
auto e = mean_error(c,
r); not e.empty())
3104 else if (
auto e2 = howard_error(
r); not e2.empty())
3107 unless_it_overflows(howard, s,
false,
"howard_minimum_mean_cycle_value()", [&]
3112 long long num = 0, den = 1;
3113 const long double mean = least_mean_value(c, has,
num, den);
3114 if (
r.has_cycle != has or
r.minimum_mean !=
mean)
3115 howard.
fail(s,
"the value gives " + text_of(
r.minimum_mean));
3132 struct Johnson_Answer_Checks
3142 explicit Johnson_Answer_Checks(
const std::string & how)
3143 :
build(
"Johnson, its constructor" + how),
3144 distance(
"Johnson::get_distance()" + how),
3145 path(
"Johnson::find_path()" + how),
3146 all_pairs(
"Johnson::compute_all_pairs_distances()" + how),
3147 potential(
"Johnson::get_potential()" + how),
3148 copy(
"Johnson::get_reweighted_graph() and get_copy_node()" + how),
3149 graph(
"Johnson, the graph that it takes" + how)
3154 for (
const Check *
k : {&
build, &distance, &path, &all_pairs, &potential, &
copy, &graph})
3164 using Of = std::tuple<int, std::vector<bool>, long,
void *>;
3166 std::vector<Of>
nodes;
3167 std::vector<Of>
arcs;
3169 explicit Attributes(
const Built<GT> & b)
3173 std::vector<bool>
r;
3175 r.push_back(f.get_bit(i));
3178 for (
auto * p : b.
nodes)
3180 for (
auto * a : b.
arcs)
3184 bool operator == (
const Attributes &)
const =
default;
3188 template <
typename T>
3189 T unreached_by_johnson()
3191 if constexpr (std::numeric_limits<T>::has_infinity)
3192 return std::numeric_limits<T>::infinity();
3194 return std::numeric_limits<T>::max();
3200 template <
class GT,
typename T>
3201 std::string johnson_path_error(
const Case<GT, T> & c,
const Path<GT> & path,
int u,
int v)
3203 const Walk
w = walk_of(path);
3204 if (
auto e = walk_error(c.s,
w, c.show()); not e.empty())
3206 if (
w.nodes.front() != u or
w.nodes.back() != v)
3207 return "the path " +
w.str() +
" does not go from " + std::to_string(u) +
" to "
3208 + std::to_string(v);
3209 if (
const long long want = c.t.dist[
size_t(u)][
size_t(v)]; cost_of(c.s,
w) != want)
3210 return "the path " +
w.str() +
" costs " + std::to_string(cost_of(c.s,
w)) +
", not "
3211 + std::to_string(want);
3218 void guarded_query(
Check &
k,
const Spec & s,
const std::string & where,
F && f)
3223 if (
const std::string e = f(); not e.empty())
3224 k.fail(s, where + e);
3226 catch (
const std::exception & e)
3228 k.fail(s, where +
"throws " + e.what());
3236 template <
class GT,
typename T,
class SA>
3237 void check_johnson_answers(Johnson_Answer_Checks &
k, Case<GT, T> & c,
const SA & sa,
3241 const Spec & s = c.s;
3242 const Attributes<GT> before(c.b);
3243 auto want = [&] (
int u,
int v)
3245 const long long x = c.t.dist[size_t(u)][size_t(v)];
3246 return x ==
oracle::Inf ? unreached_by_johnson<T>() :
T(x) * scale;
3250 std::unique_ptr<Solver> j;
3253 j = std::make_unique<Solver>(c.b.g, c.d, sa);
3255 catch (
const std::exception & e)
3257 k.build.fail(s, c.label +
"throws " + e.what());
3261 for (
int u = 0; u < s.
n; ++u)
3262 for (
int v = 0; v < s.
n; ++v)
3264 auto * p = c.b.nodes[size_t(u)];
3265 auto * q = c.b.nodes[size_t(v)];
3266 const std::string where = c.label +
"from " + std::to_string(u) +
" to "
3267 + std::to_string(v) +
": ";
3268 guarded_query(
k.distance, s, where, [&]
3270 const T got = j->get_distance(p, q);
3271 return got == want(u, v) ? std::string()
3272 :
"gives " + text_of(got) +
", not " + text_of(want(u, v));
3274 guarded_query(
k.path, s, where, [&]
3276 const Path<GT> path = j->find_path(p, q);
3277 if (c.t.dist[size_t(u)][size_t(v)] != oracle::Inf)
3278 return johnson_path_error(c, path, u, v);
3279 return path.is_empty() ? std::string() : std::string(
"gives a path to a node not reached");
3283 guarded_query(
k.all_pairs, s, c.label, [&]
3285 const auto all = j->compute_all_pairs_distances();
3286 if (all.size() != size_t(s.n) * size_t(s.n))
3287 return std::string(
"lacks pairs");
3288 for (int u = 0; u < s.n; ++u)
3289 for (int v = 0; v < s.n; ++v)
3291 const auto * x = all.search({c.b.nodes[size_t(u)], c.b.nodes[size_t(v)]});
3292 if (x == nullptr or x->second != want(u, v))
3293 return
"from " + std::to_string(u) +
" to " + std::to_string(v) +
" gives "
3294 + (x == nullptr ? std::string(
"nothing") : text_of(x->second));
3296 return std::string();
3299 const auto h = potentials(c.t, s.
n);
3300 guarded_query(
k.potential, s, c.label, [&]
3302 for (int v = 0; v < s.n; ++v)
3303 if (const T got = j->get_potential(c.b.nodes[size_t(v)]);
3304 got != T(h[size_t(v)]) * scale)
3305 return
"of " + std::to_string(v) +
" gives " + text_of(got);
3306 return std::string();
3312 guarded_query(
k.copy, s, c.label, [&]
3314 const GT & r = std::as_const(*j).get_reweighted_graph();
3315 std::multiset<std::tuple<int, int, int>> wanted, got;
3316 for (size_t i = 0; i < s.num_arcs(); ++i)
3318 const int x = s.arcs[i][0], y = s.arcs[i][1];
3319 const T w = (T(s.w[i]) + T(h[size_t(x)]) - T(h[size_t(y)])) * scale;
3320 wanted.insert({x, y, c.show()[i] ? static_cast<int>(w) : int(i)});
3322 for (
auto it =
r.get_arc_it(); it.has_curr(); it.next_ne())
3324 auto * a = it.get_curr_ne();
3325 got.insert({
num(
r.get_src_node(a)),
num(
r.get_tgt_node(a)), a->get_info()});
3327 if (j->get_reweighted_graph().get_num_nodes() != size_t(s.
n) or got != wanted)
3328 return
std::
string("the
copy is not the graph with the new weights");
3329 for (
int v = 0; v < s.n; ++v)
3330 if (auto * p = j->get_copy_node(c.b.
nodes[
size_t(v)]); p ==
nullptr or
num(p) != v)
3332 return
std::
string();
3336 if (not (Attributes<
GT>(c.b) == before))
3337 k.graph.fail(s, c.label + "the graph changed");
3343 struct Johnson_Limit_Checks
3349 size_t saturated = 0;
3351 explicit Johnson_Limit_Checks(
const std::string & how)
3352 : distance(
"Johnson::get_distance()" + how),
3353 path(
"Johnson::find_path()" + how),
3354 all_pairs(
"Johnson::compute_all_pairs_distances()" + how),
3355 copy(
"Johnson::get_reweighted_graph()" + how)
3373 bool check_johnson_queries_near_the_limits(Johnson_Limit_Checks &
k, Case<GT, int> & c)
3376 constexpr long long lo = std::numeric_limits<int>::lowest();
3377 constexpr long long hi = std::numeric_limits<int>::max();
3378 const Spec & s = c.s;
3379 std::unique_ptr<Solver> j;
3382 j = std::make_unique<Solver>(c.b.g, c.d);
3384 catch (
const std::overflow_error &)
3389 auto fits = [&] (
int u,
int v)
3391 const long long d = c.t.dist[size_t(u)][size_t(v)];
3394 auto want = [&] (
int u,
int v)
3396 const long long d = c.t.dist[size_t(u)][size_t(v)];
3397 return d ==
oracle::Inf ? std::numeric_limits<int>::max() : int(d);
3400 bool all_fit =
true;
3401 for (
int u = 0; u < s.
n; ++u)
3402 for (
int v = 0; v < s.
n; ++v)
3404 all_fit = all_fit
and fits(u, v);
3405 auto * p = c.b.nodes[size_t(u)];
3406 auto * q = c.b.nodes[size_t(v)];
3407 const std::string where =
"from " + std::to_string(u) +
" to " + std::to_string(v)
3412 const int got = j->get_distance(p, q);
3413 if (not fits(u, v) or got != want(u, v))
3414 k.distance.fail(s, where +
"gives " + std::to_string(got));
3416 catch (
const std::overflow_error & e)
3419 k.distance.fail(s, where +
"throws " + e.what());
3425 const Path<GT> path = j->find_path(p, q);
3427 k.path.
fail(s, where +
"gives a path of a distance beyond int");
3428 else if (c.t.dist[
size_t(u)][
size_t(v)] ==
oracle::Inf)
3431 k.path.fail(s, where +
"gives a path to a node not reached");
3433 else if (
auto e = johnson_path_error(c, path, u, v); not e.empty())
3434 k.path.fail(s, where + e);
3436 catch (
const std::overflow_error & e)
3439 k.path.fail(s, where +
"throws " + e.what());
3446 const auto all = j->compute_all_pairs_distances();
3448 k.all_pairs.fail(s,
"gives distances beyond int");
3450 for (
int u = 0; u < s.
n; ++u)
3451 for (
int v = 0; v < s.
n; ++v)
3452 if (
const auto * x =
all.search({c.b.nodes[size_t(u)], c.b.nodes[size_t(v)]});
3453 x ==
nullptr or x->second != want(u, v))
3454 k.all_pairs.fail(s,
"from " + std::to_string(u) +
" to " + std::to_string(v)
3455 +
": a wrong distance");
3457 catch (
const std::overflow_error & e)
3460 k.all_pairs.fail(s, std::string(
"throws ") + e.what());
3464 const auto h = potentials(c.t, s.
n);
3465 std::multiset<std::tuple<int, int, long long>> wanted, got;
3466 bool saturated =
false;
3467 for (
size_t i = 0; i < s.
num_arcs(); ++i)
3469 const int x = s.
arcs[i][0],
y = s.
arcs[i][1];
3470 const long long w = s.
w[i] +
h[size_t(x)] -
h[size_t(
y)];
3471 saturated = saturated or
w > hi;
3472 wanted.insert({x,
y, std::min(
w, hi)});
3474 k.saturated += saturated;
3475 const GT &
r = j->get_reweighted_graph();
3476 for (
auto it =
r.get_arc_it(); it.has_curr(); it.next_ne())
3478 auto * a = it.get_curr_ne();
3479 got.insert({
num(
r.get_src_node(a)),
num(
r.get_tgt_node(a)), (
long long) a->get_info()});
3482 k.copy.fail(s,
"the copy is not the graph with the new weights");
3490 using GT = TypeParam;
3491 const std::string how =
", near the limits of int";
3492 Check floyd(
"Floyd_All_Shortest_Paths::has_negative_cycle()" + how);
3493 Check johnson(
"Johnson, its constructor" + how);
3498 Check reweighting(
"Johnson, its constructor" + how +
", reweighting beyond int");
3499 size_t reweighted_beyond = 0;
3503 for_each_weighting(shape, Johnson_Limit_Weights, [&] (
const Spec & s)
3505 Case<GT, int> c(s, shown_but(s, 0));
3513 catch (
const std::overflow_error &)
3515 const long long least = *std::min_element(s.
w.begin(), s.
w.end());
3516 if (s.
n < 2 or least >= std::numeric_limits<int>::lowest() / (2 * (s.
n - 1)))
3517 floyd.
fail(s,
"throws overflow_error");
3522 c.may_overflow = sums_may_overflow(s, c.show(), std::numeric_limits<int>::lowest(),
3523 std::numeric_limits<int>::max() - 1);
3524 const bool reweighted = not c.may_overflow
and not c.t.negative
3525 and reweighting_overflows(c);
3526 reweighted_beyond += reweighted;
3534 EXPECT_GT(reweighted_beyond, 0U);
3539 using GT = TypeParam;
3544 Johnson_Limit_Checks queries(
", near the limits of int");
3549 for_each_weighting(shape, Johnson_Query_Weights, [&] (
const Spec & s)
3551 Case<GT, int> c(s, shown_but(s, 0));
3552 if (not c.t.negative
and check_johnson_queries_near_the_limits(queries, c))
3558 EXPECT_GT(beyond, 0U);
3559 EXPECT_GT(queries.saturated, 0U);
3564 using GT = TypeParam;
3572 Johnson_Answer_Checks numbers(
", with a whitelist of numbers");
3573 Johnson_Answer_Checks pointers(
", with a whitelist of pointers");
3577 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
3579 for (
unsigned long shown = 0; shown < (1UL << s.
num_arcs()); ++shown)
3581 Case<GT, int> c(s, shown_only(s, shown));
3584 c.label =
"showing " + std::to_string(shown) +
": ";
3585 check_johnson_answers(numbers, c, Show_Mask<GT>{shown});
3586 std::set<typename GT::Arc *>
arcs;
3587 for (
size_t i = 0; i < s.
num_arcs(); ++i)
3589 arcs.insert(c.b.arcs[i]);
3590 check_johnson_answers(pointers, c, Show_Arcs<GT>{&
arcs});
3601 using GT = TypeParam;
3606 Johnson_Answer_Checks halves(
", with the halves of the weights as double costs");
3608 for_each_weighted_spec([&] (
const Spec & s)
3610 Case<GT, double> c(s, shown_but(s, 0));
3613 for (
double & x : c.w)
3623 using GT = TypeParam;
3624 const std::string how =
", on random digraphs";
3625 Search_Checks searches(how);
3626 Mean_Checks means(how);
3627 All_Pairs_Checks all_pairs(how);
3630 for (
int round = 0; round < (
heavy() ? 1000 : 100); ++round)
3632 const int n = 6 + int(
rng() % 3);
3635 Case<GT, int> c(s, shown_but(s, 0));
3649 using GT = TypeParam;
3650 Check bf(
"Bellman_Ford<GT>");
3651 Check others(
"the searches with Dft_Dist and Dft_Show_Arc");
3655 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
3657 Case<GT, int> c(s, shown_but(s, 0));
3658 const auto numbers = weights_as_infos(c.b, s);
3659 auto number = [&] (
typename GT::Arc * a)
3661 const auto it = numbers.find(a);
3662 return it == numbers.end() ? -1 : it->second;
3665 auto right = [&] (
const Path<GT> & path,
bool want)
3669 const Walk
w = walk_of(path, number);
3670 return want
and closed_walk_error(s,
w, c.show(),
true).empty()
3671 and cost_of(s,
w) < 0;
3676 for (
int u = 0; u < s.
n; ++u)
3678 auto * p = c.b.nodes[size_t(u)];
3679 const bool want = c.t.negative_from[size_t(u)];
3686 or not right(path, want))
3687 k.fail(s, from_text(u) +
"a wrong answer");
3695 or not right(path, c.t.negative))
3696 k.fail(s,
"a wrong answer in the whole graph");
3701 bool has_mean =
false;
3702 long long num = 0, den = 1;
3703 const long double mean = least_mean_value(c, has_mean,
num, den);
3704 long long least = 0;
3706 const bool has_short = least_cost(c.t, 3, least, fewest);
3711 bool johnson_throws =
false;
3716 catch (
const std::domain_error &)
3718 johnson_throws =
true;
3720 if (karp.has_cycle != has_mean or karp.minimum_mean !=
mean
3721 or howard.has_cycle != has_mean or howard.minimum_mean !=
mean
3722 or three.has_cycle != has_short or (has_short
and three.total_cost != least)
3723 or negative_bounded != (has_short
and least < 0)
3727 or johnson_throws != c.t.negative)
3728 others.
fail(s,
"a wrong answer");
3743 using GT = TypeParam;
3744 Detection_Checks
k(
", not directed");
3746 for_each_weighted_spec([&] (
const Spec & s)
3748 Case<GT, int> c(s, shown_but(s, 0));
3757 using GT = TypeParam;
3758 Witness_Checks
k(
false,
", not directed");
3760 for_each_weighted_spec([&] (
const Spec & s)
3762 Case<GT, int> c(s, shown_but(s, 0));
3771 using GT = TypeParam;
3772 Witness_Checks
k(
true,
", not directed");
3774 for_each_weighted_spec([&] (
const Spec & s)
3776 Case<GT, int> c(s, shown_but(s, 0));
3785 using GT = TypeParam;
3786 Tree_Checks
k(
", not directed");
3788 for_each_weighted_spec([&] (
const Spec & s)
3790 Case<GT, int> c(s, shown_but(s, 0));
3799 using GT = TypeParam;
3800 Functor_Checks
k(
", not directed, with Out_Iterator");
3802 for_each_weighted_spec([&] (
const Spec & s)
3804 Case<GT, int> c(s, shown_but(s, 0));
3813 using GT = TypeParam;
3814 Functor_Checks
k(
", not directed");
3816 for_each_weighted_spec([&] (
const Spec & s)
3818 Case<GT, int> c(s, shown_but(s, 0));
3827 using GT = TypeParam;
3828 Check rejected(
"Negative_Cycles.H, Karp and Howard, on a graph that is not directed");
3829 Check empty(
"Negative_Cycles.H, with max_cycles or max_length 0,"
3830 " on a graph that is not directed");
3836 Case<GT, int> c(s, shown_but(s, 0));
3837 const GT & g = c.b.g;
3839 auto count = [&] (
auto && call)
3845 catch (
const std::domain_error &)
3859 rejected.
fail(s, std::to_string(7 - thrown) +
" of 7 do not throw domain_error");
3867 empty.
fail(s,
"a cycle");
3880 std::string undirected_path_error(
const Case<GT, int> & c,
const Path<GT> & path,
int u,
3881 int v,
long long want)
3883 const Walk
w = walk_of(path);
3884 if (
w.nodes.empty() or
w.nodes.size() !=
w.arcs.size() + 1)
3885 return "the path " +
w.str() +
" is empty or lacks arcs";
3886 if (
w.nodes.front() != u or
w.nodes.back() != v)
3887 return "the path " +
w.str() +
" does not go from " + std::to_string(u) +
" to "
3888 + std::to_string(v);
3890 for (
size_t i = 0; i <
w.arcs.size(); ++i)
3892 const auto & ends = c.s.arcs[size_t(
w.arcs[i])];
3893 const std::set<int> joined = {ends[0], ends[1]}, walked = {
w.nodes[i],
w.nodes[i + 1]};
3894 if (joined != walked)
3895 return "the path " +
w.str() +
" does not follow the arc " + std::to_string(
w.arcs[i]);
3896 if (not c.show()[
size_t(
w.arcs[i])])
3897 return "the path " +
w.str() +
" takes a hidden arc";
3898 cost += c.s.w[size_t(
w.arcs[i])];
3900 return cost == want ?
"" :
"the path " +
w.str() +
" costs " + std::to_string(cost);
3907 template <
class GT,
class SA>
3908 void run_johnson_not_directed(
Check &
k, Case<GT, int> & c,
const SA & sa)
3910 bool negative =
false;
3918 k.fail(c.s, c.label +
"does not throw on a negative cycle");
3921 for (
int u = 0; u < c.s.n; ++u)
3922 for (
int v = 0; v < c.s.n; ++v)
3924 const long long want = d[size_t(u)][size_t(v)];
3925 auto * p = c.b.nodes[size_t(u)];
3926 auto * q = c.b.nodes[size_t(v)];
3927 const std::string where = c.label +
"from " + std::to_string(u) +
" to "
3928 + std::to_string(v) +
": ";
3929 const int got = j.get_distance(p, q);
3930 if (got != (want ==
oracle::Inf ? std::numeric_limits<int>::max() : int(want)))
3932 const Path<GT> path = j.find_path(p, q);
3934 : not undirected_path_error(c, path, u, v, want).empty())
3935 k.fail(c.s, where +
"a wrong path");
3938 catch (
const std::domain_error &)
3941 k.fail(c.s, c.label +
"throws domain_error without a negative cycle");
3948 using GT = TypeParam;
3949 Check k(
"Johnson, not directed");
3950 Check hiding(
"Johnson, not directed, hiding arcs");
3952 for_each_weighted_spec([&] (
const Spec & s)
3954 Case<GT, int> c(s, shown_but(s, 0));
3960 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
3962 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
3964 Case<GT, int> c(s, shown_but(s, hidden));
3965 c.label =
"hiding " + std::to_string(hidden) +
": ";
3977 using GT = TypeParam;
3978 Check k(
"Floyd_All_Shortest_Paths::has_negative_cycle(), not directed");
3979 Check hiding(
"Floyd_All_Shortest_Paths::has_negative_cycle(), not directed, hiding arcs");
3981 for_each_weighted_spec([&] (
const Spec & s)
3983 Case<GT, int> c(s, shown_but(s, 0));
3989 for_each_weighting(shape, Sign_Weights, [&] (
const Spec & s)
3991 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
3993 Case<GT, int> c(s, shown_but(s, hidden));
3994 c.label =
"hiding " + std::to_string(hidden) +
": ";
Bellman-Ford algorithm for single-source shortest paths.
Floyd-Warshall algorithm for all-pairs shortest paths.
Minimum mean cycle by Howard's policy iteration.
Johnson's algorithm for all-pairs shortest paths.
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Bellman-Ford algorithm for shortest paths with negative weights.
bool paint_spanning_tree(Node *start)
Paint the shortest paths tree from a start node.
std::tuple< Path< GT >, size_t > search_negative_cycle(Node *start, double it_factor, const size_t step)
Searches a negative cycle using the faster version of Bellman-Ford algorithm and iteratively searchin...
bool has_negative_cycle(Node *start)
Test if a negative cycle exists starting from a specific node.
Path< GT > test_negative_cycle(Node *start)
Search a negative cycle on all possible paths starting from start node.
Bit fields for nodes and arcs used for marking visit state during processing.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
constexpr bool has_negative_cycle() const noexcept
Check if the graph contains a negative cycle.
Johnson's algorithm for all-pairs shortest paths.
Functor wrapper for value-only minimum mean cycle.
Functor wrapper for Karp minimum mean cycle.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Filtered iterator for outcoming arcs of a node.
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.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
constexpr size_t size() const noexcept
Returns the number of entries in the table.
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_SUITE(ExhaustiveBellmanFord, Directed_Kinds)
TYPED_TEST(ExhaustiveBellmanFord, DetectionMatchesTheOracle)
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
Min_Mean_Cycle_Value_Result minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle_value().
DynList< Negative_Cycle_Item< GT, typename Distance::Distance_Type > > find_disjoint_negative_cycles(const GT &g, const size_t max_cycles, const Negative_Cycle_Exclusion exclusion=Negative_Cycle_Exclusion::Min_Weight_Arc, Distance distance=Distance(), SA sa=SA())
Find up to max_cycles negative cycles, excluding arcs of each cycle found before searching for the ne...
#define ARC_COOKIE(p)
Return the arc cookie
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
#define NODE_COUNTER(p)
Get the counter of a node.
#define ARC_COUNTER(p)
Return the counter of arc p.
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle().
Min_Mean_Cycle_Value_Result karp_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute only the minimum mean value (without witness walk).
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
Negative_Cycle_Exclusion
Which arcs of a found negative cycle are excluded before searching for the next one in find_disjoint_...
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > karp_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute minimum mean cycle by Karp's algorithm.
Short_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_up_to_3(const GT &g, const size_t max_length=3, Distance distance=Distance(), SA sa=SA())
Minimum-total-cost simple cycle with at most one, two or three arcs.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > howard_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle by Howard's policy iteration.
Min_Mean_Cycle_Value_Result howard_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle value by Howard's policy iteration, without the witness.
#define NODE_BITS(p)
Get the control bits of a node.
@ All_Arcs
Exclude every arc of the cycle.
User-facing facade exported by the umbrella search header.
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
constexpr Word bounded(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word: the method of...
Main namespace for Aleph-w library functions.
@ Run
Execute the lowered HIR module with Interpreter_Runtime.
@ Call
dst <- callee(args...)
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
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.
@ Cycle
Graph has an Eulerian cycle.
and
Check uniqueness with explicit hash + equality functors.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
void next()
Advance all underlying iterators (bounds-checked).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
constexpr bool shown(unsigned long hidden, int i) noexcept
Whether the arc number i is shown by the mask hidden.
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
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.
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 reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
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.
Detects if a negative cycle exists and eventually computes it.
Default filter for filtered iterators on arcs.
Filtered iterator of adjacent arcs of a node.
Hides the arcs of a mask; copyable, assignable, noexcept.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
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)
void run_tree(unsigned long n, Gsl_Compatible_Rng &r)
Generic graph and digraph implementations.