lemon/capacity_scaling.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 12 Nov 2009 23:34:35 +0100
changeset 876 3b53491bf643
parent 873 78071e00de00
child 877 fe80a8145653
permissions -rw-r--r--
More options for run() in scaling MCF algorithms (#180)

- Three methods can be selected and the scaling factor can be
given for CostScaling.
- The scaling factor can be given for CapacityScaling.
kpeter@871
     1
/* -*- C++ -*-
kpeter@871
     2
 *
kpeter@871
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@871
     4
 *
kpeter@871
     5
 * Copyright (C) 2003-2008
kpeter@871
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@871
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@871
     8
 *
kpeter@871
     9
 * Permission to use, modify and distribute this software is granted
kpeter@871
    10
 * provided that this copyright notice appears in all copies. For
kpeter@871
    11
 * precise terms see the accompanying LICENSE file.
kpeter@871
    12
 *
kpeter@871
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@871
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@871
    15
 * purpose.
kpeter@871
    16
 *
kpeter@871
    17
 */
kpeter@871
    18
kpeter@871
    19
#ifndef LEMON_CAPACITY_SCALING_H
kpeter@871
    20
#define LEMON_CAPACITY_SCALING_H
kpeter@871
    21
kpeter@872
    22
/// \ingroup min_cost_flow_algs
kpeter@871
    23
///
kpeter@871
    24
/// \file
kpeter@872
    25
/// \brief Capacity Scaling algorithm for finding a minimum cost flow.
kpeter@871
    26
kpeter@871
    27
#include <vector>
kpeter@872
    28
#include <limits>
kpeter@872
    29
#include <lemon/core.h>
kpeter@871
    30
#include <lemon/bin_heap.h>
kpeter@871
    31
kpeter@871
    32
namespace lemon {
kpeter@871
    33
kpeter@873
    34
  /// \brief Default traits class of CapacityScaling algorithm.
kpeter@873
    35
  ///
kpeter@873
    36
  /// Default traits class of CapacityScaling algorithm.
kpeter@873
    37
  /// \tparam GR Digraph type.
kpeter@873
    38
  /// \tparam V The value type used for flow amounts, capacity bounds
kpeter@873
    39
  /// and supply values. By default it is \c int.
kpeter@873
    40
  /// \tparam C The value type used for costs and potentials.
kpeter@873
    41
  /// By default it is the same as \c V.
kpeter@873
    42
  template <typename GR, typename V = int, typename C = V>
kpeter@873
    43
  struct CapacityScalingDefaultTraits
kpeter@873
    44
  {
kpeter@873
    45
    /// The type of the digraph
kpeter@873
    46
    typedef GR Digraph;
kpeter@873
    47
    /// The type of the flow amounts, capacity bounds and supply values
kpeter@873
    48
    typedef V Value;
kpeter@873
    49
    /// The type of the arc costs
kpeter@873
    50
    typedef C Cost;
kpeter@873
    51
kpeter@873
    52
    /// \brief The type of the heap used for internal Dijkstra computations.
kpeter@873
    53
    ///
kpeter@873
    54
    /// The type of the heap used for internal Dijkstra computations.
kpeter@873
    55
    /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
kpeter@873
    56
    /// its priority type must be \c Cost and its cross reference type
kpeter@873
    57
    /// must be \ref RangeMap "RangeMap<int>".
kpeter@873
    58
    typedef BinHeap<Cost, RangeMap<int> > Heap;
kpeter@873
    59
  };
kpeter@873
    60
kpeter@872
    61
  /// \addtogroup min_cost_flow_algs
kpeter@871
    62
  /// @{
kpeter@871
    63
kpeter@872
    64
  /// \brief Implementation of the Capacity Scaling algorithm for
kpeter@872
    65
  /// finding a \ref min_cost_flow "minimum cost flow".
kpeter@871
    66
  ///
kpeter@871
    67
  /// \ref CapacityScaling implements the capacity scaling version
kpeter@872
    68
  /// of the successive shortest path algorithm for finding a
kpeter@872
    69
  /// \ref min_cost_flow "minimum cost flow". It is an efficient dual
kpeter@872
    70
  /// solution method.
kpeter@871
    71
  ///
kpeter@872
    72
  /// Most of the parameters of the problem (except for the digraph)
kpeter@872
    73
  /// can be given using separate functions, and the algorithm can be
kpeter@872
    74
  /// executed using the \ref run() function. If some parameters are not
kpeter@872
    75
  /// specified, then default values will be used.
kpeter@871
    76
  ///
kpeter@872
    77
  /// \tparam GR The digraph type the algorithm runs on.
kpeter@872
    78
  /// \tparam V The value type used for flow amounts, capacity bounds
kpeter@872
    79
  /// and supply values in the algorithm. By default it is \c int.
kpeter@872
    80
  /// \tparam C The value type used for costs and potentials in the
kpeter@872
    81
  /// algorithm. By default it is the same as \c V.
kpeter@871
    82
  ///
kpeter@872
    83
  /// \warning Both value types must be signed and all input data must
kpeter@872
    84
  /// be integer.
kpeter@872
    85
  /// \warning This algorithm does not support negative costs for such
kpeter@872
    86
  /// arcs that have infinite upper bound.
kpeter@873
    87
#ifdef DOXYGEN
kpeter@873
    88
  template <typename GR, typename V, typename C, typename TR>
kpeter@873
    89
#else
kpeter@873
    90
  template < typename GR, typename V = int, typename C = V,
kpeter@873
    91
             typename TR = CapacityScalingDefaultTraits<GR, V, C> >
kpeter@873
    92
#endif
kpeter@871
    93
  class CapacityScaling
kpeter@871
    94
  {
kpeter@872
    95
  public:
kpeter@871
    96
kpeter@873
    97
    /// The type of the digraph
kpeter@873
    98
    typedef typename TR::Digraph Digraph;
kpeter@872
    99
    /// The type of the flow amounts, capacity bounds and supply values
kpeter@873
   100
    typedef typename TR::Value Value;
kpeter@872
   101
    /// The type of the arc costs
kpeter@873
   102
    typedef typename TR::Cost Cost;
kpeter@873
   103
kpeter@873
   104
    /// The type of the heap used for internal Dijkstra computations
kpeter@873
   105
    typedef typename TR::Heap Heap;
kpeter@873
   106
kpeter@873
   107
    /// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm
kpeter@873
   108
    typedef TR Traits;
kpeter@871
   109
kpeter@871
   110
  public:
kpeter@871
   111
kpeter@872
   112
    /// \brief Problem type constants for the \c run() function.
kpeter@872
   113
    ///
kpeter@872
   114
    /// Enum type containing the problem type constants that can be
kpeter@872
   115
    /// returned by the \ref run() function of the algorithm.
kpeter@872
   116
    enum ProblemType {
kpeter@872
   117
      /// The problem has no feasible solution (flow).
kpeter@872
   118
      INFEASIBLE,
kpeter@872
   119
      /// The problem has optimal solution (i.e. it is feasible and
kpeter@872
   120
      /// bounded), and the algorithm has found optimal flow and node
kpeter@872
   121
      /// potentials (primal and dual solutions).
kpeter@872
   122
      OPTIMAL,
kpeter@872
   123
      /// The digraph contains an arc of negative cost and infinite
kpeter@872
   124
      /// upper bound. It means that the objective function is unbounded
kpeter@872
   125
      /// on that arc, however note that it could actually be bounded
kpeter@872
   126
      /// over the feasible flows, but this algroithm cannot handle
kpeter@872
   127
      /// these cases.
kpeter@872
   128
      UNBOUNDED
kpeter@872
   129
    };
kpeter@872
   130
  
kpeter@872
   131
  private:
kpeter@872
   132
kpeter@872
   133
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@872
   134
kpeter@872
   135
    typedef std::vector<int> IntVector;
kpeter@872
   136
    typedef std::vector<bool> BoolVector;
kpeter@872
   137
    typedef std::vector<Value> ValueVector;
kpeter@872
   138
    typedef std::vector<Cost> CostVector;
kpeter@871
   139
kpeter@871
   140
  private:
kpeter@871
   141
kpeter@872
   142
    // Data related to the underlying digraph
kpeter@872
   143
    const GR &_graph;
kpeter@872
   144
    int _node_num;
kpeter@872
   145
    int _arc_num;
kpeter@872
   146
    int _res_arc_num;
kpeter@872
   147
    int _root;
kpeter@872
   148
kpeter@872
   149
    // Parameters of the problem
kpeter@872
   150
    bool _have_lower;
kpeter@872
   151
    Value _sum_supply;
kpeter@872
   152
kpeter@872
   153
    // Data structures for storing the digraph
kpeter@872
   154
    IntNodeMap _node_id;
kpeter@872
   155
    IntArcMap _arc_idf;
kpeter@872
   156
    IntArcMap _arc_idb;
kpeter@872
   157
    IntVector _first_out;
kpeter@872
   158
    BoolVector _forward;
kpeter@872
   159
    IntVector _source;
kpeter@872
   160
    IntVector _target;
kpeter@872
   161
    IntVector _reverse;
kpeter@872
   162
kpeter@872
   163
    // Node and arc data
kpeter@872
   164
    ValueVector _lower;
kpeter@872
   165
    ValueVector _upper;
kpeter@872
   166
    CostVector _cost;
kpeter@872
   167
    ValueVector _supply;
kpeter@872
   168
kpeter@872
   169
    ValueVector _res_cap;
kpeter@872
   170
    CostVector _pi;
kpeter@872
   171
    ValueVector _excess;
kpeter@872
   172
    IntVector _excess_nodes;
kpeter@872
   173
    IntVector _deficit_nodes;
kpeter@872
   174
kpeter@872
   175
    Value _delta;
kpeter@876
   176
    int _factor;
kpeter@872
   177
    IntVector _pred;
kpeter@872
   178
kpeter@872
   179
  public:
kpeter@872
   180
  
kpeter@872
   181
    /// \brief Constant for infinite upper bounds (capacities).
kpeter@871
   182
    ///
kpeter@872
   183
    /// Constant for infinite upper bounds (capacities).
kpeter@872
   184
    /// It is \c std::numeric_limits<Value>::infinity() if available,
kpeter@872
   185
    /// \c std::numeric_limits<Value>::max() otherwise.
kpeter@872
   186
    const Value INF;
kpeter@872
   187
kpeter@872
   188
  private:
kpeter@872
   189
kpeter@872
   190
    // Special implementation of the Dijkstra algorithm for finding
kpeter@872
   191
    // shortest paths in the residual network of the digraph with
kpeter@872
   192
    // respect to the reduced arc costs and modifying the node
kpeter@872
   193
    // potentials according to the found distance labels.
kpeter@871
   194
    class ResidualDijkstra
kpeter@871
   195
    {
kpeter@871
   196
    private:
kpeter@871
   197
kpeter@872
   198
      int _node_num;
kpeter@872
   199
      const IntVector &_first_out;
kpeter@872
   200
      const IntVector &_target;
kpeter@872
   201
      const CostVector &_cost;
kpeter@872
   202
      const ValueVector &_res_cap;
kpeter@872
   203
      const ValueVector &_excess;
kpeter@872
   204
      CostVector &_pi;
kpeter@872
   205
      IntVector &_pred;
kpeter@872
   206
      
kpeter@872
   207
      IntVector _proc_nodes;
kpeter@872
   208
      CostVector _dist;
kpeter@872
   209
      
kpeter@871
   210
    public:
kpeter@871
   211
kpeter@872
   212
      ResidualDijkstra(CapacityScaling& cs) :
kpeter@872
   213
        _node_num(cs._node_num), _first_out(cs._first_out), 
kpeter@872
   214
        _target(cs._target), _cost(cs._cost), _res_cap(cs._res_cap),
kpeter@872
   215
        _excess(cs._excess), _pi(cs._pi), _pred(cs._pred),
kpeter@872
   216
        _dist(cs._node_num)
kpeter@871
   217
      {}
kpeter@871
   218
kpeter@872
   219
      int run(int s, Value delta = 1) {
kpeter@873
   220
        RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
kpeter@871
   221
        Heap heap(heap_cross_ref);
kpeter@871
   222
        heap.push(s, 0);
kpeter@872
   223
        _pred[s] = -1;
kpeter@871
   224
        _proc_nodes.clear();
kpeter@871
   225
kpeter@872
   226
        // Process nodes
kpeter@871
   227
        while (!heap.empty() && _excess[heap.top()] > -delta) {
kpeter@872
   228
          int u = heap.top(), v;
kpeter@872
   229
          Cost d = heap.prio() + _pi[u], dn;
kpeter@871
   230
          _dist[u] = heap.prio();
kpeter@872
   231
          _proc_nodes.push_back(u);
kpeter@871
   232
          heap.pop();
kpeter@871
   233
kpeter@872
   234
          // Traverse outgoing residual arcs
kpeter@872
   235
          for (int a = _first_out[u]; a != _first_out[u+1]; ++a) {
kpeter@872
   236
            if (_res_cap[a] < delta) continue;
kpeter@872
   237
            v = _target[a];
kpeter@872
   238
            switch (heap.state(v)) {
kpeter@871
   239
              case Heap::PRE_HEAP:
kpeter@872
   240
                heap.push(v, d + _cost[a] - _pi[v]);
kpeter@872
   241
                _pred[v] = a;
kpeter@871
   242
                break;
kpeter@871
   243
              case Heap::IN_HEAP:
kpeter@872
   244
                dn = d + _cost[a] - _pi[v];
kpeter@872
   245
                if (dn < heap[v]) {
kpeter@872
   246
                  heap.decrease(v, dn);
kpeter@872
   247
                  _pred[v] = a;
kpeter@871
   248
                }
kpeter@871
   249
                break;
kpeter@871
   250
              case Heap::POST_HEAP:
kpeter@871
   251
                break;
kpeter@871
   252
            }
kpeter@871
   253
          }
kpeter@871
   254
        }
kpeter@872
   255
        if (heap.empty()) return -1;
kpeter@871
   256
kpeter@872
   257
        // Update potentials of processed nodes
kpeter@872
   258
        int t = heap.top();
kpeter@872
   259
        Cost dt = heap.prio();
kpeter@872
   260
        for (int i = 0; i < int(_proc_nodes.size()); ++i) {
kpeter@872
   261
          _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
kpeter@872
   262
        }
kpeter@871
   263
kpeter@871
   264
        return t;
kpeter@871
   265
      }
kpeter@871
   266
kpeter@871
   267
    }; //class ResidualDijkstra
kpeter@871
   268
kpeter@871
   269
  public:
kpeter@871
   270
kpeter@873
   271
    /// \name Named Template Parameters
kpeter@873
   272
    /// @{
kpeter@873
   273
kpeter@873
   274
    template <typename T>
kpeter@873
   275
    struct SetHeapTraits : public Traits {
kpeter@873
   276
      typedef T Heap;
kpeter@873
   277
    };
kpeter@873
   278
kpeter@873
   279
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@873
   280
    /// \c Heap type.
kpeter@873
   281
    ///
kpeter@873
   282
    /// \ref named-templ-param "Named parameter" for setting \c Heap
kpeter@873
   283
    /// type, which is used for internal Dijkstra computations.
kpeter@873
   284
    /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
kpeter@873
   285
    /// its priority type must be \c Cost and its cross reference type
kpeter@873
   286
    /// must be \ref RangeMap "RangeMap<int>".
kpeter@873
   287
    template <typename T>
kpeter@873
   288
    struct SetHeap
kpeter@873
   289
      : public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
kpeter@873
   290
      typedef  CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
kpeter@873
   291
    };
kpeter@873
   292
kpeter@873
   293
    /// @}
kpeter@873
   294
kpeter@873
   295
  public:
kpeter@873
   296
kpeter@872
   297
    /// \brief Constructor.
kpeter@871
   298
    ///
kpeter@872
   299
    /// The constructor of the class.
kpeter@871
   300
    ///
kpeter@872
   301
    /// \param graph The digraph the algorithm runs on.
kpeter@872
   302
    CapacityScaling(const GR& graph) :
kpeter@872
   303
      _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
kpeter@872
   304
      INF(std::numeric_limits<Value>::has_infinity ?
kpeter@872
   305
          std::numeric_limits<Value>::infinity() :
kpeter@872
   306
          std::numeric_limits<Value>::max())
kpeter@871
   307
    {
kpeter@872
   308
      // Check the value types
kpeter@872
   309
      LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
kpeter@872
   310
        "The flow type of CapacityScaling must be signed");
kpeter@872
   311
      LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
kpeter@872
   312
        "The cost type of CapacityScaling must be signed");
kpeter@872
   313
kpeter@872
   314
      // Resize vectors
kpeter@872
   315
      _node_num = countNodes(_graph);
kpeter@872
   316
      _arc_num = countArcs(_graph);
kpeter@872
   317
      _res_arc_num = 2 * (_arc_num + _node_num);
kpeter@872
   318
      _root = _node_num;
kpeter@872
   319
      ++_node_num;
kpeter@872
   320
kpeter@872
   321
      _first_out.resize(_node_num + 1);
kpeter@872
   322
      _forward.resize(_res_arc_num);
kpeter@872
   323
      _source.resize(_res_arc_num);
kpeter@872
   324
      _target.resize(_res_arc_num);
kpeter@872
   325
      _reverse.resize(_res_arc_num);
kpeter@872
   326
kpeter@872
   327
      _lower.resize(_res_arc_num);
kpeter@872
   328
      _upper.resize(_res_arc_num);
kpeter@872
   329
      _cost.resize(_res_arc_num);
kpeter@872
   330
      _supply.resize(_node_num);
kpeter@872
   331
      
kpeter@872
   332
      _res_cap.resize(_res_arc_num);
kpeter@872
   333
      _pi.resize(_node_num);
kpeter@872
   334
      _excess.resize(_node_num);
kpeter@872
   335
      _pred.resize(_node_num);
kpeter@872
   336
kpeter@872
   337
      // Copy the graph
kpeter@872
   338
      int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1;
kpeter@872
   339
      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
kpeter@872
   340
        _node_id[n] = i;
kpeter@871
   341
      }
kpeter@872
   342
      i = 0;
kpeter@872
   343
      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
kpeter@872
   344
        _first_out[i] = j;
kpeter@872
   345
        for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
kpeter@872
   346
          _arc_idf[a] = j;
kpeter@872
   347
          _forward[j] = true;
kpeter@872
   348
          _source[j] = i;
kpeter@872
   349
          _target[j] = _node_id[_graph.runningNode(a)];
kpeter@872
   350
        }
kpeter@872
   351
        for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
kpeter@872
   352
          _arc_idb[a] = j;
kpeter@872
   353
          _forward[j] = false;
kpeter@872
   354
          _source[j] = i;
kpeter@872
   355
          _target[j] = _node_id[_graph.runningNode(a)];
kpeter@872
   356
        }
kpeter@872
   357
        _forward[j] = false;
kpeter@872
   358
        _source[j] = i;
kpeter@872
   359
        _target[j] = _root;
kpeter@872
   360
        _reverse[j] = k;
kpeter@872
   361
        _forward[k] = true;
kpeter@872
   362
        _source[k] = _root;
kpeter@872
   363
        _target[k] = i;
kpeter@872
   364
        _reverse[k] = j;
kpeter@872
   365
        ++j; ++k;
kpeter@872
   366
      }
kpeter@872
   367
      _first_out[i] = j;
kpeter@872
   368
      _first_out[_node_num] = k;
kpeter@871
   369
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   370
        int fi = _arc_idf[a];
kpeter@872
   371
        int bi = _arc_idb[a];
kpeter@872
   372
        _reverse[fi] = bi;
kpeter@872
   373
        _reverse[bi] = fi;
kpeter@871
   374
      }
kpeter@872
   375
      
kpeter@872
   376
      // Reset parameters
kpeter@872
   377
      reset();
kpeter@871
   378
    }
kpeter@871
   379
kpeter@872
   380
    /// \name Parameters
kpeter@872
   381
    /// The parameters of the algorithm can be specified using these
kpeter@872
   382
    /// functions.
kpeter@872
   383
kpeter@872
   384
    /// @{
kpeter@872
   385
kpeter@872
   386
    /// \brief Set the lower bounds on the arcs.
kpeter@871
   387
    ///
kpeter@872
   388
    /// This function sets the lower bounds on the arcs.
kpeter@872
   389
    /// If it is not used before calling \ref run(), the lower bounds
kpeter@872
   390
    /// will be set to zero on all arcs.
kpeter@871
   391
    ///
kpeter@872
   392
    /// \param map An arc map storing the lower bounds.
kpeter@872
   393
    /// Its \c Value type must be convertible to the \c Value type
kpeter@872
   394
    /// of the algorithm.
kpeter@872
   395
    ///
kpeter@872
   396
    /// \return <tt>(*this)</tt>
kpeter@872
   397
    template <typename LowerMap>
kpeter@872
   398
    CapacityScaling& lowerMap(const LowerMap& map) {
kpeter@872
   399
      _have_lower = true;
kpeter@872
   400
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   401
        _lower[_arc_idf[a]] = map[a];
kpeter@872
   402
        _lower[_arc_idb[a]] = map[a];
kpeter@871
   403
      }
kpeter@871
   404
      return *this;
kpeter@871
   405
    }
kpeter@871
   406
kpeter@872
   407
    /// \brief Set the upper bounds (capacities) on the arcs.
kpeter@871
   408
    ///
kpeter@872
   409
    /// This function sets the upper bounds (capacities) on the arcs.
kpeter@872
   410
    /// If it is not used before calling \ref run(), the upper bounds
kpeter@872
   411
    /// will be set to \ref INF on all arcs (i.e. the flow value will be
kpeter@872
   412
    /// unbounded from above on each arc).
kpeter@871
   413
    ///
kpeter@872
   414
    /// \param map An arc map storing the upper bounds.
kpeter@872
   415
    /// Its \c Value type must be convertible to the \c Value type
kpeter@872
   416
    /// of the algorithm.
kpeter@872
   417
    ///
kpeter@872
   418
    /// \return <tt>(*this)</tt>
kpeter@872
   419
    template<typename UpperMap>
kpeter@872
   420
    CapacityScaling& upperMap(const UpperMap& map) {
kpeter@872
   421
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   422
        _upper[_arc_idf[a]] = map[a];
kpeter@871
   423
      }
kpeter@871
   424
      return *this;
kpeter@871
   425
    }
kpeter@871
   426
kpeter@872
   427
    /// \brief Set the costs of the arcs.
kpeter@872
   428
    ///
kpeter@872
   429
    /// This function sets the costs of the arcs.
kpeter@872
   430
    /// If it is not used before calling \ref run(), the costs
kpeter@872
   431
    /// will be set to \c 1 on all arcs.
kpeter@872
   432
    ///
kpeter@872
   433
    /// \param map An arc map storing the costs.
kpeter@872
   434
    /// Its \c Value type must be convertible to the \c Cost type
kpeter@872
   435
    /// of the algorithm.
kpeter@872
   436
    ///
kpeter@872
   437
    /// \return <tt>(*this)</tt>
kpeter@872
   438
    template<typename CostMap>
kpeter@872
   439
    CapacityScaling& costMap(const CostMap& map) {
kpeter@872
   440
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   441
        _cost[_arc_idf[a]] =  map[a];
kpeter@872
   442
        _cost[_arc_idb[a]] = -map[a];
kpeter@872
   443
      }
kpeter@872
   444
      return *this;
kpeter@872
   445
    }
kpeter@872
   446
kpeter@872
   447
    /// \brief Set the supply values of the nodes.
kpeter@872
   448
    ///
kpeter@872
   449
    /// This function sets the supply values of the nodes.
kpeter@872
   450
    /// If neither this function nor \ref stSupply() is used before
kpeter@872
   451
    /// calling \ref run(), the supply of each node will be set to zero.
kpeter@872
   452
    ///
kpeter@872
   453
    /// \param map A node map storing the supply values.
kpeter@872
   454
    /// Its \c Value type must be convertible to the \c Value type
kpeter@872
   455
    /// of the algorithm.
kpeter@872
   456
    ///
kpeter@872
   457
    /// \return <tt>(*this)</tt>
kpeter@872
   458
    template<typename SupplyMap>
kpeter@872
   459
    CapacityScaling& supplyMap(const SupplyMap& map) {
kpeter@872
   460
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@872
   461
        _supply[_node_id[n]] = map[n];
kpeter@872
   462
      }
kpeter@872
   463
      return *this;
kpeter@872
   464
    }
kpeter@872
   465
kpeter@872
   466
    /// \brief Set single source and target nodes and a supply value.
kpeter@872
   467
    ///
kpeter@872
   468
    /// This function sets a single source node and a single target node
kpeter@872
   469
    /// and the required flow value.
kpeter@872
   470
    /// If neither this function nor \ref supplyMap() is used before
kpeter@872
   471
    /// calling \ref run(), the supply of each node will be set to zero.
kpeter@872
   472
    ///
kpeter@872
   473
    /// Using this function has the same effect as using \ref supplyMap()
kpeter@872
   474
    /// with such a map in which \c k is assigned to \c s, \c -k is
kpeter@872
   475
    /// assigned to \c t and all other nodes have zero supply value.
kpeter@872
   476
    ///
kpeter@872
   477
    /// \param s The source node.
kpeter@872
   478
    /// \param t The target node.
kpeter@872
   479
    /// \param k The required amount of flow from node \c s to node \c t
kpeter@872
   480
    /// (i.e. the supply of \c s and the demand of \c t).
kpeter@872
   481
    ///
kpeter@872
   482
    /// \return <tt>(*this)</tt>
kpeter@872
   483
    CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
kpeter@872
   484
      for (int i = 0; i != _node_num; ++i) {
kpeter@872
   485
        _supply[i] = 0;
kpeter@872
   486
      }
kpeter@872
   487
      _supply[_node_id[s]] =  k;
kpeter@872
   488
      _supply[_node_id[t]] = -k;
kpeter@872
   489
      return *this;
kpeter@872
   490
    }
kpeter@872
   491
    
kpeter@872
   492
    /// @}
kpeter@872
   493
kpeter@871
   494
    /// \name Execution control
kpeter@873
   495
    /// The algorithm can be executed using \ref run().
kpeter@871
   496
kpeter@871
   497
    /// @{
kpeter@871
   498
kpeter@871
   499
    /// \brief Run the algorithm.
kpeter@871
   500
    ///
kpeter@871
   501
    /// This function runs the algorithm.
kpeter@872
   502
    /// The paramters can be specified using functions \ref lowerMap(),
kpeter@872
   503
    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
kpeter@872
   504
    /// For example,
kpeter@872
   505
    /// \code
kpeter@872
   506
    ///   CapacityScaling<ListDigraph> cs(graph);
kpeter@872
   507
    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
kpeter@872
   508
    ///     .supplyMap(sup).run();
kpeter@872
   509
    /// \endcode
kpeter@872
   510
    ///
kpeter@872
   511
    /// This function can be called more than once. All the parameters
kpeter@872
   512
    /// that have been given are kept for the next call, unless
kpeter@872
   513
    /// \ref reset() is called, thus only the modified parameters
kpeter@872
   514
    /// have to be set again. See \ref reset() for examples.
kpeter@872
   515
    /// However the underlying digraph must not be modified after this
kpeter@876
   516
    /// class have been constructed, since it copies and extends the graph.
kpeter@871
   517
    ///
kpeter@876
   518
    /// \param factor The capacity scaling factor. It must be larger than
kpeter@876
   519
    /// one to use scaling. If it is less or equal to one, then scaling
kpeter@876
   520
    /// will be disabled.
kpeter@871
   521
    ///
kpeter@872
   522
    /// \return \c INFEASIBLE if no feasible flow exists,
kpeter@872
   523
    /// \n \c OPTIMAL if the problem has optimal solution
kpeter@872
   524
    /// (i.e. it is feasible and bounded), and the algorithm has found
kpeter@872
   525
    /// optimal flow and node potentials (primal and dual solutions),
kpeter@872
   526
    /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
kpeter@872
   527
    /// and infinite upper bound. It means that the objective function
kpeter@872
   528
    /// is unbounded on that arc, however note that it could actually be
kpeter@872
   529
    /// bounded over the feasible flows, but this algroithm cannot handle
kpeter@872
   530
    /// these cases.
kpeter@872
   531
    ///
kpeter@872
   532
    /// \see ProblemType
kpeter@876
   533
    ProblemType run(int factor = 4) {
kpeter@876
   534
      _factor = factor;
kpeter@876
   535
      ProblemType pt = init();
kpeter@872
   536
      if (pt != OPTIMAL) return pt;
kpeter@872
   537
      return start();
kpeter@872
   538
    }
kpeter@872
   539
kpeter@872
   540
    /// \brief Reset all the parameters that have been given before.
kpeter@872
   541
    ///
kpeter@872
   542
    /// This function resets all the paramaters that have been given
kpeter@872
   543
    /// before using functions \ref lowerMap(), \ref upperMap(),
kpeter@872
   544
    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
kpeter@872
   545
    ///
kpeter@872
   546
    /// It is useful for multiple run() calls. If this function is not
kpeter@872
   547
    /// used, all the parameters given before are kept for the next
kpeter@872
   548
    /// \ref run() call.
kpeter@876
   549
    /// However, the underlying digraph must not be modified after this
kpeter@872
   550
    /// class have been constructed, since it copies and extends the graph.
kpeter@872
   551
    ///
kpeter@872
   552
    /// For example,
kpeter@872
   553
    /// \code
kpeter@872
   554
    ///   CapacityScaling<ListDigraph> cs(graph);
kpeter@872
   555
    ///
kpeter@872
   556
    ///   // First run
kpeter@872
   557
    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
kpeter@872
   558
    ///     .supplyMap(sup).run();
kpeter@872
   559
    ///
kpeter@872
   560
    ///   // Run again with modified cost map (reset() is not called,
kpeter@872
   561
    ///   // so only the cost map have to be set again)
kpeter@872
   562
    ///   cost[e] += 100;
kpeter@872
   563
    ///   cs.costMap(cost).run();
kpeter@872
   564
    ///
kpeter@872
   565
    ///   // Run again from scratch using reset()
kpeter@872
   566
    ///   // (the lower bounds will be set to zero on all arcs)
kpeter@872
   567
    ///   cs.reset();
kpeter@872
   568
    ///   cs.upperMap(capacity).costMap(cost)
kpeter@872
   569
    ///     .supplyMap(sup).run();
kpeter@872
   570
    /// \endcode
kpeter@872
   571
    ///
kpeter@872
   572
    /// \return <tt>(*this)</tt>
kpeter@872
   573
    CapacityScaling& reset() {
kpeter@872
   574
      for (int i = 0; i != _node_num; ++i) {
kpeter@872
   575
        _supply[i] = 0;
kpeter@872
   576
      }
kpeter@872
   577
      for (int j = 0; j != _res_arc_num; ++j) {
kpeter@872
   578
        _lower[j] = 0;
kpeter@872
   579
        _upper[j] = INF;
kpeter@872
   580
        _cost[j] = _forward[j] ? 1 : -1;
kpeter@872
   581
      }
kpeter@872
   582
      _have_lower = false;
kpeter@872
   583
      return *this;
kpeter@871
   584
    }
kpeter@871
   585
kpeter@871
   586
    /// @}
kpeter@871
   587
kpeter@871
   588
    /// \name Query Functions
kpeter@871
   589
    /// The results of the algorithm can be obtained using these
kpeter@871
   590
    /// functions.\n
kpeter@872
   591
    /// The \ref run() function must be called before using them.
kpeter@871
   592
kpeter@871
   593
    /// @{
kpeter@871
   594
kpeter@872
   595
    /// \brief Return the total cost of the found flow.
kpeter@871
   596
    ///
kpeter@872
   597
    /// This function returns the total cost of the found flow.
kpeter@872
   598
    /// Its complexity is O(e).
kpeter@872
   599
    ///
kpeter@872
   600
    /// \note The return type of the function can be specified as a
kpeter@872
   601
    /// template parameter. For example,
kpeter@872
   602
    /// \code
kpeter@872
   603
    ///   cs.totalCost<double>();
kpeter@872
   604
    /// \endcode
kpeter@872
   605
    /// It is useful if the total cost cannot be stored in the \c Cost
kpeter@872
   606
    /// type of the algorithm, which is the default return type of the
kpeter@872
   607
    /// function.
kpeter@871
   608
    ///
kpeter@871
   609
    /// \pre \ref run() must be called before using this function.
kpeter@872
   610
    template <typename Number>
kpeter@872
   611
    Number totalCost() const {
kpeter@872
   612
      Number c = 0;
kpeter@872
   613
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   614
        int i = _arc_idb[a];
kpeter@872
   615
        c += static_cast<Number>(_res_cap[i]) *
kpeter@872
   616
             (-static_cast<Number>(_cost[i]));
kpeter@872
   617
      }
kpeter@872
   618
      return c;
kpeter@871
   619
    }
kpeter@871
   620
kpeter@872
   621
#ifndef DOXYGEN
kpeter@872
   622
    Cost totalCost() const {
kpeter@872
   623
      return totalCost<Cost>();
kpeter@871
   624
    }
kpeter@872
   625
#endif
kpeter@871
   626
kpeter@871
   627
    /// \brief Return the flow on the given arc.
kpeter@871
   628
    ///
kpeter@872
   629
    /// This function returns the flow on the given arc.
kpeter@871
   630
    ///
kpeter@871
   631
    /// \pre \ref run() must be called before using this function.
kpeter@872
   632
    Value flow(const Arc& a) const {
kpeter@872
   633
      return _res_cap[_arc_idb[a]];
kpeter@871
   634
    }
kpeter@871
   635
kpeter@872
   636
    /// \brief Return the flow map (the primal solution).
kpeter@871
   637
    ///
kpeter@872
   638
    /// This function copies the flow value on each arc into the given
kpeter@872
   639
    /// map. The \c Value type of the algorithm must be convertible to
kpeter@872
   640
    /// the \c Value type of the map.
kpeter@871
   641
    ///
kpeter@871
   642
    /// \pre \ref run() must be called before using this function.
kpeter@872
   643
    template <typename FlowMap>
kpeter@872
   644
    void flowMap(FlowMap &map) const {
kpeter@872
   645
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@872
   646
        map.set(a, _res_cap[_arc_idb[a]]);
kpeter@872
   647
      }
kpeter@871
   648
    }
kpeter@871
   649
kpeter@872
   650
    /// \brief Return the potential (dual value) of the given node.
kpeter@871
   651
    ///
kpeter@872
   652
    /// This function returns the potential (dual value) of the
kpeter@872
   653
    /// given node.
kpeter@871
   654
    ///
kpeter@871
   655
    /// \pre \ref run() must be called before using this function.
kpeter@872
   656
    Cost potential(const Node& n) const {
kpeter@872
   657
      return _pi[_node_id[n]];
kpeter@872
   658
    }
kpeter@872
   659
kpeter@872
   660
    /// \brief Return the potential map (the dual solution).
kpeter@872
   661
    ///
kpeter@872
   662
    /// This function copies the potential (dual value) of each node
kpeter@872
   663
    /// into the given map.
kpeter@872
   664
    /// The \c Cost type of the algorithm must be convertible to the
kpeter@872
   665
    /// \c Value type of the map.
kpeter@872
   666
    ///
kpeter@872
   667
    /// \pre \ref run() must be called before using this function.
kpeter@872
   668
    template <typename PotentialMap>
kpeter@872
   669
    void potentialMap(PotentialMap &map) const {
kpeter@872
   670
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@872
   671
        map.set(n, _pi[_node_id[n]]);
kpeter@872
   672
      }
kpeter@871
   673
    }
kpeter@871
   674
kpeter@871
   675
    /// @}
kpeter@871
   676
kpeter@871
   677
  private:
kpeter@871
   678
kpeter@872
   679
    // Initialize the algorithm
kpeter@876
   680
    ProblemType init() {
kpeter@872
   681
      if (_node_num == 0) return INFEASIBLE;
kpeter@871
   682
kpeter@872
   683
      // Check the sum of supply values
kpeter@872
   684
      _sum_supply = 0;
kpeter@872
   685
      for (int i = 0; i != _root; ++i) {
kpeter@872
   686
        _sum_supply += _supply[i];
kpeter@871
   687
      }
kpeter@872
   688
      if (_sum_supply > 0) return INFEASIBLE;
kpeter@872
   689
      
kpeter@872
   690
      // Initialize maps
kpeter@872
   691
      for (int i = 0; i != _root; ++i) {
kpeter@872
   692
        _pi[i] = 0;
kpeter@872
   693
        _excess[i] = _supply[i];
kpeter@871
   694
      }
kpeter@871
   695
kpeter@872
   696
      // Remove non-zero lower bounds
kpeter@872
   697
      if (_have_lower) {
kpeter@872
   698
        for (int i = 0; i != _root; ++i) {
kpeter@872
   699
          for (int j = _first_out[i]; j != _first_out[i+1]; ++j) {
kpeter@872
   700
            if (_forward[j]) {
kpeter@872
   701
              Value c = _lower[j];
kpeter@872
   702
              if (c >= 0) {
kpeter@872
   703
                _res_cap[j] = _upper[j] < INF ? _upper[j] - c : INF;
kpeter@872
   704
              } else {
kpeter@872
   705
                _res_cap[j] = _upper[j] < INF + c ? _upper[j] - c : INF;
kpeter@872
   706
              }
kpeter@872
   707
              _excess[i] -= c;
kpeter@872
   708
              _excess[_target[j]] += c;
kpeter@872
   709
            } else {
kpeter@872
   710
              _res_cap[j] = 0;
kpeter@872
   711
            }
kpeter@872
   712
          }
kpeter@872
   713
        }
kpeter@872
   714
      } else {
kpeter@872
   715
        for (int j = 0; j != _res_arc_num; ++j) {
kpeter@872
   716
          _res_cap[j] = _forward[j] ? _upper[j] : 0;
kpeter@872
   717
        }
kpeter@872
   718
      }
kpeter@871
   719
kpeter@872
   720
      // Handle negative costs
kpeter@872
   721
      for (int u = 0; u != _root; ++u) {
kpeter@872
   722
        for (int a = _first_out[u]; a != _first_out[u+1]; ++a) {
kpeter@872
   723
          Value rc = _res_cap[a];
kpeter@872
   724
          if (_cost[a] < 0 && rc > 0) {
kpeter@872
   725
            if (rc == INF) return UNBOUNDED;
kpeter@872
   726
            _excess[u] -= rc;
kpeter@872
   727
            _excess[_target[a]] += rc;
kpeter@872
   728
            _res_cap[a] = 0;
kpeter@872
   729
            _res_cap[_reverse[a]] += rc;
kpeter@872
   730
          }
kpeter@872
   731
        }
kpeter@872
   732
      }
kpeter@872
   733
      
kpeter@872
   734
      // Handle GEQ supply type
kpeter@872
   735
      if (_sum_supply < 0) {
kpeter@872
   736
        _pi[_root] = 0;
kpeter@872
   737
        _excess[_root] = -_sum_supply;
kpeter@872
   738
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
kpeter@872
   739
          int u = _target[a];
kpeter@872
   740
          if (_excess[u] < 0) {
kpeter@872
   741
            _res_cap[a] = -_excess[u] + 1;
kpeter@872
   742
          } else {
kpeter@872
   743
            _res_cap[a] = 1;
kpeter@872
   744
          }
kpeter@872
   745
          _res_cap[_reverse[a]] = 0;
kpeter@872
   746
          _cost[a] = 0;
kpeter@872
   747
          _cost[_reverse[a]] = 0;
kpeter@872
   748
        }
kpeter@872
   749
      } else {
kpeter@872
   750
        _pi[_root] = 0;
kpeter@872
   751
        _excess[_root] = 0;
kpeter@872
   752
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
kpeter@872
   753
          _res_cap[a] = 1;
kpeter@872
   754
          _res_cap[_reverse[a]] = 0;
kpeter@872
   755
          _cost[a] = 0;
kpeter@872
   756
          _cost[_reverse[a]] = 0;
kpeter@872
   757
        }
kpeter@872
   758
      }
kpeter@872
   759
kpeter@872
   760
      // Initialize delta value
kpeter@876
   761
      if (_factor > 1) {
kpeter@871
   762
        // With scaling
kpeter@872
   763
        Value max_sup = 0, max_dem = 0;
kpeter@872
   764
        for (int i = 0; i != _node_num; ++i) {
kpeter@872
   765
          if ( _excess[i] > max_sup) max_sup =  _excess[i];
kpeter@872
   766
          if (-_excess[i] > max_dem) max_dem = -_excess[i];
kpeter@871
   767
        }
kpeter@872
   768
        Value max_cap = 0;
kpeter@872
   769
        for (int j = 0; j != _res_arc_num; ++j) {
kpeter@872
   770
          if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
kpeter@871
   771
        }
kpeter@871
   772
        max_sup = std::min(std::min(max_sup, max_dem), max_cap);
kpeter@876
   773
        for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
kpeter@871
   774
      } else {
kpeter@871
   775
        // Without scaling
kpeter@871
   776
        _delta = 1;
kpeter@871
   777
      }
kpeter@871
   778
kpeter@872
   779
      return OPTIMAL;
kpeter@871
   780
    }
kpeter@871
   781
kpeter@872
   782
    ProblemType start() {
kpeter@872
   783
      // Execute the algorithm
kpeter@872
   784
      ProblemType pt;
kpeter@871
   785
      if (_delta > 1)
kpeter@872
   786
        pt = startWithScaling();
kpeter@871
   787
      else
kpeter@872
   788
        pt = startWithoutScaling();
kpeter@872
   789
kpeter@872
   790
      // Handle non-zero lower bounds
kpeter@872
   791
      if (_have_lower) {
kpeter@872
   792
        for (int j = 0; j != _res_arc_num - _node_num + 1; ++j) {
kpeter@872
   793
          if (!_forward[j]) _res_cap[j] += _lower[j];
kpeter@872
   794
        }
kpeter@872
   795
      }
kpeter@872
   796
kpeter@872
   797
      // Shift potentials if necessary
kpeter@872
   798
      Cost pr = _pi[_root];
kpeter@872
   799
      if (_sum_supply < 0 || pr > 0) {
kpeter@872
   800
        for (int i = 0; i != _node_num; ++i) {
kpeter@872
   801
          _pi[i] -= pr;
kpeter@872
   802
        }        
kpeter@872
   803
      }
kpeter@872
   804
      
kpeter@872
   805
      return pt;
kpeter@871
   806
    }
kpeter@871
   807
kpeter@872
   808
    // Execute the capacity scaling algorithm
kpeter@872
   809
    ProblemType startWithScaling() {
kpeter@873
   810
      // Perform capacity scaling phases
kpeter@872
   811
      int s, t;
kpeter@872
   812
      ResidualDijkstra _dijkstra(*this);
kpeter@871
   813
      while (true) {
kpeter@872
   814
        // Saturate all arcs not satisfying the optimality condition
kpeter@872
   815
        for (int u = 0; u != _node_num; ++u) {
kpeter@872
   816
          for (int a = _first_out[u]; a != _first_out[u+1]; ++a) {
kpeter@872
   817
            int v = _target[a];
kpeter@872
   818
            Cost c = _cost[a] + _pi[u] - _pi[v];
kpeter@872
   819
            Value rc = _res_cap[a];
kpeter@872
   820
            if (c < 0 && rc >= _delta) {
kpeter@872
   821
              _excess[u] -= rc;
kpeter@872
   822
              _excess[v] += rc;
kpeter@872
   823
              _res_cap[a] = 0;
kpeter@872
   824
              _res_cap[_reverse[a]] += rc;
kpeter@872
   825
            }
kpeter@871
   826
          }
kpeter@871
   827
        }
kpeter@871
   828
kpeter@872
   829
        // Find excess nodes and deficit nodes
kpeter@871
   830
        _excess_nodes.clear();
kpeter@871
   831
        _deficit_nodes.clear();
kpeter@872
   832
        for (int u = 0; u != _node_num; ++u) {
kpeter@872
   833
          if (_excess[u] >=  _delta) _excess_nodes.push_back(u);
kpeter@872
   834
          if (_excess[u] <= -_delta) _deficit_nodes.push_back(u);
kpeter@871
   835
        }
kpeter@871
   836
        int next_node = 0, next_def_node = 0;
kpeter@871
   837
kpeter@872
   838
        // Find augmenting shortest paths
kpeter@871
   839
        while (next_node < int(_excess_nodes.size())) {
kpeter@872
   840
          // Check deficit nodes
kpeter@871
   841
          if (_delta > 1) {
kpeter@871
   842
            bool delta_deficit = false;
kpeter@871
   843
            for ( ; next_def_node < int(_deficit_nodes.size());
kpeter@871
   844
                    ++next_def_node ) {
kpeter@871
   845
              if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
kpeter@871
   846
                delta_deficit = true;
kpeter@871
   847
                break;
kpeter@871
   848
              }
kpeter@871
   849
            }
kpeter@871
   850
            if (!delta_deficit) break;
kpeter@871
   851
          }
kpeter@871
   852
kpeter@872
   853
          // Run Dijkstra in the residual network
kpeter@871
   854
          s = _excess_nodes[next_node];
kpeter@872
   855
          if ((t = _dijkstra.run(s, _delta)) == -1) {
kpeter@871
   856
            if (_delta > 1) {
kpeter@871
   857
              ++next_node;
kpeter@871
   858
              continue;
kpeter@871
   859
            }
kpeter@872
   860
            return INFEASIBLE;
kpeter@871
   861
          }
kpeter@871
   862
kpeter@872
   863
          // Augment along a shortest path from s to t
kpeter@872
   864
          Value d = std::min(_excess[s], -_excess[t]);
kpeter@872
   865
          int u = t;
kpeter@872
   866
          int a;
kpeter@871
   867
          if (d > _delta) {
kpeter@872
   868
            while ((a = _pred[u]) != -1) {
kpeter@872
   869
              if (_res_cap[a] < d) d = _res_cap[a];
kpeter@872
   870
              u = _source[a];
kpeter@871
   871
            }
kpeter@871
   872
          }
kpeter@871
   873
          u = t;
kpeter@872
   874
          while ((a = _pred[u]) != -1) {
kpeter@872
   875
            _res_cap[a] -= d;
kpeter@872
   876
            _res_cap[_reverse[a]] += d;
kpeter@872
   877
            u = _source[a];
kpeter@871
   878
          }
kpeter@871
   879
          _excess[s] -= d;
kpeter@871
   880
          _excess[t] += d;
kpeter@871
   881
kpeter@871
   882
          if (_excess[s] < _delta) ++next_node;
kpeter@871
   883
        }
kpeter@871
   884
kpeter@871
   885
        if (_delta == 1) break;
kpeter@876
   886
        _delta = _delta <= _factor ? 1 : _delta / _factor;
kpeter@871
   887
      }
kpeter@871
   888
kpeter@872
   889
      return OPTIMAL;
kpeter@871
   890
    }
kpeter@871
   891
kpeter@872
   892
    // Execute the successive shortest path algorithm
kpeter@872
   893
    ProblemType startWithoutScaling() {
kpeter@872
   894
      // Find excess nodes
kpeter@872
   895
      _excess_nodes.clear();
kpeter@872
   896
      for (int i = 0; i != _node_num; ++i) {
kpeter@872
   897
        if (_excess[i] > 0) _excess_nodes.push_back(i);
kpeter@872
   898
      }
kpeter@872
   899
      if (_excess_nodes.size() == 0) return OPTIMAL;
kpeter@871
   900
      int next_node = 0;
kpeter@871
   901
kpeter@872
   902
      // Find shortest paths
kpeter@872
   903
      int s, t;
kpeter@872
   904
      ResidualDijkstra _dijkstra(*this);
kpeter@871
   905
      while ( _excess[_excess_nodes[next_node]] > 0 ||
kpeter@871
   906
              ++next_node < int(_excess_nodes.size()) )
kpeter@871
   907
      {
kpeter@872
   908
        // Run Dijkstra in the residual network
kpeter@871
   909
        s = _excess_nodes[next_node];
kpeter@872
   910
        if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE;
kpeter@871
   911
kpeter@872
   912
        // Augment along a shortest path from s to t
kpeter@872
   913
        Value d = std::min(_excess[s], -_excess[t]);
kpeter@872
   914
        int u = t;
kpeter@872
   915
        int a;
kpeter@871
   916
        if (d > 1) {
kpeter@872
   917
          while ((a = _pred[u]) != -1) {
kpeter@872
   918
            if (_res_cap[a] < d) d = _res_cap[a];
kpeter@872
   919
            u = _source[a];
kpeter@871
   920
          }
kpeter@871
   921
        }
kpeter@871
   922
        u = t;
kpeter@872
   923
        while ((a = _pred[u]) != -1) {
kpeter@872
   924
          _res_cap[a] -= d;
kpeter@872
   925
          _res_cap[_reverse[a]] += d;
kpeter@872
   926
          u = _source[a];
kpeter@871
   927
        }
kpeter@871
   928
        _excess[s] -= d;
kpeter@871
   929
        _excess[t] += d;
kpeter@871
   930
      }
kpeter@871
   931
kpeter@872
   932
      return OPTIMAL;
kpeter@871
   933
    }
kpeter@871
   934
kpeter@871
   935
  }; //class CapacityScaling
kpeter@871
   936
kpeter@871
   937
  ///@}
kpeter@871
   938
kpeter@871
   939
} //namespace lemon
kpeter@871
   940
kpeter@871
   941
#endif //LEMON_CAPACITY_SCALING_H