lemon/cycle_canceling.h
author alpar
Tue, 05 Jun 2007 17:29:46 +0000
changeset 2451 d7b7048e045b
child 2457 8c791ee69a45
permissions -rw-r--r--
Two new constructors added to dim2::BoundingBox
deba@2440
     1
/* -*- C++ -*-
deba@2440
     2
 *
deba@2440
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2440
     4
 *
deba@2440
     5
 * Copyright (C) 2003-2007
deba@2440
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2440
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2440
     8
 *
deba@2440
     9
 * Permission to use, modify and distribute this software is granted
deba@2440
    10
 * provided that this copyright notice appears in all copies. For
deba@2440
    11
 * precise terms see the accompanying LICENSE file.
deba@2440
    12
 *
deba@2440
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2440
    14
 * express or implied, and with no claim as to its suitability for any
deba@2440
    15
 * purpose.
deba@2440
    16
 *
deba@2440
    17
 */
deba@2440
    18
deba@2440
    19
#ifndef LEMON_CYCLE_CANCELING_H
deba@2440
    20
#define LEMON_CYCLE_CANCELING_H
deba@2440
    21
deba@2440
    22
/// \ingroup min_cost_flow
deba@2440
    23
///
deba@2440
    24
/// \file
deba@2440
    25
/// \brief A cycle canceling algorithm for finding a minimum cost flow.
deba@2440
    26
deba@2440
    27
#include <vector>
deba@2440
    28
#include <lemon/circulation.h>
deba@2440
    29
#include <lemon/graph_adaptor.h>
deba@2440
    30
deba@2440
    31
/// \brief The used cycle canceling method.
deba@2440
    32
#define LIMITED_CYCLE_CANCELING
deba@2440
    33
//#define MIN_MEAN_CYCLE_CANCELING
deba@2440
    34
deba@2440
    35
#ifdef LIMITED_CYCLE_CANCELING
deba@2440
    36
  #include <lemon/bellman_ford.h>
deba@2440
    37
  /// \brief The maximum number of iterations for the first execution
deba@2440
    38
  /// of the \ref lemon::BellmanFord "Bellman-Ford" algorithm.
deba@2440
    39
  #define STARTING_LIMIT	2
deba@2440
    40
  /// \brief The iteration limit for the
deba@2440
    41
  /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
deba@2440
    42
  /// <tt>ALPHA_MUL % ALPHA_DIV</tt> in every round.
deba@2440
    43
  #define ALPHA_MUL		3
deba@2440
    44
  /// \brief The iteration limit for the
deba@2440
    45
  /// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
deba@2440
    46
  /// <tt>ALPHA_MUL % ALPHA_DIV</tt> in every round.
deba@2440
    47
  #define ALPHA_DIV		2
deba@2440
    48
deba@2440
    49
//#define _ONLY_ONE_CYCLE_
deba@2440
    50
//#define _DEBUG_ITER_
deba@2440
    51
#endif
deba@2440
    52
deba@2440
    53
#ifdef MIN_MEAN_CYCLE_CANCELING
deba@2440
    54
  #include <lemon/min_mean_cycle.h>
deba@2440
    55
  #include <lemon/path.h>
deba@2440
    56
#endif
deba@2440
    57
deba@2440
    58
namespace lemon {
deba@2440
    59
deba@2440
    60
  /// \addtogroup min_cost_flow
deba@2440
    61
  /// @{
deba@2440
    62
deba@2440
    63
  /// \brief Implementation of a cycle canceling algorithm for finding
deba@2440
    64
  /// a minimum cost flow.
deba@2440
    65
  ///
deba@2440
    66
  /// \ref lemon::CycleCanceling "CycleCanceling" implements a cycle
deba@2440
    67
  /// canceling algorithm for finding a minimum cost flow.
deba@2440
    68
  ///
deba@2440
    69
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    70
  /// \param LowerMap The type of the lower bound map.
deba@2440
    71
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    72
  /// \param CostMap The type of the cost (length) map.
deba@2440
    73
  /// \param SupplyMap The type of the supply map.
deba@2440
    74
  ///
deba@2440
    75
  /// \warning
deba@2440
    76
  /// - Edge capacities and costs should be nonnegative integers.
deba@2440
    77
  ///	However \c CostMap::Value should be signed type.
deba@2440
    78
  /// - Supply values should be integers.
deba@2440
    79
  /// - \c LowerMap::Value must be convertible to
deba@2440
    80
  ///	\c CapacityMap::Value and \c CapacityMap::Value must be
deba@2440
    81
  ///	convertible to \c SupplyMap::Value.
deba@2440
    82
  ///
deba@2440
    83
  /// \author Peter Kovacs
deba@2440
    84
deba@2440
    85
template < typename Graph,
deba@2440
    86
	   typename LowerMap = typename Graph::template EdgeMap<int>,
deba@2440
    87
	   typename CapacityMap = LowerMap,
deba@2440
    88
	   typename CostMap = typename Graph::template EdgeMap<int>,
deba@2440
    89
	   typename SupplyMap = typename Graph::template NodeMap
deba@2440
    90
				<typename CapacityMap::Value> >
deba@2440
    91
  class CycleCanceling
deba@2440
    92
  {
deba@2440
    93
    typedef typename Graph::Node Node;
deba@2440
    94
    typedef typename Graph::NodeIt NodeIt;
deba@2440
    95
    typedef typename Graph::Edge Edge;
deba@2440
    96
    typedef typename Graph::EdgeIt EdgeIt;
deba@2440
    97
    typedef typename Graph::InEdgeIt InEdgeIt;
deba@2440
    98
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2440
    99
deba@2440
   100
    typedef typename LowerMap::Value Lower;
deba@2440
   101
    typedef typename CapacityMap::Value Capacity;
deba@2440
   102
    typedef typename CostMap::Value Cost;
deba@2440
   103
    typedef typename SupplyMap::Value Supply;
deba@2440
   104
    typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
deba@2440
   105
    typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
deba@2440
   106
deba@2440
   107
    typedef ResGraphAdaptor< const Graph, Capacity,
deba@2440
   108
			     CapacityRefMap, CapacityRefMap > ResGraph;
deba@2440
   109
    typedef typename ResGraph::Node ResNode;
deba@2440
   110
    typedef typename ResGraph::NodeIt ResNodeIt;
deba@2440
   111
    typedef typename ResGraph::Edge ResEdge;
deba@2440
   112
    typedef typename ResGraph::EdgeIt ResEdgeIt;
deba@2440
   113
deba@2440
   114
  public:
deba@2440
   115
deba@2440
   116
    /// \brief The type of the flow map.
deba@2440
   117
    typedef CapacityRefMap FlowMap;
deba@2440
   118
deba@2440
   119
  protected:
deba@2440
   120
deba@2440
   121
    /// \brief Map adaptor class for demand map.
deba@2440
   122
    class DemandMap : public MapBase<Node, Supply>
deba@2440
   123
    {
deba@2440
   124
    private:
deba@2440
   125
deba@2440
   126
      const SupplyMap *map;
deba@2440
   127
deba@2440
   128
    public:
deba@2440
   129
deba@2440
   130
      typedef typename MapBase<Node, Supply>::Value Value;
deba@2440
   131
      typedef typename MapBase<Node, Supply>::Key Key;
deba@2440
   132
deba@2440
   133
      DemandMap(const SupplyMap &_map) : map(&_map) {}
deba@2440
   134
deba@2440
   135
      Value operator[](const Key &e) const {
deba@2440
   136
	return -(*map)[e];
deba@2440
   137
      }
deba@2440
   138
deba@2440
   139
    }; //class DemandMap
deba@2440
   140
deba@2440
   141
    /// \brief Map adaptor class for handling residual edge costs.
deba@2440
   142
    class ResCostMap : public MapBase<ResEdge, Cost>
deba@2440
   143
    {
deba@2440
   144
    private:
deba@2440
   145
deba@2440
   146
      const CostMap &cost_map;
deba@2440
   147
deba@2440
   148
    public:
deba@2440
   149
deba@2440
   150
      typedef typename MapBase<ResEdge, Cost>::Value Value;
deba@2440
   151
      typedef typename MapBase<ResEdge, Cost>::Key Key;
deba@2440
   152
deba@2440
   153
      ResCostMap(const CostMap &_cost) : cost_map(_cost) {}
deba@2440
   154
deba@2440
   155
      Value operator[](const Key &e) const {
deba@2440
   156
	return ResGraph::forward(e) ? cost_map[e] : -cost_map[e];
deba@2440
   157
      }
deba@2440
   158
deba@2440
   159
    }; //class ResCostMap
deba@2440
   160
deba@2440
   161
  protected:
deba@2440
   162
deba@2440
   163
    /// \brief The directed graph the algorithm runs on.
deba@2440
   164
    const Graph &graph;
deba@2440
   165
    /// \brief The original lower bound map.
deba@2440
   166
    const LowerMap *lower;
deba@2440
   167
    /// \brief The modified capacity map.
deba@2440
   168
    CapacityRefMap capacity;
deba@2440
   169
    /// \brief The cost map.
deba@2440
   170
    const CostMap &cost;
deba@2440
   171
    /// \brief The modified supply map.
deba@2440
   172
    SupplyRefMap supply;
deba@2440
   173
    /// \brief The modified demand map.
deba@2440
   174
    DemandMap demand;
deba@2440
   175
    /// \brief The sum of supply values equals zero.
deba@2440
   176
    bool valid_supply;
deba@2440
   177
deba@2440
   178
    /// \brief The current flow.
deba@2440
   179
    FlowMap flow;
deba@2440
   180
    /// \brief The residual graph.
deba@2440
   181
    ResGraph res_graph;
deba@2440
   182
    /// \brief The residual cost map.
deba@2440
   183
    ResCostMap res_cost;
deba@2440
   184
deba@2440
   185
  public :
deba@2440
   186
deba@2440
   187
    /// \brief General constructor of the class (with lower bounds).
deba@2440
   188
    ///
deba@2440
   189
    /// General constructor of the class (with lower bounds).
deba@2440
   190
    ///
deba@2440
   191
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   192
    /// \param _lower The lower bounds of the edges.
deba@2440
   193
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   194
    /// \param _cost The cost (length) values of the edges.
deba@2440
   195
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   196
    CycleCanceling( const Graph &_graph,
deba@2440
   197
		    const LowerMap &_lower,
deba@2440
   198
		    const CapacityMap &_capacity,
deba@2440
   199
		    const CostMap &_cost,
deba@2440
   200
		    const SupplyMap &_supply ) :
deba@2440
   201
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   202
      supply(_graph), demand(supply), flow(_graph, 0),
deba@2440
   203
      res_graph(_graph, capacity, flow), res_cost(cost)
deba@2440
   204
    {
deba@2440
   205
      // Removing nonzero lower bounds
deba@2440
   206
      capacity = subMap(_capacity, _lower);
deba@2440
   207
      Supply sum = 0;
deba@2440
   208
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2440
   209
	Supply s = _supply[n];
deba@2440
   210
	for (InEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   211
	  s += _lower[e];
deba@2440
   212
	for (OutEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   213
	  s -= _lower[e];
deba@2440
   214
	sum += (supply[n] = s);
deba@2440
   215
      }
deba@2440
   216
      valid_supply = sum == 0;
deba@2440
   217
    }
deba@2440
   218
deba@2440
   219
    /// \brief General constructor of the class (without lower bounds).
deba@2440
   220
    ///
deba@2440
   221
    /// General constructor of the class (without lower bounds).
deba@2440
   222
    ///
deba@2440
   223
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   224
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   225
    /// \param _cost The cost (length) values of the edges.
deba@2440
   226
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   227
    CycleCanceling( const Graph &_graph,
deba@2440
   228
		    const CapacityMap &_capacity,
deba@2440
   229
		    const CostMap &_cost,
deba@2440
   230
		    const SupplyMap &_supply ) :
deba@2440
   231
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   232
      supply(_supply), demand(supply), flow(_graph, 0),
deba@2440
   233
      res_graph(_graph, capacity, flow), res_cost(cost)
deba@2440
   234
    {
deba@2440
   235
      // Checking the sum of supply values
deba@2440
   236
      Supply sum = 0;
deba@2440
   237
      for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
deba@2440
   238
      valid_supply = sum == 0;
deba@2440
   239
    }
deba@2440
   240
deba@2440
   241
deba@2440
   242
    /// \brief Simple constructor of the class (with lower bounds).
deba@2440
   243
    ///
deba@2440
   244
    /// Simple constructor of the class (with lower bounds).
deba@2440
   245
    ///
deba@2440
   246
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   247
    /// \param _lower The lower bounds of the edges.
deba@2440
   248
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   249
    /// \param _cost The cost (length) values of the edges.
deba@2440
   250
    /// \param _s The source node.
deba@2440
   251
    /// \param _t The target node.
deba@2440
   252
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   253
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   254
    /// \c _t).
deba@2440
   255
    CycleCanceling( const Graph &_graph,
deba@2440
   256
		    const LowerMap &_lower,
deba@2440
   257
		    const CapacityMap &_capacity,
deba@2440
   258
		    const CostMap &_cost,
deba@2440
   259
		    Node _s, Node _t,
deba@2440
   260
		    Supply _flow_value ) :
deba@2440
   261
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   262
      supply(_graph), demand(supply), flow(_graph, 0),
deba@2440
   263
      res_graph(_graph, capacity, flow), res_cost(cost)
deba@2440
   264
    {
deba@2440
   265
      // Removing nonzero lower bounds
deba@2440
   266
      capacity = subMap(_capacity, _lower);
deba@2440
   267
      for (NodeIt n(graph); n != INVALID; ++n) {
deba@2440
   268
	Supply s = 0;
deba@2440
   269
	if (n == _s) s =  _flow_value;
deba@2440
   270
	if (n == _t) s = -_flow_value;
deba@2440
   271
	for (InEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   272
	  s += _lower[e];
deba@2440
   273
	for (OutEdgeIt e(graph, n); e != INVALID; ++e)
deba@2440
   274
	  s -= _lower[e];
deba@2440
   275
	supply[n] = s;
deba@2440
   276
      }
deba@2440
   277
      valid_supply = true;
deba@2440
   278
    }
deba@2440
   279
deba@2440
   280
    /// \brief Simple constructor of the class (without lower bounds).
deba@2440
   281
    ///
deba@2440
   282
    /// Simple constructor of the class (without lower bounds).
deba@2440
   283
    ///
deba@2440
   284
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   285
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   286
    /// \param _cost The cost (length) values of the edges.
deba@2440
   287
    /// \param _s The source node.
deba@2440
   288
    /// \param _t The target node.
deba@2440
   289
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   290
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   291
    /// \c _t).
deba@2440
   292
    CycleCanceling( const Graph &_graph,
deba@2440
   293
		    const CapacityMap &_capacity,
deba@2440
   294
		    const CostMap &_cost,
deba@2440
   295
		    Node _s, Node _t,
deba@2440
   296
		    Supply _flow_value ) :
deba@2440
   297
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   298
      supply(_graph, 0), demand(supply), flow(_graph, 0),
deba@2440
   299
      res_graph(_graph, capacity, flow), res_cost(cost)
deba@2440
   300
    {
deba@2440
   301
      supply[_s] =  _flow_value;
deba@2440
   302
      supply[_t] = -_flow_value;
deba@2440
   303
      valid_supply = true;
deba@2440
   304
    }
deba@2440
   305
deba@2440
   306
    /// \brief Returns a const reference to the flow map.
deba@2440
   307
    ///
deba@2440
   308
    /// Returns a const reference to the flow map.
deba@2440
   309
    ///
deba@2440
   310
    /// \pre \ref run() must be called before using this function.
deba@2440
   311
    const FlowMap& flowMap() const {
deba@2440
   312
      return flow;
deba@2440
   313
    }
deba@2440
   314
deba@2440
   315
    /// \brief Returns the total cost of the found flow.
deba@2440
   316
    ///
deba@2440
   317
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   318
    /// function is \f$ O(e) \f$.
deba@2440
   319
    ///
deba@2440
   320
    /// \pre \ref run() must be called before using this function.
deba@2440
   321
    Cost totalCost() const {
deba@2440
   322
      Cost c = 0;
deba@2440
   323
      for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   324
	c += flow[e] * cost[e];
deba@2440
   325
      return c;
deba@2440
   326
    }
deba@2440
   327
deba@2440
   328
    /// \brief Runs the algorithm.
deba@2440
   329
    ///
deba@2440
   330
    /// Runs the algorithm.
deba@2440
   331
    ///
deba@2440
   332
    /// \return \c true if a feasible flow can be found.
deba@2440
   333
    bool run() {
deba@2440
   334
      return init() && start();
deba@2440
   335
    }
deba@2440
   336
deba@2440
   337
  protected:
deba@2440
   338
deba@2440
   339
    /// \brief Initializes the algorithm.
deba@2440
   340
    bool init() {
deba@2440
   341
      // Checking the sum of supply values
deba@2440
   342
      Supply sum = 0;
deba@2440
   343
      for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
deba@2440
   344
      if (sum != 0) return false;
deba@2440
   345
deba@2440
   346
      // Finding a feasible flow
deba@2440
   347
      Circulation< Graph, Capacity, FlowMap, ConstMap<Edge, Capacity>,
deba@2440
   348
		   CapacityRefMap, DemandMap >
deba@2440
   349
	circulation( graph, constMap<Edge>((Capacity)0),
deba@2440
   350
		     capacity, demand, flow );
deba@2440
   351
      return circulation.run() == -1;
deba@2440
   352
    }
deba@2440
   353
deba@2440
   354
#ifdef LIMITED_CYCLE_CANCELING
deba@2440
   355
    /// \brief Executes a cycle canceling algorithm using
deba@2440
   356
    /// \ref lemon::BellmanFord "Bellman-Ford" algorithm with limited
deba@2440
   357
    /// iteration count.
deba@2440
   358
    bool start() {
deba@2440
   359
      typename BellmanFord<ResGraph, ResCostMap>::PredMap pred(res_graph);
deba@2440
   360
      typename ResGraph::template NodeMap<int> visited(res_graph);
deba@2440
   361
      std::vector<ResEdge> cycle;
deba@2440
   362
      int node_num = countNodes(graph);
deba@2440
   363
deba@2440
   364
#ifdef _DEBUG_ITER_
deba@2440
   365
      int cycle_num = 0;
deba@2440
   366
#endif
deba@2440
   367
      int length_bound = STARTING_LIMIT;
deba@2440
   368
      bool optimal = false;
deba@2440
   369
      while (!optimal) {
deba@2440
   370
	BellmanFord<ResGraph, ResCostMap> bf(res_graph, res_cost);
deba@2440
   371
	bf.predMap(pred);
deba@2440
   372
	bf.init(0);
deba@2440
   373
	int iter_num = 0;
deba@2440
   374
	bool cycle_found = false;
deba@2440
   375
	while (!cycle_found) {
deba@2440
   376
	  int curr_iter_num = iter_num + length_bound <= node_num ?
deba@2440
   377
			      length_bound : node_num - iter_num;
deba@2440
   378
	  iter_num += curr_iter_num;
deba@2440
   379
	  int real_iter_num = curr_iter_num;
deba@2440
   380
	  for (int i = 0; i < curr_iter_num; ++i) {
deba@2440
   381
	    if (bf.processNextWeakRound()) {
deba@2440
   382
	      real_iter_num = i;
deba@2440
   383
	      break;
deba@2440
   384
	    }
deba@2440
   385
	  }
deba@2440
   386
	  if (real_iter_num < curr_iter_num) {
deba@2440
   387
	    optimal = true;
deba@2440
   388
	    break;
deba@2440
   389
	  } else {
deba@2440
   390
	    // Searching for node disjoint negative cycles
deba@2440
   391
	    for (ResNodeIt n(res_graph); n != INVALID; ++n)
deba@2440
   392
	      visited[n] = 0;
deba@2440
   393
	    int id = 0;
deba@2440
   394
	    for (ResNodeIt n(res_graph); n != INVALID; ++n) {
deba@2440
   395
	      if (visited[n] > 0) continue;
deba@2440
   396
	      visited[n] = ++id;
deba@2440
   397
	      ResNode u = pred[n] == INVALID ?
deba@2440
   398
			  INVALID : res_graph.source(pred[n]);
deba@2440
   399
	      while (u != INVALID && visited[u] == 0) {
deba@2440
   400
		visited[u] = id;
deba@2440
   401
		u = pred[u] == INVALID ?
deba@2440
   402
		    INVALID : res_graph.source(pred[u]);
deba@2440
   403
	      }
deba@2440
   404
	      if (u != INVALID && visited[u] == id) {
deba@2440
   405
		// Finding the negative cycle
deba@2440
   406
		cycle_found = true;
deba@2440
   407
		cycle.clear();
deba@2440
   408
		ResEdge e = pred[u];
deba@2440
   409
		cycle.push_back(e);
deba@2440
   410
		Capacity d = res_graph.rescap(e);
deba@2440
   411
		while (res_graph.source(e) != u) {
deba@2440
   412
		  cycle.push_back(e = pred[res_graph.source(e)]);
deba@2440
   413
		  if (res_graph.rescap(e) < d)
deba@2440
   414
		    d = res_graph.rescap(e);
deba@2440
   415
		}
deba@2440
   416
#ifdef _DEBUG_ITER_
deba@2440
   417
		++cycle_num;
deba@2440
   418
#endif
deba@2440
   419
		// Augmenting along the cycle
deba@2440
   420
		for (int i = 0; i < cycle.size(); ++i)
deba@2440
   421
		  res_graph.augment(cycle[i], d);
deba@2440
   422
#ifdef _ONLY_ONE_CYCLE_
deba@2440
   423
		break;
deba@2440
   424
#endif
deba@2440
   425
	      }
deba@2440
   426
	    }
deba@2440
   427
	  }
deba@2440
   428
deba@2440
   429
	  if (!cycle_found)
deba@2440
   430
	    length_bound = length_bound * ALPHA_MUL / ALPHA_DIV;
deba@2440
   431
	}
deba@2440
   432
      }
deba@2440
   433
deba@2440
   434
#ifdef _DEBUG_ITER_
deba@2440
   435
      std::cout << "Limited cycle canceling algorithm finished. "
deba@2440
   436
		<< "Found " << cycle_num << " negative cycles."
deba@2440
   437
		<< std::endl;
deba@2440
   438
#endif
deba@2440
   439
deba@2440
   440
      // Handling nonzero lower bounds
deba@2440
   441
      if (lower) {
deba@2440
   442
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   443
	  flow[e] += (*lower)[e];
deba@2440
   444
      }
deba@2440
   445
      return true;
deba@2440
   446
    }
deba@2440
   447
#endif
deba@2440
   448
deba@2440
   449
#ifdef MIN_MEAN_CYCLE_CANCELING
deba@2440
   450
    /// \brief Executes the minimum mean cycle canceling algorithm
deba@2440
   451
    /// using \ref lemon::MinMeanCycle "MinMeanCycle" class.
deba@2440
   452
    bool start() {
deba@2440
   453
      typedef Path<ResGraph> ResPath;
deba@2440
   454
      MinMeanCycle<ResGraph, ResCostMap> mmc(res_graph, res_cost);
deba@2440
   455
      ResPath cycle;
deba@2440
   456
deba@2440
   457
#ifdef _DEBUG_ITER_
deba@2440
   458
      int cycle_num = 0;
deba@2440
   459
#endif
deba@2440
   460
      mmc.cyclePath(cycle).init();
deba@2440
   461
      if (mmc.findMinMean()) {
deba@2440
   462
	while (mmc.cycleLength() < 0) {
deba@2440
   463
#ifdef _DEBUG_ITER_
deba@2440
   464
	  ++iter;
deba@2440
   465
#endif
deba@2440
   466
	  // Finding the cycle
deba@2440
   467
	  mmc.findCycle();
deba@2440
   468
deba@2440
   469
	  // Finding the largest flow amount that can be augmented
deba@2440
   470
	  // along the cycle
deba@2440
   471
	  Capacity delta = 0;
deba@2440
   472
	  for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e) {
deba@2440
   473
	    if (delta == 0 || res_graph.rescap(e) < delta)
deba@2440
   474
	      delta = res_graph.rescap(e);
deba@2440
   475
	  }
deba@2440
   476
deba@2440
   477
	  // Augmenting along the cycle
deba@2440
   478
	  for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e)
deba@2440
   479
	    res_graph.augment(e, delta);
deba@2440
   480
deba@2440
   481
	  // Finding the minimum cycle mean for the modified residual
deba@2440
   482
	  // graph
deba@2440
   483
	  mmc.reset();
deba@2440
   484
	  if (!mmc.findMinMean()) break;
deba@2440
   485
	}
deba@2440
   486
      }
deba@2440
   487
deba@2440
   488
#ifdef _DEBUG_ITER_
deba@2440
   489
      std::cout << "Minimum mean cycle canceling algorithm finished. "
deba@2440
   490
		<< "Found " << cycle_num << " negative cycles."
deba@2440
   491
		<< std::endl;
deba@2440
   492
#endif
deba@2440
   493
deba@2440
   494
      // Handling nonzero lower bounds
deba@2440
   495
      if (lower) {
deba@2440
   496
	for (EdgeIt e(graph); e != INVALID; ++e)
deba@2440
   497
	  flow[e] += (*lower)[e];
deba@2440
   498
      }
deba@2440
   499
      return true;
deba@2440
   500
    }
deba@2440
   501
#endif
deba@2440
   502
deba@2440
   503
  }; //class CycleCanceling
deba@2440
   504
deba@2440
   505
  ///@}
deba@2440
   506
deba@2440
   507
} //namespace lemon
deba@2440
   508
deba@2440
   509
#endif //LEMON_CYCLE_CANCELING_H