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