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

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.
 

Enumeration Type Documentation

◆ Karp_Sums

How karp_solve() keeps the sums of a table (see there).

Enumerator
Cost_Type_Checked 

The cost type itself, every addition checked.

Long_Long 

long long, which the bound keeps the sums inside.

Long_Long_Checked 

long long, every addition checked (no 128-bit integers).

Int128 

128-bit integers, which the bound keeps the sums inside.

Definition at line 750 of file Min_Mean_Cycle.H.

Function Documentation

◆ incoming_lists()

template<AlephGraph GT, ArcDistance< GT > Distance, ArcFilter< GT > SA>
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.

Parameters
[in]gDirected graph.
[in]distanceArc cost accessor.
[in]saArc filter.
[out]nodesReceives the nodes of g by position.
Returns
One list per node position: the arcs entering it.
Exceptions
std::domain_errorIf 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()

template<bool With_Witness, typename Acc , bool Checked, bool Exact_Difference, AlephGraph GT, typename Cost_Type >
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.

Template Parameters
With_WitnessWhether to extract a witness (pred is filled).
AccType of the sums of the table and of the witness.
CheckedWhether an addition can overflow Acc and has to be checked.
Exact_DifferenceWhether Acc holds the difference of two sums exactly.
Parameters
[in]incomingIncoming arcs of every node.
[in]dpThe sums: dp[k n + v] is the cheapest walk of k arcs that ends at v.
[in]predPosition, in incoming[v], of the arc that reaches each state; empty without the witness.
[in]infValue of Acc that marks an unreachable state.
[in]fnName of the public function, for error messages.
Returns
As karp_table().
Exceptions
std::overflow_errorIf a checked addition overflows.
std::domain_errorIf 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_solve()

template<bool With_Witness, AlephGraph GT, typename Cost_Type >
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:

  • Integral types that fit a 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.
  • Floating-point types. The sums are Cost_Type; one that is not finite throws.
  • Other types (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.

Template Parameters
With_WitnessWhether to keep predecessors and extract a witness.
Parameters
[in]incomingIncoming arcs of every node.
[in]fnName of the public function, for error messages.
Returns
The minimum mean and, if With_Witness, the witness, or witness_fits false if its cost does not fit Cost_Type.
Exceptions
std::overflow_errorIf the bytes of the tables do not fit a size_t, or a checked addition overflows.
std::domain_errorIf 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()

template<AlephGraph GT, typename Cost_Type >
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.

Parameters
[in]incomingIncoming arcs of every node.
Returns
The choice. O(E).

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_table()

template<bool With_Witness, typename Acc , bool Checked, bool Exact_Difference, AlephGraph GT, typename Cost_Type >
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().

Template Parameters
With_WitnessWhether to keep predecessors and extract a witness.
AccType of the sums of the table and of the witness.
CheckedWhether an addition can overflow Acc and has to be checked (it cannot when karp_solve() has bounded the sums).
Exact_DifferenceWhether Acc holds the difference of two sums exactly, so that it is rounded once, not each sum.
Parameters
[in]incomingIncoming arcs of every node.
[in]infValue of Acc that marks an unreachable state; no sum can take it.
[in]fnName of the public function, for error messages.
Returns
The minimum mean and, if 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.
Exceptions
std::overflow_errorIf a checked addition overflows.
std::domain_errorIf a floating-point accumulation is not finite.
Note
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.

◆ karp_table_bytes()

template<bool With_Witness, typename Cost_Type >
bool Aleph::min_mean_cycle_detail::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.

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.

Template Parameters
With_WitnessWhether the table of predecessors is kept.
Cost_TypeArc weight type.
Parameters
[in]nNumber of nodes.
[in]sumsHow the sums are kept (karp_sums()).
[out]bytesThe bytes, when they fit a size_t.
Returns
False if they do not fit a size_t.

Definition at line 816 of file Min_Mean_Cycle.H.

References Int128, Long_Long, Long_Long_Checked, and max().

◆ mean_less()

template<typename Acc >
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.

Parameters
[in]c1Total cost of the first cycle.
[in]l1Its length, positive.
[in]c2Total cost of the second cycle.
[in]l2Its length, positive.
Returns
True if the first mean is smaller.

Definition at line 295 of file Min_Mean_Cycle.H.

References l1, and l2.

Referenced by karp_answer(), and TEST().

◆ mean_order()

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

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.

Parameters
[in]aAn answer with a cycle.
[in]bAnother answer with a cycle.
Returns
Negative, zero or positive as the mean of 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().

◆ validate_finite_accumulator()

template<typename Cost_Type >
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.

Parameters
[in]valueValue to check.
[in]contextMessage of the exception.
Exceptions
std::domain_errorIf 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().