* 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
#ifndef LEMON_MIN_MEAN_CYCLE_H
#define LEMON_MIN_MEAN_CYCLE_H
/// \ingroup shortest_path
/// \brief Howard's algorithm for finding a minimum mean cycle.
#include <lemon/tolerance.h>
#include <lemon/connectivity.h>
/// \addtogroup shortest_path
/// \brief Implementation of Howard's algorithm for finding a minimum
/// \ref MinMeanCycle implements Howard's algorithm for finding a
/// directed cycle of minimum mean length (cost) in a digraph.
/// \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>".
/// \warning \c LEN::Value must be convertible to \c double.
template <typename GR, typename LEN>
typename LEN = typename GR::template ArcMap<int> >
/// The type of the digraph the algorithm runs on
/// The type of the length map
/// The type of the arc lengths
typedef typename LengthMap::Value Value;
/// The type of the paths
typedef lemon::Path<Digraph> Path;
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
// The digraph the algorithm runs on
// The length of the arcs
const LengthMap &_length;
// Data for the found cycles
bool _curr_found, _best_found;
Value _curr_length, _best_length;
int _curr_size, _best_size;
Node _curr_node, _best_node;
// Internal data used by the algorithm
typename Digraph::template NodeMap<Arc> _policy;
typename Digraph::template NodeMap<bool> _reached;
typename Digraph::template NodeMap<int> _level;
typename Digraph::template NodeMap<double> _dist;
// 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> > _in_arcs;
// Queue used for BFS search
std::vector<Node> _queue;
/// The constructor of the class.
/// \param digraph The digraph the algorithm runs on.
/// \param length The lengths (costs) of the arcs.
MinMeanCycle( const Digraph &digraph,
const LengthMap &length ) :
_gr(digraph), _length(length), _cycle_path(NULL), _local_path(false),
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
_comp(digraph), _in_arcs(digraph)
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::addBack()
/// "addBack()" function of the given path structure.
/// \return <tt>(*this)</tt>
MinMeanCycle& cyclePath(Path &path) {
/// \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.
// Initialize and find strongly connected components
// Find the minimum cycle mean in the components
for (int comp = 0; comp < _comp_num; ++comp) {
// Find the minimum mean cycle in the current component
if (!buildPolicyGraph(comp)) continue;
if (!computeNodeDistances()) break;
// Update the best cycle (global minimum mean cycle)
if ( !_best_found || (_curr_found &&
_curr_length * _best_size < _best_length * _curr_size) ) {
_best_length = _curr_length;
/// \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 (!_best_found) return false;
_cycle_path->addBack(_policy[_best_node]);
for ( Node v = _best_node;
(v = _gr.target(_policy[v])) != _best_node; ) {
_cycle_path->addBack(_policy[v]);
/// \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 {
/// \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>(_best_length) / _best_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 {
_queue.resize(countNodes(_gr));
// 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 (InArcIt a(_gr, n); a != INVALID; ++a) {
_in_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 (InArcIt a(_gr, n); a != INVALID; ++a) {
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
// Build the policy graph in the given strongly connected component
// (the out-degree of every node is 1)
bool buildPolicyGraph(int comp) {
_nodes = &(_comp_nodes[comp]);
if (_nodes->size() < 1 ||
(_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
for (int i = 0; i < int(_nodes->size()); ++i) {
_dist[(*_nodes)[i]] = std::numeric_limits<double>::max();
for (int i = 0; i < int(_nodes->size()); ++i) {
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
if (_length[e] < _dist[u]) {
// Find the minimum mean cycle in the policy graph
for (int i = 0; i < int(_nodes->size()); ++i) {
_level[(*_nodes)[i]] = -1;
for (int i = 0; i < int(_nodes->size()); ++i) {
if (_level[u] >= 0) continue;
for (; _level[u] < 0; u = _gr.target(_policy[u])) {
clength = _length[_policy[u]];
for (v = u; (v = _gr.target(_policy[v])) != u; ) {
clength += _length[_policy[v]];
(clength * _curr_size < _curr_length * csize) ) {
// Contract the policy graph and compute node distances
bool computeNodeDistances() {
// Find the component of the main cycle and compute node distances
for (int i = 0; i < int(_nodes->size()); ++i) {
_reached[(*_nodes)[i]] = false;
double curr_mean = double(_curr_length) / _curr_size;
_reached[_curr_node] = true;
while (_qfront <= _qback) {
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
if (_policy[u] == e && !_reached[u]) {
_dist[u] = _dist[v] + _length[e] - curr_mean;
// Connect all other nodes to this component and compute node
// distances using reverse BFS
while (_qback < int(_nodes->size())-1) {
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
_dist[u] = _dist[v] + _length[e] - curr_mean;
// Improve node distances
for (int i = 0; i < int(_nodes->size()); ++i) {
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
double delta = _dist[v] + _length[e] - curr_mean;
if (_tol.less(delta, _dist[u])) {
#endif //LEMON_MIN_MEAN_CYCLE_H