lemon/cost_scaling.h
author kpeter
Mon, 18 Feb 2008 03:34:16 +0000
changeset 2577 2c6204d4b0f6
child 2581 054566ac0934
permissions -rw-r--r--
Add a cost scaling min cost flow algorithm.

Add a cost scaling algorithm, which is performing generalized
push-relabel operations. It is almost as efficient as the capacity
scaling algorithm, but slower than network simplex.
kpeter@2577
     1
/* -*- C++ -*-
kpeter@2577
     2
 *
kpeter@2577
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@2577
     4
 *
kpeter@2577
     5
 * Copyright (C) 2003-2008
kpeter@2577
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@2577
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@2577
     8
 *
kpeter@2577
     9
 * Permission to use, modify and distribute this software is granted
kpeter@2577
    10
 * provided that this copyright notice appears in all copies. For
kpeter@2577
    11
 * precise terms see the accompanying LICENSE file.
kpeter@2577
    12
 *
kpeter@2577
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@2577
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@2577
    15
 * purpose.
kpeter@2577
    16
 *
kpeter@2577
    17
 */
kpeter@2577
    18
kpeter@2577
    19
#ifndef LEMON_COST_SCALING_H
kpeter@2577
    20
#define LEMON_COST_SCALING_H
kpeter@2577
    21
kpeter@2577
    22
/// \ingroup min_cost_flow
kpeter@2577
    23
///
kpeter@2577
    24
/// \file
kpeter@2577
    25
/// \brief Cost scaling algorithm for finding a minimum cost flow.
kpeter@2577
    26
kpeter@2577
    27
#include <deque>
kpeter@2577
    28
#include <lemon/graph_adaptor.h>
kpeter@2577
    29
#include <lemon/graph_utils.h>
kpeter@2577
    30
#include <lemon/maps.h>
kpeter@2577
    31
#include <lemon/math.h>
kpeter@2577
    32
kpeter@2577
    33
#include <lemon/circulation.h>
kpeter@2577
    34
#include <lemon/bellman_ford.h>
kpeter@2577
    35
kpeter@2577
    36
namespace lemon {
kpeter@2577
    37
kpeter@2577
    38
  /// \addtogroup min_cost_flow
kpeter@2577
    39
  /// @{
kpeter@2577
    40
kpeter@2577
    41
  /// \brief Implementation of the cost scaling algorithm for finding a
kpeter@2577
    42
  /// minimum cost flow.
kpeter@2577
    43
  ///
kpeter@2577
    44
  /// \ref CostScaling implements the cost scaling algorithm performing
kpeter@2577
    45
  /// generalized push-relabel operations for finding a minimum cost
kpeter@2577
    46
  /// flow.
kpeter@2577
    47
  ///
kpeter@2577
    48
  /// \tparam Graph The directed graph type the algorithm runs on.
kpeter@2577
    49
  /// \tparam LowerMap The type of the lower bound map.
kpeter@2577
    50
  /// \tparam CapacityMap The type of the capacity (upper bound) map.
kpeter@2577
    51
  /// \tparam CostMap The type of the cost (length) map.
kpeter@2577
    52
  /// \tparam SupplyMap The type of the supply map.
kpeter@2577
    53
  ///
kpeter@2577
    54
  /// \warning
kpeter@2577
    55
  /// - Edge capacities and costs should be \e non-negative \e integers.
kpeter@2577
    56
  /// - Supply values should be \e signed \e integers.
kpeter@2577
    57
  /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value.
kpeter@2577
    58
  /// - \c CapacityMap::Value and \c SupplyMap::Value must be
kpeter@2577
    59
  ///   convertible to each other.
kpeter@2577
    60
  /// - All value types must be convertible to \c CostMap::Value, which
kpeter@2577
    61
  ///   must be signed type.
kpeter@2577
    62
  ///
kpeter@2577
    63
  /// \note Edge costs are multiplied with the number of nodes during
kpeter@2577
    64
  /// the algorithm so overflow problems may arise more easily than with
kpeter@2577
    65
  /// other minimum cost flow algorithms.
kpeter@2577
    66
  /// If it is available, <tt>long long int</tt> type is used instead of
kpeter@2577
    67
  /// <tt>long int</tt> in the inside computations.
kpeter@2577
    68
  ///
kpeter@2577
    69
  /// \author Peter Kovacs
kpeter@2577
    70
kpeter@2577
    71
  template < typename Graph,
kpeter@2577
    72
             typename LowerMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    73
             typename CapacityMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    74
             typename CostMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    75
             typename SupplyMap = typename Graph::template NodeMap<int> >
kpeter@2577
    76
  class CostScaling
kpeter@2577
    77
  {
kpeter@2577
    78
    GRAPH_TYPEDEFS(typename Graph);
kpeter@2577
    79
kpeter@2577
    80
    typedef typename CapacityMap::Value Capacity;
kpeter@2577
    81
    typedef typename CostMap::Value Cost;
kpeter@2577
    82
    typedef typename SupplyMap::Value Supply;
kpeter@2577
    83
    typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap;
kpeter@2577
    84
    typedef typename Graph::template NodeMap<Supply> SupplyNodeMap;
kpeter@2577
    85
kpeter@2577
    86
    typedef ResGraphAdaptor< const Graph, Capacity,
kpeter@2577
    87
                             CapacityEdgeMap, CapacityEdgeMap > ResGraph;
kpeter@2577
    88
    typedef typename ResGraph::Edge ResEdge;
kpeter@2577
    89
kpeter@2577
    90
#if defined __GNUC__ && !defined __STRICT_ANSI__
kpeter@2577
    91
    typedef long long int LCost;
kpeter@2577
    92
#else
kpeter@2577
    93
    typedef long int LCost;
kpeter@2577
    94
#endif
kpeter@2577
    95
    typedef typename Graph::template EdgeMap<LCost> LargeCostMap;
kpeter@2577
    96
kpeter@2577
    97
  public:
kpeter@2577
    98
kpeter@2577
    99
    /// The type of the flow map.
kpeter@2577
   100
    typedef CapacityEdgeMap FlowMap;
kpeter@2577
   101
    /// The type of the potential map.
kpeter@2577
   102
    typedef typename Graph::template NodeMap<LCost> PotentialMap;
kpeter@2577
   103
kpeter@2577
   104
  private:
kpeter@2577
   105
kpeter@2577
   106
    /// \brief Map adaptor class for handling residual edge costs.
kpeter@2577
   107
    ///
kpeter@2577
   108
    /// \ref ResidualCostMap is a map adaptor class for handling
kpeter@2577
   109
    /// residual edge costs.
kpeter@2577
   110
    class ResidualCostMap : public MapBase<ResEdge, LCost>
kpeter@2577
   111
    {
kpeter@2577
   112
    private:
kpeter@2577
   113
kpeter@2577
   114
      const LargeCostMap &_cost_map;
kpeter@2577
   115
kpeter@2577
   116
    public:
kpeter@2577
   117
kpeter@2577
   118
      ///\e
kpeter@2577
   119
      ResidualCostMap(const LargeCostMap &cost_map) :
kpeter@2577
   120
        _cost_map(cost_map) {}
kpeter@2577
   121
kpeter@2577
   122
      ///\e
kpeter@2577
   123
      LCost operator[](const ResEdge &e) const {
kpeter@2577
   124
        return ResGraph::forward(e) ?  _cost_map[e] : -_cost_map[e];
kpeter@2577
   125
      }
kpeter@2577
   126
kpeter@2577
   127
    }; //class ResidualCostMap
kpeter@2577
   128
kpeter@2577
   129
    /// \brief Map adaptor class for handling reduced edge costs.
kpeter@2577
   130
    ///
kpeter@2577
   131
    /// \ref ReducedCostMap is a map adaptor class for handling reduced
kpeter@2577
   132
    /// edge costs.
kpeter@2577
   133
    class ReducedCostMap : public MapBase<Edge, LCost>
kpeter@2577
   134
    {
kpeter@2577
   135
    private:
kpeter@2577
   136
kpeter@2577
   137
      const Graph &_gr;
kpeter@2577
   138
      const LargeCostMap &_cost_map;
kpeter@2577
   139
      const PotentialMap &_pot_map;
kpeter@2577
   140
kpeter@2577
   141
    public:
kpeter@2577
   142
kpeter@2577
   143
      ///\e
kpeter@2577
   144
      ReducedCostMap( const Graph &gr,
kpeter@2577
   145
                      const LargeCostMap &cost_map,
kpeter@2577
   146
                      const PotentialMap &pot_map ) :
kpeter@2577
   147
        _gr(gr), _cost_map(cost_map), _pot_map(pot_map) {}
kpeter@2577
   148
kpeter@2577
   149
      ///\e
kpeter@2577
   150
      LCost operator[](const Edge &e) const {
kpeter@2577
   151
        return _cost_map[e] + _pot_map[_gr.source(e)]
kpeter@2577
   152
                            - _pot_map[_gr.target(e)];
kpeter@2577
   153
      }
kpeter@2577
   154
kpeter@2577
   155
    }; //class ReducedCostMap
kpeter@2577
   156
kpeter@2577
   157
  private:
kpeter@2577
   158
kpeter@2577
   159
    // Scaling factor
kpeter@2577
   160
    static const int ALPHA = 4;
kpeter@2577
   161
kpeter@2577
   162
    // Paramters for heuristics
kpeter@2577
   163
    static const int BF_HEURISTIC_EPSILON_BOUND    = 5000;
kpeter@2577
   164
    static const int BF_HEURISTIC_BOUND_FACTOR = 3;
kpeter@2577
   165
kpeter@2577
   166
  private:
kpeter@2577
   167
kpeter@2577
   168
    // The directed graph the algorithm runs on
kpeter@2577
   169
    const Graph &_graph;
kpeter@2577
   170
    // The original lower bound map
kpeter@2577
   171
    const LowerMap *_lower;
kpeter@2577
   172
    // The modified capacity map
kpeter@2577
   173
    CapacityEdgeMap _capacity;
kpeter@2577
   174
    // The original cost map
kpeter@2577
   175
    const CostMap &_orig_cost;
kpeter@2577
   176
    // The scaled cost map
kpeter@2577
   177
    LargeCostMap _cost;
kpeter@2577
   178
    // The modified supply map
kpeter@2577
   179
    SupplyNodeMap _supply;
kpeter@2577
   180
    bool _valid_supply;
kpeter@2577
   181
kpeter@2577
   182
    // Edge map of the current flow
kpeter@2577
   183
    FlowMap _flow;
kpeter@2577
   184
    // Node map of the current potentials
kpeter@2577
   185
    PotentialMap _potential;
kpeter@2577
   186
kpeter@2577
   187
    // The residual graph
kpeter@2577
   188
    ResGraph _res_graph;
kpeter@2577
   189
    // The residual cost map
kpeter@2577
   190
    ResidualCostMap _res_cost;
kpeter@2577
   191
    // The reduced cost map
kpeter@2577
   192
    ReducedCostMap _red_cost;
kpeter@2577
   193
    // The excess map
kpeter@2577
   194
    SupplyNodeMap _excess;
kpeter@2577
   195
    // The epsilon parameter used for cost scaling
kpeter@2577
   196
    LCost _epsilon;
kpeter@2577
   197
kpeter@2577
   198
  public:
kpeter@2577
   199
kpeter@2577
   200
    /// \brief General constructor of the class (with lower bounds).
kpeter@2577
   201
    ///
kpeter@2577
   202
    /// General constructor of the class (with lower bounds).
kpeter@2577
   203
    ///
kpeter@2577
   204
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   205
    /// \param lower The lower bounds of the edges.
kpeter@2577
   206
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   207
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   208
    /// \param supply The supply values of the nodes (signed).
kpeter@2577
   209
    CostScaling( const Graph &graph,
kpeter@2577
   210
                 const LowerMap &lower,
kpeter@2577
   211
                 const CapacityMap &capacity,
kpeter@2577
   212
                 const CostMap &cost,
kpeter@2577
   213
                 const SupplyMap &supply ) :
kpeter@2577
   214
      _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
kpeter@2577
   215
      _cost(graph), _supply(graph), _flow(graph, 0), _potential(graph, 0),
kpeter@2577
   216
      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
kpeter@2577
   217
      _red_cost(graph, _cost, _potential), _excess(graph, 0)
kpeter@2577
   218
    {
kpeter@2577
   219
      // Removing non-zero lower bounds
kpeter@2577
   220
      _capacity = subMap(capacity, lower);
kpeter@2577
   221
      Supply sum = 0;
kpeter@2577
   222
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2577
   223
        Supply s = supply[n];
kpeter@2577
   224
        for (InEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   225
          s += lower[e];
kpeter@2577
   226
        for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   227
          s -= lower[e];
kpeter@2577
   228
        _supply[n] = s;
kpeter@2577
   229
        sum += s;
kpeter@2577
   230
      }
kpeter@2577
   231
      _valid_supply = sum == 0;
kpeter@2577
   232
    }
kpeter@2577
   233
kpeter@2577
   234
    /// \brief General constructor of the class (without lower bounds).
kpeter@2577
   235
    ///
kpeter@2577
   236
    /// General constructor of the class (without lower bounds).
kpeter@2577
   237
    ///
kpeter@2577
   238
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   239
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   240
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   241
    /// \param supply The supply values of the nodes (signed).
kpeter@2577
   242
    CostScaling( const Graph &graph,
kpeter@2577
   243
                 const CapacityMap &capacity,
kpeter@2577
   244
                 const CostMap &cost,
kpeter@2577
   245
                 const SupplyMap &supply ) :
kpeter@2577
   246
      _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
kpeter@2577
   247
      _cost(graph), _supply(supply), _flow(graph, 0), _potential(graph, 0),
kpeter@2577
   248
      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
kpeter@2577
   249
      _red_cost(graph, _cost, _potential), _excess(graph, 0)
kpeter@2577
   250
    {
kpeter@2577
   251
      // Checking the sum of supply values
kpeter@2577
   252
      Supply sum = 0;
kpeter@2577
   253
      for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
kpeter@2577
   254
      _valid_supply = sum == 0;
kpeter@2577
   255
    }
kpeter@2577
   256
kpeter@2577
   257
    /// \brief Simple constructor of the class (with lower bounds).
kpeter@2577
   258
    ///
kpeter@2577
   259
    /// Simple constructor of the class (with lower bounds).
kpeter@2577
   260
    ///
kpeter@2577
   261
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   262
    /// \param lower The lower bounds of the edges.
kpeter@2577
   263
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   264
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   265
    /// \param s The source node.
kpeter@2577
   266
    /// \param t The target node.
kpeter@2577
   267
    /// \param flow_value The required amount of flow from node \c s
kpeter@2577
   268
    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
kpeter@2577
   269
    CostScaling( const Graph &graph,
kpeter@2577
   270
                 const LowerMap &lower,
kpeter@2577
   271
                 const CapacityMap &capacity,
kpeter@2577
   272
                 const CostMap &cost,
kpeter@2577
   273
                 Node s, Node t,
kpeter@2577
   274
                 Supply flow_value ) :
kpeter@2577
   275
      _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
kpeter@2577
   276
      _cost(graph), _supply(graph), _flow(graph, 0), _potential(graph, 0),
kpeter@2577
   277
      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
kpeter@2577
   278
      _red_cost(graph, _cost, _potential), _excess(graph, 0)
kpeter@2577
   279
    {
kpeter@2577
   280
      // Removing nonzero lower bounds
kpeter@2577
   281
      _capacity = subMap(capacity, lower);
kpeter@2577
   282
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2577
   283
        Supply sum = 0;
kpeter@2577
   284
        if (n == s) sum =  flow_value;
kpeter@2577
   285
        if (n == t) sum = -flow_value;
kpeter@2577
   286
        for (InEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   287
          sum += lower[e];
kpeter@2577
   288
        for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   289
          sum -= lower[e];
kpeter@2577
   290
        _supply[n] = sum;
kpeter@2577
   291
      }
kpeter@2577
   292
      _valid_supply = true;
kpeter@2577
   293
    }
kpeter@2577
   294
kpeter@2577
   295
    /// \brief Simple constructor of the class (without lower bounds).
kpeter@2577
   296
    ///
kpeter@2577
   297
    /// Simple constructor of the class (without lower bounds).
kpeter@2577
   298
    ///
kpeter@2577
   299
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   300
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   301
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   302
    /// \param s The source node.
kpeter@2577
   303
    /// \param t The target node.
kpeter@2577
   304
    /// \param flow_value The required amount of flow from node \c s
kpeter@2577
   305
    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
kpeter@2577
   306
    CostScaling( const Graph &graph,
kpeter@2577
   307
                 const CapacityMap &capacity,
kpeter@2577
   308
                 const CostMap &cost,
kpeter@2577
   309
                 Node s, Node t,
kpeter@2577
   310
                 Supply flow_value ) :
kpeter@2577
   311
      _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
kpeter@2577
   312
      _cost(graph), _supply(graph, 0), _flow(graph, 0), _potential(graph, 0),
kpeter@2577
   313
      _res_graph(graph, _capacity, _flow), _res_cost(_cost),
kpeter@2577
   314
      _red_cost(graph, _cost, _potential), _excess(graph, 0)
kpeter@2577
   315
    {
kpeter@2577
   316
      _supply[s] =  flow_value;
kpeter@2577
   317
      _supply[t] = -flow_value;
kpeter@2577
   318
      _valid_supply = true;
kpeter@2577
   319
    }
kpeter@2577
   320
kpeter@2577
   321
    /// \brief Runs the algorithm.
kpeter@2577
   322
    ///
kpeter@2577
   323
    /// Runs the algorithm.
kpeter@2577
   324
    ///
kpeter@2577
   325
    /// \return \c true if a feasible flow can be found.
kpeter@2577
   326
    bool run() {
kpeter@2577
   327
      init() && start();
kpeter@2577
   328
    }
kpeter@2577
   329
kpeter@2577
   330
    /// \brief Returns a const reference to the edge map storing the
kpeter@2577
   331
    /// found flow.
kpeter@2577
   332
    ///
kpeter@2577
   333
    /// Returns a const reference to the edge map storing the found flow.
kpeter@2577
   334
    ///
kpeter@2577
   335
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   336
    const FlowMap& flowMap() const {
kpeter@2577
   337
      return _flow;
kpeter@2577
   338
    }
kpeter@2577
   339
kpeter@2577
   340
    /// \brief Returns a const reference to the node map storing the
kpeter@2577
   341
    /// found potentials (the dual solution).
kpeter@2577
   342
    ///
kpeter@2577
   343
    /// Returns a const reference to the node map storing the found
kpeter@2577
   344
    /// potentials (the dual solution).
kpeter@2577
   345
    ///
kpeter@2577
   346
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   347
    const PotentialMap& potentialMap() const {
kpeter@2577
   348
      return _potential;
kpeter@2577
   349
    }
kpeter@2577
   350
kpeter@2577
   351
    /// \brief Returns the total cost of the found flow.
kpeter@2577
   352
    ///
kpeter@2577
   353
    /// Returns the total cost of the found flow. The complexity of the
kpeter@2577
   354
    /// function is \f$ O(e) \f$.
kpeter@2577
   355
    ///
kpeter@2577
   356
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   357
    Cost totalCost() const {
kpeter@2577
   358
      Cost c = 0;
kpeter@2577
   359
      for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2577
   360
        c += _flow[e] * _orig_cost[e];
kpeter@2577
   361
      return c;
kpeter@2577
   362
    }
kpeter@2577
   363
kpeter@2577
   364
  private:
kpeter@2577
   365
kpeter@2577
   366
    /// Initializes the algorithm.
kpeter@2577
   367
    bool init() {
kpeter@2577
   368
      if (!_valid_supply) return false;
kpeter@2577
   369
kpeter@2577
   370
      // Initializing the scaled cost map and the epsilon parameter
kpeter@2577
   371
      Cost max_cost = 0;
kpeter@2577
   372
      int node_num = countNodes(_graph);
kpeter@2577
   373
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2577
   374
        _cost[e] = LCost(_orig_cost[e]) * node_num * ALPHA;
kpeter@2577
   375
        if (_orig_cost[e] > max_cost) max_cost = _orig_cost[e];
kpeter@2577
   376
      }
kpeter@2577
   377
      _epsilon = max_cost * node_num;
kpeter@2577
   378
kpeter@2577
   379
      // Finding a feasible flow using Circulation
kpeter@2577
   380
      Circulation< Graph, ConstMap<Edge, Capacity>, CapacityEdgeMap,
kpeter@2577
   381
                   SupplyMap >
kpeter@2577
   382
        circulation( _graph, constMap<Edge>((Capacity)0), _capacity,
kpeter@2577
   383
                     _supply );
kpeter@2577
   384
      return circulation.flowMap(_flow).run();
kpeter@2577
   385
    }
kpeter@2577
   386
kpeter@2577
   387
kpeter@2577
   388
    /// Executes the algorithm.
kpeter@2577
   389
    bool start() {
kpeter@2577
   390
      std::deque<Node> active_nodes;
kpeter@2577
   391
      typename Graph::template NodeMap<bool> hyper(_graph, false);
kpeter@2577
   392
kpeter@2577
   393
      int node_num = countNodes(_graph);
kpeter@2577
   394
      for ( ; _epsilon >= 1; _epsilon = _epsilon < ALPHA && _epsilon > 1 ?
kpeter@2577
   395
                                        1 : _epsilon / ALPHA )
kpeter@2577
   396
      {
kpeter@2577
   397
        // Performing price refinement heuristic using Bellman-Ford
kpeter@2577
   398
        // algorithm
kpeter@2577
   399
        if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
kpeter@2577
   400
          typedef ShiftMap<ResidualCostMap> ShiftCostMap;
kpeter@2577
   401
          ShiftCostMap shift_cost(_res_cost, _epsilon);
kpeter@2577
   402
          BellmanFord<ResGraph, ShiftCostMap> bf(_res_graph, shift_cost);
kpeter@2577
   403
          bf.init(0);
kpeter@2577
   404
          bool done = false;
kpeter@2577
   405
          int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(node_num));
kpeter@2577
   406
          for (int i = 0; i < K && !done; ++i)
kpeter@2577
   407
            done = bf.processNextWeakRound();
kpeter@2577
   408
          if (done) {
kpeter@2577
   409
            for (NodeIt n(_graph); n != INVALID; ++n)
kpeter@2577
   410
              _potential[n] = bf.dist(n);
kpeter@2577
   411
            continue;
kpeter@2577
   412
          }
kpeter@2577
   413
        }
kpeter@2577
   414
kpeter@2577
   415
        // Saturating edges not satisfying the optimality condition
kpeter@2577
   416
        Capacity delta;
kpeter@2577
   417
        for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2577
   418
          if (_capacity[e] - _flow[e] > 0 && _red_cost[e] < 0) {
kpeter@2577
   419
            delta = _capacity[e] - _flow[e];
kpeter@2577
   420
            _excess[_graph.source(e)] -= delta;
kpeter@2577
   421
            _excess[_graph.target(e)] += delta;
kpeter@2577
   422
            _flow[e] = _capacity[e];
kpeter@2577
   423
          }
kpeter@2577
   424
          if (_flow[e] > 0 && -_red_cost[e] < 0) {
kpeter@2577
   425
            _excess[_graph.target(e)] -= _flow[e];
kpeter@2577
   426
            _excess[_graph.source(e)] += _flow[e];
kpeter@2577
   427
            _flow[e] = 0;
kpeter@2577
   428
          }
kpeter@2577
   429
        }
kpeter@2577
   430
kpeter@2577
   431
        // Finding active nodes (i.e. nodes with positive excess)
kpeter@2577
   432
        for (NodeIt n(_graph); n != INVALID; ++n)
kpeter@2577
   433
          if (_excess[n] > 0) active_nodes.push_back(n);
kpeter@2577
   434
kpeter@2577
   435
        // Performing push and relabel operations
kpeter@2577
   436
        while (active_nodes.size() > 0) {
kpeter@2577
   437
          Node n = active_nodes[0], t;
kpeter@2577
   438
          bool relabel_enabled = true;
kpeter@2577
   439
kpeter@2577
   440
          // Performing push operations if there are admissible edges
kpeter@2577
   441
          if (_excess[n] > 0) {
kpeter@2577
   442
            for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
kpeter@2577
   443
              if (_capacity[e] - _flow[e] > 0 && _red_cost[e] < 0) {
kpeter@2577
   444
                delta = _capacity[e] - _flow[e] <= _excess[n] ?
kpeter@2577
   445
                        _capacity[e] - _flow[e] : _excess[n];
kpeter@2577
   446
                t = _graph.target(e);
kpeter@2577
   447
kpeter@2577
   448
                // Push-look-ahead heuristic
kpeter@2577
   449
                Capacity ahead = -_excess[t];
kpeter@2577
   450
                for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
kpeter@2577
   451
                  if (_capacity[oe] - _flow[oe] > 0 && _red_cost[oe] < 0)
kpeter@2577
   452
                    ahead += _capacity[oe] - _flow[oe];
kpeter@2577
   453
                }
kpeter@2577
   454
                for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
kpeter@2577
   455
                  if (_flow[ie] > 0 && -_red_cost[ie] < 0)
kpeter@2577
   456
                    ahead += _flow[ie];
kpeter@2577
   457
                }
kpeter@2577
   458
                if (ahead < 0) ahead = 0;
kpeter@2577
   459
kpeter@2577
   460
                // Pushing flow along the edge
kpeter@2577
   461
                if (ahead < delta) {
kpeter@2577
   462
                  _flow[e] += ahead;
kpeter@2577
   463
                  _excess[n] -= ahead;
kpeter@2577
   464
                  _excess[t] += ahead;
kpeter@2577
   465
                  active_nodes.push_front(t);
kpeter@2577
   466
                  hyper[t] = true;
kpeter@2577
   467
                  relabel_enabled = false;
kpeter@2577
   468
                  break;
kpeter@2577
   469
                } else {
kpeter@2577
   470
                  _flow[e] += delta;
kpeter@2577
   471
                  _excess[n] -= delta;
kpeter@2577
   472
                  _excess[t] += delta;
kpeter@2577
   473
                  if (_excess[t] > 0 && _excess[t] <= delta)
kpeter@2577
   474
                    active_nodes.push_back(t);
kpeter@2577
   475
                }
kpeter@2577
   476
kpeter@2577
   477
                if (_excess[n] == 0) break;
kpeter@2577
   478
              }
kpeter@2577
   479
            }
kpeter@2577
   480
          }
kpeter@2577
   481
kpeter@2577
   482
          if (_excess[n] > 0) {
kpeter@2577
   483
            for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
kpeter@2577
   484
              if (_flow[e] > 0 && -_red_cost[e] < 0) {
kpeter@2577
   485
                delta = _flow[e] <= _excess[n] ? _flow[e] : _excess[n];
kpeter@2577
   486
                t = _graph.source(e);
kpeter@2577
   487
kpeter@2577
   488
                // Push-look-ahead heuristic
kpeter@2577
   489
                Capacity ahead = -_excess[t];
kpeter@2577
   490
                for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
kpeter@2577
   491
                  if (_capacity[oe] - _flow[oe] > 0 && _red_cost[oe] < 0)
kpeter@2577
   492
                    ahead += _capacity[oe] - _flow[oe];
kpeter@2577
   493
                }
kpeter@2577
   494
                for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
kpeter@2577
   495
                  if (_flow[ie] > 0 && -_red_cost[ie] < 0)
kpeter@2577
   496
                    ahead += _flow[ie];
kpeter@2577
   497
                }
kpeter@2577
   498
                if (ahead < 0) ahead = 0;
kpeter@2577
   499
kpeter@2577
   500
                // Pushing flow along the edge
kpeter@2577
   501
                if (ahead < delta) {
kpeter@2577
   502
                  _flow[e] -= ahead;
kpeter@2577
   503
                  _excess[n] -= ahead;
kpeter@2577
   504
                  _excess[t] += ahead;
kpeter@2577
   505
                  active_nodes.push_front(t);
kpeter@2577
   506
                  hyper[t] = true;
kpeter@2577
   507
                  relabel_enabled = false;
kpeter@2577
   508
                  break;
kpeter@2577
   509
                } else {
kpeter@2577
   510
                  _flow[e] -= delta;
kpeter@2577
   511
                  _excess[n] -= delta;
kpeter@2577
   512
                  _excess[t] += delta;
kpeter@2577
   513
                  if (_excess[t] > 0 && _excess[t] <= delta)
kpeter@2577
   514
                    active_nodes.push_back(t);
kpeter@2577
   515
                }
kpeter@2577
   516
kpeter@2577
   517
                if (_excess[n] == 0) break;
kpeter@2577
   518
              }
kpeter@2577
   519
            }
kpeter@2577
   520
          }
kpeter@2577
   521
kpeter@2577
   522
          if (relabel_enabled && (_excess[n] > 0 || hyper[n])) {
kpeter@2577
   523
            // Performing relabel operation if the node is still active
kpeter@2577
   524
            LCost min_red_cost = std::numeric_limits<LCost>::max();
kpeter@2577
   525
            for (OutEdgeIt oe(_graph, n); oe != INVALID; ++oe) {
kpeter@2577
   526
              if ( _capacity[oe] - _flow[oe] > 0 &&
kpeter@2577
   527
                   _red_cost[oe] < min_red_cost )
kpeter@2577
   528
                min_red_cost = _red_cost[oe];
kpeter@2577
   529
            }
kpeter@2577
   530
            for (InEdgeIt ie(_graph, n); ie != INVALID; ++ie) {
kpeter@2577
   531
              if (_flow[ie] > 0 && -_red_cost[ie] < min_red_cost)
kpeter@2577
   532
                min_red_cost = -_red_cost[ie];
kpeter@2577
   533
            }
kpeter@2577
   534
            _potential[n] -= min_red_cost + _epsilon;
kpeter@2577
   535
            hyper[n] = false;
kpeter@2577
   536
          }
kpeter@2577
   537
kpeter@2577
   538
          // Removing active nodes with non-positive excess
kpeter@2577
   539
          while ( active_nodes.size() > 0 &&
kpeter@2577
   540
                  _excess[active_nodes[0]] <= 0 &&
kpeter@2577
   541
                  !hyper[active_nodes[0]] ) {
kpeter@2577
   542
            active_nodes.pop_front();
kpeter@2577
   543
          }
kpeter@2577
   544
        }
kpeter@2577
   545
      }
kpeter@2577
   546
kpeter@2577
   547
      // Handling non-zero lower bounds
kpeter@2577
   548
      if (_lower) {
kpeter@2577
   549
        for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2577
   550
          _flow[e] += (*_lower)[e];
kpeter@2577
   551
      }
kpeter@2577
   552
      return true;
kpeter@2577
   553
    }
kpeter@2577
   554
kpeter@2577
   555
  }; //class CostScaling
kpeter@2577
   556
kpeter@2577
   557
  ///@}
kpeter@2577
   558
kpeter@2577
   559
} //namespace lemon
kpeter@2577
   560
kpeter@2577
   561
#endif //LEMON_COST_SCALING_H