lemon/min_mean_cycle.h
author deba
Thu, 20 Mar 2008 16:25:47 +0000
changeset 2596 9c00e972cdfd
parent 2583 7216b6a52ab9
child 2618 6aa6fcaeaea5
permissions -rw-r--r--
Back porting commit 81563e019fa4
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@2588
   137
    /// The simplest way to execute the algorithm is to call the run()
kpeter@2588
   138
    /// function.
kpeter@2583
   139
    /// \n
kpeter@2583
   140
    /// If you only need the minimum mean value, you may call init()
kpeter@2583
   141
    /// and findMinMean().
kpeter@2583
   142
    /// \n
kpeter@2583
   143
    /// If you would like to run the algorithm again (e.g. the
kpeter@2583
   144
    /// underlaying graph and/or the edge costs were modified), you may
kpeter@2583
   145
    /// not create a new instance of the class, rather call reset(),
kpeter@2583
   146
    /// findMinMean(), and findCycle() instead.
kpeter@2555
   147
kpeter@2583
   148
    /// @{
deba@2437
   149
alpar@2409
   150
    /// \brief Runs the algorithm.
alpar@2409
   151
    ///
alpar@2409
   152
    /// Runs the algorithm.
alpar@2409
   153
    ///
kpeter@2555
   154
    /// \return Returns \c true if a directed cycle exists in the graph.
deba@2413
   155
    ///
kpeter@2555
   156
    /// \note Apart from the return value, <tt>mmc.run()</tt> is just a
kpeter@2517
   157
    /// shortcut of the following code.
deba@2413
   158
    /// \code
kpeter@2555
   159
    ///   mmc.init();
kpeter@2555
   160
    ///   mmc.findMinMean();
kpeter@2555
   161
    ///   mmc.findCycle();
deba@2413
   162
    /// \endcode
alpar@2409
   163
    bool run() {
alpar@2409
   164
      init();
kpeter@2555
   165
      return findMinMean() && findCycle();
deba@2413
   166
    }
deba@2437
   167
deba@2413
   168
    /// \brief Initializes the internal data structures.
kpeter@2517
   169
    ///
kpeter@2517
   170
    /// Initializes the internal data structures.
kpeter@2517
   171
    ///
kpeter@2517
   172
    /// \sa reset()
deba@2413
   173
    void init() {
kpeter@2583
   174
      _tolerance.epsilon(1e-6);
kpeter@2555
   175
      if (!_cycle_path) {
kpeter@2555
   176
        _local_path = true;
kpeter@2555
   177
        _cycle_path = new Path;
deba@2413
   178
      }
kpeter@2555
   179
      _cycle_found = false;
kpeter@2555
   180
      _component_num = stronglyConnectedComponents(_graph, _component);
deba@2413
   181
    }
deba@2413
   182
deba@2413
   183
    /// \brief Resets the internal data structures.
deba@2413
   184
    ///
deba@2437
   185
    /// Resets the internal data structures so that \ref findMinMean()
deba@2437
   186
    /// and \ref findCycle() can be called again (e.g. when the
deba@2413
   187
    /// underlaying graph has been modified).
kpeter@2517
   188
    ///
kpeter@2517
   189
    /// \sa init()
deba@2413
   190
    void reset() {
kpeter@2555
   191
      if (_cycle_path) _cycle_path->clear();
kpeter@2555
   192
      _cycle_found = false;
kpeter@2555
   193
      _component_num = stronglyConnectedComponents(_graph, _component);
kpeter@2555
   194
    }
kpeter@2555
   195
kpeter@2555
   196
    /// \brief Finds the minimum cycle mean length in the graph.
kpeter@2555
   197
    ///
kpeter@2555
   198
    /// Computes all the required data and finds the minimum cycle mean
kpeter@2555
   199
    /// length in the graph.
kpeter@2555
   200
    ///
kpeter@2555
   201
    /// \return Returns \c true if a directed cycle exists in the graph.
kpeter@2555
   202
    ///
kpeter@2555
   203
    /// \pre \ref init() must be called before using this function.
kpeter@2555
   204
    bool findMinMean() {
kpeter@2555
   205
      // Finding the minimum mean cycle in the components
kpeter@2555
   206
      for (int comp = 0; comp < _component_num; ++comp) {
kpeter@2555
   207
        if (!initCurrentComponent(comp)) continue;
kpeter@2555
   208
        while (true) {
kpeter@2583
   209
          if (!findPolicyCycles()) break;
kpeter@2555
   210
          contractPolicyGraph(comp);
kpeter@2583
   211
          if (!computeNodeDistances(comp)) break;
kpeter@2555
   212
        }
kpeter@2555
   213
      }
kpeter@2583
   214
      return _cycle_found;
kpeter@2555
   215
    }
kpeter@2555
   216
kpeter@2555
   217
    /// \brief Finds a critical (minimum mean) directed cycle.
kpeter@2555
   218
    ///
kpeter@2555
   219
    /// Finds a critical (minimum mean) directed cycle using the data
kpeter@2555
   220
    /// computed in the \ref findMinMean() function.
kpeter@2555
   221
    ///
kpeter@2555
   222
    /// \return Returns \c true if a directed cycle exists in the graph.
kpeter@2555
   223
    ///
kpeter@2555
   224
    /// \pre \ref init() and \ref findMinMean() must be called before
kpeter@2555
   225
    /// using this function.
kpeter@2555
   226
    bool findCycle() {
kpeter@2555
   227
      if (!_cycle_found) return false;
kpeter@2555
   228
      _cycle_path->addBack(_policy[_cycle_node]);
kpeter@2555
   229
      for ( Node v = _cycle_node;
kpeter@2555
   230
            (v = _graph.target(_policy[v])) != _cycle_node; ) {
kpeter@2555
   231
        _cycle_path->addBack(_policy[v]);
kpeter@2555
   232
      }
kpeter@2555
   233
      return true;
deba@2413
   234
    }
kpeter@2583
   235
    
kpeter@2583
   236
    /// @}
deba@2437
   237
kpeter@2583
   238
    /// \name Query Functions
kpeter@2583
   239
    /// The result of the algorithm can be obtained using these
kpeter@2583
   240
    /// functions.
kpeter@2588
   241
    /// \n The algorithm should be executed before using them.
kpeter@2583
   242
kpeter@2583
   243
    /// @{
kpeter@2583
   244
    
deba@2413
   245
    /// \brief Returns the total length of the found cycle.
deba@2413
   246
    ///
deba@2413
   247
    /// Returns the total length of the found cycle.
alpar@2409
   248
    ///
kpeter@2555
   249
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2555
   250
    /// using this function.
deba@2437
   251
    Length cycleLength() const {
kpeter@2555
   252
      return _cycle_length;
alpar@2409
   253
    }
deba@2437
   254
kpeter@2555
   255
    /// \brief Returns the number of edges on the found cycle.
alpar@2409
   256
    ///
kpeter@2555
   257
    /// Returns the number of edges on the found cycle.
alpar@2409
   258
    ///
kpeter@2555
   259
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2555
   260
    /// using this function.
deba@2437
   261
    int cycleEdgeNum() const {
kpeter@2555
   262
      return _cycle_size;
alpar@2409
   263
    }
deba@2437
   264
deba@2413
   265
    /// \brief Returns the mean length of the found cycle.
alpar@2409
   266
    ///
deba@2413
   267
    /// Returns the mean length of the found cycle.
alpar@2409
   268
    ///
kpeter@2517
   269
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@2517
   270
    /// using this function.
alpar@2409
   271
    ///
kpeter@2555
   272
    /// \note <tt>mmc.cycleMean()</tt> is just a shortcut of the
kpeter@2555
   273
    /// following code.
deba@2413
   274
    /// \code
kpeter@2555
   275
    ///   return double(mmc.cycleLength()) / mmc.cycleEdgeNum();
deba@2413
   276
    /// \endcode
kpeter@2555
   277
    double cycleMean() const {
kpeter@2583
   278
      return double(_cycle_length) / _cycle_size;
alpar@2409
   279
    }
alpar@2409
   280
kpeter@2555
   281
    /// \brief Returns a const reference to the \ref Path "path"
kpeter@2555
   282
    /// structure storing the found cycle.
alpar@2409
   283
    ///
kpeter@2555
   284
    /// Returns a const reference to the \ref Path "path"
kpeter@2555
   285
    /// structure storing the found cycle.
alpar@2409
   286
    ///
kpeter@2555
   287
    /// \pre \ref run() or \ref findCycle() must be called before using
kpeter@2555
   288
    /// this function.
alpar@2409
   289
    ///
kpeter@2555
   290
    /// \sa cyclePath()
deba@2437
   291
    const Path& cycle() const {
kpeter@2555
   292
      return *_cycle_path;
alpar@2409
   293
    }
kpeter@2583
   294
    
kpeter@2583
   295
    ///@}
kpeter@2583
   296
    
kpeter@2583
   297
  private:
deba@2437
   298
kpeter@2583
   299
    // Initializes the internal data structures for the current strongly
kpeter@2583
   300
    // connected component and creating the policy graph.
kpeter@2583
   301
    // The policy graph can be represented by the _policy map because
kpeter@2583
   302
    // the out degree of every node is 1.
kpeter@2583
   303
    bool initCurrentComponent(int comp) {
kpeter@2583
   304
      // Finding the nodes of the current component
kpeter@2583
   305
      _nodes.clear();
kpeter@2583
   306
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2583
   307
        if (_component[n] == comp) _nodes.push_back(n);
alpar@2409
   308
      }
kpeter@2583
   309
      if (_nodes.size() <= 1) return false;
kpeter@2583
   310
      // Finding the edges of the current component
kpeter@2583
   311
      _edges.clear();
kpeter@2583
   312
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2583
   313
        if ( _component[_graph.source(e)] == comp &&
kpeter@2583
   314
             _component[_graph.target(e)] == comp )
kpeter@2583
   315
          _edges.push_back(e);
kpeter@2583
   316
      }
kpeter@2583
   317
      // Initializing _reached, _dist, _policy maps
kpeter@2583
   318
      for (int i = 0; i < int(_nodes.size()); ++i) {
kpeter@2583
   319
        _reached[_nodes[i]] = false;
kpeter@2583
   320
        _policy[_nodes[i]] = INVALID;
kpeter@2583
   321
      }
kpeter@2583
   322
      Node u; Edge e;
kpeter@2583
   323
      for (int j = 0; j < int(_edges.size()); ++j) {
kpeter@2583
   324
        e = _edges[j];
kpeter@2583
   325
        u = _graph.source(e);
kpeter@2583
   326
        if (!_reached[u] || _length[e] < _dist[u]) {
kpeter@2583
   327
          _dist[u] = _length[e];
kpeter@2583
   328
          _policy[u] = e;
kpeter@2583
   329
          _reached[u] = true;
kpeter@2583
   330
        }
kpeter@2583
   331
      }
kpeter@2583
   332
      return true;
kpeter@2583
   333
    }
kpeter@2583
   334
kpeter@2583
   335
    // Finds all cycles in the policy graph.
kpeter@2583
   336
    // Sets _cycle_found to true if a cycle is found and sets
kpeter@2583
   337
    // _cycle_length, _cycle_size, _cycle_node to represent the minimum
kpeter@2583
   338
    // mean cycle in the policy graph.
kpeter@2583
   339
    bool findPolicyCycles() {
kpeter@2583
   340
      typename Graph::template NodeMap<int> level(_graph, -1);
kpeter@2583
   341
      bool curr_cycle_found = false;
kpeter@2583
   342
      Length clength;
kpeter@2583
   343
      int csize;
kpeter@2583
   344
      int path_cnt = 0;
kpeter@2583
   345
      Node u, v;
kpeter@2583
   346
      // Searching for cycles
kpeter@2583
   347
      for (int i = 0; i < int(_nodes.size()); ++i) {
kpeter@2583
   348
        if (level[_nodes[i]] < 0) {
kpeter@2583
   349
          u = _nodes[i];
kpeter@2583
   350
          level[u] = path_cnt;
kpeter@2583
   351
          while (level[u = _graph.target(_policy[u])] < 0)
kpeter@2583
   352
            level[u] = path_cnt;
kpeter@2583
   353
          if (level[u] == path_cnt) {
kpeter@2583
   354
            // A cycle is found
kpeter@2583
   355
            curr_cycle_found = true;
kpeter@2583
   356
            clength = _length[_policy[u]];
kpeter@2583
   357
            csize = 1;
kpeter@2583
   358
            for (v = u; (v = _graph.target(_policy[v])) != u; ) {
kpeter@2583
   359
              clength += _length[_policy[v]];
kpeter@2583
   360
              ++csize;
kpeter@2583
   361
            }
kpeter@2583
   362
            if ( !_cycle_found ||
kpeter@2583
   363
                 clength * _cycle_size < _cycle_length * csize ) {
kpeter@2583
   364
              _cycle_found = true;
kpeter@2583
   365
              _cycle_length = clength;
kpeter@2583
   366
              _cycle_size = csize;
kpeter@2583
   367
              _cycle_node = u;
kpeter@2583
   368
            }
kpeter@2583
   369
          }
kpeter@2583
   370
          ++path_cnt;
kpeter@2583
   371
        }
kpeter@2583
   372
      }
kpeter@2583
   373
      return curr_cycle_found;
kpeter@2583
   374
    }
kpeter@2583
   375
kpeter@2583
   376
    // Contracts the policy graph to be connected by cutting all cycles
kpeter@2583
   377
    // except for the main cycle (i.e. the minimum mean cycle).
kpeter@2583
   378
    void contractPolicyGraph(int comp) {
kpeter@2583
   379
      // Finding the component of the main cycle using
kpeter@2583
   380
      // reverse BFS search
kpeter@2583
   381
      typename Graph::template NodeMap<int> found(_graph, false);
kpeter@2583
   382
      std::deque<Node> queue;
kpeter@2583
   383
      queue.push_back(_cycle_node);
kpeter@2583
   384
      found[_cycle_node] = true;
kpeter@2583
   385
      Node u, v;
kpeter@2583
   386
      while (!queue.empty()) {
kpeter@2583
   387
        v = queue.front(); queue.pop_front();
kpeter@2583
   388
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   389
          u = _graph.source(e);
kpeter@2583
   390
          if (_component[u] == comp && !found[u] && _policy[u] == e) {
kpeter@2583
   391
            found[u] = true;
kpeter@2583
   392
            queue.push_back(u);
kpeter@2583
   393
          }
kpeter@2583
   394
        }
kpeter@2583
   395
      }
kpeter@2583
   396
      // Connecting all other nodes to this component using
kpeter@2583
   397
      // reverse BFS search
kpeter@2583
   398
      queue.clear();
kpeter@2583
   399
      for (int i = 0; i < int(_nodes.size()); ++i)
kpeter@2583
   400
        if (found[_nodes[i]]) queue.push_back(_nodes[i]);
kpeter@2583
   401
      int found_cnt = queue.size();
kpeter@2583
   402
      while (found_cnt < int(_nodes.size()) && !queue.empty()) {
kpeter@2583
   403
        v = queue.front(); queue.pop_front();
kpeter@2583
   404
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   405
          u = _graph.source(e);
kpeter@2583
   406
          if (_component[u] == comp && !found[u]) {
kpeter@2583
   407
            found[u] = true;
kpeter@2583
   408
            ++found_cnt;
kpeter@2583
   409
            _policy[u] = e;
kpeter@2583
   410
            queue.push_back(u);
kpeter@2583
   411
          }
kpeter@2583
   412
        }
kpeter@2583
   413
      }
kpeter@2583
   414
    }
kpeter@2583
   415
kpeter@2583
   416
    // Computes node distances in the policy graph and updates the
kpeter@2583
   417
    // policy graph if the node distances can be improved.
kpeter@2583
   418
    bool computeNodeDistances(int comp) {
kpeter@2583
   419
      // Computing node distances using reverse BFS search
kpeter@2583
   420
      double cycle_mean = double(_cycle_length) / _cycle_size;
kpeter@2583
   421
      typename Graph::template NodeMap<int> found(_graph, false);
kpeter@2583
   422
      std::deque<Node> queue;
kpeter@2583
   423
      queue.push_back(_cycle_node);
kpeter@2583
   424
      found[_cycle_node] = true;
kpeter@2583
   425
      _dist[_cycle_node] = 0;
kpeter@2583
   426
      Node u, v;
kpeter@2583
   427
      while (!queue.empty()) {
kpeter@2583
   428
        v = queue.front(); queue.pop_front();
kpeter@2583
   429
        for (InEdgeIt e(_graph, v); e != INVALID; ++e) {
kpeter@2583
   430
          u = _graph.source(e);
kpeter@2583
   431
          if (_component[u] == comp && !found[u] && _policy[u] == e) {
kpeter@2583
   432
            found[u] = true;
kpeter@2583
   433
            _dist[u] = _dist[v] + _length[e] - cycle_mean;
kpeter@2583
   434
            queue.push_back(u);
kpeter@2583
   435
          }
kpeter@2583
   436
        }
kpeter@2583
   437
      }
kpeter@2583
   438
      // Improving node distances
kpeter@2583
   439
      bool improved = false;
kpeter@2583
   440
      for (int j = 0; j < int(_edges.size()); ++j) {
kpeter@2583
   441
        Edge e = _edges[j];
kpeter@2583
   442
        u = _graph.source(e); v = _graph.target(e);
kpeter@2583
   443
        double delta = _dist[v] + _length[e] - cycle_mean;
kpeter@2583
   444
        if (_tolerance.less(delta, _dist[u])) {
kpeter@2583
   445
          improved = true;
kpeter@2583
   446
          _dist[u] = delta;
kpeter@2583
   447
          _policy[u] = e;
kpeter@2583
   448
        }
kpeter@2583
   449
      }
kpeter@2583
   450
      return improved;
alpar@2409
   451
    }
alpar@2409
   452
alpar@2409
   453
  }; //class MinMeanCycle
alpar@2409
   454
alpar@2409
   455
  ///@}
alpar@2409
   456
alpar@2409
   457
} //namespace lemon
alpar@2409
   458
alpar@2409
   459
#endif //LEMON_MIN_MEAN_CYCLE_H