Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::negative_cycles_detail Namespace Reference

Classes

struct  Arc_Rec
 Snapshot of one graph arc: endpoint positions, the arc and its weight (read once from the distance accessor). More...
 
struct  Best_Cycle
 Best simple cycle found so far by most_negative_cycle_bounded(). More...
 
struct  Decomposition_Scratch
 Scratch space of offer_simple_components(), allocated once. More...
 
class  Exact_Sum
 Exact sum of floating-point numbers. More...
 
struct  Indexed_Graph
 Positional snapshot of a graph: nodes by position and the arcs accepted by the arc filter, in iteration order. More...
 
class  Node_Index
 Map from node pointers to their positions, built once and never modified afterwards. More...
 
struct  Out_Adjacency
 Out-adjacency of a snapshot in compressed form. More...
 
class  Short_Cycle_Best
 Rank cycles of at most three arcs exactly, reusing scratch sums. More...
 

Typedefs

template<typename T >
using exact_float_t = T
 Floating-point format in which the error-free transformations on values of T are carried out.
 

Functions

template<typename Cost_Type >
void check_finite (const Cost_Type &value, const char *context)
 
template<typename F >
void two_sum (const F a, const F b, F &s, F &e) noexcept
 Error-free sum (Knuth's TwoSum): s = fl(a + b) and s + e == a + b exactly.
 
template<typename T >
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 2^low, and 2^high <= |x| < 2^(high + 1).
 
template<typename T >
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 bit_span()).
 
template<AlephGraph GT, typename Cost_Type >
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).
 
template<AlephGraph GT, typename Cost_Type >
bool short_sums_are_exact (const Indexed_Graph< GT, Cost_Type > &ig, const size_t L, Cost_Type &error_bound)
 Rounding behaviour of the sums of at most L weights of ig, added left to right in a floating-point Cost_Type.
 
template<AlephGraph GT, ArcDistance< GT > Distance, ArcFilter< GT > SA>
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.
 
template<AlephGraph GT, typename Cost_Type >
long find_predecessor_cycle (const Indexed_Graph< GT, Cost_Type > &ig, const Array< long > &pred, Array< long > &mark)
 Scan the predecessor graph for a cycle.
 
template<AlephGraph GT, typename Cost_Type >
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).
 
template<AlephGraph GT, typename Cost_Type >
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 least one arc.
 
template<AlephGraph GT, typename Cost_Type >
void offer_simple_components (const Indexed_Graph< GT, Cost_Type > &ig, const Array< size_t > &walk, Array< long > &pos, Decomposition_Scratch &scratch, Best_Cycle< Cost_Type > &best)
 Decompose the closed walk walk (arc indices, in order, starting and ending at the same node) into simple cycles and offer each one to best.
 
template<AlephGraph GT, typename Cost_Type >
Out_Adjacency< Cost_Type > cheapest_parallel_arcs (const Indexed_Graph< GT, Cost_Type > &ig, const Out_Adjacency< Cost_Type > &adj)
 Keep the cheapest parallel arc of each ordered pair.
 

Typedef Documentation

◆ exact_float_t

template<typename T >
using Aleph::negative_cycles_detail::exact_float_t = typedef T

Floating-point format in which the error-free transformations on values of T are carried out.

T itself when the compiler evaluates T in its own format (FLT_EVAL_METHOD == 0, the case of x86-64, AArch64 and MSVC); otherwise long double, which holds every value of T exactly, so that excess precision cannot break the transformations.

Definition at line 411 of file Negative_Cycles.H.

Function Documentation

◆ bit_span()

template<typename T >
void Aleph::negative_cycles_detail::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 2^low, and 2^high <= |x| < 2^(high + 1).

Parameters
[in]xA nonzero finite number.
[out]lowExponent of the lowest set bit.
[out]highExponent of the highest set bit.

Definition at line 663 of file Negative_Cycles.H.

References Aleph::and, Aleph::bits(), Aleph::low(), and m.

Referenced by odd_significand(), short_sums_are_exact(), and Aleph::howard_detail::verify_exactly().

◆ build_out_adjacency()

◆ cheapest_parallel_arcs()

template<AlephGraph GT, typename Cost_Type >
Out_Adjacency< Cost_Type > Aleph::negative_cycles_detail::cheapest_parallel_arcs ( const Indexed_Graph< GT, Cost_Type > &  ig,
const Out_Adjacency< Cost_Type > &  adj 
)

Keep the cheapest parallel arc of each ordered pair.

Parameters
[in]igIndexed graph.
[in]adjIts out-adjacency, in snapshot order within each node.
Returns
Out-adjacency retaining original arc indices; equal weights keep the first arc. O(V + E) time and working space.

Definition at line 2012 of file Negative_Cycles.H.

References Aleph::Array< T >::append(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::arc, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::none(), out, Aleph::Array< T >::size(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt.

Referenced by Aleph::most_negative_cycle_up_to_3().

◆ check_finite()

template<typename Cost_Type >
void Aleph::negative_cycles_detail::check_finite ( const Cost_Type &  value,
const char *  context 
)
inline

◆ cycle_cost()

template<AlephGraph GT, typename Cost_Type >
Cost_Type Aleph::negative_cycles_detail::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).

Integer costs are summed exactly, with overflow checks. Floating-point costs are summed exactly as well (Exact_Sum) and rounded once at the end, so the sign of the result is always the sign of the exact sum of the weights, and the value is within one unit in the last place. (A cycle such as 2^100, 1, 2^-100, -1, -2^100, -2^-100, whose exact total is zero, costs exactly zero.)

Parameters
[in]igIndexed snapshot of the graph.
[in]arcsIndices of the arcs to add.
[in]contextPrefix for the error message if a partial sum is not finite.
Returns
The total cost.
Exceptions
std::overflow_errorInteger overflow.
std::domain_errorFloating-point sum outside the range of the type.

Definition at line 752 of file Negative_Cycles.H.

References ah_domain_error_if, arcs, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, Aleph::shortest_path_detail::checked_add(), and Aleph::sum().

Referenced by Aleph::find_disjoint_negative_cycles(), Aleph::howard_detail::minimum_mean_cycle(), and Aleph::most_negative_cycle_bounded().

◆ cyclic_core()

template<AlephGraph GT, typename Cost_Type >
bool Aleph::negative_cycles_detail::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 least one arc.

Every cycle, closed walk included, stays inside one strongly connected component, so an arc whose ends belong to different components, and a node that has no arc inside its component, can take part in none. The core keeps exactly the arcs with both ends in the same component (parallel arcs and self-loops included, so a single node with a loop is a cyclic component) and the nodes they touch. Both keep their relative order, which makes a search over the core visit its candidates in the order the full snapshot would.

The components come from an iterative Tarjan scan over arrays built from the snapshot: neither the graph nor its cookies and bits are touched, and the recursion depth does not depend on the graph.

Parameters
[in]igIndexed snapshot of the graph.
[in]adjOut-adjacency of ig.
[out]coreReceives the core nodes and arcs (ascending position), both renumbered; left empty when the core is ig itself.
Returns
True if every node and arc of ig is in the core (ig is then its own core and nothing is copied); false otherwise, in which case core holds the core, empty if the graph has no cycle. O(V + E) time and space.

Definition at line 1078 of file Negative_Cycles.H.

References Aleph::and, Aleph::Array< T >::append(), Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, counter, Aleph::Array< T >::get_last(), Aleph::Array< T >::is_empty(), Aleph::low(), m, Aleph::next(), Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::Array< T >::remove_last(), Aleph::Array< T >::reserve(), root(), Aleph::Array< T >::size(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt, and w.

Referenced by Aleph::howard_detail::minimum_mean_cycle(), Aleph::most_negative_cycle_bounded(), and Aleph::most_negative_cycle_up_to_3().

◆ find_predecessor_cycle()

template<AlephGraph GT, typename Cost_Type >
long Aleph::negative_cycles_detail::find_predecessor_cycle ( const Indexed_Graph< GT, Cost_Type > &  ig,
const Array< long > &  pred,
Array< long > &  mark 
)

Scan the predecessor graph for a cycle.

pred(v) holds the index into ig.arcs of the arc that last relaxed v, or -1. Any cycle of that graph certifies a negative cycle of the underlying graph: each pred assignment records an arc that strictly improved its target, so the weights around a predecessor cycle telescope to a strictly negative sum (the "walk to the root" check of Cherkassky and Goldberg, 1999).

Parameters
[in]igIndexed snapshot of the graph.
[in]predPredecessor arc index per node, -1 for none.
[in,out]markScratch array of pred.size() entries; overwritten.
Returns
The index of a node lying on a predecessor cycle, or -1 if the predecessor graph is acyclic. O(V): every node is walked at most once.

Definition at line 971 of file Negative_Cycles.H.

References Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, chain(), pred, and Aleph::Array< T >::size().

Referenced by Aleph::find_disjoint_negative_cycles().

◆ index_graph()

◆ odd_significand()

template<typename T >
std::uint64_t Aleph::negative_cycles_detail::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 bit_span()).

Parameters
[in]xA nonzero finite number whose significand fits 64 bits.
Returns
The odd part of its significand.

Definition at line 708 of file Negative_Cycles.H.

References Aleph::and, bit_span(), Aleph::bits(), Aleph::low(), and m.

Referenced by Aleph::howard_detail::try_exact().

◆ offer_simple_components()

template<AlephGraph GT, typename Cost_Type >
void Aleph::negative_cycles_detail::offer_simple_components ( const Indexed_Graph< GT, Cost_Type > &  ig,
const Array< size_t > &  walk,
Array< long > &  pos,
Decomposition_Scratch &  scratch,
Best_Cycle< Cost_Type > &  best 
)

Decompose the closed walk walk (arc indices, in order, starting and ending at the same node) into simple cycles and offer each one to best.

Every node repetition closes one simple cycle; after popping it the scan continues, so a walk that repeats a triangle yields the triangle, and a figure-eight yields its two loops.

Definition at line 1525 of file Negative_Cycles.H.

References Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, check_finite(), Aleph::shortest_path_detail::checked_add(), Aleph::negative_cycles_detail::Decomposition_Scratch::component, Aleph::Array< T >::is_empty(), Aleph::negative_cycles_detail::Best_Cycle< Cost_Type >::offer(), Aleph::Array< T >::size(), Aleph::negative_cycles_detail::Decomposition_Scratch::st_in_arc, and Aleph::negative_cycles_detail::Decomposition_Scratch::st_nodes.

Referenced by Aleph::most_negative_cycle_bounded().

◆ short_sums_are_exact()

template<AlephGraph GT, typename Cost_Type >
bool Aleph::negative_cycles_detail::short_sums_are_exact ( const Indexed_Graph< GT, Cost_Type > &  ig,
const size_t  L,
Cost_Type &  error_bound 
)

Rounding behaviour of the sums of at most L weights of ig, added left to right in a floating-point Cost_Type.

If every weight is a multiple of 2^low (its lowest set bit) and L times the largest one stays below 2^(digits + low), every such sum is a multiple of 2^low with at most digits significant bits: it is computed exactly. Otherwise the rounding error of a sum of k <= L terms is at most gamma_(k-1) times the sum of their magnitudes (Higham, "Accuracy and Stability of Numerical Algorithms", ยง4.2), so at most 2 (L - 1) L u max|w|, with u the unit roundoff, which is what error_bound receives (rounded up).

Parameters
[in]igIndexed snapshot (the cyclic core searched).
[in]LLargest number of terms of a sum.
[out]error_boundBound of the rounding error of any such sum: zero when they are all exact.
Returns
True if every such sum is exact.

Definition at line 793 of file Negative_Cycles.H.

References Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, bit_span(), Aleph::low(), and w.

Referenced by Aleph::negative_cycles_detail::Best_Cycle< Cost_Type >::Best_Cycle(), and Aleph::negative_cycles_detail::Short_Cycle_Best< Cost_Type >::Short_Cycle_Best().

◆ two_sum()

template<typename F >
void Aleph::negative_cycles_detail::two_sum ( const F  a,
const F  b,
F &  s,
F &  e 
)
inlinenoexcept

Error-free sum (Knuth's TwoSum): s = fl(a + b) and s + e == a + b exactly.

Needs IEEE round-to-nearest arithmetic evaluated as written (no -ffast-math or other reassociation) and a finite a + b. TwoSum's s - a can overflow although s does not, when an operand is the largest finite value (Boldo, Graillat and Muller, "On the robustness of the 2Sum and Fast2Sum algorithms", 2017); the error is then recomputed with Fast2Sum, the larger magnitude first, which is exact and cannot overflow while s is finite.

Parameters
[in]aFirst term.
[in]bSecond term.
[out]sThe rounded sum.
[out]eIts rounding error.

Definition at line 431 of file Negative_Cycles.H.

Referenced by Aleph::negative_cycles_detail::Exact_Sum< T >::compress(), and Aleph::negative_cycles_detail::Exact_Sum< T >::grow_expansion().