|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Classes | |
| struct | Incoming_Arc |
| Arc entering a node, as Karp's table reads it. More... | |
| struct | Karp_Answer |
Answer of karp_solve(), in the node positions of its input. More... | |
Enumerations | |
| enum class | Karp_Sums { Cost_Type_Checked , Long_Long , Long_Long_Checked , Int128 } |
How karp_solve() keeps the sums of a table (see there). More... | |
Functions | |
| template<typename Cost_Type > | |
| void | validate_finite_accumulator (const Cost_Type &value, const char *context) |
Throw std::domain_error with context if a floating-point value is not finite. | |
| template<AlephGraph GT, ArcDistance< GT > Distance, ArcFilter< GT > SA> | |
| Array< Array< Incoming_Arc< GT, typename Distance::Distance_Type > > > | incoming_lists (const GT &g, Distance &distance, SA &sa, Array< typename GT::Node * > &nodes) |
Build the incoming lists of the arcs of g accepted by sa, in arc-iteration order, reading each weight once. | |
| template<AlephGraph GT, typename Cost_Type > | |
| 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. | |
| template<typename Acc > | |
| bool | mean_less (const Acc c1, const size_t l1, const Acc c2, const size_t l2) |
Whether the mean c1 / l1 is smaller than c2 / l2. | |
| template<bool With_Witness, typename Acc , bool Checked, bool Exact_Difference, AlephGraph GT, typename Cost_Type > | |
| Karp_Answer< GT, Cost_Type > | karp_answer (const Array< Array< Incoming_Arc< GT, Cost_Type > > > &incoming, const Array< Acc > &dp, const Array< size_t > &pred, const Acc inf, const char *fn) |
Karp's minimum mean, its vertex and the witness, from a filled table; see karp_table(). | |
| template<bool With_Witness, typename Acc , bool Checked, bool Exact_Difference, AlephGraph GT, typename Cost_Type > | |
| Karp_Answer< GT, Cost_Type > | karp_table (const Array< Array< Incoming_Arc< GT, Cost_Type > > > &incoming, const Acc inf, const char *fn) |
Karp's table, with sums kept in Acc; see karp_solve(). | |
| template<AlephGraph GT, typename Cost_Type > | |
| 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. | |
| template<bool With_Witness, typename Cost_Type > | |
| bool | karp_table_bytes (const size_t n, const Karp_Sums sums, size_t &bytes) noexcept |
Bytes that karp_solve() reserves for the tables of a graph of n nodes whose sums are kept as sums. | |
| template<bool With_Witness, AlephGraph GT, typename Cost_Type > | |
| Karp_Answer< GT, Cost_Type > | karp_solve (const Array< Array< Incoming_Arc< GT, Cost_Type > > > &incoming, const char *fn) |
| Karp's algorithm on a graph given by its incoming lists. | |
|
strong |
How karp_solve() keeps the sums of a table (see there).
Definition at line 750 of file Min_Mean_Cycle.H.
| Array< Array< Incoming_Arc< GT, typename Distance::Distance_Type > > > Aleph::min_mean_cycle_detail::incoming_lists | ( | const GT & | g, |
| Distance & | distance, | ||
| SA & | sa, | ||
| Array< typename GT::Node * > & | nodes | ||
| ) |
Build the incoming lists of the arcs of g accepted by sa, in arc-iteration order, reading each weight once.
| [in] | g | Directed graph. |
| [in] | distance | Arc cost accessor. |
| [in] | sa | Arc filter. |
| [out] | nodes | Receives the nodes of g by position. |
| std::domain_error | If a floating-point weight is not finite. |
Definition at line 184 of file Min_Mean_Cycle.H.
References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::append(), Aleph::Array< T >::append(), Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), nodes, Aleph::Array< T >::reserve(), and w.
Referenced by Aleph::karp_minimum_mean_cycle(), and Aleph::karp_minimum_mean_cycle_value().
| Karp_Answer< GT, Cost_Type > Aleph::min_mean_cycle_detail::karp_answer | ( | const Array< Array< Incoming_Arc< GT, Cost_Type > > > & | incoming, |
| const Array< Acc > & | dp, | ||
| const Array< size_t > & | pred, | ||
| const Acc | inf, | ||
| const char * | fn | ||
| ) |
Karp's minimum mean, its vertex and the witness, from a filled table; see karp_table().
Separate from the table so that the hot loop that fills it is compiled on its own.
| With_Witness | Whether to extract a witness (pred is filled). |
| Acc | Type of the sums of the table and of the witness. |
| Checked | Whether an addition can overflow Acc and has to be checked. |
| Exact_Difference | Whether Acc holds the difference of two sums exactly. |
| [in] | incoming | Incoming arcs of every node. |
| [in] | dp | The sums: dp[k n + v] is the cheapest walk of k arcs that ends at v. |
| [in] | pred | Position, in incoming[v], of the arc that reaches each state; empty without the witness. |
| [in] | inf | Value of Acc that marks an unreachable state. |
| [in] | fn | Name of the public function, for error messages. |
karp_table(). | std::overflow_error | If a checked addition overflows. |
| std::domain_error | If a floating-point accumulation is not finite. |
Definition at line 331 of file Min_Mean_Cycle.H.
References ah_runtime_error_if, Aleph::and, Aleph::Array< T >::append(), arcs, Aleph::shortest_path_detail::checked_add(), Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::cycle_arcs, Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::cycle_nodes, Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::cycle_total_cost, Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::cycle_weights, Aleph::Array< T >::get_last(), Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::has_cycle, k, mean_less(), Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::minimum_mean, nodes, Aleph::none(), pred, r, Aleph::Array< T >::remove_last(), Aleph::Array< T >::reserve(), Aleph::Array< T >::size(), validate_finite_accumulator(), w, Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::witness, and Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >::witness_fits.
| Karp_Answer< GT, Cost_Type > Aleph::min_mean_cycle_detail::karp_solve | ( | const Array< Array< Incoming_Arc< GT, Cost_Type > > > & | incoming, |
| const char * | fn | ||
| ) |
Karp's algorithm on a graph given by its incoming lists.
The node positions are 0 .. incoming.size() - 1, and incoming[v] holds the arcs entering v. Nothing else is read: the graph, its accessor and its filter are not touched, so the public functions and the fallback of howard_minimum_mean_cycle() (which runs this on each strongly connected component of its own snapshot) share the same arithmetic and the same tie-breaking.
The table holds (n + 1) * n sums, plus as many predecessor positions when With_Witness is true. Its arithmetic depends on Cost_Type:
long long. Every sum of the table and of the witness adds at most n weights, so it is bounded by n * max|w|. When that bound is below LLONG_MAX the sums are long long and cannot overflow. Otherwise they are 128-bit integers (ALEPH_KARP_INT128), which cannot overflow either; without them (MSVC) they stay long long and every addition is checked. Only the cost of the reported witness has to fit Cost_Type: the vertex is chosen with exact means where 128-bit integers exist, and among minimum-mean cycles of the same mean one whose cost fits is preferred, from the walk of the chosen vertex or else of another vertex of the same mean.Cost_Type; one that is not finite throws.unsigned long long, 128-bit costs) keep their own type with checked additions.An unreachable state is marked with a value no sum can take: the maximum of a type the bound keeps the sums below, or an infinity. Only in the last case of each list, with checked long long or the cost type's own sums, is it their maximum, which a sum could equal.
The choice is karp_sums(), and the bytes of the tables are karp_table_bytes(); both are separate so that a caller can know the bytes before running the table.
| With_Witness | Whether to keep predecessors and extract a witness. |
| [in] | incoming | Incoming arcs of every node. |
| [in] | fn | Name of the public function, for error messages. |
With_Witness, the witness, or witness_fits false if its cost does not fit Cost_Type. | std::overflow_error | If the bytes of the tables do not fit a size_t, or a checked addition overflows. |
| std::domain_error | If a floating-point accumulation is not finite. |
Definition at line 897 of file Min_Mean_Cycle.H.
References ah_overflow_error_if, Aleph::and, karp_sums(), and Long_Long.
| Karp_Sums Aleph::min_mean_cycle_detail::karp_sums | ( | const Array< Array< Incoming_Arc< GT, Cost_Type > > > & | incoming | ) |
Choose how the sums of Karp's table over incoming are kept.
For integral costs that fit a long long, every sum adds at most n weights, so n * max|w| bounds it: long long when the bound is below LLONG_MAX, 128-bit integers otherwise, or checked long long without them. Any other cost type keeps its own sums, checked.
| [in] | incoming | Incoming arcs of every node. |
Definition at line 769 of file Min_Mean_Cycle.H.
References Aleph::and, Cost_Type_Checked, Int128, Long_Long, Long_Long_Checked, and w.
Referenced by Aleph::howard_detail::karp_by_component(), and karp_solve().
| Karp_Answer< GT, Cost_Type > Aleph::min_mean_cycle_detail::karp_table | ( | const Array< Array< Incoming_Arc< GT, Cost_Type > > > & | incoming, |
| const Acc | inf, | ||
| const char * | fn | ||
| ) |
Karp's table, with sums kept in Acc; see karp_solve().
| With_Witness | Whether to keep predecessors and extract a witness. |
| Acc | Type of the sums of the table and of the witness. |
| Checked | Whether an addition can overflow Acc and has to be checked (it cannot when karp_solve() has bounded the sums). |
| Exact_Difference | Whether Acc holds the difference of two sums exactly, so that it is rounded once, not each sum. |
| [in] | incoming | Incoming arcs of every node. |
| [in] | inf | Value of Acc that marks an unreachable state; no sum can take it. |
| [in] | fn | Name of the public function, for error messages. |
With_Witness, the witness. If the cost of no minimum-mean cycle the walks gave fits Cost_Type, witness_fits is false and there is no witness. | std::overflow_error | If a checked addition overflows. |
| std::domain_error | If a floating-point accumulation is not finite. |
karp_solve() has checked that the tables fit a size_t of bytes. Definition at line 680 of file Min_Mean_Cycle.H.
References Aleph::and, Aleph::shortest_path_detail::checked_add(), k, Aleph::none(), pred, Aleph::Array< T >::size(), validate_finite_accumulator(), and w.
|
noexcept |
Bytes that karp_solve() reserves for the tables of a graph of n nodes whose sums are kept as sums.
The table of sums has (n + 1) * n entries, and so has the table of predecessor positions (size_t), kept only with the witness. Array reserves a power of two of elements, the smallest not below the count, so that is what is counted: up to twice the entries. The arrays of O(n) elements that the witness extraction uses are not counted.
| With_Witness | Whether the table of predecessors is kept. |
| Cost_Type | Arc weight type. |
| [in] | n | Number of nodes. |
| [in] | sums | How the sums are kept (karp_sums()). |
| [out] | bytes | The bytes, when they fit a size_t. |
size_t. Definition at line 816 of file Min_Mean_Cycle.H.
References Int128, Long_Long, Long_Long_Checked, and max().
| bool Aleph::min_mean_cycle_detail::mean_less | ( | const Acc | c1, |
| const size_t | l1, | ||
| const Acc | c2, | ||
| const size_t | l2 | ||
| ) |
Whether the mean c1 / l1 is smaller than c2 / l2.
Integral sums are compared exactly, cross-multiplied in 128 bits (ALEPH_KARP_INT128): they are below n * 2^63 and the lengths at most n < 2^32, so the products fit. Otherwise the two quotients are compared in long double.
| [in] | c1 | Total cost of the first cycle. |
| [in] | l1 | Its length, positive. |
| [in] | c2 | Total cost of the second cycle. |
| [in] | l2 | Its length, positive. |
Definition at line 295 of file Min_Mean_Cycle.H.
Referenced by karp_answer(), and TEST().
|
noexcept |
Order of the minimum means of two answers: -1, 0 or 1.
Exact when both hold their mean as a fraction (integral sums where 128-bit integers exist): the numerators are below n 2^64 and the denominators at most n, and with at least 8 bytes per entry a table fits a size_t of bytes only for n < 2^31, so the cross products stay below 2^126. Otherwise the long double means are compared.
| [in] | a | An answer with a cycle. |
| [in] | b | Another answer with a cycle. |
a is smaller than, equal to or larger than that of b. Definition at line 267 of file Min_Mean_Cycle.H.
References Aleph::and, l, and r.
Referenced by Aleph::howard_detail::karp_by_component().
| void Aleph::min_mean_cycle_detail::validate_finite_accumulator | ( | const Cost_Type & | value, |
| const char * | context | ||
| ) |
Throw std::domain_error with context if a floating-point value is not finite.
| [in] | value | Value to check. |
| [in] | context | Message of the exception. |
| std::domain_error | If value is a NaN or an infinity. |
Definition at line 167 of file Min_Mean_Cycle.H.
References ah_domain_error_if, and value.
Referenced by karp_answer(), and karp_table().