lemon/capacity_scaling.h
author deba
Sun, 30 Dec 2007 18:23:32 +0000
changeset 2550 f26368148b9c
parent 2533 aea952a1af99
child 2553 bfced05fa852
permissions -rw-r--r--
Changing degree of tournament tree
Bug fix in union find
Small efficiency improvment in bipartite matchings
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_CAPACITY_SCALING_H
deba@2440
    20
#define LEMON_CAPACITY_SCALING_H
deba@2440
    21
deba@2440
    22
/// \ingroup min_cost_flow
deba@2440
    23
///
deba@2440
    24
/// \file
deba@2440
    25
/// \brief The capacity scaling algorithm for finding a minimum cost
deba@2440
    26
/// flow.
deba@2440
    27
kpeter@2535
    28
#include <lemon/graph_adaptor.h>
kpeter@2535
    29
#include <lemon/bin_heap.h>
deba@2440
    30
#include <vector>
deba@2457
    31
deba@2440
    32
namespace lemon {
deba@2440
    33
deba@2440
    34
  /// \addtogroup min_cost_flow
deba@2440
    35
  /// @{
deba@2440
    36
deba@2440
    37
  /// \brief Implementation of the capacity scaling version of the
kpeter@2509
    38
  /// successive shortest path algorithm for finding a minimum cost
kpeter@2509
    39
  /// flow.
deba@2440
    40
  ///
kpeter@2535
    41
  /// \ref CapacityScaling implements the capacity scaling version
kpeter@2535
    42
  /// of the successive shortest path algorithm for finding a minimum
kpeter@2535
    43
  /// cost flow.
deba@2440
    44
  ///
deba@2440
    45
  /// \param Graph The directed graph type the algorithm runs on.
deba@2440
    46
  /// \param LowerMap The type of the lower bound map.
deba@2440
    47
  /// \param CapacityMap The type of the capacity (upper bound) map.
deba@2440
    48
  /// \param CostMap The type of the cost (length) map.
deba@2440
    49
  /// \param SupplyMap The type of the supply map.
deba@2440
    50
  ///
deba@2440
    51
  /// \warning
deba@2440
    52
  /// - Edge capacities and costs should be nonnegative integers.
kpeter@2535
    53
  ///   However \c CostMap::Value should be signed type.
kpeter@2509
    54
  /// - Supply values should be signed integers.
deba@2440
    55
  /// - \c LowerMap::Value must be convertible to
kpeter@2535
    56
  ///   \c CapacityMap::Value and \c CapacityMap::Value must be
kpeter@2535
    57
  ///   convertible to \c SupplyMap::Value.
deba@2440
    58
  ///
deba@2440
    59
  /// \author Peter Kovacs
deba@2440
    60
kpeter@2533
    61
  template < typename Graph,
kpeter@2535
    62
             typename LowerMap = typename Graph::template EdgeMap<int>,
kpeter@2535
    63
             typename CapacityMap = LowerMap,
kpeter@2535
    64
             typename CostMap = typename Graph::template EdgeMap<int>,
kpeter@2535
    65
             typename SupplyMap = typename Graph::template NodeMap
kpeter@2535
    66
                                  <typename CapacityMap::Value> >
deba@2440
    67
  class CapacityScaling
deba@2440
    68
  {
deba@2440
    69
    typedef typename Graph::Node Node;
deba@2440
    70
    typedef typename Graph::NodeIt NodeIt;
deba@2440
    71
    typedef typename Graph::Edge Edge;
deba@2440
    72
    typedef typename Graph::EdgeIt EdgeIt;
deba@2440
    73
    typedef typename Graph::InEdgeIt InEdgeIt;
deba@2440
    74
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2440
    75
deba@2440
    76
    typedef typename LowerMap::Value Lower;
deba@2440
    77
    typedef typename CapacityMap::Value Capacity;
deba@2440
    78
    typedef typename CostMap::Value Cost;
deba@2440
    79
    typedef typename SupplyMap::Value Supply;
deba@2440
    80
    typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
deba@2440
    81
    typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
kpeter@2535
    82
    typedef typename Graph::template NodeMap<Edge> PredMap;
deba@2440
    83
deba@2440
    84
  public:
deba@2440
    85
kpeter@2535
    86
    /// \brief Type to enable or disable capacity scaling.
kpeter@2535
    87
    enum ScalingEnum {
kpeter@2535
    88
      WITH_SCALING = 0,
kpeter@2535
    89
      WITHOUT_SCALING = -1
kpeter@2535
    90
    };
kpeter@2535
    91
deba@2440
    92
    /// \brief The type of the flow map.
deba@2440
    93
    typedef CapacityRefMap FlowMap;
deba@2440
    94
    /// \brief The type of the potential map.
deba@2440
    95
    typedef typename Graph::template NodeMap<Cost> PotentialMap;
deba@2440
    96
deba@2440
    97
  protected:
deba@2440
    98
kpeter@2535
    99
    /// \brief Special implementation of the \ref Dijkstra algorithm
kpeter@2535
   100
    /// for finding shortest paths in the residual network of the graph
kpeter@2535
   101
    /// with respect to the reduced edge costs and modifying the
kpeter@2535
   102
    /// node potentials according to the distance of the nodes.
kpeter@2535
   103
    class ResidualDijkstra
deba@2440
   104
    {
kpeter@2535
   105
      typedef typename Graph::template NodeMap<Cost> CostNodeMap;
kpeter@2535
   106
      typedef typename Graph::template NodeMap<Edge> PredMap;
deba@2440
   107
kpeter@2535
   108
      typedef typename Graph::template NodeMap<int> HeapCrossRef;
kpeter@2535
   109
      typedef BinHeap<Cost, HeapCrossRef> Heap;
kpeter@2535
   110
kpeter@2535
   111
    protected:
kpeter@2535
   112
kpeter@2535
   113
      /// \brief The directed graph the algorithm runs on.
kpeter@2535
   114
      const Graph &graph;
kpeter@2535
   115
kpeter@2535
   116
      /// \brief The flow map.
kpeter@2535
   117
      const FlowMap &flow;
kpeter@2535
   118
      /// \brief The residual capacity map.
kpeter@2535
   119
      const CapacityRefMap &res_cap;
kpeter@2535
   120
      /// \brief The cost map.
kpeter@2535
   121
      const CostMap &cost;
kpeter@2535
   122
      /// \brief The excess map.
kpeter@2535
   123
      const SupplyRefMap &excess;
kpeter@2535
   124
kpeter@2535
   125
      /// \brief The potential map.
kpeter@2535
   126
      PotentialMap &potential;
kpeter@2535
   127
kpeter@2535
   128
      /// \brief The distance map.
kpeter@2535
   129
      CostNodeMap dist;
kpeter@2535
   130
      /// \brief The map of predecessors edges.
kpeter@2535
   131
      PredMap &pred;
kpeter@2535
   132
      /// \brief The processed (i.e. permanently labeled) nodes.
kpeter@2535
   133
      std::vector<Node> proc_nodes;
deba@2440
   134
deba@2440
   135
    public:
deba@2440
   136
kpeter@2535
   137
      /// \brief The constructor of the class.
kpeter@2535
   138
      ResidualDijkstra( const Graph &_graph,
kpeter@2535
   139
                        const FlowMap &_flow,
kpeter@2535
   140
                        const CapacityRefMap &_res_cap,
kpeter@2535
   141
                        const CostMap &_cost,
kpeter@2535
   142
                        const SupplyMap &_excess,
kpeter@2535
   143
                        PotentialMap &_potential,
kpeter@2535
   144
                        PredMap &_pred ) :
kpeter@2535
   145
        graph(_graph), flow(_flow), res_cap(_res_cap), cost(_cost),
kpeter@2535
   146
        excess(_excess), potential(_potential), dist(_graph),
kpeter@2535
   147
        pred(_pred)
kpeter@2535
   148
      {}
deba@2440
   149
kpeter@2535
   150
      /// \brief Runs the algorithm from the given source node.
kpeter@2535
   151
      Node run(Node s, Capacity delta) {
kpeter@2535
   152
        HeapCrossRef heap_cross_ref(graph, Heap::PRE_HEAP);
kpeter@2535
   153
        Heap heap(heap_cross_ref);
kpeter@2535
   154
        heap.push(s, 0);
kpeter@2535
   155
        pred[s] = INVALID;
kpeter@2535
   156
        proc_nodes.clear();
kpeter@2535
   157
kpeter@2535
   158
        // Processing nodes
kpeter@2535
   159
        while (!heap.empty() && excess[heap.top()] > -delta) {
kpeter@2535
   160
          Node u = heap.top(), v;
kpeter@2535
   161
          Cost d = heap.prio() - potential[u], nd;
kpeter@2535
   162
          dist[u] = heap.prio();
kpeter@2535
   163
          heap.pop();
kpeter@2535
   164
          proc_nodes.push_back(u);
kpeter@2535
   165
kpeter@2535
   166
          // Traversing outgoing edges
kpeter@2535
   167
          for (OutEdgeIt e(graph, u); e != INVALID; ++e) {
kpeter@2535
   168
            if (res_cap[e] >= delta) {
kpeter@2535
   169
              v = graph.target(e);
kpeter@2535
   170
              switch(heap.state(v)) {
kpeter@2535
   171
              case Heap::PRE_HEAP:
kpeter@2535
   172
                heap.push(v, d + cost[e] + potential[v]);
kpeter@2535
   173
                pred[v] = e;
kpeter@2535
   174
                break;
kpeter@2535
   175
              case Heap::IN_HEAP:
kpeter@2535
   176
                nd = d + cost[e] + potential[v];
kpeter@2535
   177
                if (nd < heap[v]) {
kpeter@2535
   178
                  heap.decrease(v, nd);
kpeter@2535
   179
                  pred[v] = e;
kpeter@2535
   180
                }
kpeter@2535
   181
                break;
kpeter@2535
   182
              case Heap::POST_HEAP:
kpeter@2535
   183
                break;
kpeter@2535
   184
              }
kpeter@2535
   185
            }
kpeter@2535
   186
          }
kpeter@2535
   187
kpeter@2535
   188
          // Traversing incoming edges
kpeter@2535
   189
          for (InEdgeIt e(graph, u); e != INVALID; ++e) {
kpeter@2535
   190
            if (flow[e] >= delta) {
kpeter@2535
   191
              v = graph.source(e);
kpeter@2535
   192
              switch(heap.state(v)) {
kpeter@2535
   193
              case Heap::PRE_HEAP:
kpeter@2535
   194
                heap.push(v, d - cost[e] + potential[v]);
kpeter@2535
   195
                pred[v] = e;
kpeter@2535
   196
                break;
kpeter@2535
   197
              case Heap::IN_HEAP:
kpeter@2535
   198
                nd = d - cost[e] + potential[v];
kpeter@2535
   199
                if (nd < heap[v]) {
kpeter@2535
   200
                  heap.decrease(v, nd);
kpeter@2535
   201
                  pred[v] = e;
kpeter@2535
   202
                }
kpeter@2535
   203
                break;
kpeter@2535
   204
              case Heap::POST_HEAP:
kpeter@2535
   205
                break;
kpeter@2535
   206
              }
kpeter@2535
   207
            }
kpeter@2535
   208
          }
kpeter@2535
   209
        }
kpeter@2535
   210
        if (heap.empty()) return INVALID;
kpeter@2535
   211
kpeter@2535
   212
        // Updating potentials of processed nodes
kpeter@2535
   213
        Node t = heap.top();
kpeter@2535
   214
        Cost dt = heap.prio();
kpeter@2535
   215
        for (int i = 0; i < proc_nodes.size(); ++i)
kpeter@2535
   216
          potential[proc_nodes[i]] -= dist[proc_nodes[i]] - dt;
kpeter@2535
   217
kpeter@2535
   218
        return t;
deba@2440
   219
      }
deba@2440
   220
kpeter@2535
   221
    }; //class ResidualDijkstra
deba@2440
   222
deba@2440
   223
  protected:
deba@2440
   224
deba@2440
   225
    /// \brief The directed graph the algorithm runs on.
deba@2440
   226
    const Graph &graph;
deba@2440
   227
    /// \brief The original lower bound map.
deba@2440
   228
    const LowerMap *lower;
deba@2440
   229
    /// \brief The modified capacity map.
deba@2440
   230
    CapacityRefMap capacity;
deba@2440
   231
    /// \brief The cost map.
deba@2440
   232
    const CostMap &cost;
deba@2440
   233
    /// \brief The modified supply map.
deba@2440
   234
    SupplyRefMap supply;
deba@2440
   235
    /// \brief The sum of supply values equals zero.
deba@2440
   236
    bool valid_supply;
deba@2440
   237
deba@2440
   238
    /// \brief The edge map of the current flow.
deba@2440
   239
    FlowMap flow;
deba@2440
   240
    /// \brief The potential node map.
deba@2440
   241
    PotentialMap potential;
deba@2440
   242
kpeter@2535
   243
    /// \brief The residual capacity map.
kpeter@2535
   244
    CapacityRefMap res_cap;
kpeter@2535
   245
    /// \brief The excess map.
kpeter@2535
   246
    SupplyRefMap excess;
kpeter@2535
   247
    /// \brief The excess nodes (i.e. nodes with positive excess).
kpeter@2535
   248
    std::vector<Node> excess_nodes;
deba@2440
   249
    /// \brief The index of the next excess node.
deba@2440
   250
    int next_node;
deba@2440
   251
kpeter@2535
   252
    /// \brief The scaling status (enabled or disabled).
kpeter@2535
   253
    ScalingEnum scaling;
deba@2440
   254
    /// \brief The delta parameter used for capacity scaling.
deba@2440
   255
    Capacity delta;
kpeter@2535
   256
    /// \brief The maximum number of phases.
kpeter@2535
   257
    Capacity phase_num;
kpeter@2535
   258
    /// \brief The deficit nodes.
kpeter@2535
   259
    std::vector<Node> deficit_nodes;
deba@2440
   260
kpeter@2535
   261
    /// \brief Implementation of the \ref Dijkstra algorithm for
kpeter@2535
   262
    /// finding augmenting shortest paths in the residual network.
kpeter@2535
   263
    ResidualDijkstra dijkstra;
kpeter@2535
   264
    /// \brief The map of predecessors edges.
kpeter@2535
   265
    PredMap pred;
deba@2440
   266
deba@2440
   267
  public :
deba@2440
   268
deba@2440
   269
    /// \brief General constructor of the class (with lower bounds).
deba@2440
   270
    ///
deba@2440
   271
    /// General constructor of the class (with lower bounds).
deba@2440
   272
    ///
deba@2440
   273
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   274
    /// \param _lower The lower bounds of the edges.
deba@2440
   275
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   276
    /// \param _cost The cost (length) values of the edges.
deba@2440
   277
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   278
    CapacityScaling( const Graph &_graph,
kpeter@2535
   279
                     const LowerMap &_lower,
kpeter@2535
   280
                     const CapacityMap &_capacity,
kpeter@2535
   281
                     const CostMap &_cost,
kpeter@2535
   282
                     const SupplyMap &_supply ) :
deba@2440
   283
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   284
      supply(_graph), flow(_graph, 0), potential(_graph, 0),
kpeter@2535
   285
      res_cap(_graph), excess(_graph), pred(_graph),
kpeter@2535
   286
      dijkstra(graph, flow, res_cap, cost, excess, potential, pred)
deba@2440
   287
    {
deba@2440
   288
      // Removing nonzero lower bounds
deba@2440
   289
      capacity = subMap(_capacity, _lower);
kpeter@2535
   290
      res_cap = capacity;
deba@2440
   291
      Supply sum = 0;
deba@2440
   292
      for (NodeIt n(graph); n != INVALID; ++n) {
kpeter@2535
   293
        Supply s = _supply[n];
kpeter@2535
   294
        for (InEdgeIt e(graph, n); e != INVALID; ++e)
kpeter@2535
   295
          s += _lower[e];
kpeter@2535
   296
        for (OutEdgeIt e(graph, n); e != INVALID; ++e)
kpeter@2535
   297
          s -= _lower[e];
kpeter@2535
   298
        supply[n] = s;
kpeter@2535
   299
        sum += s;
deba@2440
   300
      }
deba@2440
   301
      valid_supply = sum == 0;
deba@2440
   302
    }
deba@2440
   303
deba@2440
   304
    /// \brief General constructor of the class (without lower bounds).
deba@2440
   305
    ///
deba@2440
   306
    /// General constructor of the class (without lower bounds).
deba@2440
   307
    ///
deba@2440
   308
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   309
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   310
    /// \param _cost The cost (length) values of the edges.
deba@2440
   311
    /// \param _supply The supply values of the nodes (signed).
deba@2440
   312
    CapacityScaling( const Graph &_graph,
kpeter@2535
   313
                     const CapacityMap &_capacity,
kpeter@2535
   314
                     const CostMap &_cost,
kpeter@2535
   315
                     const SupplyMap &_supply ) :
deba@2440
   316
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   317
      supply(_supply), flow(_graph, 0), potential(_graph, 0),
kpeter@2535
   318
      res_cap(_capacity), excess(_graph), pred(_graph),
kpeter@2535
   319
      dijkstra(graph, flow, res_cap, cost, excess, potential)
deba@2440
   320
    {
deba@2440
   321
      // Checking the sum of supply values
deba@2440
   322
      Supply sum = 0;
deba@2440
   323
      for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
deba@2440
   324
      valid_supply = sum == 0;
deba@2440
   325
    }
deba@2440
   326
deba@2440
   327
    /// \brief Simple constructor of the class (with lower bounds).
deba@2440
   328
    ///
deba@2440
   329
    /// Simple constructor of the class (with lower bounds).
deba@2440
   330
    ///
deba@2440
   331
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   332
    /// \param _lower The lower bounds of the edges.
deba@2440
   333
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   334
    /// \param _cost The cost (length) values of the edges.
deba@2440
   335
    /// \param _s The source node.
deba@2440
   336
    /// \param _t The target node.
deba@2440
   337
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   338
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   339
    /// \c _t).
deba@2440
   340
    CapacityScaling( const Graph &_graph,
kpeter@2535
   341
                     const LowerMap &_lower,
kpeter@2535
   342
                     const CapacityMap &_capacity,
kpeter@2535
   343
                     const CostMap &_cost,
kpeter@2535
   344
                     Node _s, Node _t,
kpeter@2535
   345
                     Supply _flow_value ) :
deba@2440
   346
      graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
deba@2440
   347
      supply(_graph), flow(_graph, 0), potential(_graph, 0),
kpeter@2535
   348
      res_cap(_graph), excess(_graph), pred(_graph),
kpeter@2535
   349
      dijkstra(graph, flow, res_cap, cost, excess, potential)
deba@2440
   350
    {
deba@2440
   351
      // Removing nonzero lower bounds
deba@2440
   352
      capacity = subMap(_capacity, _lower);
kpeter@2535
   353
      res_cap = capacity;
deba@2440
   354
      for (NodeIt n(graph); n != INVALID; ++n) {
kpeter@2535
   355
        Supply s = 0;
kpeter@2535
   356
        if (n == _s) s =  _flow_value;
kpeter@2535
   357
        if (n == _t) s = -_flow_value;
kpeter@2535
   358
        for (InEdgeIt e(graph, n); e != INVALID; ++e)
kpeter@2535
   359
          s += _lower[e];
kpeter@2535
   360
        for (OutEdgeIt e(graph, n); e != INVALID; ++e)
kpeter@2535
   361
          s -= _lower[e];
kpeter@2535
   362
        supply[n] = s;
deba@2440
   363
      }
deba@2440
   364
      valid_supply = true;
deba@2440
   365
    }
deba@2440
   366
deba@2440
   367
    /// \brief Simple constructor of the class (without lower bounds).
deba@2440
   368
    ///
deba@2440
   369
    /// Simple constructor of the class (without lower bounds).
deba@2440
   370
    ///
deba@2440
   371
    /// \param _graph The directed graph the algorithm runs on.
deba@2440
   372
    /// \param _capacity The capacities (upper bounds) of the edges.
deba@2440
   373
    /// \param _cost The cost (length) values of the edges.
deba@2440
   374
    /// \param _s The source node.
deba@2440
   375
    /// \param _t The target node.
deba@2440
   376
    /// \param _flow_value The required amount of flow from node \c _s
deba@2440
   377
    /// to node \c _t (i.e. the supply of \c _s and the demand of
deba@2440
   378
    /// \c _t).
deba@2440
   379
    CapacityScaling( const Graph &_graph,
kpeter@2535
   380
                     const CapacityMap &_capacity,
kpeter@2535
   381
                     const CostMap &_cost,
kpeter@2535
   382
                     Node _s, Node _t,
kpeter@2535
   383
                     Supply _flow_value ) :
deba@2440
   384
      graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
deba@2440
   385
      supply(_graph, 0), flow(_graph, 0), potential(_graph, 0),
kpeter@2535
   386
      res_cap(_capacity), excess(_graph), pred(_graph),
kpeter@2535
   387
      dijkstra(graph, flow, res_cap, cost, excess, potential)
deba@2440
   388
    {
deba@2440
   389
      supply[_s] =  _flow_value;
deba@2440
   390
      supply[_t] = -_flow_value;
deba@2440
   391
      valid_supply = true;
deba@2440
   392
    }
deba@2440
   393
deba@2440
   394
    /// \brief Returns a const reference to the flow map.
deba@2440
   395
    ///
deba@2440
   396
    /// Returns a const reference to the flow map.
deba@2440
   397
    ///
deba@2440
   398
    /// \pre \ref run() must be called before using this function.
deba@2440
   399
    const FlowMap& flowMap() const {
deba@2440
   400
      return flow;
deba@2440
   401
    }
deba@2440
   402
deba@2440
   403
    /// \brief Returns a const reference to the potential map (the dual
deba@2440
   404
    /// solution).
deba@2440
   405
    ///
deba@2440
   406
    /// Returns a const reference to the potential map (the dual
deba@2440
   407
    /// solution).
deba@2440
   408
    ///
deba@2440
   409
    /// \pre \ref run() must be called before using this function.
deba@2440
   410
    const PotentialMap& potentialMap() const {
deba@2440
   411
      return potential;
deba@2440
   412
    }
deba@2440
   413
deba@2440
   414
    /// \brief Returns the total cost of the found flow.
deba@2440
   415
    ///
deba@2440
   416
    /// Returns the total cost of the found flow. The complexity of the
deba@2440
   417
    /// function is \f$ O(e) \f$.
deba@2440
   418
    ///
deba@2440
   419
    /// \pre \ref run() must be called before using this function.
deba@2440
   420
    Cost totalCost() const {
deba@2440
   421
      Cost c = 0;
deba@2440
   422
      for (EdgeIt e(graph); e != INVALID; ++e)
kpeter@2535
   423
        c += flow[e] * cost[e];
deba@2440
   424
      return c;
deba@2440
   425
    }
deba@2440
   426
deba@2457
   427
    /// \brief Runs the algorithm.
deba@2440
   428
    ///
deba@2457
   429
    /// Runs the algorithm.
deba@2440
   430
    ///
kpeter@2535
   431
    /// \param scaling_mode The scaling mode. In case of WITH_SCALING
kpeter@2535
   432
    /// capacity scaling is enabled in the algorithm (this is the
kpeter@2535
   433
    /// default value) otherwise it is disabled.
kpeter@2535
   434
    /// If the maximum edge capacity and/or the amount of total supply
kpeter@2535
   435
    /// is small, the algorithm could be faster without scaling.
kpeter@2535
   436
    ///
deba@2440
   437
    /// \return \c true if a feasible flow can be found.
kpeter@2535
   438
    bool run(int scaling_mode = WITH_SCALING) {
kpeter@2535
   439
      return init(scaling_mode) && start();
deba@2440
   440
    }
deba@2440
   441
deba@2440
   442
  protected:
deba@2440
   443
deba@2440
   444
    /// \brief Initializes the algorithm.
kpeter@2535
   445
    bool init(int scaling_mode) {
deba@2440
   446
      if (!valid_supply) return false;
kpeter@2535
   447
      excess = supply;
deba@2440
   448
deba@2440
   449
      // Initilaizing delta value
kpeter@2535
   450
      if (scaling_mode == WITH_SCALING) {
kpeter@2535
   451
        // With scaling
kpeter@2535
   452
        Supply max_sup = 0, max_dem = 0;
kpeter@2535
   453
        for (NodeIt n(graph); n != INVALID; ++n) {
kpeter@2535
   454
          if ( supply[n] > max_sup) max_sup =  supply[n];
kpeter@2535
   455
          if (-supply[n] > max_dem) max_dem = -supply[n];
kpeter@2535
   456
        }
kpeter@2535
   457
        if (max_dem < max_sup) max_sup = max_dem;
kpeter@2535
   458
        phase_num = 0;
kpeter@2535
   459
        for (delta = 1; 2 * delta <= max_sup; delta *= 2)
kpeter@2535
   460
          ++phase_num;
kpeter@2535
   461
      } else {
kpeter@2535
   462
        // Without scaling
kpeter@2535
   463
        delta = 1;
deba@2440
   464
      }
deba@2440
   465
      return true;
deba@2440
   466
    }
deba@2440
   467
kpeter@2535
   468
    /// \brief Executes the algorithm.
kpeter@2535
   469
    bool start() {
kpeter@2535
   470
      if (delta > 1)
kpeter@2535
   471
        return startWithScaling();
kpeter@2535
   472
      else
kpeter@2535
   473
        return startWithoutScaling();
kpeter@2535
   474
    }
kpeter@2535
   475
kpeter@2535
   476
    /// \brief Executes the capacity scaling version of the successive
kpeter@2535
   477
    /// shortest path algorithm.
kpeter@2535
   478
    bool startWithScaling() {
kpeter@2535
   479
      // Processing capacity scaling phases
kpeter@2535
   480
      Node s, t;
kpeter@2535
   481
      int phase_cnt = 0;
kpeter@2535
   482
      int factor = 4;
kpeter@2535
   483
      while (true) {
kpeter@2535
   484
        // Saturating all edges not satisfying the optimality condition
kpeter@2535
   485
        for (EdgeIt e(graph); e != INVALID; ++e) {
kpeter@2535
   486
          Node u = graph.source(e), v = graph.target(e);
kpeter@2535
   487
          Cost c = cost[e] - potential[u] + potential[v];
kpeter@2535
   488
          if (c < 0 && res_cap[e] >= delta) {
kpeter@2535
   489
            excess[u] -= res_cap[e];
kpeter@2535
   490
            excess[v] += res_cap[e];
kpeter@2535
   491
            flow[e] = capacity[e];
kpeter@2535
   492
            res_cap[e] = 0;
kpeter@2535
   493
          }
kpeter@2535
   494
          else if (c > 0 && flow[e] >= delta) {
kpeter@2535
   495
            excess[u] += flow[e];
kpeter@2535
   496
            excess[v] -= flow[e];
kpeter@2535
   497
            flow[e] = 0;
kpeter@2535
   498
            res_cap[e] = capacity[e];
kpeter@2535
   499
          }
kpeter@2535
   500
        }
kpeter@2535
   501
kpeter@2535
   502
        // Finding excess nodes and deficit nodes
kpeter@2535
   503
        excess_nodes.clear();
kpeter@2535
   504
        deficit_nodes.clear();
kpeter@2535
   505
        for (NodeIt n(graph); n != INVALID; ++n) {
kpeter@2535
   506
          if (excess[n] >=  delta) excess_nodes.push_back(n);
kpeter@2535
   507
          if (excess[n] <= -delta) deficit_nodes.push_back(n);
kpeter@2535
   508
        }
kpeter@2535
   509
        next_node = 0;
kpeter@2535
   510
kpeter@2535
   511
        // Finding augmenting shortest paths
kpeter@2535
   512
        while (next_node < excess_nodes.size()) {
kpeter@2535
   513
          // Checking deficit nodes
kpeter@2535
   514
          if (delta > 1) {
kpeter@2535
   515
            bool delta_deficit = false;
kpeter@2535
   516
            for (int i = 0; i < deficit_nodes.size(); ++i) {
kpeter@2535
   517
              if (excess[deficit_nodes[i]] <= -delta) {
kpeter@2535
   518
                delta_deficit = true;
kpeter@2535
   519
                break;
kpeter@2535
   520
              }
kpeter@2535
   521
            }
kpeter@2535
   522
            if (!delta_deficit) break;
kpeter@2535
   523
          }
kpeter@2535
   524
kpeter@2535
   525
          // Running Dijkstra
kpeter@2535
   526
          s = excess_nodes[next_node];
kpeter@2535
   527
          if ((t = dijkstra.run(s, delta)) == INVALID) {
kpeter@2535
   528
            if (delta > 1) {
kpeter@2535
   529
              ++next_node;
kpeter@2535
   530
              continue;
kpeter@2535
   531
            }
kpeter@2535
   532
            return false;
kpeter@2535
   533
          }
kpeter@2535
   534
kpeter@2535
   535
          // Augmenting along a shortest path from s to t.
kpeter@2535
   536
          Capacity d = excess[s] < -excess[t] ? excess[s] : -excess[t];
kpeter@2535
   537
          Node u = t;
kpeter@2535
   538
          Edge e;
kpeter@2535
   539
          if (d > delta) {
kpeter@2535
   540
            while ((e = pred[u]) != INVALID) {
kpeter@2535
   541
              Capacity rc;
kpeter@2535
   542
              if (u == graph.target(e)) {
kpeter@2535
   543
                rc = res_cap[e];
kpeter@2535
   544
                u = graph.source(e);
kpeter@2535
   545
              } else {
kpeter@2535
   546
                rc = flow[e];
kpeter@2535
   547
                u = graph.target(e);
kpeter@2535
   548
              }
kpeter@2535
   549
              if (rc < d) d = rc;
kpeter@2535
   550
            }
kpeter@2535
   551
          }
kpeter@2535
   552
          u = t;
kpeter@2535
   553
          while ((e = pred[u]) != INVALID) {
kpeter@2535
   554
            if (u == graph.target(e)) {
kpeter@2535
   555
              flow[e] += d;
kpeter@2535
   556
              res_cap[e] -= d;
kpeter@2535
   557
              u = graph.source(e);
kpeter@2535
   558
            } else {
kpeter@2535
   559
              flow[e] -= d;
kpeter@2535
   560
              res_cap[e] += d;
kpeter@2535
   561
              u = graph.target(e);
kpeter@2535
   562
            }
kpeter@2535
   563
          }
kpeter@2535
   564
          excess[s] -= d;
kpeter@2535
   565
          excess[t] += d;
kpeter@2535
   566
kpeter@2535
   567
          if (excess[s] < delta) ++next_node;
kpeter@2535
   568
        }
kpeter@2535
   569
kpeter@2535
   570
        if (delta == 1) break;
kpeter@2535
   571
        if (++phase_cnt > phase_num / 4) factor = 2;
kpeter@2535
   572
        delta = delta <= factor ? 1 : delta / factor;
kpeter@2535
   573
      }
kpeter@2535
   574
kpeter@2535
   575
      // Handling nonzero lower bounds
kpeter@2535
   576
      if (lower) {
kpeter@2535
   577
        for (EdgeIt e(graph); e != INVALID; ++e)
kpeter@2535
   578
          flow[e] += (*lower)[e];
kpeter@2535
   579
      }
kpeter@2535
   580
      return true;
kpeter@2535
   581
    }
kpeter@2535
   582
deba@2440
   583
    /// \brief Executes the successive shortest path algorithm without
deba@2440
   584
    /// capacity scaling.
kpeter@2535
   585
    bool startWithoutScaling() {
deba@2440
   586
      // Finding excess nodes
kpeter@2535
   587
      for (NodeIt n(graph); n != INVALID; ++n) {
kpeter@2535
   588
        if (excess[n] > 0) excess_nodes.push_back(n);
deba@2440
   589
      }
deba@2440
   590
      if (excess_nodes.size() == 0) return true;
deba@2440
   591
      next_node = 0;
deba@2440
   592
deba@2457
   593
      // Finding shortest paths
kpeter@2535
   594
      Node s, t;
kpeter@2535
   595
      while ( excess[excess_nodes[next_node]] > 0 ||
kpeter@2535
   596
              ++next_node < excess_nodes.size() )
deba@2440
   597
      {
kpeter@2535
   598
        // Running Dijkstra
kpeter@2535
   599
        s = excess_nodes[next_node];
kpeter@2535
   600
        if ((t = dijkstra.run(s, 1)) == INVALID)
kpeter@2535
   601
          return false;
deba@2440
   602
kpeter@2535
   603
        // Augmenting along a shortest path from s to t
kpeter@2535
   604
        Capacity d = excess[s] < -excess[t] ? excess[s] : -excess[t];
kpeter@2535
   605
        Node u = t;
kpeter@2535
   606
        Edge e;
kpeter@2535
   607
        while ((e = pred[u]) != INVALID) {
kpeter@2535
   608
          Capacity rc;
kpeter@2535
   609
          if (u == graph.target(e)) {
kpeter@2535
   610
            rc = res_cap[e];
kpeter@2535
   611
            u = graph.source(e);
kpeter@2535
   612
          } else {
kpeter@2535
   613
            rc = flow[e];
kpeter@2535
   614
            u = graph.target(e);
kpeter@2535
   615
          }
kpeter@2535
   616
          if (rc < d) d = rc;
kpeter@2535
   617
        }
kpeter@2535
   618
        u = t;
kpeter@2535
   619
        while ((e = pred[u]) != INVALID) {
kpeter@2535
   620
          if (u == graph.target(e)) {
kpeter@2535
   621
            flow[e] += d;
kpeter@2535
   622
            res_cap[e] -= d;
kpeter@2535
   623
            u = graph.source(e);
kpeter@2535
   624
          } else {
kpeter@2535
   625
            flow[e] -= d;
kpeter@2535
   626
            res_cap[e] += d;
kpeter@2535
   627
            u = graph.target(e);
kpeter@2535
   628
          }
kpeter@2535
   629
        }
kpeter@2535
   630
        excess[s] -= d;
kpeter@2535
   631
        excess[t] += d;
deba@2440
   632
      }
deba@2440
   633
deba@2440
   634
      // Handling nonzero lower bounds
deba@2440
   635
      if (lower) {
kpeter@2535
   636
        for (EdgeIt e(graph); e != INVALID; ++e)
kpeter@2535
   637
          flow[e] += (*lower)[e];
deba@2440
   638
      }
deba@2440
   639
      return true;
deba@2440
   640
    }
deba@2440
   641
deba@2440
   642
  }; //class CapacityScaling
deba@2440
   643
deba@2440
   644
  ///@}
deba@2440
   645
deba@2440
   646
} //namespace lemon
deba@2440
   647
deba@2440
   648
#endif //LEMON_CAPACITY_SCALING_H