lemon/min_mean_cycle.h
author kpeter
Fri, 29 Feb 2008 15:55:39 +0000
changeset 2587 061be2e64eca
parent 2555 a84e52e99f57
child 2588 4d3bc1d04c1d
permissions -rw-r--r--
External flow and potential maps can be used in MinCostMaxFlow.
alpar@2409
     1
/* -*- C++ -*-
alpar@2409
     2
 *
alpar@2409
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2409
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2409
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2409
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2409
     8
 *
alpar@2409
     9
 * Permission to use, modify and distribute this software is granted
alpar@2409
    10
 * provided that this copyright notice appears in all copies. For
alpar@2409
    11
 * precise terms see the accompanying LICENSE file.
alpar@2409
    12
 *
alpar@2409
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2409
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2409
    15
 * purpose.
alpar@2409
    16
 *
alpar@2409
    17
 */
alpar@2409
    18
alpar@2409
    19
#ifndef LEMON_MIN_MEAN_CYCLE_H
alpar@2409
    20
#define LEMON_MIN_MEAN_CYCLE_H
alpar@2409
    21
deba@2529
    22
/// \ingroup shortest_path
alpar@2409
    23
///
deba@2437
    24
/// \file
kpeter@2555
    25
/// \brief Howard's algorithm for finding a minimum mean directed cycle.
alpar@2409
    26
kpeter@2509
    27
#include <vector>
alpar@2409
    28
#include <lemon/graph_utils.h>
alpar@2409
    29
#include <lemon/path.h>
kpeter@2555
    30
#include <lemon/tolerance.h>
kpeter@2583
    31
#include <lemon/topology.h>
alpar@2409
    32
alpar@2409
    33
namespace lemon {
alpar@2409
    34
deba@2529
    35
  /// \addtogroup shortest_path
alpar@2409
    36
  /// @{
alpar@2409
    37
kpeter@2555
    38
  /// \brief Implementation of Howard's algorithm for finding a minimum
kpeter@2555
    39
  /// mean directed cycle.
alpar@2409
    40
  ///
kpeter@2555
    41
  /// \ref MinMeanCycle implements Howard's algorithm for finding a
kpeter@2555
    42
  /// minimum mean directed cycle.
alpar@2409
    43
  ///
kpeter@2583
    44
  /// \tparam Graph The directed graph type the algorithm runs on.
kpeter@2583
    45
  /// \tparam LengthMap The type of the length (cost) map.
alpar@2409
    46
  ///
kpeter@2555
    47
  /// \warning \c LengthMap::Value must be convertible to \c double.
kpeter@2555
    48
  ///
alpar@2409
    49
  /// \author Peter Kovacs
alpar@2409
    50
kpeter@2555
    51
  template < typename Graph,
kpeter@2555
    52
             typename LengthMap = typename Graph::template EdgeMap<int> >
alpar@2409
    53
  class MinMeanCycle
alpar@2409
    54
  {
kpeter@2555
    55
    GRAPH_TYPEDEFS(typename Graph);
alpar@2409
    56
alpar@2409
    57
    typedef typename LengthMap::Value Length;
alpar@2409
    58
    typedef Path<Graph> Path;
alpar@2409
    59
kpeter@2583
    60
  private:
deba@2413
    61
kpeter@2583
    62
    // The directed graph the algorithm runs on
kpeter@2555
    63
    const Graph &_graph;
kpeter@2583
    64
    // The length of the edges
kpeter@2555
    65
    const LengthMap &_length;
deba@2413
    66
kpeter@2583
    67
    // The total length of the found cycle
kpeter@2555
    68
    Length _cycle_length;
kpeter@2583
    69
    // The number of edges on the found cycle
kpeter@2555
    70
    int _cycle_size;
kpeter@2583
    71
    // The found cycle
kpeter@2555
    72
    Path *_cycle_path;
deba@2413
    73
kpeter@2555
    74
    bool _local_path;
kpeter@2555
    75
    bool _cycle_found;
kpeter@2555
    76
    Node _cycle_node;
alpar@2409
    77
kpeter@2583
    78
    typename Graph::template NodeMap<bool> _reached;
kpeter@2583
    79
    typename Graph::template NodeMap<double> _dist;
kpeter@2583
    80
    typename Graph::template NodeMap<Edge> _policy;
kpeter@2583
    81
kpeter@2555
    82
    typename Graph::template NodeMap<int> _component;
kpeter@2555
    83
    int _component_num;
deba@2437
    84
kpeter@2555
    85
    std::vector<Node> _nodes;
kpeter@2555
    86
    std::vector<Edge> _edges;
kpeter@2555
    87
    Tolerance<double> _tolerance;
deba@2437
    88
kpeter@2555
    89
  public:
alpar@2409
    90
alpar@2409
    91
    /// \brief The constructor of the class.
alpar@2409
    92
    ///
alpar@2409
    93
    /// The constructor of the class.
alpar@2409
    94
    ///
kpeter@2555
    95
    /// \param graph The directed graph the algorithm runs on.
kpeter@2555
    96
    /// \param length The length (cost) of the edges.
kpeter@2555
    97
    MinMeanCycle( const Graph &graph,
kpeter@2555
    98
                  const LengthMap &length ) :
kpeter@2583
    99
      _graph(graph), _length(length), _cycle_length(0), _cycle_size(-1),
kpeter@2583
   100
      _cycle_path(NULL), _local_path(false), _reached(graph),
kpeter@2583
   101
      _dist(graph), _policy(graph), _component(graph)
kpeter@2583
   102
    {}
alpar@2409
   103
kpeter@2555
   104
    /// The destructor of the class.
deba@2437
   105
    ~MinMeanCycle() {
kpeter@2555
   106
      if (_local_path) delete _cycle_path;
alpar@2409
   107
    }
alpar@2409
   108
kpeter@2583
   109
    /// \brief Sets the \ref Path "path" structure for storing the found
kpeter@2583
   110
    /// cycle.
kpeter@2583
   111
    ///
kpeter@2583
   112
    /// Sets an external \ref Path "path" structure for storing the
kpeter@2583
   113
    /// found cycle.
kpeter@2583
   114
    ///
kpeter@2583
   115
    /// If you don't call this function before calling \ref run() or
kpeter@2583
   116
    /// \ref init(), it will allocate a local \ref Path "path"
kpeter@2583
   117
    /// structure.
kpeter@2583
   118
    /// The destuctor deallocates this automatically allocated map,
kpeter@2583
   119
    /// of course.
kpeter@2583
   120
    ///
kpeter@2583
   121
    /// \note The algorithm calls only the \ref lemon::Path::addBack()
kpeter@2583
   122
    /// "addBack()" function of the given \ref Path "path" structure.
kpeter@2583
   123
    ///
kpeter@2583
   124
    /// \return <tt>(*this)</tt>
kpeter@2583
   125
    ///
kpeter@2583
   126
    /// \sa cycle()
kpeter@2583
   127
    MinMeanCycle& cyclePath(Path &path) {
kpeter@2583
   128
      if (_local_path) {
kpeter@2583
   129
        delete _cycle_path;
kpeter@2583
   130
        _local_path = false;
alpar@2409
   131
      }
kpeter@2583
   132
      _cycle_path = &path;
kpeter@2583
   133
      return *this;
kpeter@2555
   134
    }
kpeter@2555
   135
kpeter@2583
   136
    /// \name Execution control
kpeter@2583
   137
    /// The simplest way to execute the algorithm is to call run().
kpeter@2583
   138
    /// \n
kpeter@2583
   139
    /// If you only need the minimum mean value, you may call init()
kpeter@2583
   140
    /// and findMinMean().
kpeter@2583
   141
    /// \n
kpeter@2583
   142
    /// If you would like to run the algorithm again (e.g. the
kpeter@2583
   143
    /// underlaying graph and/or the edge costs were modified), you may
kpeter@2583
   144
    /// not create a new instance of the class, rather call reset(),
kpeter@2583
   145
    /// findMinMean(), and findCycle() instead.
kpeter@2555
   146
kpeter@2583
   147
    /// @{
deba@2437
   148
alpar@2409
   149
    /// \brief Runs the algorithm.
alpar@2409
   150
    ///
alpar@2409
   151
    /// Runs the algorithm.
alpar@2409
   152
    ///
kpeter@2555
   153
    /// \return Returns \c true if a directed cycle exists in the graph.
deba@2413
   154
    ///
kpeter@2555
   155
    /// \note Apart from the return value, <tt>mmc.run()</tt> is just a
kpeter@2517
   156
    /// shortcut of the following code.
deba@2413
   157
    /// \code
kpeter@2555
   158
    ///   mmc.init();
kpeter@2555
   159
    ///   mmc.findMinMean();
kpeter@2555
   160
    ///   mmc.findCycle();
deba@2413
   161
    /// \endcode
alpar@2409
   162
    bool run() {
alpar@2409
   163
      init();
kpeter@2555
   164
      return findMinMean() && findCycle();
deba@2413
   165
    }
deba@2437
   166
deba@2413
   167
    /// \brief Initializes the internal data structures.
kpeter@2517
   168
    ///
kpeter@2517
   169
    /// Initializes the internal data structures.
kpeter@2517
   170
    ///
kpeter@2517
   171
    /// \sa reset()
deba@2413
   172
    void init() {
kpeter@2583
   173
      _tolerance.epsilon(1e-6);
kpeter@2555
   174
      if (!_cycle_path) {
kpeter@2555
   175
        _local_path = true;
kpeter@2555
   176
        _cycle_path = new Path;
deba@2413
   177
      }
kpeter@2555
   178
      _cycle_found = false;
kpeter@2555
   179
      _component_num = stronglyConnectedComponents(_graph, _component);
deba@2413
   180
    }
deba@2413
   181
deba@2413
   182
    /// \brief Resets the internal data structures.
deba@2413
   183
    ///
deba@2437
   184
    /// Resets the internal data structures so that \ref findMinMean()
deba@2437
   185
    /// and \ref findCycle() can be called again (e.g. when the
deba@2413
   186
    /// underlaying graph has been modified).
kpeter@2517
   187
    ///
kpeter@2517
   188
    /// \sa init()
deba@2413
   189
    void reset() {
kpeter@2555
   190
      if (_cycle_path) _cycle_path->clear();
kpeter@2555
   191
      _cycle_found = false;
kpeter@2555
   192
      _component_num = stronglyConnectedComponents(_graph, _component);
kpeter@2555
   193
    }
kpeter@2555
   194
kpeter@2555
   195
    /// \brief Finds the minimum cycle mean length in the graph.
kpeter@2555
   196
    ///
kpeter@2555
   197
    /// Computes all the required data and finds the minimum cycle mean
kpeter@2555
   198
    /// length in the graph.
kpeter@2555
   199
    ///
kpeter@2555
   200
    /// \return Returns \c true if a directed cycle exists in the graph.
kpeter@2555
   201
    ///
kpeter@2555
   202
    /// \pre \ref init() must be called before using this function.
kpeter@2555
   203
    bool findMinMean() {
kpeter@2555
   204
      // Finding the minimum mean cycle in the components
kpeter@2555
   205
      for (int comp = 0; comp < _component_num; ++comp) {
kpeter@2555
   206
        if (!initCurrentComponent(comp)) continue;
kpeter@2555
   207
        while (true) {
kpeter@2583
   208
          if (!findPolicyCycles()) break;
kpeter@2555
   209
          contractPolicyGraph(comp);
kpeter@2583
   210
          if (!computeNodeDistances(comp)) break;
kpeter@2555
   211
        }
kpeter@2555
   212
      }
kpeter@2583
   213
      return _cycle_found;
kpeter@2555
   214
    }
kpeter@2555
   215
kpeter@2555
   216
    /// \brief Finds a critical (minimum mean) directed cycle.
kpeter@2555
   217
    ///
kpeter@2555
   218
    /// Finds a critical (minimum mean) directed cycle using the data
kpeter@2555
   219
    /// computed in the \ref findMinMean() function.
kpeter@2555
   220
    ///
kpeter@2555
   221
    /// \return Returns \c true if a directed cycle exists in the graph.
kpeter@2555
   222
    ///
kpeter@2555
   223
    /// \pre \ref init() and \ref findMinMean() must be called before
kpeter@2555
   224
    /// using this function.
kpeter@2555
   225
    bool findCycle() {
kpeter@2555
   226
      if (!_cycle_found) return false;
kpeter@2555
   227
      _cycle_path->addBack(_policy[_cycle_node]);
kpeter@2555
   228
      for ( Node v = _cycle_node;
kpeter@2555
   229
            (v = _graph.target(_policy[v])) != _cycle_node; ) {
kpeter@2555
   230
        _cycle_path->addBack(_policy[v]);
kpeter@2555
   231
      }
kpeter@2555
   232
      return true;
deba@2413
   233
    }
kpeter@2583
   234
    
kpeter@2583
   235
    /// @}
deba@2437
   236
kpeter@2583
   237
    /// \name Query Functions
kpeter@2583
   238
    /// The result of the algorithm can be obtained using these
kpeter@2583
   239
    /// functions.
kpeter@2583
   240
    /// \n run() must be called before using them.
kpeter@2583
   241
kpeter@2583
   242
    /// @{
kpeter@2583
   243
    
deba@2413
   244
    /// \brief Returns the total length of the found cycle.
deba@2413
   245
    ///
deba@2413
   246
    /// Returns the total length of the found cycle.
alpar@2409
   247
    ///
kpeter@2555
   248
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2555
   249
    /// using this function.
deba@2437
   250
    Length cycleLength() const {
kpeter@2555
   251
      return _cycle_length;
alpar@2409
   252
    }
deba@2437
   253
kpeter@2555
   254
    /// \brief Returns the number of edges on the found cycle.
alpar@2409
   255
    ///
kpeter@2555
   256
    /// Returns the number of edges on the found cycle.
alpar@2409
   257
    ///
kpeter@2555
   258
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2555
   259
    /// using this function.
deba@2437
   260
    int cycleEdgeNum() const {
kpeter@2555
   261
      return _cycle_size;
alpar@2409
   262
    }
deba@2437
   263
deba@2413
   264
    /// \brief Returns the mean length of the found cycle.
alpar@2409
   265
    ///
deba@2413
   266
    /// Returns the mean length of the found cycle.
alpar@2409
   267
    ///
kpeter@2517
   268
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2517
   269
    /// using this function.
alpar@2409
   270
    ///
kpeter@2555
   271
    /// \note <tt>mmc.cycleMean()</tt> is just a shortcut of the
kpeter@2555
   272
    /// following code.
deba@2413
   273
    /// \code
kpeter@2555
   274
    ///   return double(mmc.cycleLength()) / mmc.cycleEdgeNum();
deba@2413
   275
    /// \endcode
kpeter@2555
   276
    double cycleMean() const {
kpeter@2583
   277
      return double(_cycle_length) / _cycle_size;
alpar@2409
   278
    }
alpar@2409
   279
kpeter@2555
   280
    /// \brief Returns a const reference to the \ref Path "path"
kpeter@2555
   281
    /// structure storing the found cycle.
alpar@2409
   282
    ///
kpeter@2555
   283
    /// Returns a const reference to the \ref Path "path"
kpeter@2555
   284
    /// structure storing the found cycle.
alpar@2409
   285
    ///
kpeter@2555
   286
    /// \pre \ref run() or \ref findCycle() must be called before using
kpeter@2555
   287
    /// this function.
alpar@2409
   288
    ///
kpeter@2555
   289
    /// \sa cyclePath()
deba@2437
   290
    const Path& cycle() const {
kpeter@2555
   291
      return *_cycle_path;
alpar@2409
   292
    }
kpeter@2583
   293
    
kpeter@2583
   294
    ///@}
kpeter@2583
   295
    
kpeter@2583
   296
  private:
deba@2437
   297
kpeter@2583
   298
    // Initializes the internal data structures for the current strongly
kpeter@2583
   299
    // connected component and creating the policy graph.
kpeter@2583
   300
    // The policy graph can be represented by the _policy map because
kpeter@2583
   301
    // the out degree of every node is 1.
kpeter@2583
   302
    bool initCurrentComponent(int comp) {
kpeter@2583
   303
      // Finding the nodes of the current component
kpeter@2583
   304
      _nodes.clear();
kpeter@2583
   305
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2583
   306
        if (_component[n] == comp) _nodes.push_back(n);
alpar@2409
   307
      }
kpeter@2583
   308
      if (_nodes.size() <= 1) return false;
kpeter@2583
   309
      // Finding the edges of the current component
kpeter@2583
   310
      _edges.clear();
kpeter@2583
   311
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2583
   312
        if ( _component[_graph.source(e)] == comp &&
kpeter@2583
   313
             _component[_graph.target(e)] == comp )
kpeter@2583
   314
          _edges.push_back(e);
kpeter@2583
   315
      }
kpeter@2583
   316
      // Initializing _reached, _dist, _policy maps
kpeter@2583
   317
      for (int i = 0; i < int(_nodes.size()); ++i) {
kpeter@2583
   318
        _reached[_nodes[i]] = false;
kpeter@2583
   319
        _policy[_nodes[i]] = INVALID;
kpeter@2583
   320
      }
kpeter@2583
   321
      Node u; Edge e;
kpeter@2583
   322
      for (int j = 0; j < int(_edges.size()); ++j) {
kpeter@2583
   323
        e = _edges[j];
kpeter@2583
   324
        u = _graph.source(e);
kpeter@2583
   325
        if (!_reached[u] || _length[e] < _dist[u]) {
kpeter@2583
   326
          _dist[u] = _length[e];
kpeter@2583
   327
          _policy[u] = e;
kpeter@2583
   328
          _reached[u] = true;
kpeter@2583
   329
        }
kpeter@2583
   330
      }
kpeter@2583
   331
      return true;
kpeter@2583
   332
    }
kpeter@2583
   333
kpeter@2583
   334
    // Finds all cycles in the policy graph.
kpeter@2583
   335
    // Sets _cycle_found to true if a cycle is found and sets
kpeter@2583
   336
    // _cycle_length, _cycle_size, _cycle_node to represent the minimum
kpeter@2583
   337
    // mean cycle in the policy graph.
kpeter@2583
   338
    bool findPolicyCycles() {
kpeter@2583
   339
      typename Graph::template NodeMap<int> level(_graph, -1);
kpeter@2583
   340
      bool curr_cycle_found = false;
kpeter@2583
   341
      Length clength;
kpeter@2583
   342
      int csize;
kpeter@2583
   343
      int path_cnt = 0;
kpeter@2583
   344
      Node u, v;
kpeter@2583
   345
      // Searching for cycles
kpeter@2583
   346
      for (int i = 0; i < int(_nodes.size()); ++i) {
kpeter@2583
   347
        if (level[_nodes[i]] < 0) {
kpeter@2583
   348
          u = _nodes[i];
kpeter@2583
   349
          level[u] = path_cnt;
kpeter@2583
   350
          while (level[u = _graph.target(_policy[u])] < 0)
kpeter@2583
   351
            level[u] = path_cnt;
kpeter@2583
   352
          if (level[u] == path_cnt) {
kpeter@2583
   353
            // A cycle is found
kpeter@2583
   354
            curr_cycle_found = true;
kpeter@2583
   355
            clength = _length[_policy[u]];
kpeter@2583
   356
            csize = 1;
kpeter@2583
   357
            for (v = u; (v = _graph.target(_policy[v])) != u; ) {
kpeter@2583
   358
              clength += _length[_policy[v]];
kpeter@2583
   359
              ++csize;
kpeter@2583
   360
            }
kpeter@2583
   361
            if ( !_cycle_found ||
kpeter@2583
   362
                 clength * _cycle_size < _cycle_length * csize ) {
kpeter@2583
   363
              _cycle_found = true;
kpeter@2583
   364
              _cycle_length = clength;
kpeter@2583
   365
              _cycle_size = csize;
kpeter@2583
   366
              _cycle_node = u;
kpeter@2583
   367
            }
kpeter@2583
   368
          }
kpeter@2583
   369
          ++path_cnt;
kpeter@2583
   370
        }
kpeter@2583
   371
      }
kpeter@2583
   372
      return curr_cycle_found;
kpeter@2583
   373
    }
kpeter@2583
   374
kpeter@2583
   375
    // Contracts the policy graph to be connected by cutting all cycles
kpeter@2583
   376
    // except for the main cycle (i.e. the minimum mean cycle).
kpeter@2583
   377
    void contractPolicyGraph(int comp) {
kpeter@2583
   378
      // Finding the component of the main cycle using
kpeter@2583
   379
      // reverse BFS search
kpeter@2583
   380
      typename Graph::template NodeMap<int> found(_graph, false);
kpeter@2583
   381
      std::deque<Node> queue;
kpeter@2583
   382
      queue.push_back(_cycle_node);
kpeter@2583
   383
      found[_cycle_node] = true;
kpeter@2583
   384
      Node u, v;
kpeter@2583
   385
      while (!queue.empty()) {
kpeter@2583
   386
        v = queue.front(); queue.pop_front();
kpeter@2583
   387
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   388
          u = _graph.source(e);
kpeter@2583
   389
          if (_component[u] == comp && !found[u] && _policy[u] == e) {
kpeter@2583
   390
            found[u] = true;
kpeter@2583
   391
            queue.push_back(u);
kpeter@2583
   392
          }
kpeter@2583
   393
        }
kpeter@2583
   394
      }
kpeter@2583
   395
      // Connecting all other nodes to this component using
kpeter@2583
   396
      // reverse BFS search
kpeter@2583
   397
      queue.clear();
kpeter@2583
   398
      for (int i = 0; i < int(_nodes.size()); ++i)
kpeter@2583
   399
        if (found[_nodes[i]]) queue.push_back(_nodes[i]);
kpeter@2583
   400
      int found_cnt = queue.size();
kpeter@2583
   401
      while (found_cnt < int(_nodes.size()) && !queue.empty()) {
kpeter@2583
   402
        v = queue.front(); queue.pop_front();
kpeter@2583
   403
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   404
          u = _graph.source(e);
kpeter@2583
   405
          if (_component[u] == comp && !found[u]) {
kpeter@2583
   406
            found[u] = true;
kpeter@2583
   407
            ++found_cnt;
kpeter@2583
   408
            _policy[u] = e;
kpeter@2583
   409
            queue.push_back(u);
kpeter@2583
   410
          }
kpeter@2583
   411
        }
kpeter@2583
   412
      }
kpeter@2583
   413
    }
kpeter@2583
   414
kpeter@2583
   415
    // Computes node distances in the policy graph and updates the
kpeter@2583
   416
    // policy graph if the node distances can be improved.
kpeter@2583
   417
    bool computeNodeDistances(int comp) {
kpeter@2583
   418
      // Computing node distances using reverse BFS search
kpeter@2583
   419
      double cycle_mean = double(_cycle_length) / _cycle_size;
kpeter@2583
   420
      typename Graph::template NodeMap<int> found(_graph, false);
kpeter@2583
   421
      std::deque<Node> queue;
kpeter@2583
   422
      queue.push_back(_cycle_node);
kpeter@2583
   423
      found[_cycle_node] = true;
kpeter@2583
   424
      _dist[_cycle_node] = 0;
kpeter@2583
   425
      Node u, v;
kpeter@2583
   426
      while (!queue.empty()) {
kpeter@2583
   427
        v = queue.front(); queue.pop_front();
kpeter@2583
   428
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   429
          u = _graph.source(e);
kpeter@2583
   430
          if (_component[u] == comp && !found[u] && _policy[u] == e) {
kpeter@2583
   431
            found[u] = true;
kpeter@2583
   432
            _dist[u] = _dist[v] + _length[e] - cycle_mean;
kpeter@2583
   433
            queue.push_back(u);
kpeter@2583
   434
          }
kpeter@2583
   435
        }
kpeter@2583
   436
      }
kpeter@2583
   437
      // Improving node distances
kpeter@2583
   438
      bool improved = false;
kpeter@2583
   439
      for (int j = 0; j < int(_edges.size()); ++j) {
kpeter@2583
   440
        Edge e = _edges[j];
kpeter@2583
   441
        u = _graph.source(e); v = _graph.target(e);
kpeter@2583
   442
        double delta = _dist[v] + _length[e] - cycle_mean;
kpeter@2583
   443
        if (_tolerance.less(delta, _dist[u])) {
kpeter@2583
   444
          improved = true;
kpeter@2583
   445
          _dist[u] = delta;
kpeter@2583
   446
          _policy[u] = e;
kpeter@2583
   447
        }
kpeter@2583
   448
      }
kpeter@2583
   449
      return improved;
alpar@2409
   450
    }
alpar@2409
   451
alpar@2409
   452
  }; //class MinMeanCycle
alpar@2409
   453
alpar@2409
   454
  ///@}
alpar@2409
   455
alpar@2409
   456
} //namespace lemon
alpar@2409
   457
alpar@2409
   458
#endif //LEMON_MIN_MEAN_CYCLE_H