|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
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. | |
| 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.
|
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).
| [in] | x | A nonzero finite number. |
| [out] | low | Exponent of the lowest set bit. |
| [out] | high | Exponent 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().
| Out_Adjacency< Cost_Type > Aleph::negative_cycles_detail::build_out_adjacency | ( | const Indexed_Graph< GT, Cost_Type > & | ig | ) |
Build the out-adjacency of ig by a counting sort, O(V + E).
| [in] | ig | Indexed snapshot of the graph. |
Definition at line 1024 of file Negative_Cycles.H.
References Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::arc, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, Aleph::fill(), m, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::Array< T >::size(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt, and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::weight.
Referenced by Aleph::howard_detail::minimum_mean_cycle(), Aleph::most_negative_cycle_bounded(), and Aleph::most_negative_cycle_up_to_3().
| 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.
| [in] | ig | Indexed graph. |
| [in] | adj | Its out-adjacency, in snapshot order within each node. |
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().
|
inline |
Definition at line 391 of file Negative_Cycles.H.
References ah_domain_error_if, and value.
Referenced by Aleph::find_disjoint_negative_cycles(), index_graph(), Aleph::most_negative_cycle_bounded(), and offer_simple_components().
| 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.)
| [in] | ig | Indexed snapshot of the graph. |
| [in] | arcs | Indices of the arcs to add. |
| [in] | context | Prefix for the error message if a partial sum is not finite. |
| std::overflow_error | Integer overflow. |
| std::domain_error | Floating-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().
| 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.
| [in] | ig | Indexed snapshot of the graph. |
| [in] | adj | Out-adjacency of ig. |
| [out] | core | Receives the core nodes and arcs (ascending position), both renumbered; left empty when the core is ig itself. |
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().
| 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).
| [in] | ig | Indexed snapshot of the graph. |
| [in] | pred | Predecessor arc index per node, -1 for none. |
| [in,out] | mark | Scratch array of pred.size() entries; overwritten. |
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().
| Indexed_Graph< GT, typename Distance::Distance_Type > Aleph::negative_cycles_detail::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.
Rejects undirected graphs and non-finite floating weights.
Definition at line 915 of file Negative_Cycles.H.
References ah_domain_error_if, Aleph::Array< T >::append(), Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, check_finite(), Aleph::negative_cycles_detail::Node_Index< Node >::find(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::negative_cycles_detail::Node_Index< Node >::insert(), GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::Array< T >::reserve(), and w.
Referenced by Aleph::find_disjoint_negative_cycles(), Aleph::howard_detail::minimum_mean_cycle(), Aleph::most_negative_cycle_bounded(), and Aleph::most_negative_cycle_up_to_3().
|
noexcept |
The odd integer |x| / 2^low, with low the exponent of the lowest set bit of the nonzero finite x (see bit_span()).
| [in] | x | A nonzero finite number whose significand fits 64 bits. |
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().
| 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().
| 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).
| [in] | ig | Indexed snapshot (the cyclic core searched). |
| [in] | L | Largest number of terms of a sum. |
| [out] | error_bound | Bound of the rounding error of any such sum: zero when they are all 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().
|
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.
| [in] | a | First term. |
| [in] | b | Second term. |
| [out] | s | The rounded sum. |
| [out] | e | Its 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().