lemon/network_simplex.h
author deba
Mon, 10 Dec 2007 16:34:31 +0000
changeset 2538 7bdd328de87a
parent 2509 a8081c9cd96a
child 2553 bfced05fa852
permissions -rw-r--r--
Bug fix in doc
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_NETWORK_SIMPLEX_H
deba@2440
    20
#define LEMON_NETWORK_SIMPLEX_H
deba@2440
    21
deba@2440
    22
/// \ingroup min_cost_flow
deba@2440
    23
///
deba@2440
    24
/// \file
deba@2440
    25
/// \brief The network simplex algorithm for finding a minimum cost
deba@2440
    26
/// flow.
deba@2440
    27
deba@2440
    28
#include <limits>
kpeter@2509
    29
#include <lemon/graph_adaptor.h>
kpeter@2509
    30
#include <lemon/graph_utils.h>
deba@2440
    31
#include <lemon/smart_graph.h>
deba@2440
    32
deba@2440
    33
/// \brief The pivot rule used in the algorithm.
deba@2440
    34
//#define FIRST_ELIGIBLE_PIVOT
deba@2440
    35
//#define BEST_ELIGIBLE_PIVOT
deba@2444
    36
#define EDGE_BLOCK_PIVOT
deba@2440
    37
//#define CANDIDATE_LIST_PIVOT
deba@2440
    38
//#define SORTED_LIST_PIVOT
deba@2440
    39
deba@2444
    40
//#define _DEBUG_ITER_
deba@2444
    41
deba@2444
    42
deba@2440
    43
/// \brief State constant for edges at their lower bounds.
deba@2440
    44
#define LOWER	1
deba@2440
    45
/// \brief State constant for edges in the spanning tree.
deba@2440
    46
#define TREE	0
deba@2440
    47
/// \brief State constant for edges at their upper bounds.
deba@2440
    48
#define UPPER	-1
deba@2440
    49
deba@2440
    50
#ifdef EDGE_BLOCK_PIVOT
kpeter@2471
    51
  #include <cmath>
deba@2444
    52
  /// \brief Lower bound for the size of blocks.
deba@2444
    53
  #define MIN_BLOCK_SIZE	10
deba@2440
    54
#endif
deba@2440
    55
deba@2440
    56
#ifdef CANDIDATE_LIST_PIVOT
kpeter@2471
    57
  #include <vector>
kpeter@2471
    58
  #define LIST_LENGTH_DIV           50
kpeter@2471
    59
  #define MINOR_LIMIT_DIV           200
deba@2440
    60
#endif
deba@2440
    61
deba@2440
    62
#ifdef SORTED_LIST_PIVOT
kpeter@2471
    63
  #include <vector>
deba@2440
    64
  #include <algorithm>
kpeter@2471
    65
  #define LIST_LENGTH_DIV       100
kpeter@2471
    66
  #define LOWER_DIV		4
deba@2440
    67
#endif
deba@2440
    68
deba@2440
    69
namespace lemon {
deba@2440
    70
deba@2440
    71
  /// \addtogroup min_cost_flow
deba@2440
    72
  /// @{
deba@2440
    73
deba@2440
    74
  /// \brief Implementation of the network simplex algorithm for
deba@2440
    75
  /// finding a minimum cost flow.
deba@2440
    76
  ///
deba@2440
    77
  /// \ref lemon::NetworkSimplex "NetworkSimplex" implements the
deba@2440
    78
  /// network simplex algorithm for finding a minimum cost flow.
deba@2440
    79
  ///
deba@2440
    80
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    81
  /// \param LowerMap The type of the lower bound map.
deba@2440
    82
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    83
  /// \param CostMap The type of the cost (length) map.
deba@2440
    84
  /// \param SupplyMap The type of the supply map.
deba@2440
    85
  ///
deba@2440
    86
  /// \warning
deba@2440
    87
  /// - Edge capacities and costs should be nonnegative integers.
deba@2440
    88
  ///	However \c CostMap::Value should be signed type.
kpeter@2509
    89
  /// - Supply values should be signed integers.
deba@2440
    90
  /// - \c LowerMap::Value must be convertible to
deba@2440
    91
  ///	\c CapacityMap::Value and \c CapacityMap::Value must be
deba@2440
    92
  ///	convertible to \c SupplyMap::Value.
deba@2440
    93
  ///
deba@2440
    94
  /// \author Peter Kovacs
deba@2440
    95
kpeter@2533
    96
  template < typename Graph,
kpeter@2533
    97
             typename LowerMap = typename Graph::template EdgeMap<int>,
kpeter@2533
    98
             typename CapacityMap = LowerMap,
kpeter@2533
    99
             typename CostMap = typename Graph::template EdgeMap<int>,
kpeter@2533
   100
             typename SupplyMap = typename Graph::template NodeMap
kpeter@2533
   101
                                  <typename CapacityMap::Value> >
deba@2440
   102
  class NetworkSimplex
deba@2440
   103
  {
deba@2440
   104
    typedef typename LowerMap::Value Lower;
deba@2440
   105
    typedef typename CapacityMap::Value Capacity;
deba@2440
   106
    typedef typename CostMap::Value Cost;
deba@2440
   107
    typedef typename SupplyMap::Value Supply;
deba@2440
   108
deba@2440
   109
    typedef SmartGraph SGraph;
deba@2440
   110
    typedef typename SGraph::Node Node;
deba@2440
   111
    typedef typename SGraph::NodeIt NodeIt;
deba@2440
   112
    typedef typename SGraph::Edge Edge;
deba@2440
   113
    typedef typename SGraph::EdgeIt EdgeIt;
deba@2440
   114
    typedef typename SGraph::InEdgeIt InEdgeIt;
deba@2440
   115
    typedef typename SGraph::OutEdgeIt OutEdgeIt;
deba@2440
   116
deba@2440
   117
    typedef typename SGraph::template EdgeMap<Lower> SLowerMap;
deba@2440
   118
    typedef typename SGraph::template EdgeMap<Capacity> SCapacityMap;
deba@2440
   119
    typedef typename SGraph::template EdgeMap<Cost> SCostMap;
deba@2440
   120
    typedef typename SGraph::template NodeMap<Supply> SSupplyMap;
deba@2440
   121
    typedef typename SGraph::template NodeMap<Cost> SPotentialMap;
deba@2440
   122
deba@2440
   123
    typedef typename SGraph::template NodeMap<int> IntNodeMap;
deba@2440
   124
    typedef typename SGraph::template NodeMap<bool> BoolNodeMap;
deba@2440
   125
    typedef typename SGraph::template NodeMap<Node> NodeNodeMap;
deba@2440
   126
    typedef typename SGraph::template NodeMap<Edge> EdgeNodeMap;
deba@2440
   127
    typedef typename SGraph::template EdgeMap<int> IntEdgeMap;
deba@2440
   128
deba@2440
   129
    typedef typename Graph::template NodeMap<Node> NodeRefMap;
deba@2440
   130
    typedef typename Graph::template EdgeMap<Edge> EdgeRefMap;
deba@2440
   131
deba@2440
   132
  public:
deba@2440
   133
deba@2440
   134
    /// \brief The type of the flow map.
deba@2440
   135
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
deba@2440
   136
    /// \brief The type of the potential map.
deba@2440
   137
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
   138
deba@2440
   139
  protected:
deba@2440
   140
deba@2440
   141
    /// \brief Map adaptor class for handling reduced edge costs.
deba@2440
   142
    class ReducedCostMap : public MapBase<Edge, Cost>
deba@2440
   143
    {
deba@2440
   144
    private:
deba@2440
   145
deba@2440
   146
      const SGraph &gr;
deba@2440
   147
      const SCostMap &cost_map;
deba@2440
   148
      const SPotentialMap &pot_map;
deba@2440
   149
deba@2440
   150
    public:
deba@2440
   151
deba@2440
   152
      ReducedCostMap( const SGraph &_gr,
deba@2440
   153
		      const SCostMap &_cm,
deba@2440
   154
		      const SPotentialMap &_pm ) :
deba@2440
   155
	gr(_gr), cost_map(_cm), pot_map(_pm) {}
deba@2440
   156
kpeter@2509
   157
      Cost operator[](const Edge &e) const {
deba@2440
   158
	return cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
deba@2440
   159
      }
deba@2440
   160
deba@2440
   161
    }; //class ReducedCostMap
deba@2440
   162
deba@2440
   163
  protected:
deba@2440
   164
deba@2440
   165
    /// \brief The directed graph the algorithm runs on.
deba@2440
   166
    SGraph graph;
deba@2440
   167
    /// \brief The original graph.
deba@2440
   168
    const Graph &graph_ref;
deba@2440
   169
    /// \brief The original lower bound map.
deba@2440
   170
    const LowerMap *lower;
deba@2440
   171
    /// \brief The capacity map.
deba@2440
   172
    SCapacityMap capacity;
deba@2440
   173
    /// \brief The cost map.
deba@2440
   174
    SCostMap cost;
deba@2440
   175
    /// \brief The supply map.
deba@2440
   176
    SSupplyMap supply;
deba@2440
   177
    /// \brief The reduced cost map.
deba@2440
   178
    ReducedCostMap red_cost;
deba@2440
   179
    /// \brief The sum of supply values equals zero.
deba@2440
   180
    bool valid_supply;
deba@2440
   181
deba@2440
   182
    /// \brief The edge map of the current flow.
deba@2440
   183
    SCapacityMap flow;
deba@2440
   184
    /// \brief The edge map of the found flow on the original graph.
deba@2440
   185
    FlowMap flow_result;
deba@2440
   186
    /// \brief The potential node map.
deba@2440
   187
    SPotentialMap potential;
deba@2440
   188
    /// \brief The potential node map on the original graph.
deba@2440
   189
    PotentialMap potential_result;
deba@2440
   190
deba@2440
   191
    /// \brief Node reference for the original graph.
deba@2440
   192
    NodeRefMap node_ref;
deba@2440
   193
    /// \brief Edge reference for the original graph.
deba@2440
   194
    EdgeRefMap edge_ref;
deba@2440
   195
deba@2440
   196
    /// \brief The depth node map of the spanning tree structure.
deba@2440
   197
    IntNodeMap depth;
deba@2440
   198
    /// \brief The parent node map of the spanning tree structure.
deba@2440
   199
    NodeNodeMap parent;
deba@2440
   200
    /// \brief The pred_edge node map of the spanning tree structure.
deba@2440
   201
    EdgeNodeMap pred_edge;
deba@2440
   202
    /// \brief The thread node map of the spanning tree structure.
deba@2440
   203
    NodeNodeMap thread;
deba@2440
   204
    /// \brief The forward node map of the spanning tree structure.
deba@2440
   205
    BoolNodeMap forward;
deba@2440
   206
    /// \brief The state edge map.
deba@2440
   207
    IntEdgeMap state;
deba@2440
   208
deba@2440
   209
deba@2440
   210
#ifdef EDGE_BLOCK_PIVOT
deba@2440
   211
    /// \brief The size of blocks for the "Edge Block" pivot rule.
deba@2440
   212
    int block_size;
deba@2440
   213
#endif
deba@2440
   214
#ifdef CANDIDATE_LIST_PIVOT
deba@2440
   215
    /// \brief The list of candidate edges for the "Candidate List"
deba@2440
   216
    /// pivot rule.
kpeter@2471
   217
    std::vector<Edge> candidates;
kpeter@2471
   218
    /// \brief The maximum length of the edge list for the
kpeter@2471
   219
    /// "Candidate List" pivot rule.
kpeter@2471
   220
    int list_length;
kpeter@2471
   221
    /// \brief The maximum number of minor iterations between two major
kpeter@2471
   222
    /// itarations.
kpeter@2471
   223
    int minor_limit;
deba@2440
   224
    /// \brief The number of minor iterations.
deba@2440
   225
    int minor_count;
deba@2440
   226
#endif
deba@2440
   227
#ifdef SORTED_LIST_PIVOT
deba@2440
   228
    /// \brief The list of candidate edges for the "Sorted List"
deba@2440
   229
    /// pivot rule.
kpeter@2471
   230
    std::vector<Edge> candidates;
kpeter@2471
   231
    /// \brief The maximum length of the edge list for the
kpeter@2471
   232
    /// "Sorted List" pivot rule.
kpeter@2471
   233
    int list_length;
kpeter@2471
   234
    int list_index;
deba@2440
   235
#endif
deba@2440
   236
deba@2440
   237
    // Root node of the starting spanning tree.
deba@2440
   238
    Node root;
deba@2440
   239
    // The entering edge of the current pivot iteration.
deba@2440
   240
    Edge in_edge;
deba@2440
   241
    // Temporary nodes used in the current pivot iteration.
deba@2440
   242
    Node join, u_in, v_in, u_out, v_out;
deba@2440
   243
    Node right, first, second, last;
deba@2440
   244
    Node stem, par_stem, new_stem;
deba@2440
   245
    // The maximum augment amount along the cycle in the current pivot
deba@2440
   246
    // iteration.
deba@2440
   247
    Capacity delta;
deba@2440
   248
deba@2440
   249
  public :
deba@2440
   250
deba@2440
   251
    /// \brief General constructor of the class (with lower bounds).
deba@2440
   252
    ///
deba@2440
   253
    /// General constructor of the class (with lower bounds).
deba@2440
   254
    ///
deba@2440
   255
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   256
    /// \param _lower The lower bounds of the edges.
deba@2440
   257
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   258
    /// \param _cost The cost (length) values of the edges.
deba@2440
   259
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   260
    NetworkSimplex( const Graph &_graph,
deba@2440
   261
		    const LowerMap &_lower,
deba@2440
   262
		    const CapacityMap &_capacity,
deba@2440
   263
		    const CostMap &_cost,
deba@2440
   264
		    const SupplyMap &_supply ) :
deba@2440
   265
      graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
deba@2440
   266
      supply(graph), flow(graph), flow_result(_graph), potential(graph),
deba@2440
   267
      potential_result(_graph), depth(graph), parent(graph),
deba@2440
   268
      pred_edge(graph), thread(graph), forward(graph), state(graph),
deba@2440
   269
      node_ref(graph_ref), edge_ref(graph_ref),
deba@2440
   270
      red_cost(graph, cost, potential)
deba@2440
   271
    {
deba@2440
   272
      // Checking the sum of supply values
deba@2440
   273
      Supply sum = 0;
deba@2440
   274
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
deba@2440
   275
	sum += _supply[n];
deba@2440
   276
      if (!(valid_supply = sum == 0)) return;
deba@2440
   277
deba@2440
   278
      // Copying graph_ref to graph
deba@2457
   279
      graph.reserveNode(countNodes(graph_ref) + 1);
deba@2457
   280
      graph.reserveEdge(countEdges(graph_ref) + countNodes(graph_ref));
deba@2440
   281
      copyGraph(graph, graph_ref)
deba@2440
   282
	.edgeMap(cost, _cost)
deba@2440
   283
	.nodeRef(node_ref)
deba@2440
   284
	.edgeRef(edge_ref)
deba@2440
   285
	.run();
deba@2440
   286
deba@2440
   287
      // Removing nonzero lower bounds
deba@2440
   288
      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
deba@2440
   289
	capacity[edge_ref[e]] = _capacity[e] - _lower[e];
deba@2440
   290
      }
deba@2440
   291
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
deba@2440
   292
	Supply s = _supply[n];
deba@2440
   293
	for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
deba@2440
   294
	  s += _lower[e];
deba@2440
   295
	for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
deba@2440
   296
	  s -= _lower[e];
deba@2440
   297
	supply[node_ref[n]] = s;
deba@2440
   298
      }
deba@2440
   299
    }
deba@2440
   300
deba@2440
   301
    /// \brief General constructor of the class (without lower bounds).
deba@2440
   302
    ///
deba@2440
   303
    /// General constructor of the class (without lower bounds).
deba@2440
   304
    ///
deba@2440
   305
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   306
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   307
    /// \param _cost The cost (length) values of the edges.
deba@2440
   308
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   309
    NetworkSimplex( const Graph &_graph,
deba@2440
   310
		    const CapacityMap &_capacity,
deba@2440
   311
		    const CostMap &_cost,
deba@2440
   312
		    const SupplyMap &_supply ) :
deba@2440
   313
      graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
deba@2440
   314
      supply(graph), flow(graph), flow_result(_graph), potential(graph),
deba@2440
   315
      potential_result(_graph), depth(graph), parent(graph),
deba@2440
   316
      pred_edge(graph), thread(graph), forward(graph), state(graph),
deba@2440
   317
      node_ref(graph_ref), edge_ref(graph_ref),
deba@2440
   318
      red_cost(graph, cost, potential)
deba@2440
   319
    {
deba@2440
   320
      // Checking the sum of supply values
deba@2440
   321
      Supply sum = 0;
deba@2440
   322
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
deba@2440
   323
	sum += _supply[n];
deba@2440
   324
      if (!(valid_supply = sum == 0)) return;
deba@2440
   325
deba@2440
   326
      // Copying graph_ref to graph
deba@2440
   327
      copyGraph(graph, graph_ref)
deba@2440
   328
	.edgeMap(capacity, _capacity)
deba@2440
   329
	.edgeMap(cost, _cost)
deba@2440
   330
	.nodeMap(supply, _supply)
deba@2440
   331
	.nodeRef(node_ref)
deba@2440
   332
	.edgeRef(edge_ref)
deba@2440
   333
	.run();
deba@2440
   334
    }
deba@2440
   335
deba@2440
   336
    /// \brief Simple constructor of the class (with lower bounds).
deba@2440
   337
    ///
deba@2440
   338
    /// Simple constructor of the class (with lower bounds).
deba@2440
   339
    ///
deba@2440
   340
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   341
    /// \param _lower The lower bounds of the edges.
deba@2440
   342
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   343
    /// \param _cost The cost (length) values of the edges.
deba@2440
   344
    /// \param _s The source node.
deba@2440
   345
    /// \param _t The target node.
deba@2440
   346
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   347
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   348
    /// \c _t).
deba@2440
   349
    NetworkSimplex( const Graph &_graph,
deba@2440
   350
		    const LowerMap &_lower,
deba@2440
   351
		    const CapacityMap &_capacity,
deba@2440
   352
		    const CostMap &_cost,
deba@2440
   353
		    typename Graph::Node _s,
deba@2440
   354
		    typename Graph::Node _t,
deba@2440
   355
		    typename SupplyMap::Value _flow_value ) :
deba@2440
   356
      graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
deba@2440
   357
      supply(graph), flow(graph), flow_result(_graph), potential(graph),
deba@2440
   358
      potential_result(_graph), depth(graph), parent(graph),
deba@2440
   359
      pred_edge(graph), thread(graph), forward(graph), state(graph),
deba@2440
   360
      node_ref(graph_ref), edge_ref(graph_ref),
deba@2440
   361
      red_cost(graph, cost, potential)
deba@2440
   362
    {
deba@2440
   363
      // Copying graph_ref to graph
deba@2440
   364
      copyGraph(graph, graph_ref)
deba@2440
   365
	.edgeMap(cost, _cost)
deba@2440
   366
	.nodeRef(node_ref)
deba@2440
   367
	.edgeRef(edge_ref)
deba@2440
   368
	.run();
deba@2440
   369
deba@2440
   370
      // Removing nonzero lower bounds
deba@2440
   371
      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
deba@2440
   372
	capacity[edge_ref[e]] = _capacity[e] - _lower[e];
deba@2440
   373
      }
deba@2440
   374
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
deba@2440
   375
	Supply s = 0;
deba@2440
   376
	if (n == _s) s =  _flow_value;
deba@2440
   377
	if (n == _t) s = -_flow_value;
deba@2440
   378
	for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
deba@2440
   379
	  s += _lower[e];
deba@2440
   380
	for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
deba@2440
   381
	  s -= _lower[e];
deba@2440
   382
	supply[node_ref[n]] = s;
deba@2440
   383
      }
deba@2440
   384
      valid_supply = true;
deba@2440
   385
    }
deba@2440
   386
deba@2440
   387
    /// \brief Simple constructor of the class (without lower bounds).
deba@2440
   388
    ///
deba@2440
   389
    /// Simple constructor of the class (without lower bounds).
deba@2440
   390
    ///
deba@2440
   391
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   392
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   393
    /// \param _cost The cost (length) values of the edges.
deba@2440
   394
    /// \param _s The source node.
deba@2440
   395
    /// \param _t The target node.
deba@2440
   396
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   397
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   398
    /// \c _t).
deba@2440
   399
    NetworkSimplex( const Graph &_graph,
deba@2440
   400
		    const CapacityMap &_capacity,
deba@2440
   401
		    const CostMap &_cost,
deba@2440
   402
		    typename Graph::Node _s,
deba@2440
   403
		    typename Graph::Node _t,
deba@2440
   404
		    typename SupplyMap::Value _flow_value ) :
deba@2440
   405
      graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
deba@2440
   406
      supply(graph, 0), flow(graph), flow_result(_graph), potential(graph),
deba@2440
   407
      potential_result(_graph), depth(graph), parent(graph),
deba@2440
   408
      pred_edge(graph), thread(graph), forward(graph), state(graph),
deba@2440
   409
      node_ref(graph_ref), edge_ref(graph_ref),
deba@2440
   410
      red_cost(graph, cost, potential)
deba@2440
   411
    {
deba@2440
   412
      // Copying graph_ref to graph
deba@2440
   413
      copyGraph(graph, graph_ref)
deba@2440
   414
	.edgeMap(capacity, _capacity)
deba@2440
   415
	.edgeMap(cost, _cost)
deba@2440
   416
	.nodeRef(node_ref)
deba@2440
   417
	.edgeRef(edge_ref)
deba@2440
   418
	.run();
deba@2440
   419
      supply[node_ref[_s]] =  _flow_value;
deba@2440
   420
      supply[node_ref[_t]] = -_flow_value;
deba@2440
   421
      valid_supply = true;
deba@2440
   422
    }
deba@2440
   423
deba@2440
   424
    /// \brief Returns a const reference to the flow map.
deba@2440
   425
    ///
deba@2440
   426
    /// Returns a const reference to the flow map.
deba@2440
   427
    ///
deba@2440
   428
    /// \pre \ref run() must be called before using this function.
deba@2440
   429
    const FlowMap& flowMap() const {
deba@2440
   430
      return flow_result;
deba@2440
   431
    }
deba@2440
   432
deba@2440
   433
    /// \brief Returns a const reference to the potential map (the dual
deba@2440
   434
    /// solution).
deba@2440
   435
    ///
deba@2440
   436
    /// Returns a const reference to the potential map (the dual
deba@2440
   437
    /// solution).
deba@2440
   438
    ///
deba@2440
   439
    /// \pre \ref run() must be called before using this function.
deba@2440
   440
    const PotentialMap& potentialMap() const {
deba@2440
   441
      return potential_result;
deba@2440
   442
    }
deba@2440
   443
deba@2440
   444
    /// \brief Returns the total cost of the found flow.
deba@2440
   445
    ///
deba@2440
   446
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   447
    /// function is \f$ O(e) \f$.
deba@2440
   448
    ///
deba@2440
   449
    /// \pre \ref run() must be called before using this function.
deba@2440
   450
    Cost totalCost() const {
deba@2440
   451
      Cost c = 0;
deba@2440
   452
      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
deba@2440
   453
	c += flow_result[e] * cost[edge_ref[e]];
deba@2440
   454
      return c;
deba@2440
   455
    }
deba@2440
   456
deba@2440
   457
    /// \brief Runs the algorithm.
deba@2440
   458
    ///
deba@2440
   459
    /// Runs the algorithm.
deba@2440
   460
    ///
deba@2440
   461
    /// \return \c true if a feasible flow can be found.
deba@2440
   462
    bool run() {
deba@2440
   463
      return init() && start();
deba@2440
   464
    }
deba@2440
   465
deba@2440
   466
  protected:
deba@2440
   467
deba@2440
   468
    /// \brief Extends the underlaying graph and initializes all the
deba@2440
   469
    /// node and edge maps.
deba@2440
   470
    bool init() {
deba@2440
   471
      if (!valid_supply) return false;
deba@2440
   472
deba@2440
   473
      // Initializing state and flow maps
deba@2440
   474
      for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   475
	flow[e] = 0;
deba@2440
   476
	state[e] = LOWER;
deba@2440
   477
      }
deba@2440
   478
deba@2440
   479
      // Adding an artificial root node to the graph
deba@2440
   480
      root = graph.addNode();
deba@2440
   481
      parent[root] = INVALID;
deba@2440
   482
      pred_edge[root] = INVALID;
deba@2457
   483
      depth[root] = 0;
deba@2457
   484
      supply[root] = 0;
deba@2457
   485
      potential[root] = 0;
deba@2440
   486
deba@2440
   487
      // Adding artificial edges to the graph and initializing the node
deba@2440
   488
      // maps of the spanning tree data structure
deba@2440
   489
      Supply sum = 0;
deba@2440
   490
      Node last = root;
deba@2440
   491
      Edge e;
deba@2440
   492
      Cost max_cost = std::numeric_limits<Cost>::max() / 4;
deba@2440
   493
      for (NodeIt u(graph); u != INVALID; ++u) {
deba@2440
   494
	if (u == root) continue;
deba@2440
   495
	thread[last] = u;
deba@2440
   496
	last = u;
deba@2440
   497
	parent[u] = root;
deba@2440
   498
	depth[u] = 1;
deba@2440
   499
	sum += supply[u];
deba@2440
   500
	if (supply[u] >= 0) {
deba@2440
   501
	  e = graph.addEdge(u, root);
deba@2440
   502
	  flow[e] = supply[u];
deba@2440
   503
	  forward[u] = true;
deba@2440
   504
	  potential[u] = max_cost;
deba@2440
   505
	} else {
deba@2440
   506
	  e = graph.addEdge(root, u);
deba@2440
   507
	  flow[e] = -supply[u];
deba@2440
   508
	  forward[u] = false;
deba@2440
   509
	  potential[u] = -max_cost;
deba@2440
   510
	}
deba@2440
   511
	cost[e] = max_cost;
deba@2440
   512
	capacity[e] = std::numeric_limits<Capacity>::max();
deba@2440
   513
	state[e] = TREE;
deba@2440
   514
	pred_edge[u] = e;
deba@2440
   515
      }
deba@2440
   516
      thread[last] = root;
deba@2440
   517
deba@2440
   518
#ifdef EDGE_BLOCK_PIVOT
deba@2440
   519
      // Initializing block_size for the edge block pivot rule
deba@2440
   520
      int edge_num = countEdges(graph);
kpeter@2471
   521
      block_size = 2 * int(sqrt(countEdges(graph)));
kpeter@2471
   522
      if (block_size < MIN_BLOCK_SIZE) block_size = MIN_BLOCK_SIZE;
kpeter@2471
   523
//      block_size = edge_num >= BLOCK_NUM * MIN_BLOCK_SIZE ?
kpeter@2471
   524
//                   edge_num / BLOCK_NUM : MIN_BLOCK_SIZE;
deba@2440
   525
#endif
deba@2440
   526
#ifdef CANDIDATE_LIST_PIVOT
kpeter@2471
   527
      int edge_num = countEdges(graph);
deba@2440
   528
      minor_count = 0;
kpeter@2471
   529
      list_length = edge_num / LIST_LENGTH_DIV;
kpeter@2471
   530
      minor_limit = edge_num / MINOR_LIMIT_DIV;
kpeter@2471
   531
#endif
kpeter@2471
   532
#ifdef SORTED_LIST_PIVOT
kpeter@2471
   533
      int edge_num = countEdges(graph);
kpeter@2471
   534
      list_index = 0;
kpeter@2471
   535
      list_length = edge_num / LIST_LENGTH_DIV;
deba@2440
   536
#endif
deba@2440
   537
deba@2440
   538
      return sum == 0;
deba@2440
   539
    }
deba@2440
   540
deba@2440
   541
#ifdef FIRST_ELIGIBLE_PIVOT
deba@2440
   542
    /// \brief Finds entering edge according to the "First Eligible"
deba@2440
   543
    /// pivot rule.
deba@2440
   544
    bool findEnteringEdge(EdgeIt &next_edge) {
deba@2440
   545
      for (EdgeIt e = next_edge; e != INVALID; ++e) {
deba@2440
   546
	if (state[e] * red_cost[e] < 0) {
deba@2440
   547
	  in_edge = e;
deba@2440
   548
	  next_edge = ++e;
deba@2440
   549
	  return true;
deba@2440
   550
	}
deba@2440
   551
      }
deba@2440
   552
      for (EdgeIt e(graph); e != next_edge; ++e) {
deba@2440
   553
	if (state[e] * red_cost[e] < 0) {
deba@2440
   554
	  in_edge = e;
deba@2440
   555
	  next_edge = ++e;
deba@2440
   556
	  return true;
deba@2440
   557
	}
deba@2440
   558
      }
deba@2440
   559
      return false;
deba@2440
   560
    }
deba@2440
   561
#endif
deba@2440
   562
deba@2440
   563
#ifdef BEST_ELIGIBLE_PIVOT
deba@2440
   564
    /// \brief Finds entering edge according to the "Best Eligible"
deba@2440
   565
    /// pivot rule.
deba@2440
   566
    bool findEnteringEdge() {
deba@2440
   567
      Cost min = 0;
deba@2440
   568
      for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   569
	if (state[e] * red_cost[e] < min) {
deba@2440
   570
	  min = state[e] * red_cost[e];
deba@2440
   571
	  in_edge = e;
deba@2440
   572
	}
deba@2440
   573
      }
deba@2440
   574
      return min < 0;
deba@2440
   575
    }
deba@2440
   576
#endif
deba@2440
   577
deba@2440
   578
#ifdef EDGE_BLOCK_PIVOT
deba@2440
   579
    /// \brief Finds entering edge according to the "Edge Block"
deba@2440
   580
    /// pivot rule.
deba@2440
   581
    bool findEnteringEdge(EdgeIt &next_edge) {
deba@2444
   582
      // Performing edge block selection
deba@2444
   583
      Cost curr, min = 0;
deba@2444
   584
      EdgeIt min_edge(graph);
deba@2444
   585
      int cnt = 0;
deba@2444
   586
      for (EdgeIt e = next_edge; e != INVALID; ++e) {
deba@2444
   587
	if ((curr = state[e] * red_cost[e]) < min) {
deba@2444
   588
	  min = curr;
deba@2444
   589
	  min_edge = e;
deba@2440
   590
	}
deba@2444
   591
	if (++cnt == block_size) {
deba@2444
   592
	  if (min < 0) break;
deba@2444
   593
	  cnt = 0;
deba@2444
   594
	}
deba@2444
   595
      }
deba@2444
   596
      if (!(min < 0)) {
deba@2440
   597
	for (EdgeIt e(graph); e != next_edge; ++e) {
deba@2440
   598
	  if ((curr = state[e] * red_cost[e]) < min) {
deba@2440
   599
	    min = curr;
deba@2440
   600
	    min_edge = e;
deba@2440
   601
	  }
deba@2440
   602
	  if (++cnt == block_size) {
deba@2440
   603
	    if (min < 0) break;
deba@2440
   604
	    cnt = 0;
deba@2440
   605
	  }
deba@2440
   606
	}
deba@2440
   607
      }
deba@2444
   608
      in_edge = min_edge;
deba@2444
   609
      if ((next_edge = ++min_edge) == INVALID)
deba@2444
   610
	next_edge = EdgeIt(graph);
deba@2444
   611
      return min < 0;
deba@2440
   612
    }
deba@2440
   613
#endif
deba@2440
   614
deba@2440
   615
#ifdef CANDIDATE_LIST_PIVOT
deba@2440
   616
    /// \brief Finds entering edge according to the "Candidate List"
deba@2440
   617
    /// pivot rule.
deba@2440
   618
    bool findEnteringEdge() {
kpeter@2471
   619
      typedef typename std::vector<Edge>::iterator ListIt;
deba@2440
   620
kpeter@2471
   621
      if (minor_count >= minor_limit || candidates.size() == 0) {
deba@2440
   622
	// Major iteration
kpeter@2471
   623
	candidates.clear();
deba@2440
   624
	for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   625
	  if (state[e] * red_cost[e] < 0) {
deba@2440
   626
	    candidates.push_back(e);
kpeter@2471
   627
	    if (candidates.size() == list_length) break;
deba@2440
   628
	  }
deba@2440
   629
	}
deba@2440
   630
	if (candidates.size() == 0) return false;
deba@2440
   631
      }
deba@2440
   632
deba@2440
   633
      // Minor iteration
deba@2440
   634
      ++minor_count;
deba@2440
   635
      Cost min = 0;
kpeter@2471
   636
      Edge e;
kpeter@2471
   637
      for (int i = 0; i < candidates.size(); ++i) {
kpeter@2471
   638
        e = candidates[i];
kpeter@2471
   639
	if (state[e] * red_cost[e] < min) {
kpeter@2471
   640
	  min = state[e] * red_cost[e];
kpeter@2471
   641
	  in_edge = e;
deba@2440
   642
	}
deba@2440
   643
      }
deba@2440
   644
      return true;
deba@2440
   645
    }
deba@2440
   646
#endif
deba@2440
   647
deba@2440
   648
#ifdef SORTED_LIST_PIVOT
deba@2440
   649
    /// \brief Functor class to compare edges during sort of the
deba@2440
   650
    /// candidate list.
deba@2440
   651
    class SortFunc
deba@2440
   652
    {
deba@2440
   653
    private:
deba@2440
   654
      const IntEdgeMap &st;
deba@2440
   655
      const ReducedCostMap &rc;
deba@2440
   656
    public:
deba@2440
   657
      SortFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
deba@2440
   658
	st(_st), rc(_rc) {}
deba@2440
   659
      bool operator()(const Edge &e1, const Edge &e2) {
deba@2440
   660
	return st[e1] * rc[e1] < st[e2] * rc[e2];
deba@2440
   661
      }
deba@2440
   662
    };
deba@2440
   663
deba@2440
   664
    /// \brief Finds entering edge according to the "Sorted List"
deba@2440
   665
    /// pivot rule.
deba@2440
   666
    bool findEnteringEdge() {
deba@2440
   667
      static SortFunc sort_func(state, red_cost);
deba@2440
   668
deba@2440
   669
      // Minor iteration
kpeter@2471
   670
      while (list_index < candidates.size()) {
kpeter@2471
   671
	in_edge = candidates[list_index++];
deba@2440
   672
	if (state[in_edge] * red_cost[in_edge] < 0) return true;
deba@2440
   673
      }
deba@2440
   674
deba@2440
   675
      // Major iteration
kpeter@2471
   676
      candidates.clear();
deba@2440
   677
      Cost curr, min = 0;
deba@2440
   678
      for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   679
	if ((curr = state[e] * red_cost[e]) < min / LOWER_DIV) {
deba@2440
   680
	  candidates.push_back(e);
deba@2440
   681
	  if (curr < min) min = curr;
kpeter@2471
   682
	  if (candidates.size() == list_length) break;
deba@2440
   683
	}
deba@2440
   684
      }
deba@2440
   685
      if (candidates.size() == 0) return false;
deba@2440
   686
      sort(candidates.begin(), candidates.end(), sort_func);
kpeter@2471
   687
      in_edge = candidates[0];
kpeter@2471
   688
      list_index = 1;
deba@2440
   689
      return true;
deba@2440
   690
    }
deba@2440
   691
#endif
deba@2440
   692
deba@2440
   693
    /// \brief Finds the join node.
deba@2440
   694
    Node findJoinNode() {
deba@2440
   695
      // Finding the join node
deba@2440
   696
      Node u = graph.source(in_edge);
deba@2440
   697
      Node v = graph.target(in_edge);
deba@2440
   698
      while (u != v) {
deba@2440
   699
	if (depth[u] == depth[v]) {
deba@2440
   700
	  u = parent[u];
deba@2440
   701
	  v = parent[v];
deba@2440
   702
	}
deba@2440
   703
	else if (depth[u] > depth[v]) u = parent[u];
deba@2440
   704
	else v = parent[v];
deba@2440
   705
      }
deba@2440
   706
      return u;
deba@2440
   707
    }
deba@2440
   708
deba@2440
   709
    /// \brief Finds the leaving edge of the cycle. Returns \c true if
deba@2440
   710
    /// the leaving edge is not the same as the entering edge.
deba@2440
   711
    bool findLeavingEdge() {
deba@2440
   712
      // Initializing first and second nodes according to the direction
deba@2440
   713
      // of the cycle
deba@2440
   714
      if (state[in_edge] == LOWER) {
deba@2440
   715
	first = graph.source(in_edge);
deba@2440
   716
	second	= graph.target(in_edge);
deba@2440
   717
      } else {
deba@2440
   718
	first = graph.target(in_edge);
deba@2440
   719
	second	= graph.source(in_edge);
deba@2440
   720
      }
deba@2440
   721
      delta = capacity[in_edge];
deba@2440
   722
      bool result = false;
deba@2440
   723
      Capacity d;
deba@2440
   724
      Edge e;
deba@2440
   725
deba@2440
   726
      // Searching the cycle along the path form the first node to the
deba@2440
   727
      // root node
deba@2440
   728
      for (Node u = first; u != join; u = parent[u]) {
deba@2440
   729
	e = pred_edge[u];
deba@2440
   730
	d = forward[u] ? flow[e] : capacity[e] - flow[e];
deba@2440
   731
	if (d < delta) {
deba@2440
   732
	  delta = d;
deba@2440
   733
	  u_out = u;
deba@2440
   734
	  u_in = first;
deba@2440
   735
	  v_in = second;
deba@2440
   736
	  result = true;
deba@2440
   737
	}
deba@2440
   738
      }
deba@2440
   739
      // Searching the cycle along the path form the second node to the
deba@2440
   740
      // root node
deba@2440
   741
      for (Node u = second; u != join; u = parent[u]) {
deba@2440
   742
	e = pred_edge[u];
deba@2440
   743
	d = forward[u] ? capacity[e] - flow[e] : flow[e];
deba@2440
   744
	if (d <= delta) {
deba@2440
   745
	  delta = d;
deba@2440
   746
	  u_out = u;
deba@2440
   747
	  u_in = second;
deba@2440
   748
	  v_in = first;
deba@2440
   749
	  result = true;
deba@2440
   750
	}
deba@2440
   751
      }
deba@2440
   752
      return result;
deba@2440
   753
    }
deba@2440
   754
deba@2440
   755
    /// \brief Changes flow and state edge maps.
deba@2440
   756
    void changeFlows(bool change) {
deba@2440
   757
      // Augmenting along the cycle
deba@2440
   758
      if (delta > 0) {
deba@2440
   759
	Capacity val = state[in_edge] * delta;
deba@2440
   760
	flow[in_edge] += val;
deba@2440
   761
	for (Node u = graph.source(in_edge); u != join; u = parent[u]) {
deba@2440
   762
	  flow[pred_edge[u]] += forward[u] ? -val : val;
deba@2440
   763
	}
deba@2440
   764
	for (Node u = graph.target(in_edge); u != join; u = parent[u]) {
deba@2440
   765
	  flow[pred_edge[u]] += forward[u] ? val : -val;
deba@2440
   766
	}
deba@2440
   767
      }
deba@2440
   768
      // Updating the state of the entering and leaving edges
deba@2440
   769
      if (change) {
deba@2440
   770
	state[in_edge] = TREE;
deba@2440
   771
	state[pred_edge[u_out]] =
deba@2440
   772
	  (flow[pred_edge[u_out]] == 0) ? LOWER : UPPER;
deba@2440
   773
      } else {
deba@2440
   774
	state[in_edge] = -state[in_edge];
deba@2440
   775
      }
deba@2440
   776
    }
deba@2440
   777
deba@2440
   778
    /// \brief Updates thread and parent node maps.
deba@2440
   779
    void updateThreadParent() {
deba@2440
   780
      Node u;
deba@2440
   781
      v_out = parent[u_out];
deba@2440
   782
deba@2440
   783
      // Handling the case when join and v_out coincide
deba@2440
   784
      bool par_first = false;
deba@2440
   785
      if (join == v_out) {
deba@2440
   786
	for (u = join; u != u_in && u != v_in; u = thread[u]) ;
deba@2440
   787
	if (u == v_in) {
deba@2440
   788
	  par_first = true;
deba@2440
   789
	  while (thread[u] != u_out) u = thread[u];
deba@2440
   790
	  first = u;
deba@2440
   791
	}
deba@2440
   792
      }
deba@2440
   793
deba@2440
   794
      // Finding the last successor of u_in (u) and the node after it
deba@2440
   795
      // (right) according to the thread index
deba@2440
   796
      for (u = u_in; depth[thread[u]] > depth[u_in]; u = thread[u]) ;
deba@2440
   797
      right = thread[u];
deba@2440
   798
      if (thread[v_in] == u_out) {
deba@2440
   799
	for (last = u; depth[last] > depth[u_out]; last = thread[last]) ;
deba@2440
   800
	if (last == u_out) last = thread[last];
deba@2440
   801
      }
deba@2440
   802
      else last = thread[v_in];
deba@2440
   803
deba@2440
   804
      // Updating stem nodes
deba@2440
   805
      thread[v_in] = stem = u_in;
deba@2440
   806
      par_stem = v_in;
deba@2440
   807
      while (stem != u_out) {
deba@2440
   808
	thread[u] = new_stem = parent[stem];
deba@2440
   809
deba@2440
   810
	// Finding the node just before the stem node (u) according to
deba@2440
   811
	// the original thread index
deba@2440
   812
	for (u = new_stem; thread[u] != stem; u = thread[u]) ;
deba@2440
   813
	thread[u] = right;
deba@2440
   814
deba@2440
   815
	// Changing the parent node of stem and shifting stem and
deba@2440
   816
	// par_stem nodes
deba@2440
   817
	parent[stem] = par_stem;
deba@2440
   818
	par_stem = stem;
deba@2440
   819
	stem = new_stem;
deba@2440
   820
deba@2440
   821
	// Finding the last successor of stem (u) and the node after it
deba@2440
   822
	// (right) according to the thread index
deba@2440
   823
	for (u = stem; depth[thread[u]] > depth[stem]; u = thread[u]) ;
deba@2440
   824
	right = thread[u];
deba@2440
   825
      }
deba@2440
   826
      parent[u_out] = par_stem;
deba@2440
   827
      thread[u] = last;
deba@2440
   828
deba@2440
   829
      if (join == v_out && par_first) {
deba@2440
   830
	if (first != v_in) thread[first] = right;
deba@2440
   831
      } else {
deba@2440
   832
	for (u = v_out; thread[u] != u_out; u = thread[u]) ;
deba@2440
   833
	thread[u] = right;
deba@2440
   834
      }
deba@2440
   835
    }
deba@2440
   836
deba@2440
   837
    /// \brief Updates pred_edge and forward node maps.
deba@2440
   838
    void updatePredEdge() {
deba@2440
   839
      Node u = u_out, v;
deba@2440
   840
      while (u != u_in) {
deba@2440
   841
	v = parent[u];
deba@2440
   842
	pred_edge[u] = pred_edge[v];
deba@2440
   843
	forward[u] = !forward[v];
deba@2440
   844
	u = v;
deba@2440
   845
      }
deba@2440
   846
      pred_edge[u_in] = in_edge;
deba@2440
   847
      forward[u_in] = (u_in == graph.source(in_edge));
deba@2440
   848
    }
deba@2440
   849
deba@2440
   850
    /// \brief Updates depth and potential node maps.
deba@2440
   851
    void updateDepthPotential() {
deba@2440
   852
      depth[u_in] = depth[v_in] + 1;
deba@2440
   853
      potential[u_in] = forward[u_in] ?
deba@2440
   854
	potential[v_in] + cost[pred_edge[u_in]] :
deba@2440
   855
	potential[v_in] - cost[pred_edge[u_in]];
deba@2440
   856
deba@2440
   857
      Node u = thread[u_in], v;
deba@2440
   858
      while (true) {
deba@2440
   859
	v = parent[u];
deba@2440
   860
	if (v == INVALID) break;
deba@2440
   861
	depth[u] = depth[v] + 1;
deba@2440
   862
	potential[u] = forward[u] ?
deba@2440
   863
	  potential[v] + cost[pred_edge[u]] :
deba@2440
   864
	  potential[v] - cost[pred_edge[u]];
deba@2440
   865
	if (depth[u] <= depth[v_in]) break;
deba@2440
   866
	u = thread[u];
deba@2440
   867
      }
deba@2440
   868
    }
deba@2440
   869
deba@2440
   870
    /// \brief Executes the algorithm.
deba@2440
   871
    bool start() {
deba@2440
   872
      // Processing pivots
deba@2440
   873
#ifdef _DEBUG_ITER_
deba@2440
   874
      int iter_num = 0;
deba@2440
   875
#endif
deba@2440
   876
#if defined(FIRST_ELIGIBLE_PIVOT) || defined(EDGE_BLOCK_PIVOT)
deba@2440
   877
      EdgeIt next_edge(graph);
deba@2440
   878
      while (findEnteringEdge(next_edge))
deba@2440
   879
#else
deba@2440
   880
      while (findEnteringEdge())
deba@2440
   881
#endif
deba@2440
   882
      {
deba@2440
   883
	join = findJoinNode();
deba@2440
   884
	bool change = findLeavingEdge();
deba@2440
   885
	changeFlows(change);
deba@2440
   886
	if (change) {
deba@2440
   887
	  updateThreadParent();
deba@2440
   888
	  updatePredEdge();
deba@2440
   889
	  updateDepthPotential();
deba@2440
   890
	}
deba@2440
   891
#ifdef _DEBUG_ITER_
deba@2440
   892
	++iter_num;
deba@2440
   893
#endif
deba@2440
   894
      }
deba@2440
   895
deba@2440
   896
#ifdef _DEBUG_ITER_
deba@2440
   897
      std::cout << "Network Simplex algorithm finished. " << iter_num
deba@2444
   898
		<< " pivot iterations performed." << std::endl;
deba@2440
   899
#endif
deba@2440
   900
deba@2440
   901
      // Checking if the flow amount equals zero on all the
deba@2440
   902
      // artificial edges
deba@2440
   903
      for (InEdgeIt e(graph, root); e != INVALID; ++e)
deba@2440
   904
	if (flow[e] > 0) return false;
deba@2440
   905
      for (OutEdgeIt e(graph, root); e != INVALID; ++e)
deba@2440
   906
	if (flow[e] > 0) return false;
deba@2440
   907
deba@2440
   908
      // Copying flow values to flow_result
deba@2440
   909
      if (lower) {
deba@2440
   910
	for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
deba@2440
   911
	  flow_result[e] = (*lower)[e] + flow[edge_ref[e]];
deba@2440
   912
      } else {
deba@2440
   913
	for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
deba@2440
   914
	  flow_result[e] = flow[edge_ref[e]];
deba@2440
   915
      }
deba@2440
   916
      // Copying potential values to potential_result
deba@2440
   917
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
deba@2440
   918
	potential_result[n] = potential[node_ref[n]];
deba@2440
   919
deba@2440
   920
      return true;
deba@2440
   921
    }
deba@2440
   922
deba@2440
   923
  }; //class NetworkSimplex
deba@2440
   924
deba@2440
   925
  ///@}
deba@2440
   926
deba@2440
   927
} //namespace lemon
deba@2440
   928
deba@2440
   929
#endif //LEMON_NETWORK_SIMPLEX_H