lemon/network_simplex.h
author deba
Mon, 07 May 2007 11:42:18 +0000
changeset 2440 c9218405595b
child 2444 06f3702bf18d
permissions -rw-r--r--
Various min cost flow solvers

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 EDGE_BLOCK_PIVOT
deba@2440
    34
//#define FIRST_ELIGIBLE_PIVOT
deba@2440
    35
//#define BEST_ELIGIBLE_PIVOT
deba@2440
    36
//#define CANDIDATE_LIST_PIVOT
deba@2440
    37
//#define SORTED_LIST_PIVOT
deba@2440
    38
deba@2440
    39
/// \brief State constant for edges at their lower bounds.
deba@2440
    40
#define LOWER	1
deba@2440
    41
/// \brief State constant for edges in the spanning tree.
deba@2440
    42
#define TREE	0
deba@2440
    43
/// \brief State constant for edges at their upper bounds.
deba@2440
    44
#define UPPER	-1
deba@2440
    45
deba@2440
    46
#ifdef EDGE_BLOCK_PIVOT
deba@2440
    47
  /// \brief Number of blocks for the "Edge Block" pivot rule.
deba@2440
    48
  #define BLOCK_NUM	  100
deba@2440
    49
  /// \brief Lower bound for the number of edges to use "Edge Block"
deba@2440
    50
  // pivot rule instead of "First Eligible" pivot rule.
deba@2440
    51
  #define MIN_BOUND	  1000
deba@2440
    52
#endif
deba@2440
    53
deba@2440
    54
#ifdef CANDIDATE_LIST_PIVOT
deba@2440
    55
  #include <list>
deba@2440
    56
  /// \brief The maximum length of the edge list for the
deba@2440
    57
  /// "Candidate List" pivot rule.
deba@2440
    58
  #define LIST_LENGTH	  100
deba@2440
    59
  /// \brief The maximum number of minor iterations between two major
deba@2440
    60
  /// itarations.
deba@2440
    61
  #define MINOR_LIMIT	  50
deba@2440
    62
#endif
deba@2440
    63
deba@2440
    64
#ifdef SORTED_LIST_PIVOT
deba@2440
    65
  #include <deque>
deba@2440
    66
  #include <algorithm>
deba@2440
    67
  /// \brief The maximum length of the edge list for the
deba@2440
    68
  /// "Sorted List" pivot rule.
deba@2440
    69
  #define LIST_LENGTH	  500
deba@2440
    70
  #define LOWER_DIV	  4
deba@2440
    71
#endif
deba@2440
    72
deba@2440
    73
//#define _DEBUG_ITER_
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@2440
   516
      block_size = edge_num > MIN_BOUND ? edge_num / BLOCK_NUM + 1 : 1;
deba@2440
   517
#endif
deba@2440
   518
#ifdef CANDIDATE_LIST_PIVOT
deba@2440
   519
      minor_count = 0;
deba@2440
   520
#endif
deba@2440
   521
deba@2440
   522
      return sum == 0;
deba@2440
   523
    }
deba@2440
   524
deba@2440
   525
#ifdef FIRST_ELIGIBLE_PIVOT
deba@2440
   526
    /// \brief Finds entering edge according to the "First Eligible"
deba@2440
   527
    /// pivot rule.
deba@2440
   528
    bool findEnteringEdge(EdgeIt &next_edge) {
deba@2440
   529
      for (EdgeIt e = next_edge; e != INVALID; ++e) {
deba@2440
   530
	if (state[e] * red_cost[e] < 0) {
deba@2440
   531
	  in_edge = e;
deba@2440
   532
	  next_edge = ++e;
deba@2440
   533
	  return true;
deba@2440
   534
	}
deba@2440
   535
      }
deba@2440
   536
      for (EdgeIt e(graph); e != next_edge; ++e) {
deba@2440
   537
	if (state[e] * red_cost[e] < 0) {
deba@2440
   538
	  in_edge = e;
deba@2440
   539
	  next_edge = ++e;
deba@2440
   540
	  return true;
deba@2440
   541
	}
deba@2440
   542
      }
deba@2440
   543
      return false;
deba@2440
   544
    }
deba@2440
   545
#endif
deba@2440
   546
deba@2440
   547
#ifdef BEST_ELIGIBLE_PIVOT
deba@2440
   548
    /// \brief Finds entering edge according to the "Best Eligible"
deba@2440
   549
    /// pivot rule.
deba@2440
   550
    bool findEnteringEdge() {
deba@2440
   551
      Cost min = 0;
deba@2440
   552
      for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   553
	if (state[e] * red_cost[e] < min) {
deba@2440
   554
	  min = state[e] * red_cost[e];
deba@2440
   555
	  in_edge = e;
deba@2440
   556
	}
deba@2440
   557
      }
deba@2440
   558
      return min < 0;
deba@2440
   559
    }
deba@2440
   560
#endif
deba@2440
   561
deba@2440
   562
#ifdef EDGE_BLOCK_PIVOT
deba@2440
   563
    /// \brief Finds entering edge according to the "Edge Block"
deba@2440
   564
    /// pivot rule.
deba@2440
   565
    bool findEnteringEdge(EdgeIt &next_edge) {
deba@2440
   566
      if (block_size == 1) {
deba@2440
   567
	// Performing first eligible selection
deba@2440
   568
	for (EdgeIt e = next_edge; e != INVALID; ++e) {
deba@2440
   569
	  if (state[e] * red_cost[e] < 0) {
deba@2440
   570
	    in_edge = e;
deba@2440
   571
	    next_edge = ++e;
deba@2440
   572
	    return true;
deba@2440
   573
	  }
deba@2440
   574
	}
deba@2440
   575
	for (EdgeIt e(graph); e != next_edge; ++e) {
deba@2440
   576
	  if (state[e] * red_cost[e] < 0) {
deba@2440
   577
	    in_edge = e;
deba@2440
   578
	    next_edge = ++e;
deba@2440
   579
	    return true;
deba@2440
   580
	  }
deba@2440
   581
	}
deba@2440
   582
	return false;
deba@2440
   583
      } else {
deba@2440
   584
	// Performing edge block selection
deba@2440
   585
	Cost curr, min = 0;
deba@2440
   586
	EdgeIt min_edge(graph);
deba@2440
   587
	int cnt = 0;
deba@2440
   588
	for (EdgeIt e = next_edge; e != INVALID; ++e) {
deba@2440
   589
	  if ((curr = state[e] * red_cost[e]) < min) {
deba@2440
   590
	    min = curr;
deba@2440
   591
	    min_edge = e;
deba@2440
   592
	  }
deba@2440
   593
	  if (++cnt == block_size) {
deba@2440
   594
	    if (min < 0) break;
deba@2440
   595
	    cnt = 0;
deba@2440
   596
	  }
deba@2440
   597
	}
deba@2440
   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@2440
   610
	in_edge = min_edge;
deba@2440
   611
	if ((next_edge = ++min_edge) == INVALID)
deba@2440
   612
	  next_edge = EdgeIt(graph);
deba@2440
   613
	return min < 0;
deba@2440
   614
      }
deba@2440
   615
    }
deba@2440
   616
#endif
deba@2440
   617
deba@2440
   618
#ifdef CANDIDATE_LIST_PIVOT
deba@2440
   619
    /// \brief Functor class for removing non-eligible edges from the
deba@2440
   620
    /// candidate list.
deba@2440
   621
    class RemoveFunc
deba@2440
   622
    {
deba@2440
   623
    private:
deba@2440
   624
      const IntEdgeMap &st;
deba@2440
   625
      const ReducedCostMap &rc;
deba@2440
   626
    public:
deba@2440
   627
      RemoveFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
deba@2440
   628
	st(_st), rc(_rc) {}
deba@2440
   629
      bool operator()(const Edge &e) {
deba@2440
   630
	return st[e] * rc[e] >= 0;
deba@2440
   631
      }
deba@2440
   632
    };
deba@2440
   633
deba@2440
   634
    /// \brief Finds entering edge according to the "Candidate List"
deba@2440
   635
    /// pivot rule.
deba@2440
   636
    bool findEnteringEdge() {
deba@2440
   637
      static RemoveFunc remove_func(state, red_cost);
deba@2440
   638
      typedef typename std::list<Edge>::iterator ListIt;
deba@2440
   639
deba@2440
   640
      candidates.remove_if(remove_func);
deba@2440
   641
      if (minor_count >= MINOR_LIMIT || candidates.size() == 0) {
deba@2440
   642
	// Major iteration
deba@2440
   643
	for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   644
	  if (state[e] * red_cost[e] < 0) {
deba@2440
   645
	    candidates.push_back(e);
deba@2440
   646
	    if (candidates.size() == LIST_LENGTH) break;
deba@2440
   647
	  }
deba@2440
   648
	}
deba@2440
   649
	if (candidates.size() == 0) return false;
deba@2440
   650
      }
deba@2440
   651
deba@2440
   652
      // Minor iteration
deba@2440
   653
      ++minor_count;
deba@2440
   654
      Cost min = 0;
deba@2440
   655
      for (ListIt it = candidates.begin(); it != candidates.end(); ++it) {
deba@2440
   656
	if (state[*it] * red_cost[*it] < min) {
deba@2440
   657
	  min = state[*it] * red_cost[*it];
deba@2440
   658
	  in_edge = *it;
deba@2440
   659
	}
deba@2440
   660
      }
deba@2440
   661
      return true;
deba@2440
   662
    }
deba@2440
   663
#endif
deba@2440
   664
deba@2440
   665
#ifdef SORTED_LIST_PIVOT
deba@2440
   666
    /// \brief Functor class to compare edges during sort of the
deba@2440
   667
    /// candidate list.
deba@2440
   668
    class SortFunc
deba@2440
   669
    {
deba@2440
   670
    private:
deba@2440
   671
      const IntEdgeMap &st;
deba@2440
   672
      const ReducedCostMap &rc;
deba@2440
   673
    public:
deba@2440
   674
      SortFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
deba@2440
   675
	st(_st), rc(_rc) {}
deba@2440
   676
      bool operator()(const Edge &e1, const Edge &e2) {
deba@2440
   677
	return st[e1] * rc[e1] < st[e2] * rc[e2];
deba@2440
   678
      }
deba@2440
   679
    };
deba@2440
   680
deba@2440
   681
    /// \brief Finds entering edge according to the "Sorted List"
deba@2440
   682
    /// pivot rule.
deba@2440
   683
    bool findEnteringEdge() {
deba@2440
   684
      static SortFunc sort_func(state, red_cost);
deba@2440
   685
deba@2440
   686
      // Minor iteration
deba@2440
   687
      while (candidates.size() > 0) {
deba@2440
   688
	in_edge = candidates.front();
deba@2440
   689
	candidates.pop_front();
deba@2440
   690
	if (state[in_edge] * red_cost[in_edge] < 0) return true;
deba@2440
   691
      }
deba@2440
   692
deba@2440
   693
      // Major iteration
deba@2440
   694
      Cost curr, min = 0;
deba@2440
   695
      for (EdgeIt e(graph); e != INVALID; ++e) {
deba@2440
   696
	if ((curr = state[e] * red_cost[e]) < min / LOWER_DIV) {
deba@2440
   697
	  candidates.push_back(e);
deba@2440
   698
	  if (curr < min) min = curr;
deba@2440
   699
	  if (candidates.size() >= LIST_LENGTH) break;
deba@2440
   700
	}
deba@2440
   701
      }
deba@2440
   702
      if (candidates.size() == 0) return false;
deba@2440
   703
      sort(candidates.begin(), candidates.end(), sort_func);
deba@2440
   704
      return true;
deba@2440
   705
    }
deba@2440
   706
#endif
deba@2440
   707
deba@2440
   708
    /// \brief Finds the join node.
deba@2440
   709
    Node findJoinNode() {
deba@2440
   710
      // Finding the join node
deba@2440
   711
      Node u = graph.source(in_edge);
deba@2440
   712
      Node v = graph.target(in_edge);
deba@2440
   713
      while (u != v) {
deba@2440
   714
	if (depth[u] == depth[v]) {
deba@2440
   715
	  u = parent[u];
deba@2440
   716
	  v = parent[v];
deba@2440
   717
	}
deba@2440
   718
	else if (depth[u] > depth[v]) u = parent[u];
deba@2440
   719
	else v = parent[v];
deba@2440
   720
      }
deba@2440
   721
      return u;
deba@2440
   722
    }
deba@2440
   723
deba@2440
   724
    /// \brief Finds the leaving edge of the cycle. Returns \c true if
deba@2440
   725
    /// the leaving edge is not the same as the entering edge.
deba@2440
   726
    bool findLeavingEdge() {
deba@2440
   727
      // Initializing first and second nodes according to the direction
deba@2440
   728
      // of the cycle
deba@2440
   729
      if (state[in_edge] == LOWER) {
deba@2440
   730
	first = graph.source(in_edge);
deba@2440
   731
	second	= graph.target(in_edge);
deba@2440
   732
      } else {
deba@2440
   733
	first = graph.target(in_edge);
deba@2440
   734
	second	= graph.source(in_edge);
deba@2440
   735
      }
deba@2440
   736
      delta = capacity[in_edge];
deba@2440
   737
      bool result = false;
deba@2440
   738
      Capacity d;
deba@2440
   739
      Edge e;
deba@2440
   740
deba@2440
   741
      // Searching the cycle along the path form the first node to the
deba@2440
   742
      // root node
deba@2440
   743
      for (Node u = first; u != join; u = parent[u]) {
deba@2440
   744
	e = pred_edge[u];
deba@2440
   745
	d = forward[u] ? flow[e] : capacity[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 = first;
deba@2440
   750
	  v_in = second;
deba@2440
   751
	  result = true;
deba@2440
   752
	}
deba@2440
   753
      }
deba@2440
   754
      // Searching the cycle along the path form the second node to the
deba@2440
   755
      // root node
deba@2440
   756
      for (Node u = second; u != join; u = parent[u]) {
deba@2440
   757
	e = pred_edge[u];
deba@2440
   758
	d = forward[u] ? capacity[e] - flow[e] : flow[e];
deba@2440
   759
	if (d <= delta) {
deba@2440
   760
	  delta = d;
deba@2440
   761
	  u_out = u;
deba@2440
   762
	  u_in = second;
deba@2440
   763
	  v_in = first;
deba@2440
   764
	  result = true;
deba@2440
   765
	}
deba@2440
   766
      }
deba@2440
   767
      return result;
deba@2440
   768
    }
deba@2440
   769
deba@2440
   770
    /// \brief Changes flow and state edge maps.
deba@2440
   771
    void changeFlows(bool change) {
deba@2440
   772
      // Augmenting along the cycle
deba@2440
   773
      if (delta > 0) {
deba@2440
   774
	Capacity val = state[in_edge] * delta;
deba@2440
   775
	flow[in_edge] += val;
deba@2440
   776
	for (Node u = graph.source(in_edge); u != join; u = parent[u]) {
deba@2440
   777
	  flow[pred_edge[u]] += forward[u] ? -val : val;
deba@2440
   778
	}
deba@2440
   779
	for (Node u = graph.target(in_edge); u != join; u = parent[u]) {
deba@2440
   780
	  flow[pred_edge[u]] += forward[u] ? val : -val;
deba@2440
   781
	}
deba@2440
   782
      }
deba@2440
   783
      // Updating the state of the entering and leaving edges
deba@2440
   784
      if (change) {
deba@2440
   785
	state[in_edge] = TREE;
deba@2440
   786
	state[pred_edge[u_out]] =
deba@2440
   787
	  (flow[pred_edge[u_out]] == 0) ? LOWER : UPPER;
deba@2440
   788
      } else {
deba@2440
   789
	state[in_edge] = -state[in_edge];
deba@2440
   790
      }
deba@2440
   791
    }
deba@2440
   792
deba@2440
   793
    /// \brief Updates thread and parent node maps.
deba@2440
   794
    void updateThreadParent() {
deba@2440
   795
      Node u;
deba@2440
   796
      v_out = parent[u_out];
deba@2440
   797
deba@2440
   798
      // Handling the case when join and v_out coincide
deba@2440
   799
      bool par_first = false;
deba@2440
   800
      if (join == v_out) {
deba@2440
   801
	for (u = join; u != u_in && u != v_in; u = thread[u]) ;
deba@2440
   802
	if (u == v_in) {
deba@2440
   803
	  par_first = true;
deba@2440
   804
	  while (thread[u] != u_out) u = thread[u];
deba@2440
   805
	  first = u;
deba@2440
   806
	}
deba@2440
   807
      }
deba@2440
   808
deba@2440
   809
      // Finding the last successor of u_in (u) and the node after it
deba@2440
   810
      // (right) according to the thread index
deba@2440
   811
      for (u = u_in; depth[thread[u]] > depth[u_in]; u = thread[u]) ;
deba@2440
   812
      right = thread[u];
deba@2440
   813
      if (thread[v_in] == u_out) {
deba@2440
   814
	for (last = u; depth[last] > depth[u_out]; last = thread[last]) ;
deba@2440
   815
	if (last == u_out) last = thread[last];
deba@2440
   816
      }
deba@2440
   817
      else last = thread[v_in];
deba@2440
   818
deba@2440
   819
      // Updating stem nodes
deba@2440
   820
      thread[v_in] = stem = u_in;
deba@2440
   821
      par_stem = v_in;
deba@2440
   822
      while (stem != u_out) {
deba@2440
   823
	thread[u] = new_stem = parent[stem];
deba@2440
   824
deba@2440
   825
	// Finding the node just before the stem node (u) according to
deba@2440
   826
	// the original thread index
deba@2440
   827
	for (u = new_stem; thread[u] != stem; u = thread[u]) ;
deba@2440
   828
	thread[u] = right;
deba@2440
   829
deba@2440
   830
	// Changing the parent node of stem and shifting stem and
deba@2440
   831
	// par_stem nodes
deba@2440
   832
	parent[stem] = par_stem;
deba@2440
   833
	par_stem = stem;
deba@2440
   834
	stem = new_stem;
deba@2440
   835
deba@2440
   836
	// Finding the last successor of stem (u) and the node after it
deba@2440
   837
	// (right) according to the thread index
deba@2440
   838
	for (u = stem; depth[thread[u]] > depth[stem]; u = thread[u]) ;
deba@2440
   839
	right = thread[u];
deba@2440
   840
      }
deba@2440
   841
      parent[u_out] = par_stem;
deba@2440
   842
      thread[u] = last;
deba@2440
   843
deba@2440
   844
      if (join == v_out && par_first) {
deba@2440
   845
	if (first != v_in) thread[first] = right;
deba@2440
   846
      } else {
deba@2440
   847
	for (u = v_out; thread[u] != u_out; u = thread[u]) ;
deba@2440
   848
	thread[u] = right;
deba@2440
   849
      }
deba@2440
   850
    }
deba@2440
   851
deba@2440
   852
    /// \brief Updates pred_edge and forward node maps.
deba@2440
   853
    void updatePredEdge() {
deba@2440
   854
      Node u = u_out, v;
deba@2440
   855
      while (u != u_in) {
deba@2440
   856
	v = parent[u];
deba@2440
   857
	pred_edge[u] = pred_edge[v];
deba@2440
   858
	forward[u] = !forward[v];
deba@2440
   859
	u = v;
deba@2440
   860
      }
deba@2440
   861
      pred_edge[u_in] = in_edge;
deba@2440
   862
      forward[u_in] = (u_in == graph.source(in_edge));
deba@2440
   863
    }
deba@2440
   864
deba@2440
   865
    /// \brief Updates depth and potential node maps.
deba@2440
   866
    void updateDepthPotential() {
deba@2440
   867
      depth[u_in] = depth[v_in] + 1;
deba@2440
   868
      potential[u_in] = forward[u_in] ?
deba@2440
   869
	potential[v_in] + cost[pred_edge[u_in]] :
deba@2440
   870
	potential[v_in] - cost[pred_edge[u_in]];
deba@2440
   871
deba@2440
   872
      Node u = thread[u_in], v;
deba@2440
   873
      while (true) {
deba@2440
   874
	v = parent[u];
deba@2440
   875
	if (v == INVALID) break;
deba@2440
   876
	depth[u] = depth[v] + 1;
deba@2440
   877
	potential[u] = forward[u] ?
deba@2440
   878
	  potential[v] + cost[pred_edge[u]] :
deba@2440
   879
	  potential[v] - cost[pred_edge[u]];
deba@2440
   880
	if (depth[u] <= depth[v_in]) break;
deba@2440
   881
	u = thread[u];
deba@2440
   882
      }
deba@2440
   883
    }
deba@2440
   884
deba@2440
   885
    /// \brief Executes the algorithm.
deba@2440
   886
    bool start() {
deba@2440
   887
      // Processing pivots
deba@2440
   888
#ifdef _DEBUG_ITER_
deba@2440
   889
      int iter_num = 0;
deba@2440
   890
#endif
deba@2440
   891
#if defined(FIRST_ELIGIBLE_PIVOT) || defined(EDGE_BLOCK_PIVOT)
deba@2440
   892
      EdgeIt next_edge(graph);
deba@2440
   893
      while (findEnteringEdge(next_edge))
deba@2440
   894
#else
deba@2440
   895
      while (findEnteringEdge())
deba@2440
   896
#endif
deba@2440
   897
      {
deba@2440
   898
	join = findJoinNode();
deba@2440
   899
	bool change = findLeavingEdge();
deba@2440
   900
	changeFlows(change);
deba@2440
   901
	if (change) {
deba@2440
   902
	  updateThreadParent();
deba@2440
   903
	  updatePredEdge();
deba@2440
   904
	  updateDepthPotential();
deba@2440
   905
	}
deba@2440
   906
#ifdef _DEBUG_ITER_
deba@2440
   907
	++iter_num;
deba@2440
   908
#endif
deba@2440
   909
      }
deba@2440
   910
deba@2440
   911
#ifdef _DEBUG_ITER_
deba@2440
   912
      t_iter.stop();
deba@2440
   913
      std::cout << "Network Simplex algorithm finished. " << iter_num
deba@2440
   914
		<< " pivot iterations performed.";
deba@2440
   915
#endif
deba@2440
   916
deba@2440
   917
      // Checking if the flow amount equals zero on all the
deba@2440
   918
      // artificial edges
deba@2440
   919
      for (InEdgeIt e(graph, root); e != INVALID; ++e)
deba@2440
   920
	if (flow[e] > 0) return false;
deba@2440
   921
      for (OutEdgeIt e(graph, root); e != INVALID; ++e)
deba@2440
   922
	if (flow[e] > 0) return false;
deba@2440
   923
deba@2440
   924
      // Copying flow values to flow_result
deba@2440
   925
      if (lower) {
deba@2440
   926
	for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
deba@2440
   927
	  flow_result[e] = (*lower)[e] + flow[edge_ref[e]];
deba@2440
   928
      } else {
deba@2440
   929
	for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
deba@2440
   930
	  flow_result[e] = flow[edge_ref[e]];
deba@2440
   931
      }
deba@2440
   932
      // Copying potential values to potential_result
deba@2440
   933
      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
deba@2440
   934
	potential_result[n] = potential[node_ref[n]];
deba@2440
   935
deba@2440
   936
      return true;
deba@2440
   937
    }
deba@2440
   938
deba@2440
   939
  }; //class NetworkSimplex
deba@2440
   940
deba@2440
   941
  ///@}
deba@2440
   942
deba@2440
   943
} //namespace lemon
deba@2440
   944
deba@2440
   945
#endif //LEMON_NETWORK_SIMPLEX_H