Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Min_Mean_Cycle.H File Reference

Minimum mean cycle in directed graphs (Karp algorithm). More...

#include <ah-graph-concepts.H>
#include <cmath>
#include <cstddef>
#include <limits>
#include <type_traits>
#include <utility>
#include <ah-errors.H>
#include <shortest_path_common.H>
#include <htlist.H>
#include <tpl_array.H>
#include <tpl_dynMapTree.H>
Include dependency graph for Min_Mean_Cycle.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >
 Result of minimum mean cycle computation. More...
 
struct  Aleph::Min_Mean_Cycle_Value_Result
 Lightweight minimum-mean-cycle value result. More...
 
struct  Aleph::min_mean_cycle_detail::Incoming_Arc< GT, Cost_Type >
 Arc entering a node, as Karp's table reads it. More...
 
struct  Aleph::min_mean_cycle_detail::Karp_Answer< GT, Cost_Type >
 Answer of karp_solve(), in the node positions of its input. More...
 
class  Aleph::Karp_Minimum_Mean_Cycle< GT, Distance, SA >
 Functor wrapper for Karp minimum mean cycle. More...
 
class  Aleph::Karp_Minimum_Mean_Cycle_Value< GT, Distance, SA >
 Functor wrapper for value-only minimum mean cycle. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::min_mean_cycle_detail
 

Macros

#define ALEPH_KARP_INT128   0
 Whether Karp's table can use 128-bit integers when the sums of a long long table could overflow: GCC and Clang (__SIZEOF_INT128__), except clang-cl, as ALEPH_HOWARD_INT128 in Howard_Min_Mean_Cycle.H.
 

Enumerations

enum class  Aleph::min_mean_cycle_detail::Karp_Sums { Aleph::min_mean_cycle_detail::Cost_Type_Checked , Aleph::min_mean_cycle_detail::Long_Long , Aleph::min_mean_cycle_detail::Long_Long_Checked , Aleph::min_mean_cycle_detail::Int128 }
 How karp_solve() keeps the sums of a table (see there). More...
 

Functions

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.
 
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.
 
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.
 
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.
 
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().
 
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<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.
 
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.
 
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.
 
template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > Aleph::karp_minimum_mean_cycle (const GT &g, Distance distance=Distance(), SA sa=SA())
 Compute minimum mean cycle by Karp's algorithm.
 
template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Min_Mean_Cycle_Value_Result Aleph::karp_minimum_mean_cycle_value (const GT &g, Distance distance=Distance(), SA sa=SA())
 Compute only the minimum mean value (without witness walk).
 
template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Min_Mean_Cycle_Value_Result Aleph::minimum_mean_cycle_value (const GT &g, Distance distance=Distance(), SA sa=SA())
 Alias for karp_minimum_mean_cycle_value().
 
template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > Aleph::minimum_mean_cycle (const GT &g, Distance distance=Distance(), SA sa=SA())
 Alias for karp_minimum_mean_cycle().
 

Detailed Description

Minimum mean cycle in directed graphs (Karp algorithm).

This header provides a dedicated API to compute the minimum cycle mean in a directed weighted graph using Karp's dynamic programming algorithm.

Problem solved

Given a directed graph \(G=(V,E)\)with arc costs \(w(e)\), find the cycle \(C\)minimizing the average weight:

\[ \mu^* = \min_C \frac{w(C)}{|C|} \]

Highlights

  • Works with Aleph directed graph backends (List_Digraph, Array_Graph, ...)
  • Supports arc filters (SA) and custom distance accessors
  • Accepts negative and positive weights (only finite arithmetic costs)
  • Returns minimum mean and a cycle witness when available
  • Includes value-only API when only the optimal mean is required

Arithmetic

The table adds up to \(V\) weights per entry. Integer costs of at most 64 bits never overflow it: the sums are long long when \(V \cdot \max|w|\) stays below LLONG_MAX, and 128-bit integers otherwise (GCC and Clang, ALEPH_KARP_INT128); only the cost of the reported witness has to fit the cost type, and a minimum-mean cycle whose cost fits is preferred to one of the same mean that does not. Without 128-bit integers (MSVC), and for unsigned long long or 128-bit costs, the sums are checked and may overflow. Floating-point sums must stay finite. Integral means are compared exactly where 128-bit integers exist, and in long double otherwise; the published minimum_mean is a long double.

Complexity

  • Time: \(O(VE)\)
  • Space: \(O(V^2)\) (twice as many bytes for the table when its sums need 128 bits)
Author
Leandro Rabindranath León

Definition in file Min_Mean_Cycle.H.

Macro Definition Documentation

◆ ALEPH_KARP_INT128

#define ALEPH_KARP_INT128   0

Whether Karp's table can use 128-bit integers when the sums of a long long table could overflow: GCC and Clang (__SIZEOF_INT128__), except clang-cl, as ALEPH_HOWARD_INT128 in Howard_Min_Mean_Cycle.H.

Definition at line 101 of file Min_Mean_Cycle.H.