lemon/cycle_canceling.h
changeset 814 0643a9c2c3ae
child 815 aef153f430e1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/cycle_canceling.h	Fri Nov 13 00:09:35 2009 +0100
     1.3 @@ -0,0 +1,559 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_CYCLE_CANCELING_H
    1.23 +#define LEMON_CYCLE_CANCELING_H
    1.24 +
    1.25 +/// \ingroup min_cost_flow
    1.26 +///
    1.27 +/// \file
    1.28 +/// \brief Cycle-canceling algorithm for finding a minimum cost flow.
    1.29 +
    1.30 +#include <vector>
    1.31 +#include <lemon/adaptors.h>
    1.32 +#include <lemon/path.h>
    1.33 +
    1.34 +#include <lemon/circulation.h>
    1.35 +#include <lemon/bellman_ford.h>
    1.36 +#include <lemon/howard.h>
    1.37 +
    1.38 +namespace lemon {
    1.39 +
    1.40 +  /// \addtogroup min_cost_flow
    1.41 +  /// @{
    1.42 +
    1.43 +  /// \brief Implementation of a cycle-canceling algorithm for
    1.44 +  /// finding a minimum cost flow.
    1.45 +  ///
    1.46 +  /// \ref CycleCanceling implements a cycle-canceling algorithm for
    1.47 +  /// finding a minimum cost flow.
    1.48 +  ///
    1.49 +  /// \tparam Digraph The digraph type the algorithm runs on.
    1.50 +  /// \tparam LowerMap The type of the lower bound map.
    1.51 +  /// \tparam CapacityMap The type of the capacity (upper bound) map.
    1.52 +  /// \tparam CostMap The type of the cost (length) map.
    1.53 +  /// \tparam SupplyMap The type of the supply map.
    1.54 +  ///
    1.55 +  /// \warning
    1.56 +  /// - Arc capacities and costs should be \e non-negative \e integers.
    1.57 +  /// - Supply values should be \e signed \e integers.
    1.58 +  /// - The value types of the maps should be convertible to each other.
    1.59 +  /// - \c CostMap::Value must be signed type.
    1.60 +  ///
    1.61 +  /// \note By default the \ref BellmanFord "Bellman-Ford" algorithm is
    1.62 +  /// used for negative cycle detection with limited iteration number.
    1.63 +  /// However \ref CycleCanceling also provides the "Minimum Mean
    1.64 +  /// Cycle-Canceling" algorithm, which is \e strongly \e polynomial,
    1.65 +  /// but rather slower in practice.
    1.66 +  /// To use this version of the algorithm, call \ref run() with \c true
    1.67 +  /// parameter.
    1.68 +  ///
    1.69 +  /// \author Peter Kovacs
    1.70 +  template < typename Digraph,
    1.71 +             typename LowerMap = typename Digraph::template ArcMap<int>,
    1.72 +             typename CapacityMap = typename Digraph::template ArcMap<int>,
    1.73 +             typename CostMap = typename Digraph::template ArcMap<int>,
    1.74 +             typename SupplyMap = typename Digraph::template NodeMap<int> >
    1.75 +  class CycleCanceling
    1.76 +  {
    1.77 +    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
    1.78 +
    1.79 +    typedef typename CapacityMap::Value Capacity;
    1.80 +    typedef typename CostMap::Value Cost;
    1.81 +    typedef typename SupplyMap::Value Supply;
    1.82 +    typedef typename Digraph::template ArcMap<Capacity> CapacityArcMap;
    1.83 +    typedef typename Digraph::template NodeMap<Supply> SupplyNodeMap;
    1.84 +
    1.85 +    typedef ResidualDigraph< const Digraph,
    1.86 +      CapacityArcMap, CapacityArcMap > ResDigraph;
    1.87 +    typedef typename ResDigraph::Node ResNode;
    1.88 +    typedef typename ResDigraph::NodeIt ResNodeIt;
    1.89 +    typedef typename ResDigraph::Arc ResArc;
    1.90 +    typedef typename ResDigraph::ArcIt ResArcIt;
    1.91 +
    1.92 +  public:
    1.93 +
    1.94 +    /// The type of the flow map.
    1.95 +    typedef typename Digraph::template ArcMap<Capacity> FlowMap;
    1.96 +    /// The type of the potential map.
    1.97 +    typedef typename Digraph::template NodeMap<Cost> PotentialMap;
    1.98 +
    1.99 +  private:
   1.100 +
   1.101 +    /// \brief Map adaptor class for handling residual arc costs.
   1.102 +    ///
   1.103 +    /// Map adaptor class for handling residual arc costs.
   1.104 +    class ResidualCostMap : public MapBase<ResArc, Cost>
   1.105 +    {
   1.106 +    private:
   1.107 +
   1.108 +      const CostMap &_cost_map;
   1.109 +
   1.110 +    public:
   1.111 +
   1.112 +      ///\e
   1.113 +      ResidualCostMap(const CostMap &cost_map) : _cost_map(cost_map) {}
   1.114 +
   1.115 +      ///\e
   1.116 +      Cost operator[](const ResArc &e) const {
   1.117 +        return ResDigraph::forward(e) ? _cost_map[e] : -_cost_map[e];
   1.118 +      }
   1.119 +
   1.120 +    }; //class ResidualCostMap
   1.121 +
   1.122 +  private:
   1.123 +
   1.124 +    // The maximum number of iterations for the first execution of the
   1.125 +    // Bellman-Ford algorithm. It should be at least 2.
   1.126 +    static const int BF_FIRST_LIMIT  = 2;
   1.127 +    // The iteration limit for the Bellman-Ford algorithm is multiplied
   1.128 +    // by BF_LIMIT_FACTOR/100 in every round.
   1.129 +    static const int BF_LIMIT_FACTOR = 150;
   1.130 +
   1.131 +  private:
   1.132 +
   1.133 +    // The digraph the algorithm runs on
   1.134 +    const Digraph &_graph;
   1.135 +    // The original lower bound map
   1.136 +    const LowerMap *_lower;
   1.137 +    // The modified capacity map
   1.138 +    CapacityArcMap _capacity;
   1.139 +    // The original cost map
   1.140 +    const CostMap &_cost;
   1.141 +    // The modified supply map
   1.142 +    SupplyNodeMap _supply;
   1.143 +    bool _valid_supply;
   1.144 +
   1.145 +    // Arc map of the current flow
   1.146 +    FlowMap *_flow;
   1.147 +    bool _local_flow;
   1.148 +    // Node map of the current potentials
   1.149 +    PotentialMap *_potential;
   1.150 +    bool _local_potential;
   1.151 +
   1.152 +    // The residual digraph
   1.153 +    ResDigraph *_res_graph;
   1.154 +    // The residual cost map
   1.155 +    ResidualCostMap _res_cost;
   1.156 +
   1.157 +  public:
   1.158 +
   1.159 +    /// \brief General constructor (with lower bounds).
   1.160 +    ///
   1.161 +    /// General constructor (with lower bounds).
   1.162 +    ///
   1.163 +    /// \param digraph The digraph the algorithm runs on.
   1.164 +    /// \param lower The lower bounds of the arcs.
   1.165 +    /// \param capacity The capacities (upper bounds) of the arcs.
   1.166 +    /// \param cost The cost (length) values of the arcs.
   1.167 +    /// \param supply The supply values of the nodes (signed).
   1.168 +    CycleCanceling( const Digraph &digraph,
   1.169 +                    const LowerMap &lower,
   1.170 +                    const CapacityMap &capacity,
   1.171 +                    const CostMap &cost,
   1.172 +                    const SupplyMap &supply ) :
   1.173 +      _graph(digraph), _lower(&lower), _capacity(digraph), _cost(cost),
   1.174 +      _supply(digraph), _flow(NULL), _local_flow(false),
   1.175 +      _potential(NULL), _local_potential(false),
   1.176 +      _res_graph(NULL), _res_cost(_cost)
   1.177 +    {
   1.178 +      // Check the sum of supply values
   1.179 +      Supply sum = 0;
   1.180 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   1.181 +        _supply[n] = supply[n];
   1.182 +        sum += _supply[n];
   1.183 +      }
   1.184 +      _valid_supply = sum == 0;
   1.185 +
   1.186 +      // Remove non-zero lower bounds
   1.187 +      for (ArcIt e(_graph); e != INVALID; ++e) {
   1.188 +        _capacity[e] = capacity[e];
   1.189 +        if (lower[e] != 0) {
   1.190 +          _capacity[e] -= lower[e];
   1.191 +          _supply[_graph.source(e)] -= lower[e];
   1.192 +          _supply[_graph.target(e)] += lower[e];
   1.193 +        }
   1.194 +      }
   1.195 +    }
   1.196 +/*
   1.197 +    /// \brief General constructor (without lower bounds).
   1.198 +    ///
   1.199 +    /// General constructor (without lower bounds).
   1.200 +    ///
   1.201 +    /// \param digraph The digraph the algorithm runs on.
   1.202 +    /// \param capacity The capacities (upper bounds) of the arcs.
   1.203 +    /// \param cost The cost (length) values of the arcs.
   1.204 +    /// \param supply The supply values of the nodes (signed).
   1.205 +    CycleCanceling( const Digraph &digraph,
   1.206 +                    const CapacityMap &capacity,
   1.207 +                    const CostMap &cost,
   1.208 +                    const SupplyMap &supply ) :
   1.209 +      _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost),
   1.210 +      _supply(supply), _flow(NULL), _local_flow(false),
   1.211 +      _potential(NULL), _local_potential(false), _res_graph(NULL),
   1.212 +      _res_cost(_cost)
   1.213 +    {
   1.214 +      // Check the sum of supply values
   1.215 +      Supply sum = 0;
   1.216 +      for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
   1.217 +      _valid_supply = sum == 0;
   1.218 +    }
   1.219 +
   1.220 +    /// \brief Simple constructor (with lower bounds).
   1.221 +    ///
   1.222 +    /// Simple constructor (with lower bounds).
   1.223 +    ///
   1.224 +    /// \param digraph The digraph the algorithm runs on.
   1.225 +    /// \param lower The lower bounds of the arcs.
   1.226 +    /// \param capacity The capacities (upper bounds) of the arcs.
   1.227 +    /// \param cost The cost (length) values of the arcs.
   1.228 +    /// \param s The source node.
   1.229 +    /// \param t The target node.
   1.230 +    /// \param flow_value The required amount of flow from node \c s
   1.231 +    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
   1.232 +    CycleCanceling( const Digraph &digraph,
   1.233 +                    const LowerMap &lower,
   1.234 +                    const CapacityMap &capacity,
   1.235 +                    const CostMap &cost,
   1.236 +                    Node s, Node t,
   1.237 +                    Supply flow_value ) :
   1.238 +      _graph(digraph), _lower(&lower), _capacity(capacity), _cost(cost),
   1.239 +      _supply(digraph, 0), _flow(NULL), _local_flow(false),
   1.240 +      _potential(NULL), _local_potential(false), _res_graph(NULL),
   1.241 +      _res_cost(_cost)
   1.242 +    {
   1.243 +      // Remove non-zero lower bounds
   1.244 +      _supply[s] =  flow_value;
   1.245 +      _supply[t] = -flow_value;
   1.246 +      for (ArcIt e(_graph); e != INVALID; ++e) {
   1.247 +        if (lower[e] != 0) {
   1.248 +          _capacity[e] -= lower[e];
   1.249 +          _supply[_graph.source(e)] -= lower[e];
   1.250 +          _supply[_graph.target(e)] += lower[e];
   1.251 +        }
   1.252 +      }
   1.253 +      _valid_supply = true;
   1.254 +    }
   1.255 +
   1.256 +    /// \brief Simple constructor (without lower bounds).
   1.257 +    ///
   1.258 +    /// Simple constructor (without lower bounds).
   1.259 +    ///
   1.260 +    /// \param digraph The digraph the algorithm runs on.
   1.261 +    /// \param capacity The capacities (upper bounds) of the arcs.
   1.262 +    /// \param cost The cost (length) values of the arcs.
   1.263 +    /// \param s The source node.
   1.264 +    /// \param t The target node.
   1.265 +    /// \param flow_value The required amount of flow from node \c s
   1.266 +    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
   1.267 +    CycleCanceling( const Digraph &digraph,
   1.268 +                    const CapacityMap &capacity,
   1.269 +                    const CostMap &cost,
   1.270 +                    Node s, Node t,
   1.271 +                    Supply flow_value ) :
   1.272 +      _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost),
   1.273 +      _supply(digraph, 0), _flow(NULL), _local_flow(false),
   1.274 +      _potential(NULL), _local_potential(false), _res_graph(NULL),
   1.275 +      _res_cost(_cost)
   1.276 +    {
   1.277 +      _supply[s] =  flow_value;
   1.278 +      _supply[t] = -flow_value;
   1.279 +      _valid_supply = true;
   1.280 +    }
   1.281 +*/
   1.282 +    /// Destructor.
   1.283 +    ~CycleCanceling() {
   1.284 +      if (_local_flow) delete _flow;
   1.285 +      if (_local_potential) delete _potential;
   1.286 +      delete _res_graph;
   1.287 +    }
   1.288 +
   1.289 +    /// \brief Set the flow map.
   1.290 +    ///
   1.291 +    /// Set the flow map.
   1.292 +    ///
   1.293 +    /// \return \c (*this)
   1.294 +    CycleCanceling& flowMap(FlowMap &map) {
   1.295 +      if (_local_flow) {
   1.296 +        delete _flow;
   1.297 +        _local_flow = false;
   1.298 +      }
   1.299 +      _flow = &map;
   1.300 +      return *this;
   1.301 +    }
   1.302 +
   1.303 +    /// \brief Set the potential map.
   1.304 +    ///
   1.305 +    /// Set the potential map.
   1.306 +    ///
   1.307 +    /// \return \c (*this)
   1.308 +    CycleCanceling& potentialMap(PotentialMap &map) {
   1.309 +      if (_local_potential) {
   1.310 +        delete _potential;
   1.311 +        _local_potential = false;
   1.312 +      }
   1.313 +      _potential = &map;
   1.314 +      return *this;
   1.315 +    }
   1.316 +
   1.317 +    /// \name Execution control
   1.318 +
   1.319 +    /// @{
   1.320 +
   1.321 +    /// \brief Run the algorithm.
   1.322 +    ///
   1.323 +    /// Run the algorithm.
   1.324 +    ///
   1.325 +    /// \param min_mean_cc Set this parameter to \c true to run the
   1.326 +    /// "Minimum Mean Cycle-Canceling" algorithm, which is strongly
   1.327 +    /// polynomial, but rather slower in practice.
   1.328 +    ///
   1.329 +    /// \return \c true if a feasible flow can be found.
   1.330 +    bool run(bool min_mean_cc = false) {
   1.331 +      return init() && start(min_mean_cc);
   1.332 +    }
   1.333 +
   1.334 +    /// @}
   1.335 +
   1.336 +    /// \name Query Functions
   1.337 +    /// The result of the algorithm can be obtained using these
   1.338 +    /// functions.\n
   1.339 +    /// \ref lemon::CycleCanceling::run() "run()" must be called before
   1.340 +    /// using them.
   1.341 +
   1.342 +    /// @{
   1.343 +
   1.344 +    /// \brief Return a const reference to the arc map storing the
   1.345 +    /// found flow.
   1.346 +    ///
   1.347 +    /// Return a const reference to the arc map storing the found flow.
   1.348 +    ///
   1.349 +    /// \pre \ref run() must be called before using this function.
   1.350 +    const FlowMap& flowMap() const {
   1.351 +      return *_flow;
   1.352 +    }
   1.353 +
   1.354 +    /// \brief Return a const reference to the node map storing the
   1.355 +    /// found potentials (the dual solution).
   1.356 +    ///
   1.357 +    /// Return a const reference to the node map storing the found
   1.358 +    /// potentials (the dual solution).
   1.359 +    ///
   1.360 +    /// \pre \ref run() must be called before using this function.
   1.361 +    const PotentialMap& potentialMap() const {
   1.362 +      return *_potential;
   1.363 +    }
   1.364 +
   1.365 +    /// \brief Return the flow on the given arc.
   1.366 +    ///
   1.367 +    /// Return the flow on the given arc.
   1.368 +    ///
   1.369 +    /// \pre \ref run() must be called before using this function.
   1.370 +    Capacity flow(const Arc& arc) const {
   1.371 +      return (*_flow)[arc];
   1.372 +    }
   1.373 +
   1.374 +    /// \brief Return the potential of the given node.
   1.375 +    ///
   1.376 +    /// Return the potential of the given node.
   1.377 +    ///
   1.378 +    /// \pre \ref run() must be called before using this function.
   1.379 +    Cost potential(const Node& node) const {
   1.380 +      return (*_potential)[node];
   1.381 +    }
   1.382 +
   1.383 +    /// \brief Return the total cost of the found flow.
   1.384 +    ///
   1.385 +    /// Return the total cost of the found flow. The complexity of the
   1.386 +    /// function is \f$ O(e) \f$.
   1.387 +    ///
   1.388 +    /// \pre \ref run() must be called before using this function.
   1.389 +    Cost totalCost() const {
   1.390 +      Cost c = 0;
   1.391 +      for (ArcIt e(_graph); e != INVALID; ++e)
   1.392 +        c += (*_flow)[e] * _cost[e];
   1.393 +      return c;
   1.394 +    }
   1.395 +
   1.396 +    /// @}
   1.397 +
   1.398 +  private:
   1.399 +
   1.400 +    /// Initialize the algorithm.
   1.401 +    bool init() {
   1.402 +      if (!_valid_supply) return false;
   1.403 +
   1.404 +      // Initializing flow and potential maps
   1.405 +      if (!_flow) {
   1.406 +        _flow = new FlowMap(_graph);
   1.407 +        _local_flow = true;
   1.408 +      }
   1.409 +      if (!_potential) {
   1.410 +        _potential = new PotentialMap(_graph);
   1.411 +        _local_potential = true;
   1.412 +      }
   1.413 +
   1.414 +      _res_graph = new ResDigraph(_graph, _capacity, *_flow);
   1.415 +
   1.416 +      // Finding a feasible flow using Circulation
   1.417 +      Circulation< Digraph, ConstMap<Arc, Capacity>, CapacityArcMap,
   1.418 +                   SupplyMap >
   1.419 +        circulation( _graph, constMap<Arc>(Capacity(0)), _capacity,
   1.420 +                     _supply );
   1.421 +      return circulation.flowMap(*_flow).run();
   1.422 +    }
   1.423 +
   1.424 +    bool start(bool min_mean_cc) {
   1.425 +      if (min_mean_cc)
   1.426 +        startMinMean();
   1.427 +      else
   1.428 +        start();
   1.429 +
   1.430 +      // Handling non-zero lower bounds
   1.431 +      if (_lower) {
   1.432 +        for (ArcIt e(_graph); e != INVALID; ++e)
   1.433 +          (*_flow)[e] += (*_lower)[e];
   1.434 +      }
   1.435 +      return true;
   1.436 +    }
   1.437 +
   1.438 +    /// \brief Execute the algorithm using \ref BellmanFord.
   1.439 +    ///
   1.440 +    /// Execute the algorithm using the \ref BellmanFord
   1.441 +    /// "Bellman-Ford" algorithm for negative cycle detection with
   1.442 +    /// successively larger limit for the number of iterations.
   1.443 +    void start() {
   1.444 +      typename BellmanFord<ResDigraph, ResidualCostMap>::PredMap pred(*_res_graph);
   1.445 +      typename ResDigraph::template NodeMap<int> visited(*_res_graph);
   1.446 +      std::vector<ResArc> cycle;
   1.447 +      int node_num = countNodes(_graph);
   1.448 +
   1.449 +      int length_bound = BF_FIRST_LIMIT;
   1.450 +      bool optimal = false;
   1.451 +      while (!optimal) {
   1.452 +        BellmanFord<ResDigraph, ResidualCostMap> bf(*_res_graph, _res_cost);
   1.453 +        bf.predMap(pred);
   1.454 +        bf.init(0);
   1.455 +        int iter_num = 0;
   1.456 +        bool cycle_found = false;
   1.457 +        while (!cycle_found) {
   1.458 +          int curr_iter_num = iter_num + length_bound <= node_num ?
   1.459 +                              length_bound : node_num - iter_num;
   1.460 +          iter_num += curr_iter_num;
   1.461 +          int real_iter_num = curr_iter_num;
   1.462 +          for (int i = 0; i < curr_iter_num; ++i) {
   1.463 +            if (bf.processNextWeakRound()) {
   1.464 +              real_iter_num = i;
   1.465 +              break;
   1.466 +            }
   1.467 +          }
   1.468 +          if (real_iter_num < curr_iter_num) {
   1.469 +            // Optimal flow is found
   1.470 +            optimal = true;
   1.471 +            // Setting node potentials
   1.472 +            for (NodeIt n(_graph); n != INVALID; ++n)
   1.473 +              (*_potential)[n] = bf.dist(n);
   1.474 +            break;
   1.475 +          } else {
   1.476 +            // Searching for node disjoint negative cycles
   1.477 +            for (ResNodeIt n(*_res_graph); n != INVALID; ++n)
   1.478 +              visited[n] = 0;
   1.479 +            int id = 0;
   1.480 +            for (ResNodeIt n(*_res_graph); n != INVALID; ++n) {
   1.481 +              if (visited[n] > 0) continue;
   1.482 +              visited[n] = ++id;
   1.483 +              ResNode u = pred[n] == INVALID ?
   1.484 +                          INVALID : _res_graph->source(pred[n]);
   1.485 +              while (u != INVALID && visited[u] == 0) {
   1.486 +                visited[u] = id;
   1.487 +                u = pred[u] == INVALID ?
   1.488 +                    INVALID : _res_graph->source(pred[u]);
   1.489 +              }
   1.490 +              if (u != INVALID && visited[u] == id) {
   1.491 +                // Finding the negative cycle
   1.492 +                cycle_found = true;
   1.493 +                cycle.clear();
   1.494 +                ResArc e = pred[u];
   1.495 +                cycle.push_back(e);
   1.496 +                Capacity d = _res_graph->residualCapacity(e);
   1.497 +                while (_res_graph->source(e) != u) {
   1.498 +                  cycle.push_back(e = pred[_res_graph->source(e)]);
   1.499 +                  if (_res_graph->residualCapacity(e) < d)
   1.500 +                    d = _res_graph->residualCapacity(e);
   1.501 +                }
   1.502 +
   1.503 +                // Augmenting along the cycle
   1.504 +                for (int i = 0; i < int(cycle.size()); ++i)
   1.505 +                  _res_graph->augment(cycle[i], d);
   1.506 +              }
   1.507 +            }
   1.508 +          }
   1.509 +
   1.510 +          if (!cycle_found)
   1.511 +            length_bound = length_bound * BF_LIMIT_FACTOR / 100;
   1.512 +        }
   1.513 +      }
   1.514 +    }
   1.515 +
   1.516 +    /// \brief Execute the algorithm using \ref Howard.
   1.517 +    ///
   1.518 +    /// Execute the algorithm using \ref Howard for negative
   1.519 +    /// cycle detection.
   1.520 +    void startMinMean() {
   1.521 +      typedef Path<ResDigraph> ResPath;
   1.522 +      Howard<ResDigraph, ResidualCostMap> mmc(*_res_graph, _res_cost);
   1.523 +      ResPath cycle;
   1.524 +
   1.525 +      mmc.cycle(cycle);
   1.526 +      if (mmc.findMinMean()) {
   1.527 +        while (mmc.cycleLength() < 0) {
   1.528 +          // Finding the cycle
   1.529 +          mmc.findCycle();
   1.530 +
   1.531 +          // Finding the largest flow amount that can be augmented
   1.532 +          // along the cycle
   1.533 +          Capacity delta = 0;
   1.534 +          for (typename ResPath::ArcIt e(cycle); e != INVALID; ++e) {
   1.535 +            if (delta == 0 || _res_graph->residualCapacity(e) < delta)
   1.536 +              delta = _res_graph->residualCapacity(e);
   1.537 +          }
   1.538 +
   1.539 +          // Augmenting along the cycle
   1.540 +          for (typename ResPath::ArcIt e(cycle); e != INVALID; ++e)
   1.541 +            _res_graph->augment(e, delta);
   1.542 +
   1.543 +          // Finding the minimum cycle mean for the modified residual
   1.544 +          // digraph
   1.545 +          if (!mmc.findMinMean()) break;
   1.546 +        }
   1.547 +      }
   1.548 +
   1.549 +      // Computing node potentials
   1.550 +      BellmanFord<ResDigraph, ResidualCostMap> bf(*_res_graph, _res_cost);
   1.551 +      bf.init(0); bf.start();
   1.552 +      for (NodeIt n(_graph); n != INVALID; ++n)
   1.553 +        (*_potential)[n] = bf.dist(n);
   1.554 +    }
   1.555 +
   1.556 +  }; //class CycleCanceling
   1.557 +
   1.558 +  ///@}
   1.559 +
   1.560 +} //namespace lemon
   1.561 +
   1.562 +#endif //LEMON_CYCLE_CANCELING_H