lemon/network_simplex.h
author deba
Fri, 11 May 2007 16:03:20 +0000
changeset 2444 06f3702bf18d
parent 2440 c9218405595b
child 2457 8c791ee69a45
permissions -rw-r--r--
Patch in network simplex

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