* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
/// \ingroup min_mean_cycle
/// \brief Karp's algorithm for finding a minimum mean cycle.
#include <lemon/tolerance.h>
#include <lemon/connectivity.h>
/// \brief Default traits class of Karp algorithm.
/// Default traits class of Karp algorithm.
/// \tparam GR The type of the digraph.
/// \tparam LEN The type of the length map.
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
template <typename GR, typename LEN>
template <typename GR, typename LEN,
bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
/// The type of the digraph
/// The type of the length map
/// The type of the arc lengths
typedef typename LengthMap::Value Value;
/// \brief The large value type used for internal computations
/// The large value type used for internal computations.
/// It is \c long \c long if the \c Value type is integer,
/// otherwise it is \c double.
/// \c Value must be convertible to \c LargeValue.
typedef double LargeValue;
/// The tolerance type used for internal computations
typedef lemon::Tolerance<LargeValue> Tolerance;
/// \brief The path type of the found cycles
/// The path type of the found cycles.
/// It must conform to the \ref lemon::concepts::Path "Path" concept
/// and it must have an \c addFront() function.
typedef lemon::Path<Digraph> Path;
// Default traits class for integer value types
template <typename GR, typename LEN>
struct KarpDefaultTraits<GR, LEN, true>
typedef typename LengthMap::Value Value;
#ifdef LEMON_HAVE_LONG_LONG
typedef long long LargeValue;
typedef lemon::Tolerance<LargeValue> Tolerance;
typedef lemon::Path<Digraph> Path;
/// \addtogroup min_mean_cycle
/// \brief Implementation of Karp's algorithm for finding a minimum
/// This class implements Karp's algorithm for finding a directed
/// cycle of minimum mean length (cost) in a digraph
/// \ref amo93networkflows, \ref dasdan98minmeancycle.
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
/// \tparam GR The type of the digraph the algorithm runs on.
/// \tparam LEN The type of the length map. The default
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
/// \tparam TR The traits class that defines various types used by the
/// algorithm. By default, it is \ref KarpDefaultTraits
/// "KarpDefaultTraits<GR, LEN>".
/// In most cases, this parameter should not be set directly,
/// consider to use the named template parameters instead.
template <typename GR, typename LEN, typename TR>
typename LEN = typename GR::template ArcMap<int>,
typename TR = KarpDefaultTraits<GR, LEN> >
/// The type of the digraph
typedef typename TR::Digraph Digraph;
/// The type of the length map
typedef typename TR::LengthMap LengthMap;
/// The type of the arc lengths
typedef typename TR::Value Value;
/// \brief The large value type
/// The large value type used for internal computations.
/// By default, it is \c long \c long if the \c Value type is integer,
/// otherwise it is \c double.
typedef typename TR::LargeValue LargeValue;
typedef typename TR::Tolerance Tolerance;
/// \brief The path type of the found cycles
/// The path type of the found cycles.
/// Using the \ref KarpDefaultTraits "default traits class",
/// it is \ref lemon::Path "Path<Digraph>".
typedef typename TR::Path Path;
/// The \ref KarpDefaultTraits "traits class" of the algorithm
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
// Data sturcture for path data
PathData(LargeValue d, Arc p = INVALID) :
typedef typename Digraph::template NodeMap<std::vector<PathData> >
// The digraph the algorithm runs on
// The length of the arcs
const LengthMap &_length;
// Data for storing the strongly connected components
typename Digraph::template NodeMap<int> _comp;
std::vector<std::vector<Node> > _comp_nodes;
std::vector<Node>* _nodes;
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
// Data for the found cycle
LargeValue _cycle_length;
// Node map for storing path data
// The processed nodes in the last round
std::vector<Node> _process;
/// \name Named Template Parameters
struct SetLargeValueTraits : public Traits {
typedef lemon::Tolerance<T> Tolerance;
/// \brief \ref named-templ-param "Named parameter" for setting
/// \ref named-templ-param "Named parameter" for setting \c LargeValue
/// type. It is used for internal computations in the algorithm.
: public Karp<GR, LEN, SetLargeValueTraits<T> > {
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create;
struct SetPathTraits : public Traits {
/// \brief \ref named-templ-param "Named parameter" for setting
/// \ref named-templ-param "Named parameter" for setting the \c %Path
/// type of the found cycles.
/// It must conform to the \ref lemon::concepts::Path "Path" concept
/// and it must have an \c addFront() function.
: public Karp<GR, LEN, SetPathTraits<T> > {
typedef Karp<GR, LEN, SetPathTraits<T> > Create;
/// The constructor of the class.
/// \param digraph The digraph the algorithm runs on.
/// \param length The lengths (costs) of the arcs.
Karp( const Digraph &digraph,
const LengthMap &length ) :
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
_cycle_path(NULL), _local_path(false), _data(digraph),
INF(std::numeric_limits<LargeValue>::has_infinity ?
std::numeric_limits<LargeValue>::infinity() :
std::numeric_limits<LargeValue>::max())
if (_local_path) delete _cycle_path;
/// \brief Set the path structure for storing the found cycle.
/// This function sets an external path structure for storing the
/// If you don't call this function before calling \ref run() or
/// \ref findMinMean(), it will allocate a local \ref Path "path"
/// structure. The destuctor deallocates this automatically
/// allocated object, of course.
/// \note The algorithm calls only the \ref lemon::Path::addFront()
/// "addFront()" function of the given path structure.
/// \return <tt>(*this)</tt>
Karp& cycle(Path &path) {
/// \brief Set the tolerance used by the algorithm.
/// This function sets the tolerance object used by the algorithm.
/// \return <tt>(*this)</tt>
Karp& tolerance(const Tolerance& tolerance) {
/// \brief Return a const reference to the tolerance.
/// This function returns a const reference to the tolerance object
/// used by the algorithm.
const Tolerance& tolerance() const {
/// \name Execution control
/// The simplest way to execute the algorithm is to call the \ref run()
/// If you only need the minimum mean length, you may call
/// \brief Run the algorithm.
/// This function runs the algorithm.
/// It can be called more than once (e.g. if the underlying digraph
/// and/or the arc lengths have been modified).
/// \return \c true if a directed cycle exists in the digraph.
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
/// return mmc.findMinMean() && mmc.findCycle();
return findMinMean() && findCycle();
/// \brief Find the minimum cycle mean.
/// This function finds the minimum mean length of the directed
/// cycles in the digraph.
/// \return \c true if a directed cycle exists in the digraph.
// Initialization and find strongly connected components
// Find the minimum cycle mean in the components
for (int comp = 0; comp < _comp_num; ++comp) {
if (!initComponent(comp)) continue;
return (_cycle_node != INVALID);
/// \brief Find a minimum mean directed cycle.
/// This function finds a directed cycle of minimum mean length
/// in the digraph using the data computed by findMinMean().
/// \return \c true if a directed cycle exists in the digraph.
/// \pre \ref findMinMean() must be called before using this function.
if (_cycle_node == INVALID) return false;
IntNodeMap reached(_gr, -1);
int r = _data[_cycle_node].size();
u = _gr.source(_data[u][r].pred);
Arc e = _data[u][r].pred;
_cycle_path->addFront(e);
_cycle_length = _length[e];
while ((v = _gr.source(e)) != u) {
_cycle_path->addFront(e);
_cycle_length += _length[e];
/// \name Query Functions
/// The results of the algorithm can be obtained using these
/// The algorithm should be executed before using them.
/// \brief Return the total length of the found cycle.
/// This function returns the total length of the found cycle.
/// \pre \ref run() or \ref findMinMean() must be called before
Value cycleLength() const {
return static_cast<Value>(_cycle_length);
/// \brief Return the number of arcs on the found cycle.
/// This function returns the number of arcs on the found cycle.
/// \pre \ref run() or \ref findMinMean() must be called before
int cycleArcNum() const {
/// \brief Return the mean length of the found cycle.
/// This function returns the mean length of the found cycle.
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
/// \pre \ref run() or \ref findMinMean() must be called before
double cycleMean() const {
return static_cast<double>(_cycle_length) / _cycle_size;
/// \brief Return the found cycle.
/// This function returns a const reference to the path structure
/// storing the found cycle.
/// \pre \ref run() or \ref findCycle() must be called before using
const Path& cycle() const {
for (NodeIt u(_gr); u != INVALID; ++u)
// Find strongly connected components and initialize _comp_nodes
_comp_num = stronglyConnectedComponents(_gr, _comp);
_comp_nodes.resize(_comp_num);
for (NodeIt n(_gr); n != INVALID; ++n) {
_comp_nodes[0].push_back(n);
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
_out_arcs[n].push_back(a);
for (int i = 0; i < _comp_num; ++i)
for (NodeIt n(_gr); n != INVALID; ++n) {
_comp_nodes[k].push_back(n);
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
// Initialize path data for the current component
bool initComponent(int comp) {
_nodes = &(_comp_nodes[comp]);
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
for (int i = 0; i < n; ++i) {
_data[(*_nodes)[i]].resize(n + 1, PathData(INF));
// Process all rounds of computing path data for the current component.
// _data[v][k] is the length of a shortest directed walk from the root
// node to node v containing exactly k arcs.
Node start = (*_nodes)[0];
_data[start][0] = PathData(0);
_process.push_back(start);
int k, n = _nodes->size();
for (k = 1; k <= n && int(_process.size()) < n; ++k) {
processNextBuildRound(k);
// Process one round and rebuild _process
void processNextBuildRound(int k) {
for (int i = 0; i < int(_process.size()); ++i) {
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
d = _data[u][k-1].dist + _length[e];
if (_tolerance.less(d, _data[v][k].dist)) {
if (_data[v][k].dist == INF) next.push_back(v);
_data[v][k] = PathData(d, e);
// Process one round using _nodes instead of _process
void processNextFullRound(int k) {
for (int i = 0; i < int(_nodes->size()); ++i) {
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
d = _data[u][k-1].dist + _length[e];
if (_tolerance.less(d, _data[v][k].dist)) {
_data[v][k] = PathData(d, e);
// Update the minimum cycle mean
for (int i = 0; i < n; ++i) {
if (_data[u][n].dist == INF) continue;
LargeValue length, max_length = 0;
for (int k = 0; k < n; ++k) {
if (_data[u][k].dist == INF) continue;
length = _data[u][n].dist - _data[u][k].dist;
if (!found_curr || length * max_size > max_length * size) {
if ( found_curr && (_cycle_node == INVALID ||
max_length * _cycle_size < _cycle_length * max_size) ) {
_cycle_length = max_length;