175#ifndef HOWARD_MIN_MEAN_CYCLE_H
176#define HOWARD_MIN_MEAN_CYCLE_H
184#include <type_traits>
226template <AlephGraph GT,
typename Cost_Type>
248#if defined(__SIZEOF_INT128__) and not defined(_MSC_VER)
249#define ALEPH_HOWARD_INT128 1
251#define ALEPH_HOWARD_INT128 0
254namespace howard_detail {
255#if ALEPH_HOWARD_INT128
258__extension__
typedef __int128 int128_t;
260__extension__
typedef unsigned __int128 uint128_t;
265template <
typename Cost_Type>
267 std::numeric_limits<Cost_Type>::max() <= std::numeric_limits<long long>::max();
268#if ALEPH_HOWARD_INT128
270inline constexpr bool exact_arithmetic_v<unsigned long long> =
true;
272inline constexpr bool exact_arithmetic_v<int128_t> =
true;
282template <
typename Cost_Type>
287 static constexpr Wide max = std::numeric_limits<long long>::max();
288 static constexpr Wide min = std::numeric_limits<long long>::min();
291#if ALEPH_HOWARD_INT128
296 using Wide = int128_t;
298 static constexpr Wide max =
static_cast<int128_t
>(~uint128_t(0) >> 1);
306struct Exact_Integer<unsigned long long> : Exact_Integer<int128_t>
317template <
typename Cost_Type,
bool Exact>
322template <
typename Cost_Type>
350 static bool add(
Sum &total,
const Cost_Type &
w)
noexcept
352 return not add_overflows(total,
static_cast<Wide>(
w), total);
360 static Eta
make_eta(
const Sum &total,
const size_t length)
noexcept
362 const Wide num = total;
363 const Wide den =
static_cast<Wide>(length);
366 const auto g =
static_cast<Wide>(gcd(magnitude,
static_cast<Unsigned>(den)));
367 return Eta{num / g, den / g};
377 static bool equal(
const Eta &x,
const Eta &
y)
noexcept
379 return x.num ==
y.num
and x.den ==
y.den;
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;
423 static bool less(
const Eta &x,
const Eta &
y)
noexcept
425 return not
equal(x,
y)
and compare_fractions(x.num, x.den,
y.num,
y.den) < 0;
440 if (multiply_overflows(e.den,
static_cast<Wide>(
w), scaled))
443 if (subtract_overflows(scaled, e.num, shifted))
445 return not add_overflows(shifted,
next,
out);
451 return candidate < current;
476 if (__builtin_add_overflow(a, b, &
r))
481 if ((b > 0
and a > wide_max - b) or (b < 0
and a < wide_min - b))
492 if (__builtin_sub_overflow(a, b, &
r))
497 if ((b < 0 and a > wide_max + b) or (b > 0
and a < wide_min + b))
506 if constexpr (
sizeof(
Wide) >
sizeof(
long long))
513 if (a ==
static_cast<long long>(a)
and b ==
static_cast<long long>(b))
518 return portable_multiply_overflows(a, b,
out);
524 if (__builtin_mul_overflow(a, b, &
r))
529 return portable_multiply_overflows(a, b,
out);
536 if (a == 0 or b == 0)
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)
552template <
typename Cost_Type>
568 static bool add(
Sum &total,
const Cost_Type &
w)
noexcept
570 total +=
static_cast<long double>(
w);
571 return std::isfinite(total);
577 return total /
static_cast<long double>(length);
607 out =
static_cast<long double>(
w) - e +
next;
608 return std::isfinite(
out);
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;
625template <
typename Cost_Type>
628 static constexpr bool exact = exact_arithmetic_v<Cost_Type>;
630 using Eta =
typename Ar::Eta;
631 using Bias =
typename Ar::Bias;
651 for (
size_t i = 0; i <
n_; ++i)
654 for (
size_t s = 0; s <
n_; ++s)
673 while (
path_(j) != u);
677 const size_t length = top - j;
679 for (
size_t i = 1; i < length; ++i)
689 typename Ar::Sum total = Ar::zero_sum();
690 for (
size_t i = 0; i < length; ++i)
694 const Eta e = Ar::make_eta(total, length);
695 const size_t root_node =
path_(j +
root);
697 bias_(root_node) = Ar::zero_bias();
700 for (
size_t k = 1;
k < length; ++
k)
702 const size_t v =
path_(j + (
root + length -
k) % length);
715 const size_t v =
path_(--top);
730 bool changed =
false;
733 for (
size_t u = 0; u <
n_; ++u)
737 for (
size_t p =
adj_.start(u); p <
adj_.start(u + 1); ++p)
753 for (
size_t u = 0; u <
n_; ++u)
757 for (
size_t p =
adj_.start(u); p <
adj_.start(u + 1); ++p)
759 const size_t v =
adj_.tgt(p);
760 if (not Ar::equal(
eta_(v),
eta_(u)))
763 Bias candidate = Ar::zero_bias();
764 if (not Ar::step(
adj_.weight(p),
eta_(u),
bias_(v), candidate))
769 if (Ar::improves(candidate, best_bias,
eta_(u)))
772 best_bias = candidate;
800 :
adj_(adj),
n_(n),
policy_(initial),
eta_(n,
Eta{}),
bias_(n,
Bias{}),
state_(n, 0),
path_(n, 0)
809 :
adj_(adj),
n_(n),
policy_(n, 0),
eta_(n,
Eta{}),
bias_(n,
Bias{}),
state_(n, 0),
path_(n, 0)
812 for (
size_t u = 0; u < n; ++u)
814 size_t best = adj.
start(u);
815 for (
size_t p = best + 1; p < adj.
start(u + 1); ++p)
836 if (++iterations > limit)
845 for (
size_t u = 1; u <
n_; ++u)
883template <
typename Cost_Type>
892 v = adj.
tgt(policy(v));
916template <
typename Int,
typename Cost_Type>
919 size_t &first,
size_t &iterations)
926 for (
size_t p = 0; p < adj.
weight.
size(); ++p)
928 const Cost_Type
w = adj.
weight(p);
929 if (
w == Cost_Type{0})
932 if constexpr (std::numeric_limits<Cost_Type>::digits <= 64)
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;
964template <
typename Cost_Type>
966 const size_t limit,
Array<size_t> &policy,
size_t &first,
size_t &iterations)
972 for (
size_t p = 0; p < adj.
weight.
size(); ++p)
974 if (adj.
weight(p) == Cost_Type{0})
978 low_min = any ? std::min(low_min,
low(p)) :
low(p);
979 high_max = any ? std::max(high_max, high) : high;
982 const int bits = any ? high_max - low_min + 1 : 1;
984 if (
bits <= 62
and try_exact<long long>(adj, n, limit,
low, low_min, policy, first, iterations))
986#if ALEPH_HOWARD_INT128
987 if (
bits <= 126
and try_exact<int128_t>(adj, n, limit,
low, low_min, policy, first, iterations))
1004template <
typename Cost_Type>
1014 size_t best_length = 0;
1015 for (
size_t s = 0; s < n; ++s)
1021 while (state(u) == 0)
1025 u = adj.
tgt(policy(u));
1037 v = adj.
tgt(policy(v));
1040 bool better = best == n;
1048 better = difference.
sign() < 0;
1053 best_length = length;
1054 std::swap(best_sum,
sum);
1057 for (
size_t i = 0; i < top; ++i)
1072 return "iteration limit";
1074 return "arithmetic overflow";
1076 return "cycle cost out of range";
1080 return "no fallback";
1128template <
bool With_Witness, AlephGraph GT,
typename Cost_Type>
1132 const size_t max_bytes)
1136 constexpr size_t unset = std::numeric_limits<size_t>::max();
1141 size_t components = 0;
1144 if (comp(
root) != unset)
1146 comp(
root) = components;
1152 for (
size_t p = adj.
start(u); p < adj.
start(u + 1); ++p)
1153 if (comp(adj.
tgt(p)) == unset)
1155 comp(adj.
tgt(p)) = components;
1166 for (
size_t c = 0; c < components; ++c)
1169 for (
size_t v = 0; v < n; ++v)
1171 local(v) = members(comp(v)).
size();
1172 members(comp(v)).
append(v);
1177 for (
size_t c = 0; c < components; ++c)
1181 for (
size_t i = 0; i < members(c).
size(); ++i)
1184 for (
size_t a = 0; a < ig.
arcs.size(); ++a)
1186 const auto &rec = ig.
arcs(a);
1187 incoming(comp(rec.tgt))(local(rec.tgt)).append(Incoming{local(rec.src), rec.arc, rec.weight});
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())
1196 bool representable =
true;
1198 size_t largest_nodes = 0;
1199 for (
size_t c = 0; c < components; ++c)
1202 const bool fits = min_mean_cycle_detail::karp_table_bytes<With_Witness, Cost_Type>(
1204 if (not fits or bytes > largest)
1207 largest_nodes = members(c).
size();
1211 representable =
false;
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";
1228 size_t best_comp = unset;
1229 long double mean = 0;
1230 size_t mean_vertex = unset;
1231 for (
size_t c = 0; c < components; ++c)
1233 Answer answer = min_mean_cycle_detail::karp_solve<With_Witness, GT, Cost_Type>(incoming(c), fn);
1234 if (not answer.has_cycle)
1236 const size_t vertex = members(c)(answer.witness);
1240 if (order < 0 or vertex < mean_vertex)
1242 mean = answer.minimum_mean;
1243 mean_vertex = vertex;
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)))
1248 best = std::move(answer);
1254 if (best_comp == unset)
1257 if constexpr (not With_Witness)
1265 << fn <<
"(): the cost of the witness cycle does not fit the cost type";
1274 if (best.cycle_nodes.is_empty())
1278 if constexpr (std::is_floating_point_v<Cost_Type>)
1281 for (
size_t i = 0; i < best.cycle_weights.size(); ++i)
1283 <<
"howard_minimum_mean_cycle(): cycle cost became non-finite";
1286 <<
"howard_minimum_mean_cycle(): cycle cost became non-finite";
1290 for (
size_t i = 0; i < best.cycle_nodes.size(); ++i)
1292 for (
size_t i = 0; i < best.cycle_arcs.size(); ++i)
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())
1330 if constexpr (not howard_detail::exact_arithmetic_v<Cost_Type>)
1332 const auto snapshot =
1334 if (snapshot.arcs.is_empty())
1344 if (core.
arcs.is_empty())
1348 const auto &ig = whole ? snapshot : core;
1352 size_t iterations = 0;
1353 const auto status = solver.
solve(
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;
1369 return use_karp(iterations,
1381 if constexpr (std::is_floating_point_v<Cost_Type>)
1383 if (
size_t exact_iterations = 0;
1387 iterations += exact_iterations;
1401 if constexpr (With_Witness)
1402 result.cycle_nodes.
append(ig.nodes(first));
1406 const size_t a = adj.arc(policy(u));
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);
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>)
1430 auto total = Arithmetic::zero_sum();
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>)
1437 fits = total >=
static_cast<Sum
>(std::numeric_limits<Cost_Type>::min())
and
1438 total <=
static_cast<Sum
>(std::numeric_limits<Cost_Type>::max());
1441 result.cycle_total_cost =
static_cast<Cost_Type
>(total);
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;
1512template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1515 const size_t max_fallback_bytes = std::numeric_limits<size_t>::max())
1517 return howard_detail::minimum_mean_cycle<GT, Distance, SA>(
1558template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1561 const size_t max_fallback_bytes = std::numeric_limits<size_t>::max())
1563 const auto r = howard_detail::minimum_mean_cycle<GT, Distance, SA, false>(
1567 value.minimum_mean =
r.minimum_mean;
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.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & get_last()
Return a modifiable reference to the last element.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
T & append(const T &item)
Policy iteration over the out-adjacency of a graph in which every node has at least one outgoing arc.
static constexpr bool exact
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.
Array< unsigned char > state_
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)
DynArray< Graph::Arc * > arcs
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...
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.
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
void next()
Advance all underlying iterators (bounds-checked).
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 Sum zero_sum() noexcept
The empty sum.
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 Sum zero_sum() noexcept
The empty sum.
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< typename GT::Node * > nodes
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 > start
n + 1 offsets.
Array< size_t > arc
Index of the arc in Indexed_Graph::arcs.
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.