Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Howard_Min_Mean_Cycle.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
175#ifndef HOWARD_MIN_MEAN_CYCLE_H
176#define HOWARD_MIN_MEAN_CYCLE_H
177
178#include <ah-graph-concepts.H>
179
180#include <cmath>
181#include <cstddef>
182#include <limits>
183#include <numeric>
184#include <type_traits>
185#include <utility>
186
187#include <ah-errors.H>
188#include <Min_Mean_Cycle.H>
189#include <Negative_Cycles.H>
190#include <shortest_path_common.H>
191#include <tpl_array.H>
192#include <tpl_graph.H>
193
194namespace Aleph {
213
226template <AlephGraph GT, typename Cost_Type>
243
248#if defined(__SIZEOF_INT128__) and not defined(_MSC_VER)
249#define ALEPH_HOWARD_INT128 1
250#else
251#define ALEPH_HOWARD_INT128 0
252#endif
253
254namespace howard_detail {
255#if ALEPH_HOWARD_INT128
258__extension__ typedef __int128 int128_t;
260__extension__ typedef unsigned __int128 uint128_t;
261#endif
262
265template <typename Cost_Type>
266inline constexpr bool exact_arithmetic_v = std::is_integral_v<Cost_Type> and
267 std::numeric_limits<Cost_Type>::max() <= std::numeric_limits<long long>::max();
268#if ALEPH_HOWARD_INT128
269template <>
270inline constexpr bool exact_arithmetic_v<unsigned long long> = true;
271template <>
272inline constexpr bool exact_arithmetic_v<int128_t> = true;
273#endif
274
282template <typename Cost_Type>
284{
285 using Wide = long long;
286 using Unsigned = unsigned long long;
287 static constexpr Wide max = std::numeric_limits<long long>::max();
288 static constexpr Wide min = std::numeric_limits<long long>::min();
289};
290
291#if ALEPH_HOWARD_INT128
293template <>
294struct Exact_Integer<int128_t>
295{
296 using Wide = int128_t;
297 using Unsigned = uint128_t;
298 static constexpr Wide max = static_cast<int128_t>(~uint128_t(0) >> 1);
299 static constexpr Wide min = -max - 1;
300};
301
305template <>
306struct Exact_Integer<unsigned long long> : Exact_Integer<int128_t>
307{
308};
309#endif
310
317template <typename Cost_Type, bool Exact>
319
322template <typename Cost_Type>
323struct Arithmetic<Cost_Type, true>
324{
327
330 struct Eta
331 {
332 Wide num = 0;
333 Wide den = 1;
334 };
335
337 using Bias = Wide;
338
341 using Sum = Wide;
342
344 static Sum zero_sum() noexcept
345 {
346 return 0;
347 }
348
350 static bool add(Sum &total, const Cost_Type &w) noexcept
351 {
352 return not add_overflows(total, static_cast<Wide>(w), total);
353 }
354
360 static Eta make_eta(const Sum &total, const size_t length) noexcept
361 {
362 const Wide num = total;
363 const Wide den = static_cast<Wide>(length);
364 const Unsigned magnitude =
365 num < 0 ? Unsigned(0) - static_cast<Unsigned>(num) : static_cast<Unsigned>(num);
366 const auto g = static_cast<Wide>(gcd(magnitude, static_cast<Unsigned>(den)));
367 return Eta{num / g, den / g};
368 }
369
371 static Bias zero_bias() noexcept
372 {
373 return 0;
374 }
375
377 static bool equal(const Eta &x, const Eta &y) noexcept
378 {
379 return x.num == y.num and x.den == y.den;
380 }
381
386 static int compare_fractions(Wide a1, Wide b1, Wide a2, Wide b2) noexcept
387 {
388 for (;;)
389 {
390 Wide q1 = a1 / b1;
391 Wide r1 = a1 % b1;
392 if (r1 < 0)
393 {
394 --q1;
395 r1 += b1;
396 }
397 Wide q2 = a2 / b2;
398 Wide r2 = a2 % b2;
399 if (r2 < 0)
400 {
401 --q2;
402 r2 += b2;
403 }
404
405 if (q1 != q2)
406 return q1 < q2 ? -1 : 1;
407 if (r1 == 0 or r2 == 0)
408 return r1 == 0 and r2 == 0 ? 0 : r1 == 0 ? -1 : 1;
409
410 // r1 / b1 < r2 / b2 <=> b2 / r2 < b1 / r1
411 const Wide na1 = b2;
412 const Wide nb1 = r2;
413 const Wide na2 = b1;
414 const Wide nb2 = r1;
415 a1 = na1;
416 b1 = nb1;
417 a2 = na2;
418 b2 = nb2;
419 }
420 }
421
423 static bool less(const Eta &x, const Eta &y) noexcept
424 {
425 return not equal(x, y) and compare_fractions(x.num, x.den, y.num, y.den) < 0;
426 }
427
437 static bool step(const Cost_Type &w, const Eta &e, const Bias next, Bias &out) noexcept
438 {
439 Wide scaled = 0;
440 if (multiply_overflows(e.den, static_cast<Wide>(w), scaled))
441 return false;
442 Wide shifted = 0;
443 if (subtract_overflows(scaled, e.num, shifted))
444 return false;
445 return not add_overflows(shifted, next, out);
446 }
447
449 static bool improves(const Bias candidate, const Bias current, const Eta &) noexcept
450 {
451 return candidate < current;
452 }
453
454private:
455 static constexpr Wide wide_max = Exact_Integer<Cost_Type>::max;
456 static constexpr Wide wide_min = Exact_Integer<Cost_Type>::min;
457
458 static Unsigned gcd(Unsigned a, Unsigned b) noexcept
459 {
460 while (b != 0)
461 {
462 const Unsigned r = a % b;
463 a = b;
464 b = r;
465 }
466 return a;
467 }
468
469 // The checked operations leave `out` untouched when they overflow. GCC and
470 // Clang check with their builtins, which avoid a division (a slow library
471 // call for 128-bit integers); other compilers use the portable tests.
472 static bool add_overflows(const Wide a, const Wide b, Wide &out) noexcept
473 {
474#if defined(__GNUC__)
475 Wide r;
476 if (__builtin_add_overflow(a, b, &r))
477 return true;
478 out = r;
479 return false;
480#else
481 if ((b > 0 and a > wide_max - b) or (b < 0 and a < wide_min - b))
482 return true;
483 out = a + b;
484 return false;
485#endif
486 }
487
488 static bool subtract_overflows(const Wide a, const Wide b, Wide &out) noexcept
489 {
490#if defined(__GNUC__)
491 Wide r;
492 if (__builtin_sub_overflow(a, b, &r))
493 return true;
494 out = r;
495 return false;
496#else
497 if ((b < 0 and a > wide_max + b) or (b > 0 and a < wide_min + b))
498 return true;
499 out = a - b;
500 return false;
501#endif
502 }
503
504 static bool multiply_overflows(const Wide a, const Wide b, Wide &out) noexcept
505 {
506 if constexpr (sizeof(Wide) > sizeof(long long))
507 {
508 // 128 bits. Clang turns __builtin_mul_overflow on them into a call to
509 // __muloti4, which only compiler-rt provides: with libgcc (Linux on
510 // AArch64, for instance) it does not link. Operands that fit 64 bits
511 // cannot overflow 127, which is the usual case here (a cycle length
512 // times a weight); the others take the portable test.
513 if (a == static_cast<long long>(a) and b == static_cast<long long>(b))
514 {
515 out = a * b;
516 return false;
517 }
518 return portable_multiply_overflows(a, b, out);
519 }
520 else
521 {
522#if defined(__GNUC__)
523 Wide r;
524 if (__builtin_mul_overflow(a, b, &r))
525 return true;
526 out = r;
527 return false;
528#else
529 return portable_multiply_overflows(a, b, out);
530#endif
531 }
532 }
533
534 static bool portable_multiply_overflows(const Wide a, const Wide b, Wide &out) noexcept
535 {
536 if (a == 0 or b == 0)
537 {
538 out = 0;
539 return false;
540 }
541 constexpr Wide hi = wide_max;
542 constexpr Wide lo = wide_min;
543 if (a > 0 ? (b > 0 ? a > hi / b : b < lo / a) : b > 0 ? a < lo / b : b < hi / a)
544 return true;
545 out = a * b;
546 return false;
547 }
548};
549
552template <typename Cost_Type>
553struct Arithmetic<Cost_Type, false>
554{
555 using Eta = long double;
556 using Bias = long double;
557
559 using Sum = long double;
560
562 static Sum zero_sum() noexcept
563 {
564 return 0.0L;
565 }
566
568 static bool add(Sum &total, const Cost_Type &w) noexcept
569 {
570 total += static_cast<long double>(w);
571 return std::isfinite(total);
572 }
573
575 static Eta make_eta(const Sum &total, const size_t length) noexcept
576 {
577 return total / static_cast<long double>(length);
578 }
579
581 static Bias zero_bias() noexcept
582 {
583 return 0.0L;
584 }
585
587 static bool equal(const Eta &x, const Eta &y) noexcept
588 {
589 return x == y;
590 }
591
593 static bool less(const Eta &x, const Eta &y) noexcept
594 {
595 return x < y;
596 }
597
605 static bool step(const Cost_Type &w, const Eta &e, const Bias next, Bias &out) noexcept
606 {
607 out = static_cast<long double>(w) - e + next;
608 return std::isfinite(out);
609 }
610
613 static bool improves(const Bias candidate, const Bias current, const Eta &e) noexcept
614 {
615 const long double scale = 1.0L + std::fabs(candidate) + std::fabs(current) + std::fabs(e);
616 return candidate < current - 64.0L * std::numeric_limits<long double>::epsilon() * scale;
617 }
618};
619
625template <typename Cost_Type>
627{
628 static constexpr bool exact = exact_arithmetic_v<Cost_Type>;
630 using Eta = typename Ar::Eta;
631 using Bias = typename Ar::Bias;
632
634 const size_t n_;
635 Array<size_t> policy_; // position in `adj_` of the chosen arc of each node
636 Array<Eta> eta_; // mean of the cycle each node reaches
637 Array<Bias> bias_; // bias of each node
638 Array<unsigned char> state_; // 0 unvisited, 1 on the current path, 2 evaluated
639 Array<size_t> path_; // scratch of `evaluate()`
640 bool overflow_ = false;
641
642 size_t successor(const size_t u) const
643 {
644 return adj_.tgt(policy_(u));
645 }
646
649 bool evaluate()
650 {
651 for (size_t i = 0; i < n_; ++i)
652 state_(i) = 0;
653
654 for (size_t s = 0; s < n_; ++s)
655 {
656 if (state_(s) != 0)
657 continue;
658
659 size_t top = 0;
660 size_t u = s;
661 while (state_(u) == 0)
662 {
663 state_(u) = 1;
664 path_(top++) = u;
665 u = successor(u);
666 }
667
668 if (state_(u) == 1) // the walk closed a new cycle through u
669 {
670 size_t j = top;
671 do
672 --j;
673 while (path_(j) != u);
674
675 // The cycle is path_(j), ..., path_(top - 1); each node leads to the
676 // next and the last one to the first.
677 const size_t length = top - j;
678 size_t root = 0; // offset in the cycle of its smallest node
679 for (size_t i = 1; i < length; ++i)
680 if (path_(j + i) < path_(j + root))
681 root = i;
682
683 // The root is the smallest node of the cycle, not the first one the
684 // walk met: the reference of the biases has to stay the same while the
685 // cycle does, or comparing biases across components can oscillate. For
686 // the same reason the total is summed in cyclic order from the root: a
687 // floating-point sum depends on the order, and the mean of a cycle must
688 // not depend on where the walk happened to enter it.
689 typename Ar::Sum total = Ar::zero_sum();
690 for (size_t i = 0; i < length; ++i)
691 if (not Ar::add(total, adj_.weight(policy_(path_(j + (root + i) % length)))))
692 return false;
693
694 const Eta e = Ar::make_eta(total, length);
695 const size_t root_node = path_(j + root);
696 eta_(root_node) = e;
697 bias_(root_node) = Ar::zero_bias();
698 state_(root_node) = 2;
699 Bias next = bias_(root_node);
700 for (size_t k = 1; k < length; ++k)
701 {
702 const size_t v = path_(j + (root + length - k) % length);
703 if (not Ar::step(adj_.weight(policy_(v)), e, next, bias_(v)))
704 return false;
705 eta_(v) = e;
706 state_(v) = 2;
707 next = bias_(v);
708 }
709 top = j;
710 }
711
712 // What is left of the path hangs from an evaluated node.
713 while (top > 0)
714 {
715 const size_t v = path_(--top);
716 const size_t t = successor(v);
717 eta_(v) = eta_(t);
718 if (not Ar::step(adj_.weight(policy_(v)), eta_(v), bias_(t), bias_(v)))
719 return false;
720 state_(v) = 2;
721 }
722 }
723 return true;
724 }
725
728 bool improve()
729 {
730 bool changed = false;
731
732 // First the mean: move to a successor with a smaller one.
733 for (size_t u = 0; u < n_; ++u)
734 {
735 size_t best = policy_(u);
736 size_t best_node = u;
737 for (size_t p = adj_.start(u); p < adj_.start(u + 1); ++p)
738 if (const size_t v = adj_.tgt(p); Ar::less(eta_(v), eta_(best_node)))
739 {
740 best = p;
741 best_node = v;
742 }
743 if (best != policy_(u))
744 {
745 policy_(u) = best;
746 changed = true;
747 }
748 }
749 if (changed)
750 return true;
751
752 // No mean can be improved: improve the bias among equal means.
753 for (size_t u = 0; u < n_; ++u)
754 {
755 size_t best = policy_(u);
756 Bias best_bias = bias_(u);
757 for (size_t p = adj_.start(u); p < adj_.start(u + 1); ++p)
758 {
759 const size_t v = adj_.tgt(p);
760 if (not Ar::equal(eta_(v), eta_(u)))
761 continue;
762
763 Bias candidate = Ar::zero_bias();
764 if (not Ar::step(adj_.weight(p), eta_(u), bias_(v), candidate))
765 {
766 overflow_ = true;
767 return false;
768 }
769 if (Ar::improves(candidate, best_bias, eta_(u)))
770 {
771 best = p;
772 best_bias = candidate;
773 }
774 }
775 if (best != policy_(u))
776 {
777 policy_(u) = best;
778 changed = true;
779 }
780 }
781 return changed;
782 }
783
784public:
786 enum class Status
787 {
788 Optimal,
790 Overflow
791 };
792
799 const Array<size_t> &initial)
800 : adj_(adj), n_(n), policy_(initial), eta_(n, Eta{}), bias_(n, Bias{}), state_(n, 0), path_(n, 0)
801 {
802 // empty
803 }
804
809 : adj_(adj), n_(n), policy_(n, 0), eta_(n, Eta{}), bias_(n, Bias{}), state_(n, 0), path_(n, 0)
810 {
811 // The initial policy takes the cheapest outgoing arc of each node.
812 for (size_t u = 0; u < n; ++u)
813 {
814 size_t best = adj.start(u);
815 for (size_t p = best + 1; p < adj.start(u + 1); ++p)
816 if (adj.weight(p) < adj.weight(best))
817 best = p;
818 policy_(u) = best;
819 }
820 }
821
827 Status solve(const size_t limit, size_t &iterations)
828 {
829 iterations = 0;
830 for (;;)
831 {
832 if (not evaluate())
833 return Status::Overflow;
834 if (not improve())
836 if (++iterations > limit)
838 }
839 }
840
842 size_t best_node() const
843 {
844 size_t best = 0;
845 for (size_t u = 1; u < n_; ++u)
846 if (Ar::less(eta_(u), eta_(best)))
847 best = u;
848 return best;
849 }
850
852 size_t chosen_arc(const size_t u) const
853 {
854 return policy_(u);
855 }
856
858 const Array<size_t> &policy() const noexcept
859 {
860 return policy_;
861 }
862
864 size_t next_node(const size_t u) const
865 {
866 return successor(u);
867 }
868};
869
871inline constexpr size_t iteration_slack = 64;
872
874inline constexpr size_t default_iterations = std::numeric_limits<size_t>::max();
875
883template <typename Cost_Type>
885 const Array<size_t> &policy, const size_t start)
886{
887 Array<unsigned char> seen(adj.start.size() - 1, 0);
888 size_t v = start;
889 while (seen(v) == 0)
890 {
891 seen(v) = 1;
892 v = adj.tgt(policy(v));
893 }
894 return v;
895}
896
916template <typename Int, typename Cost_Type>
918 const size_t limit, const Array<int> &low, const int low_min, Array<size_t> &policy,
919 size_t &first, size_t &iterations)
920{
922 exact_adj.start = adj.start;
923 exact_adj.arc = adj.arc;
924 exact_adj.tgt = adj.tgt;
925 exact_adj.weight = Array<Int>(adj.weight.size(), Int(0));
926 for (size_t p = 0; p < adj.weight.size(); ++p)
927 {
928 const Cost_Type w = adj.weight(p);
929 if (w == Cost_Type{0})
930 continue;
931 Int odd; // exact: an odd integer
932 if constexpr (std::numeric_limits<Cost_Type>::digits <= 64)
933 odd = static_cast<Int>(negative_cycles_detail::odd_significand(w));
934 else // wider significands (quadruple long double)
935 odd = static_cast<Int>(std::ldexp(std::fabs(w), -low(p)));
936 const Int magnitude = odd << (low(p) - low_min);
937 exact_adj.weight(p) = w < Cost_Type{0} ? -magnitude : magnitude;
938 }
939
940 Solver<Int> exact(exact_adj, n, policy);
941 size_t done = 0;
942 if (exact.solve(limit, done) != Solver<Int>::Status::Optimal)
943 return false;
944 policy = exact.policy();
945 first = cycle_entry(adj, policy, exact.best_node());
946 iterations = done;
947 return true;
948}
949
964template <typename Cost_Type>
966 const size_t limit, Array<size_t> &policy, size_t &first, size_t &iterations)
967{
968 bool any = false;
969 int low_min = 0;
970 int high_max = 0;
971 Array<int> low(adj.weight.size(), 0);
972 for (size_t p = 0; p < adj.weight.size(); ++p)
973 {
974 if (adj.weight(p) == Cost_Type{0})
975 continue;
976 int high = 0;
978 low_min = any ? std::min(low_min, low(p)) : low(p);
979 high_max = any ? std::max(high_max, high) : high;
980 any = true;
981 }
982 const int bits = any ? high_max - low_min + 1 : 1; // of the largest scaled weight
983
984 if (bits <= 62 and try_exact<long long>(adj, n, limit, low, low_min, policy, first, iterations))
985 return true;
986#if ALEPH_HOWARD_INT128
987 if (bits <= 126 and try_exact<int128_t>(adj, n, limit, low, low_min, policy, first, iterations))
988 return true;
989#endif
990 return false;
991}
992
1004template <typename Cost_Type>
1006 const Array<size_t> &policy, const size_t n)
1007{
1008 Array<unsigned char> state(n, 0); // 0 unseen, 1 on the current walk, 2 done
1009 Array<size_t> walk(n, 0);
1013 size_t best = n;
1014 size_t best_length = 0;
1015 for (size_t s = 0; s < n; ++s)
1016 {
1017 if (state(s) != 0)
1018 continue;
1019 size_t top = 0;
1020 size_t u = s;
1021 while (state(u) == 0)
1022 {
1023 state(u) = 1;
1024 walk(top++) = u;
1025 u = adj.tgt(policy(u));
1026 }
1027 if (state(u) == 1) // the walk closed a new cycle through u
1028 {
1029 sum.clear();
1030 size_t length = 0;
1031 size_t v = u;
1032 do
1033 {
1034 if (not sum.add(adj.weight(policy(v))))
1035 return n;
1036 ++length;
1037 v = adj.tgt(policy(v));
1038 } while (v != u);
1039
1040 bool better = best == n;
1041 if (not better)
1042 {
1043 // sum / length < best_sum / best_length, cross-multiplied
1044 difference.clear();
1045 if (not difference.add_multiple(sum, best_length) or
1046 not difference.add_multiple(best_sum, length, true))
1047 return n;
1048 better = difference.sign() < 0;
1049 }
1050 if (better)
1051 {
1052 best = u;
1053 best_length = length;
1054 std::swap(best_sum, sum);
1055 }
1056 }
1057 for (size_t i = 0; i < top; ++i)
1058 state(walk(i)) = 2;
1059 }
1060 return best;
1061}
1062
1067inline const char *fallback_reason_text(const Howard_Fallback_Reason reason) noexcept
1068{
1069 switch (reason)
1070 {
1072 return "iteration limit";
1074 return "arithmetic overflow";
1076 return "cycle cost out of range";
1078 break;
1079 }
1080 return "no fallback";
1081}
1082
1128template <bool With_Witness, AlephGraph GT, typename Cost_Type>
1132 const size_t max_bytes)
1133{
1136 constexpr size_t unset = std::numeric_limits<size_t>::max();
1137 const size_t n = ig.nodes.size();
1138
1139 Array<size_t> comp(n, unset);
1140 Array<size_t> stack;
1141 size_t components = 0;
1142 for (size_t root = 0; root < n; ++root)
1143 {
1144 if (comp(root) != unset)
1145 continue;
1146 comp(root) = components;
1147 stack.append(root);
1148 while (not stack.is_empty())
1149 {
1150 const size_t u = stack.get_last();
1151 (void) stack.remove_last();
1152 for (size_t p = adj.start(u); p < adj.start(u + 1); ++p)
1153 if (comp(adj.tgt(p)) == unset)
1154 {
1155 comp(adj.tgt(p)) = components;
1156 stack.append(adj.tgt(p));
1157 }
1158 }
1159 ++components;
1160 }
1161
1162 // members(c) lists the positions of component c in ascending order, and
1163 // local(v) is the position of v among them.
1164 Array<Array<size_t>> members;
1165 members.reserve(components);
1166 for (size_t c = 0; c < components; ++c)
1167 members.append(Array<size_t>());
1168 Array<size_t> local(n, 0);
1169 for (size_t v = 0; v < n; ++v)
1170 {
1171 local(v) = members(comp(v)).size();
1172 members(comp(v)).append(v);
1173 }
1174
1175 Array<Array<Array<Incoming>>> incoming;
1176 incoming.reserve(components);
1177 for (size_t c = 0; c < components; ++c)
1178 {
1179 incoming.append(Array<Array<Incoming>>());
1180 incoming(c).reserve(members(c).size());
1181 for (size_t i = 0; i < members(c).size(); ++i)
1182 incoming(c).append(Array<Incoming>());
1183 }
1184 for (size_t a = 0; a < ig.arcs.size(); ++a) // in snapshot order, as Karp reads them
1185 {
1186 const auto &rec = ig.arcs(a);
1187 incoming(comp(rec.tgt))(local(rec.tgt)).append(Incoming{local(rec.src), rec.arc, rec.weight});
1188 }
1189
1190 const char *const fn =
1191 With_Witness ? "howard_minimum_mean_cycle" : "howard_minimum_mean_cycle_value";
1192 if (max_bytes != std::numeric_limits<size_t>::max())
1193 {
1194 // The largest tables, before building any. A count that does not fit a
1195 // size_t exceeds every limit.
1196 bool representable = true;
1197 size_t largest = 0;
1198 size_t largest_nodes = 0;
1199 for (size_t c = 0; c < components; ++c)
1200 {
1201 size_t bytes = 0;
1202 const bool fits = min_mean_cycle_detail::karp_table_bytes<With_Witness, Cost_Type>(
1203 members(c).size(), min_mean_cycle_detail::karp_sums(incoming(c)), bytes);
1204 if (not fits or bytes > largest)
1205 {
1206 largest = bytes;
1207 largest_nodes = members(c).size();
1208 }
1209 if (not fits)
1210 {
1211 representable = false;
1212 break;
1213 }
1214 }
1215 ah_length_error_if(not representable or largest > max_bytes)
1216 << fn << "(): the fallback to Karp's algorithm (" << fallback_reason_text(reason)
1217 << ") needs " << (representable ? "" : "more than ")
1218 << (representable ? largest : std::numeric_limits<size_t>::max())
1219 << " bytes for the tables of a strongly connected component of " << largest_nodes
1220 << " nodes, above the limit of " << max_bytes << " bytes";
1221 }
1222
1223 // The mean is that of the component of smallest exact mean whose witness
1224 // vertex comes first. The witness comes from a component of that same
1225 // mean too, but one whose cycle cost fits the cost type is preferred: only
1226 // the reported witness has to fit, not the one of every component.
1227 Answer best; // the component of the witness
1228 size_t best_comp = unset;
1229 long double mean = 0; // the reported mean
1230 size_t mean_vertex = unset;
1231 for (size_t c = 0; c < components; ++c)
1232 {
1233 Answer answer = min_mean_cycle_detail::karp_solve<With_Witness, GT, Cost_Type>(incoming(c), fn);
1234 if (not answer.has_cycle)
1235 continue;
1236 const size_t vertex = members(c)(answer.witness);
1237 const int order = best_comp == unset ? -1 : min_mean_cycle_detail::mean_order(answer, best);
1238 if (order > 0)
1239 continue;
1240 if (order < 0 or vertex < mean_vertex)
1241 {
1242 mean = answer.minimum_mean;
1243 mean_vertex = vertex;
1244 }
1245 if (order < 0 or (answer.witness_fits and not best.witness_fits) or
1246 (answer.witness_fits == best.witness_fits and vertex < members(best_comp)(best.witness)))
1247 {
1248 best = std::move(answer);
1249 best_comp = c;
1250 }
1251 }
1252
1254 if (best_comp == unset)
1255 return result;
1256
1257 if constexpr (not With_Witness)
1258 {
1259 result.has_cycle = true;
1260 result.minimum_mean = mean;
1261 return result;
1262 }
1263
1264 ah_overflow_error_if(not best.witness_fits)
1265 << fn << "(): the cost of the witness cycle does not fit the cost type";
1266
1267 // The witness is a simple cycle (see karp_solve()). As in Howard's own
1268 // answer, `witness_node` is its first node and a floating-point cost is the
1269 // exact sum of its weights, rounded once. The mean stays Karp's value.
1270 const Array<size_t> &where = members(best_comp);
1271 result.has_cycle = true;
1272 result.minimum_mean = mean;
1273 result.cycle_length = best.cycle_arcs.size();
1274 if (best.cycle_nodes.is_empty()) // no witness extracted: Karp's vertex
1275 result.witness_node = ig.nodes(where(best.witness));
1276 else
1277 result.witness_node = ig.nodes(where(best.cycle_nodes(0)));
1278 if constexpr (std::is_floating_point_v<Cost_Type>)
1279 {
1281 for (size_t i = 0; i < best.cycle_weights.size(); ++i)
1282 ah_domain_error_if(not sum.add(best.cycle_weights(i)))
1283 << "howard_minimum_mean_cycle(): cycle cost became non-finite";
1284 result.cycle_total_cost = sum.rounded();
1285 ah_domain_error_if(not std::isfinite(result.cycle_total_cost))
1286 << "howard_minimum_mean_cycle(): cycle cost became non-finite";
1287 }
1288 else
1289 result.cycle_total_cost = best.cycle_total_cost;
1290 for (size_t i = 0; i < best.cycle_nodes.size(); ++i)
1291 result.cycle_nodes.append(ig.nodes(where(best.cycle_nodes(i))));
1292 for (size_t i = 0; i < best.cycle_arcs.size(); ++i)
1293 result.cycle_arcs.append(best.cycle_arcs(i));
1294 return result;
1295}
1296
1318template <AlephGraph GT, ArcDistance<GT> Distance, ArcFilter<GT> SA, bool With_Witness = true>
1320 const GT &g, Distance distance, SA sa, const size_t max_iterations,
1321 const size_t max_fallback_bytes = std::numeric_limits<size_t>::max())
1322{
1323 using Cost_Type = typename Distance::Distance_Type;
1326 using Arithmetic =
1328
1329 Result result;
1330 if constexpr (not howard_detail::exact_arithmetic_v<Cost_Type>)
1331 result.numeric_quality = Cycle_Numeric_Quality::Rounded;
1332 const auto snapshot =
1333 negative_cycles_detail::index_graph(g, distance, sa, "howard_minimum_mean_cycle");
1334 if (snapshot.arcs.is_empty())
1335 return result;
1336
1337 // Only the arcs inside strongly connected components lie on cycles; in
1338 // the core every node has an outgoing arc, which a policy needs.
1341 const bool whole = negative_cycles_detail::cyclic_core(snapshot, adj, core);
1342 if (not whole)
1343 {
1344 if (core.arcs.is_empty())
1345 return result; // no cycle at all
1347 }
1348 const auto &ig = whole ? snapshot : core;
1349 const size_t n = ig.nodes.size();
1350
1351 Solver solver(adj, n);
1352 size_t iterations = 0;
1353 const auto status = solver.solve(
1354 max_iterations == default_iterations ? n + iteration_slack : max_iterations, iterations);
1355
1356 const auto use_karp = [&](const size_t done, const Howard_Fallback_Reason reason)
1357 {
1358 Result karp;
1359 static_cast<Min_Mean_Cycle_Result<GT, Cost_Type> &>(karp) =
1360 karp_by_component<With_Witness>(ig, adj, reason, max_fallback_bytes);
1361 karp.iterations = done;
1362 karp.used_karp = true;
1363 karp.fallback_reason = reason;
1364 karp.numeric_quality = Cycle_Numeric_Quality::Rounded; // Karp compares means in long double
1365 return karp;
1366 };
1367
1368 if (status != Solver::Status::Optimal)
1369 return use_karp(iterations,
1373
1374 // The cycle the optimal policy reaches from a node of minimum mean. With
1375 // floating-point weights the policy was found with rounded arithmetic: it is
1376 // re-solved exactly from where it stands (usually just one evaluation) when
1377 // the weights fit 64- or 128-bit integers once scaled; otherwise at least
1378 // the choice among its cycles is made with exact sums.
1379 Array<size_t> policy = solver.policy();
1380 size_t first = 0;
1381 if constexpr (std::is_floating_point_v<Cost_Type>)
1382 {
1383 if (size_t exact_iterations = 0;
1384 verify_exactly(adj, n, n + iteration_slack, policy, first, exact_iterations))
1385 {
1386 result.numeric_quality = Cycle_Numeric_Quality::Exact;
1387 iterations += exact_iterations;
1388 }
1389 else
1390 {
1391 first = best_cycle_exactly(adj, policy, n);
1392 if (first == n) // an exact sum overflowed: keep the rounded choice
1393 first = cycle_entry(adj, policy, solver.best_node());
1394 }
1395 }
1396 else
1397 first = cycle_entry(adj, policy, solver.best_node());
1398
1399 Array<size_t> arcs; // indices into ig.arcs, in cycle order
1400 size_t root = 0; // position in `arcs` of the arc that leaves the smallest node
1401 if constexpr (With_Witness)
1402 result.cycle_nodes.append(ig.nodes(first));
1403 size_t u = first;
1404 do
1405 {
1406 const size_t a = adj.arc(policy(u));
1407 if (not arcs.is_empty() and u < ig.arcs(arcs(root)).src)
1408 root = arcs.size();
1409 arcs.append(a);
1410 if constexpr (With_Witness)
1411 result.cycle_arcs.append(ig.arcs(a).arc);
1412 u = adj.tgt(policy(u));
1413 if constexpr (With_Witness)
1414 result.cycle_nodes.append(ig.nodes(u));
1415 } while (u != first);
1416
1417 result.has_cycle = true;
1418 if constexpr (With_Witness)
1419 result.witness_node = ig.nodes(first);
1420 result.cycle_length = arcs.size();
1421 if constexpr (howard_detail::exact_arithmetic_v<Cost_Type>)
1422 {
1423 // The wide total is exact; it has to be representable in the cost type.
1424 // (A sum with a check at every step would reject totals that fit but
1425 // whose partial sums do not.) It is added in the order of the policy
1426 // evaluation, from the smallest node, whose partial sums all fit the
1427 // wide type: the evaluation of this policy did not overflow. From
1428 // another node they could leave it with costs near 2^62, and the
1429 // fallback would then be taken for a cost that fits.
1430 auto total = Arithmetic::zero_sum();
1431 bool fits = true;
1432 for (size_t i = 0; fits and i < arcs.size(); ++i)
1433 fits = Arithmetic::add(total, ig.arcs(arcs((root + i) % arcs.size())).weight);
1434 using Sum = typename Arithmetic::Sum;
1435 if constexpr (not std::is_same_v<Sum, Cost_Type>)
1436 if (fits)
1437 fits = total >= static_cast<Sum>(std::numeric_limits<Cost_Type>::min()) and
1438 total <= static_cast<Sum>(std::numeric_limits<Cost_Type>::max());
1439 if (not fits)
1440 return use_karp(iterations, Howard_Fallback_Reason::Cost_Out_Of_Range);
1441 result.cycle_total_cost = static_cast<Cost_Type>(total);
1442 }
1443 else
1444 result.cycle_total_cost = negative_cycles_detail::cycle_cost(
1445 ig, arcs, "howard_minimum_mean_cycle(): cycle cost became non-finite");
1446 result.minimum_mean = static_cast<long double>(result.cycle_total_cost) /
1447 static_cast<long double>(result.cycle_length);
1448 result.iterations = iterations;
1449 return result;
1450}
1451} // namespace howard_detail
1452
1512template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1514 const GT &g, Distance distance = Distance(), SA sa = SA(),
1515 const size_t max_fallback_bytes = std::numeric_limits<size_t>::max())
1516{
1517 return howard_detail::minimum_mean_cycle<GT, Distance, SA>(
1518 g, std::move(distance), std::move(sa), howard_detail::default_iterations, max_fallback_bytes);
1519}
1520
1558template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1560 const GT &g, Distance distance = Distance(), SA sa = SA(),
1561 const size_t max_fallback_bytes = std::numeric_limits<size_t>::max())
1562{
1563 const auto r = howard_detail::minimum_mean_cycle<GT, Distance, SA, false>(
1564 g, std::move(distance), std::move(sa), howard_detail::default_iterations, max_fallback_bytes);
1566 value.has_cycle = r.has_cycle;
1567 value.minimum_mean = r.minimum_mean;
1568 return value;
1569}
1570} // namespace Aleph
1571
1572#endif // HOWARD_MIN_MEAN_CYCLE_H
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
Definition ah-errors.H:846
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
long double w
Definition btreepic.C:153
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
T & get_last()
Return a modifiable reference to the last element.
Definition tpl_array.H:483
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
T & append(const T &item)
Definition htlist.H:1362
Policy iteration over the out-adjacency of a graph in which every node has at least one outgoing arc.
const Array< size_t > & policy() const noexcept
The policy: for each node, the position in the adjacency of its arc.
size_t chosen_arc(const size_t u) const
Position in the adjacency of the arc the policy chooses for u.
bool improve()
Improve the policy.
size_t best_node() const
A node whose mean is the smallest: the cycle it reaches is optimal.
@ Overflow
A scaled value did not fit the arithmetic.
@ Iteration_Limit
The limit was reached first.
@ Optimal
No improvement exists: the policy is optimal.
size_t successor(const size_t u) const
size_t next_node(const size_t u) const
Node the policy leads to from u.
const negative_cycles_detail::Out_Adjacency< Cost_Type > & adj_
Solver(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n)
bool evaluate()
Compute eta_ and bias_ of every node for the current policy.
Status solve(const size_t limit, size_t &iterations)
Iterate until the policy is optimal.
Solver(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n, const Array< size_t > &initial)
Exact sum of floating-point numbers.
int sign() const noexcept
Exact sign of the value: -1, 0 or 1.
void clear() noexcept
Set the value to zero; the storage is kept.
bool add_multiple(const Exact_Sum &x, size_t factor, const bool negate=false)
Add factor times the value of x, negated if asked.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Cycle_Numeric_Quality
How far the numbers behind the result of a cycle search can be trusted.
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.
Howard_Fallback_Reason
Why howard_minimum_mean_cycle() handed the problem over to Karp's algorithm (see the documentation of...
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.
@ Exact
Every sum and comparison that decided the result was exact: integral costs (an overflow throws instea...
@ Rounded
The cycle was chosen with rounded values (floating-point costs, or means compared in long double by K...
@ Iteration_Limit
The policy was not optimal after V + 64 improvements.
@ None
Howard's policy iteration answered: no fallback.
@ Cost_Out_Of_Range
The exact total cost of the optimal cycle Howard found does not fit the cost type (a narrow integral ...
@ Arithmetic_Overflow
A cycle sum or a scaled bias of the policy evaluation left the range of its arithmetic: the 64- or 12...
static mpfr_t y
Definition mpfr_mul_d.c:3
bool verify_exactly(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n, const size_t limit, Array< size_t > &policy, size_t &first, size_t &iterations)
Verify, and finish if needed, a floating-point result with exact arithmetic: try_exact() with 64-bit ...
Min_Mean_Cycle_Result< GT, Cost_Type > karp_by_component(const negative_cycles_detail::Indexed_Graph< GT, Cost_Type > &ig, const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Howard_Fallback_Reason reason, const size_t max_bytes)
Karp's algorithm on every strongly connected component of a cyclic core, from the weights already rea...
size_t cycle_entry(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Array< size_t > &policy, const size_t start)
Node where the walk that follows policy from start closes its cycle (the first node of that cycle the...
const char * fallback_reason_text(const Howard_Fallback_Reason reason) noexcept
Words for a fallback reason, for error messages.
constexpr size_t default_iterations
Value of max_iterations that selects the default limit, V + 64.
size_t best_cycle_exactly(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Array< size_t > &policy, const size_t n)
Cycle of minimum mean among the cycles of policy, with means compared exactly (Exact_Sum,...
constexpr bool exact_arithmetic_v
Integral costs that fit a long long use exact arithmetic, and so do unsigned long long and 128-bit in...
constexpr size_t iteration_slack
Iterations allowed beyond one per node before Karp takes over.
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > minimum_mean_cycle(const GT &g, Distance distance, SA sa, const size_t max_iterations, const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Body of howard_minimum_mean_cycle() with an explicit iteration limit, so that the fallback to Karp ca...
bool try_exact(const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n, const size_t limit, const Array< int > &low, const int low_min, Array< size_t > &policy, size_t &first, size_t &iterations)
Re-solve exactly, from policy, with the floating-point weights of adj scaled to integers of type Int.
Karp_Sums karp_sums(const Array< Array< Incoming_Arc< GT, Cost_Type > > > &incoming)
Choose how the sums of Karp's table over incoming are kept.
int mean_order(const Karp_Answer< GT, Cost_Type > &a, const Karp_Answer< GT, Cost_Type > &b) noexcept
Order of the minimum means of two answers: -1, 0 or 1.
Indexed_Graph< GT, typename Distance::Distance_Type > index_graph(const GT &g, Distance &distance, SA &sa, const char *fn)
Snapshot the graph: index its nodes and the arcs accepted by sa, reading each weight once.
void bit_span(const T x, int &low, int &high) noexcept
Exponents of the lowest and of the highest set bit of a nonzero finite x: |x| is an odd integer times...
Out_Adjacency< Cost_Type > build_out_adjacency(const Indexed_Graph< GT, Cost_Type > &ig)
Build the out-adjacency of ig by a counting sort, O(V + E).
Cost_Type cycle_cost(const Indexed_Graph< GT, Cost_Type > &ig, const Array< size_t > &arcs, const char *context)
Total cost of the arcs arcs (indices into ig.arcs).
bool cyclic_core(const Indexed_Graph< GT, Cost_Type > &ig, const Out_Adjacency< Cost_Type > &adj, Indexed_Graph< GT, Cost_Type > &core)
Cyclic core of a snapshot: the nodes and arcs that lie inside a strongly connected component with at ...
std::uint64_t odd_significand(const T x) noexcept
The odd integer |x| / 2^low, with low the exponent of the lowest set bit of the nonzero finite x (see...
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
Definition stat_utils.H:190
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
Definition ahAlgo.H:482
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Common utilities and base class for shortest path algorithms.
Result of howard_minimum_mean_cycle().
Howard_Fallback_Reason fallback_reason
Why Karp's algorithm produced the answer, or None if it did not.
Cycle_Numeric_Quality numeric_quality
Exact when the answer was decided with exact arithmetic: integral costs, or floating-point costs veri...
size_t iterations
Policy improvements applied before the policy was optimal.
bool used_karp
True if Karp's algorithm produced the answer: fallback_reason is not None.
Result of minimum mean cycle computation.
size_t cycle_length
Number of arcs in witness cycle.
Cost_Type cycle_total_cost
Weight sum of witness cycle.
GT::Node * witness_node
Vertex Karp's walk ends at (not necessarily on the cycle).
bool has_cycle
True if at least one directed cycle exists.
DynList< typename GT::Node * > cycle_nodes
Witness simple cycle (first node repeated at end).
DynList< typename GT::Arc * > cycle_arcs
Witness arcs aligned with cycle_nodes.
Lightweight minimum-mean-cycle value result.
bool has_cycle
True if at least one directed cycle exists.
Represents a missing value.
static bool less(const Eta &x, const Eta &y) noexcept
True if x is strictly smaller than y.
static bool equal(const Eta &x, const Eta &y) noexcept
True if the two means are equal (the means of one component are copies).
static bool add(Sum &total, const Cost_Type &w) noexcept
Add w to total; false if the sum is not finite.
static bool improves(const Bias candidate, const Bias current, const Eta &e) noexcept
True if the candidate is smaller than the current bias by more than the rounding noise of the arithme...
static Eta make_eta(const Sum &total, const size_t length) noexcept
total / length.
static Bias zero_bias() noexcept
The bias of the root of a cycle.
long double Sum
Accumulator of the total cost of a cycle.
static bool step(const Cost_Type &w, const Eta &e, const Bias next, Bias &out) noexcept
Bias of a node: w - e + next.
Wide Bias
den * x, with x the real-valued bias: an integer.
static bool portable_multiply_overflows(const Wide a, const Wide b, Wide &out) noexcept
Wide Sum
Accumulator of the total cost of a cycle: wide, so that the sum of the arcs of a cycle does not overf...
static bool multiply_overflows(const Wide a, const Wide b, Wide &out) noexcept
static bool add(Sum &total, const Cost_Type &w) noexcept
Add w to total; false if the sum does not fit.
static Eta make_eta(const Sum &total, const size_t length) noexcept
Mean of a cycle of length arcs and total cost total.
static int compare_fractions(Wide a1, Wide b1, Wide a2, Wide b2) noexcept
Compare a1 / b1 with a2 / b2 (denominators positive) without overflow, by the Euclidean algorithm.
typename Exact_Integer< Cost_Type >::Unsigned Unsigned
static bool step(const Cost_Type &w, const Eta &e, const Bias next, Bias &out) noexcept
Bias of a node whose chosen arc has weight w and leads to a node of bias next, when the mean is e: e....
static Bias zero_bias() noexcept
The bias of the root of a cycle.
static bool add_overflows(const Wide a, const Wide b, Wide &out) noexcept
static bool subtract_overflows(const Wide a, const Wide b, Wide &out) noexcept
static bool equal(const Eta &x, const Eta &y) noexcept
True if the two means are equal.
static bool improves(const Bias candidate, const Bias current, const Eta &) noexcept
True if the candidate bias is strictly smaller than the current one.
static Unsigned gcd(Unsigned a, Unsigned b) noexcept
static bool less(const Eta &x, const Eta &y) noexcept
True if x is strictly smaller than y.
typename Exact_Integer< Cost_Type >::Wide Wide
Arithmetic of the policy evaluation, specialised by exactness.
Integer type, and its limits, of the exact arithmetic for Cost_Type: long long, or int128_t for unsig...
static constexpr Wide max
Largest value.
unsigned long long Unsigned
Its unsigned counterpart.
static constexpr Wide min
Smallest value.
long long Wide
Signed integer of the arithmetic.
Arc entering a node, as Karp's table reads it.
Answer of karp_solve(), in the node positions of its input.
Positional snapshot of a graph: nodes by position and the arcs accepted by the arc filter,...
Array< Arc_Rec< GT, Cost_Type > > arcs
Out-adjacency of a snapshot in compressed form.
Array< Cost_Type > weight
Weight of the arc.
Array< size_t > tgt
Position of the target node.
Array< size_t > arc
Index of the arc in Indexed_Graph::arcs.
Distance accessor.
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.