lemon/cost_scaling.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 10 Jan 2011 09:34:50 +0100
changeset 1026 9312d6c89d02
parent 1025 140c953ad5d1
parent 1023 e0cef67fe565
child 1042 773dd96ecdd8
permissions -rw-r--r--
Merge
alpar@956
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@874
     2
 *
alpar@956
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@874
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
kpeter@874
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@874
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@874
     8
 *
kpeter@874
     9
 * Permission to use, modify and distribute this software is granted
kpeter@874
    10
 * provided that this copyright notice appears in all copies. For
kpeter@874
    11
 * precise terms see the accompanying LICENSE file.
kpeter@874
    12
 *
kpeter@874
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@874
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@874
    15
 * purpose.
kpeter@874
    16
 *
kpeter@874
    17
 */
kpeter@874
    18
kpeter@874
    19
#ifndef LEMON_COST_SCALING_H
kpeter@874
    20
#define LEMON_COST_SCALING_H
kpeter@874
    21
kpeter@874
    22
/// \ingroup min_cost_flow_algs
kpeter@874
    23
/// \file
kpeter@874
    24
/// \brief Cost scaling algorithm for finding a minimum cost flow.
kpeter@874
    25
kpeter@874
    26
#include <vector>
kpeter@874
    27
#include <deque>
kpeter@874
    28
#include <limits>
kpeter@874
    29
kpeter@874
    30
#include <lemon/core.h>
kpeter@874
    31
#include <lemon/maps.h>
kpeter@874
    32
#include <lemon/math.h>
kpeter@875
    33
#include <lemon/static_graph.h>
kpeter@874
    34
#include <lemon/circulation.h>
kpeter@874
    35
#include <lemon/bellman_ford.h>
kpeter@874
    36
kpeter@874
    37
namespace lemon {
kpeter@874
    38
kpeter@875
    39
  /// \brief Default traits class of CostScaling algorithm.
kpeter@875
    40
  ///
kpeter@875
    41
  /// Default traits class of CostScaling algorithm.
kpeter@875
    42
  /// \tparam GR Digraph type.
kpeter@878
    43
  /// \tparam V The number type used for flow amounts, capacity bounds
kpeter@875
    44
  /// and supply values. By default it is \c int.
kpeter@878
    45
  /// \tparam C The number type used for costs and potentials.
kpeter@875
    46
  /// By default it is the same as \c V.
kpeter@875
    47
#ifdef DOXYGEN
kpeter@875
    48
  template <typename GR, typename V = int, typename C = V>
kpeter@875
    49
#else
kpeter@875
    50
  template < typename GR, typename V = int, typename C = V,
kpeter@875
    51
             bool integer = std::numeric_limits<C>::is_integer >
kpeter@875
    52
#endif
kpeter@875
    53
  struct CostScalingDefaultTraits
kpeter@875
    54
  {
kpeter@875
    55
    /// The type of the digraph
kpeter@875
    56
    typedef GR Digraph;
kpeter@875
    57
    /// The type of the flow amounts, capacity bounds and supply values
kpeter@875
    58
    typedef V Value;
kpeter@875
    59
    /// The type of the arc costs
kpeter@875
    60
    typedef C Cost;
kpeter@875
    61
kpeter@875
    62
    /// \brief The large cost type used for internal computations
kpeter@875
    63
    ///
kpeter@875
    64
    /// The large cost type used for internal computations.
kpeter@875
    65
    /// It is \c long \c long if the \c Cost type is integer,
kpeter@875
    66
    /// otherwise it is \c double.
kpeter@875
    67
    /// \c Cost must be convertible to \c LargeCost.
kpeter@875
    68
    typedef double LargeCost;
kpeter@875
    69
  };
kpeter@875
    70
kpeter@875
    71
  // Default traits class for integer cost types
kpeter@875
    72
  template <typename GR, typename V, typename C>
kpeter@875
    73
  struct CostScalingDefaultTraits<GR, V, C, true>
kpeter@875
    74
  {
kpeter@875
    75
    typedef GR Digraph;
kpeter@875
    76
    typedef V Value;
kpeter@875
    77
    typedef C Cost;
kpeter@875
    78
#ifdef LEMON_HAVE_LONG_LONG
kpeter@875
    79
    typedef long long LargeCost;
kpeter@875
    80
#else
kpeter@875
    81
    typedef long LargeCost;
kpeter@875
    82
#endif
kpeter@875
    83
  };
kpeter@875
    84
kpeter@875
    85
kpeter@874
    86
  /// \addtogroup min_cost_flow_algs
kpeter@874
    87
  /// @{
kpeter@874
    88
kpeter@875
    89
  /// \brief Implementation of the Cost Scaling algorithm for
kpeter@875
    90
  /// finding a \ref min_cost_flow "minimum cost flow".
kpeter@874
    91
  ///
kpeter@875
    92
  /// \ref CostScaling implements a cost scaling algorithm that performs
kpeter@879
    93
  /// push/augment and relabel operations for finding a \ref min_cost_flow
kpeter@879
    94
  /// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation,
alpar@956
    95
  /// \ref goldberg97efficient, \ref bunnagel98efficient.
kpeter@879
    96
  /// It is a highly efficient primal-dual solution method, which
kpeter@875
    97
  /// can be viewed as the generalization of the \ref Preflow
kpeter@875
    98
  /// "preflow push-relabel" algorithm for the maximum flow problem.
kpeter@874
    99
  ///
kpeter@1023
   100
  /// In general, \ref NetworkSimplex and \ref CostScaling are the fastest
kpeter@1023
   101
  /// implementations available in LEMON for this problem.
kpeter@1023
   102
  ///
kpeter@875
   103
  /// Most of the parameters of the problem (except for the digraph)
kpeter@875
   104
  /// can be given using separate functions, and the algorithm can be
kpeter@875
   105
  /// executed using the \ref run() function. If some parameters are not
kpeter@875
   106
  /// specified, then default values will be used.
kpeter@874
   107
  ///
kpeter@875
   108
  /// \tparam GR The digraph type the algorithm runs on.
kpeter@878
   109
  /// \tparam V The number type used for flow amounts, capacity bounds
kpeter@891
   110
  /// and supply values in the algorithm. By default, it is \c int.
kpeter@878
   111
  /// \tparam C The number type used for costs and potentials in the
kpeter@891
   112
  /// algorithm. By default, it is the same as \c V.
kpeter@891
   113
  /// \tparam TR The traits class that defines various types used by the
kpeter@891
   114
  /// algorithm. By default, it is \ref CostScalingDefaultTraits
kpeter@891
   115
  /// "CostScalingDefaultTraits<GR, V, C>".
kpeter@891
   116
  /// In most cases, this parameter should not be set directly,
kpeter@891
   117
  /// consider to use the named template parameters instead.
kpeter@874
   118
  ///
kpeter@1025
   119
  /// \warning Both \c V and \c C must be signed number types.
kpeter@1025
   120
  /// \warning All input data (capacities, supply values, and costs) must
kpeter@875
   121
  /// be integer.
kpeter@1023
   122
  /// \warning This algorithm does not support negative costs for
kpeter@1023
   123
  /// arcs having infinite upper bound.
kpeter@876
   124
  ///
kpeter@876
   125
  /// \note %CostScaling provides three different internal methods,
kpeter@876
   126
  /// from which the most efficient one is used by default.
kpeter@876
   127
  /// For more information, see \ref Method.
kpeter@875
   128
#ifdef DOXYGEN
kpeter@875
   129
  template <typename GR, typename V, typename C, typename TR>
kpeter@875
   130
#else
kpeter@875
   131
  template < typename GR, typename V = int, typename C = V,
kpeter@875
   132
             typename TR = CostScalingDefaultTraits<GR, V, C> >
kpeter@875
   133
#endif
kpeter@874
   134
  class CostScaling
kpeter@874
   135
  {
kpeter@875
   136
  public:
kpeter@874
   137
kpeter@875
   138
    /// The type of the digraph
kpeter@875
   139
    typedef typename TR::Digraph Digraph;
kpeter@875
   140
    /// The type of the flow amounts, capacity bounds and supply values
kpeter@875
   141
    typedef typename TR::Value Value;
kpeter@875
   142
    /// The type of the arc costs
kpeter@875
   143
    typedef typename TR::Cost Cost;
kpeter@874
   144
kpeter@875
   145
    /// \brief The large cost type
kpeter@875
   146
    ///
kpeter@875
   147
    /// The large cost type used for internal computations.
kpeter@891
   148
    /// By default, it is \c long \c long if the \c Cost type is integer,
kpeter@875
   149
    /// otherwise it is \c double.
kpeter@875
   150
    typedef typename TR::LargeCost LargeCost;
kpeter@874
   151
kpeter@875
   152
    /// The \ref CostScalingDefaultTraits "traits class" of the algorithm
kpeter@875
   153
    typedef TR Traits;
kpeter@874
   154
kpeter@874
   155
  public:
kpeter@874
   156
kpeter@875
   157
    /// \brief Problem type constants for the \c run() function.
kpeter@875
   158
    ///
kpeter@875
   159
    /// Enum type containing the problem type constants that can be
kpeter@875
   160
    /// returned by the \ref run() function of the algorithm.
kpeter@875
   161
    enum ProblemType {
kpeter@875
   162
      /// The problem has no feasible solution (flow).
kpeter@875
   163
      INFEASIBLE,
kpeter@875
   164
      /// The problem has optimal solution (i.e. it is feasible and
kpeter@875
   165
      /// bounded), and the algorithm has found optimal flow and node
kpeter@875
   166
      /// potentials (primal and dual solutions).
kpeter@875
   167
      OPTIMAL,
kpeter@875
   168
      /// The digraph contains an arc of negative cost and infinite
kpeter@875
   169
      /// upper bound. It means that the objective function is unbounded
kpeter@878
   170
      /// on that arc, however, note that it could actually be bounded
kpeter@875
   171
      /// over the feasible flows, but this algroithm cannot handle
kpeter@875
   172
      /// these cases.
kpeter@875
   173
      UNBOUNDED
kpeter@875
   174
    };
kpeter@874
   175
kpeter@876
   176
    /// \brief Constants for selecting the internal method.
kpeter@876
   177
    ///
kpeter@876
   178
    /// Enum type containing constants for selecting the internal method
kpeter@876
   179
    /// for the \ref run() function.
kpeter@876
   180
    ///
kpeter@876
   181
    /// \ref CostScaling provides three internal methods that differ mainly
kpeter@876
   182
    /// in their base operations, which are used in conjunction with the
kpeter@876
   183
    /// relabel operation.
kpeter@876
   184
    /// By default, the so called \ref PARTIAL_AUGMENT
kpeter@1023
   185
    /// "Partial Augment-Relabel" method is used, which turned out to be
kpeter@876
   186
    /// the most efficient and the most robust on various test inputs.
kpeter@876
   187
    /// However, the other methods can be selected using the \ref run()
kpeter@876
   188
    /// function with the proper parameter.
kpeter@876
   189
    enum Method {
kpeter@876
   190
      /// Local push operations are used, i.e. flow is moved only on one
kpeter@876
   191
      /// admissible arc at once.
kpeter@876
   192
      PUSH,
kpeter@876
   193
      /// Augment operations are used, i.e. flow is moved on admissible
kpeter@876
   194
      /// paths from a node with excess to a node with deficit.
kpeter@876
   195
      AUGMENT,
alpar@956
   196
      /// Partial augment operations are used, i.e. flow is moved on
kpeter@876
   197
      /// admissible paths started from a node with excess, but the
kpeter@876
   198
      /// lengths of these paths are limited. This method can be viewed
kpeter@876
   199
      /// as a combined version of the previous two operations.
kpeter@876
   200
      PARTIAL_AUGMENT
kpeter@876
   201
    };
kpeter@876
   202
kpeter@874
   203
  private:
kpeter@874
   204
kpeter@875
   205
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
kpeter@874
   206
kpeter@875
   207
    typedef std::vector<int> IntVector;
kpeter@875
   208
    typedef std::vector<Value> ValueVector;
kpeter@875
   209
    typedef std::vector<Cost> CostVector;
kpeter@875
   210
    typedef std::vector<LargeCost> LargeCostVector;
kpeter@910
   211
    typedef std::vector<char> BoolVector;
kpeter@910
   212
    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
kpeter@874
   213
kpeter@875
   214
  private:
alpar@956
   215
kpeter@875
   216
    template <typename KT, typename VT>
kpeter@886
   217
    class StaticVectorMap {
kpeter@874
   218
    public:
kpeter@875
   219
      typedef KT Key;
kpeter@875
   220
      typedef VT Value;
alpar@956
   221
kpeter@886
   222
      StaticVectorMap(std::vector<Value>& v) : _v(v) {}
alpar@956
   223
kpeter@875
   224
      const Value& operator[](const Key& key) const {
kpeter@875
   225
        return _v[StaticDigraph::id(key)];
kpeter@874
   226
      }
kpeter@874
   227
kpeter@875
   228
      Value& operator[](const Key& key) {
kpeter@875
   229
        return _v[StaticDigraph::id(key)];
kpeter@875
   230
      }
alpar@956
   231
kpeter@875
   232
      void set(const Key& key, const Value& val) {
kpeter@875
   233
        _v[StaticDigraph::id(key)] = val;
kpeter@874
   234
      }
kpeter@874
   235
kpeter@875
   236
    private:
kpeter@875
   237
      std::vector<Value>& _v;
kpeter@875
   238
    };
kpeter@875
   239
kpeter@886
   240
    typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap;
kpeter@886
   241
    typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap;
kpeter@874
   242
kpeter@874
   243
  private:
kpeter@874
   244
kpeter@875
   245
    // Data related to the underlying digraph
kpeter@875
   246
    const GR &_graph;
kpeter@875
   247
    int _node_num;
kpeter@875
   248
    int _arc_num;
kpeter@875
   249
    int _res_node_num;
kpeter@875
   250
    int _res_arc_num;
kpeter@875
   251
    int _root;
kpeter@874
   252
kpeter@875
   253
    // Parameters of the problem
kpeter@875
   254
    bool _have_lower;
kpeter@875
   255
    Value _sum_supply;
kpeter@910
   256
    int _sup_node_num;
kpeter@874
   257
kpeter@875
   258
    // Data structures for storing the digraph
kpeter@875
   259
    IntNodeMap _node_id;
kpeter@875
   260
    IntArcMap _arc_idf;
kpeter@875
   261
    IntArcMap _arc_idb;
kpeter@875
   262
    IntVector _first_out;
kpeter@875
   263
    BoolVector _forward;
kpeter@875
   264
    IntVector _source;
kpeter@875
   265
    IntVector _target;
kpeter@875
   266
    IntVector _reverse;
kpeter@875
   267
kpeter@875
   268
    // Node and arc data
kpeter@875
   269
    ValueVector _lower;
kpeter@875
   270
    ValueVector _upper;
kpeter@875
   271
    CostVector _scost;
kpeter@875
   272
    ValueVector _supply;
kpeter@875
   273
kpeter@875
   274
    ValueVector _res_cap;
kpeter@875
   275
    LargeCostVector _cost;
kpeter@875
   276
    LargeCostVector _pi;
kpeter@875
   277
    ValueVector _excess;
kpeter@875
   278
    IntVector _next_out;
kpeter@875
   279
    std::deque<int> _active_nodes;
kpeter@875
   280
kpeter@875
   281
    // Data for scaling
kpeter@875
   282
    LargeCost _epsilon;
kpeter@874
   283
    int _alpha;
kpeter@874
   284
kpeter@910
   285
    IntVector _buckets;
kpeter@910
   286
    IntVector _bucket_next;
kpeter@910
   287
    IntVector _bucket_prev;
kpeter@910
   288
    IntVector _rank;
kpeter@910
   289
    int _max_rank;
alpar@956
   290
kpeter@875
   291
    // Data for a StaticDigraph structure
kpeter@875
   292
    typedef std::pair<int, int> IntPair;
kpeter@875
   293
    StaticDigraph _sgr;
kpeter@875
   294
    std::vector<IntPair> _arc_vec;
kpeter@875
   295
    std::vector<LargeCost> _cost_vec;
kpeter@875
   296
    LargeCostArcMap _cost_map;
kpeter@875
   297
    LargeCostNodeMap _pi_map;
alpar@956
   298
kpeter@875
   299
  public:
alpar@956
   300
kpeter@875
   301
    /// \brief Constant for infinite upper bounds (capacities).
kpeter@875
   302
    ///
kpeter@875
   303
    /// Constant for infinite upper bounds (capacities).
kpeter@875
   304
    /// It is \c std::numeric_limits<Value>::infinity() if available,
kpeter@875
   305
    /// \c std::numeric_limits<Value>::max() otherwise.
kpeter@875
   306
    const Value INF;
kpeter@875
   307
kpeter@874
   308
  public:
kpeter@874
   309
kpeter@875
   310
    /// \name Named Template Parameters
kpeter@875
   311
    /// @{
kpeter@875
   312
kpeter@875
   313
    template <typename T>
kpeter@875
   314
    struct SetLargeCostTraits : public Traits {
kpeter@875
   315
      typedef T LargeCost;
kpeter@875
   316
    };
kpeter@875
   317
kpeter@875
   318
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@875
   319
    /// \c LargeCost type.
kpeter@874
   320
    ///
kpeter@875
   321
    /// \ref named-templ-param "Named parameter" for setting \c LargeCost
kpeter@875
   322
    /// type, which is used for internal computations in the algorithm.
kpeter@875
   323
    /// \c Cost must be convertible to \c LargeCost.
kpeter@875
   324
    template <typename T>
kpeter@875
   325
    struct SetLargeCost
kpeter@875
   326
      : public CostScaling<GR, V, C, SetLargeCostTraits<T> > {
kpeter@875
   327
      typedef  CostScaling<GR, V, C, SetLargeCostTraits<T> > Create;
kpeter@875
   328
    };
kpeter@875
   329
kpeter@875
   330
    /// @}
kpeter@875
   331
kpeter@941
   332
  protected:
kpeter@941
   333
kpeter@941
   334
    CostScaling() {}
kpeter@941
   335
kpeter@875
   336
  public:
kpeter@875
   337
kpeter@875
   338
    /// \brief Constructor.
kpeter@874
   339
    ///
kpeter@875
   340
    /// The constructor of the class.
kpeter@875
   341
    ///
kpeter@875
   342
    /// \param graph The digraph the algorithm runs on.
kpeter@875
   343
    CostScaling(const GR& graph) :
kpeter@875
   344
      _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
kpeter@875
   345
      _cost_map(_cost_vec), _pi_map(_pi),
kpeter@875
   346
      INF(std::numeric_limits<Value>::has_infinity ?
kpeter@875
   347
          std::numeric_limits<Value>::infinity() :
kpeter@875
   348
          std::numeric_limits<Value>::max())
kpeter@874
   349
    {
kpeter@878
   350
      // Check the number types
kpeter@875
   351
      LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
kpeter@875
   352
        "The flow type of CostScaling must be signed");
kpeter@875
   353
      LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
kpeter@875
   354
        "The cost type of CostScaling must be signed");
alpar@956
   355
kpeter@898
   356
      // Reset data structures
kpeter@875
   357
      reset();
kpeter@874
   358
    }
kpeter@874
   359
kpeter@875
   360
    /// \name Parameters
kpeter@875
   361
    /// The parameters of the algorithm can be specified using these
kpeter@875
   362
    /// functions.
kpeter@875
   363
kpeter@875
   364
    /// @{
kpeter@875
   365
kpeter@875
   366
    /// \brief Set the lower bounds on the arcs.
kpeter@874
   367
    ///
kpeter@875
   368
    /// This function sets the lower bounds on the arcs.
kpeter@875
   369
    /// If it is not used before calling \ref run(), the lower bounds
kpeter@875
   370
    /// will be set to zero on all arcs.
kpeter@874
   371
    ///
kpeter@875
   372
    /// \param map An arc map storing the lower bounds.
kpeter@875
   373
    /// Its \c Value type must be convertible to the \c Value type
kpeter@875
   374
    /// of the algorithm.
kpeter@875
   375
    ///
kpeter@875
   376
    /// \return <tt>(*this)</tt>
kpeter@875
   377
    template <typename LowerMap>
kpeter@875
   378
    CostScaling& lowerMap(const LowerMap& map) {
kpeter@875
   379
      _have_lower = true;
kpeter@875
   380
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   381
        _lower[_arc_idf[a]] = map[a];
kpeter@875
   382
        _lower[_arc_idb[a]] = map[a];
kpeter@874
   383
      }
kpeter@874
   384
      return *this;
kpeter@874
   385
    }
kpeter@874
   386
kpeter@875
   387
    /// \brief Set the upper bounds (capacities) on the arcs.
kpeter@874
   388
    ///
kpeter@875
   389
    /// This function sets the upper bounds (capacities) on the arcs.
kpeter@875
   390
    /// If it is not used before calling \ref run(), the upper bounds
kpeter@875
   391
    /// will be set to \ref INF on all arcs (i.e. the flow value will be
kpeter@878
   392
    /// unbounded from above).
kpeter@874
   393
    ///
kpeter@875
   394
    /// \param map An arc map storing the upper bounds.
kpeter@875
   395
    /// Its \c Value type must be convertible to the \c Value type
kpeter@875
   396
    /// of the algorithm.
kpeter@875
   397
    ///
kpeter@875
   398
    /// \return <tt>(*this)</tt>
kpeter@875
   399
    template<typename UpperMap>
kpeter@875
   400
    CostScaling& upperMap(const UpperMap& map) {
kpeter@875
   401
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   402
        _upper[_arc_idf[a]] = map[a];
kpeter@874
   403
      }
kpeter@874
   404
      return *this;
kpeter@874
   405
    }
kpeter@874
   406
kpeter@875
   407
    /// \brief Set the costs of the arcs.
kpeter@875
   408
    ///
kpeter@875
   409
    /// This function sets the costs of the arcs.
kpeter@875
   410
    /// If it is not used before calling \ref run(), the costs
kpeter@875
   411
    /// will be set to \c 1 on all arcs.
kpeter@875
   412
    ///
kpeter@875
   413
    /// \param map An arc map storing the costs.
kpeter@875
   414
    /// Its \c Value type must be convertible to the \c Cost type
kpeter@875
   415
    /// of the algorithm.
kpeter@875
   416
    ///
kpeter@875
   417
    /// \return <tt>(*this)</tt>
kpeter@875
   418
    template<typename CostMap>
kpeter@875
   419
    CostScaling& costMap(const CostMap& map) {
kpeter@875
   420
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   421
        _scost[_arc_idf[a]] =  map[a];
kpeter@875
   422
        _scost[_arc_idb[a]] = -map[a];
kpeter@875
   423
      }
kpeter@875
   424
      return *this;
kpeter@875
   425
    }
kpeter@875
   426
kpeter@875
   427
    /// \brief Set the supply values of the nodes.
kpeter@875
   428
    ///
kpeter@875
   429
    /// This function sets the supply values of the nodes.
kpeter@875
   430
    /// If neither this function nor \ref stSupply() is used before
kpeter@875
   431
    /// calling \ref run(), the supply of each node will be set to zero.
kpeter@875
   432
    ///
kpeter@875
   433
    /// \param map A node map storing the supply values.
kpeter@875
   434
    /// Its \c Value type must be convertible to the \c Value type
kpeter@875
   435
    /// of the algorithm.
kpeter@875
   436
    ///
kpeter@875
   437
    /// \return <tt>(*this)</tt>
kpeter@875
   438
    template<typename SupplyMap>
kpeter@875
   439
    CostScaling& supplyMap(const SupplyMap& map) {
kpeter@875
   440
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@875
   441
        _supply[_node_id[n]] = map[n];
kpeter@875
   442
      }
kpeter@875
   443
      return *this;
kpeter@875
   444
    }
kpeter@875
   445
kpeter@875
   446
    /// \brief Set single source and target nodes and a supply value.
kpeter@875
   447
    ///
kpeter@875
   448
    /// This function sets a single source node and a single target node
kpeter@875
   449
    /// and the required flow value.
kpeter@875
   450
    /// If neither this function nor \ref supplyMap() is used before
kpeter@875
   451
    /// calling \ref run(), the supply of each node will be set to zero.
kpeter@875
   452
    ///
kpeter@875
   453
    /// Using this function has the same effect as using \ref supplyMap()
kpeter@1023
   454
    /// with a map in which \c k is assigned to \c s, \c -k is
kpeter@875
   455
    /// assigned to \c t and all other nodes have zero supply value.
kpeter@875
   456
    ///
kpeter@875
   457
    /// \param s The source node.
kpeter@875
   458
    /// \param t The target node.
kpeter@875
   459
    /// \param k The required amount of flow from node \c s to node \c t
kpeter@875
   460
    /// (i.e. the supply of \c s and the demand of \c t).
kpeter@875
   461
    ///
kpeter@875
   462
    /// \return <tt>(*this)</tt>
kpeter@875
   463
    CostScaling& stSupply(const Node& s, const Node& t, Value k) {
kpeter@875
   464
      for (int i = 0; i != _res_node_num; ++i) {
kpeter@875
   465
        _supply[i] = 0;
kpeter@875
   466
      }
kpeter@875
   467
      _supply[_node_id[s]] =  k;
kpeter@875
   468
      _supply[_node_id[t]] = -k;
kpeter@875
   469
      return *this;
kpeter@875
   470
    }
alpar@956
   471
kpeter@875
   472
    /// @}
kpeter@875
   473
kpeter@874
   474
    /// \name Execution control
kpeter@875
   475
    /// The algorithm can be executed using \ref run().
kpeter@874
   476
kpeter@874
   477
    /// @{
kpeter@874
   478
kpeter@874
   479
    /// \brief Run the algorithm.
kpeter@874
   480
    ///
kpeter@875
   481
    /// This function runs the algorithm.
kpeter@875
   482
    /// The paramters can be specified using functions \ref lowerMap(),
kpeter@875
   483
    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
kpeter@875
   484
    /// For example,
kpeter@875
   485
    /// \code
kpeter@875
   486
    ///   CostScaling<ListDigraph> cs(graph);
kpeter@875
   487
    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
kpeter@875
   488
    ///     .supplyMap(sup).run();
kpeter@875
   489
    /// \endcode
kpeter@875
   490
    ///
kpeter@898
   491
    /// This function can be called more than once. All the given parameters
kpeter@898
   492
    /// are kept for the next call, unless \ref resetParams() or \ref reset()
kpeter@898
   493
    /// is used, thus only the modified parameters have to be set again.
kpeter@898
   494
    /// If the underlying digraph was also modified after the construction
kpeter@898
   495
    /// of the class (or the last \ref reset() call), then the \ref reset()
kpeter@898
   496
    /// function must be called.
kpeter@874
   497
    ///
kpeter@876
   498
    /// \param method The internal method that will be used in the
kpeter@876
   499
    /// algorithm. For more information, see \ref Method.
kpeter@876
   500
    /// \param factor The cost scaling factor. It must be larger than one.
kpeter@874
   501
    ///
kpeter@875
   502
    /// \return \c INFEASIBLE if no feasible flow exists,
kpeter@875
   503
    /// \n \c OPTIMAL if the problem has optimal solution
kpeter@875
   504
    /// (i.e. it is feasible and bounded), and the algorithm has found
kpeter@875
   505
    /// optimal flow and node potentials (primal and dual solutions),
kpeter@875
   506
    /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
kpeter@875
   507
    /// and infinite upper bound. It means that the objective function
kpeter@878
   508
    /// is unbounded on that arc, however, note that it could actually be
kpeter@875
   509
    /// bounded over the feasible flows, but this algroithm cannot handle
kpeter@875
   510
    /// these cases.
kpeter@875
   511
    ///
kpeter@876
   512
    /// \see ProblemType, Method
kpeter@898
   513
    /// \see resetParams(), reset()
kpeter@876
   514
    ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
kpeter@876
   515
      _alpha = factor;
kpeter@875
   516
      ProblemType pt = init();
kpeter@875
   517
      if (pt != OPTIMAL) return pt;
kpeter@876
   518
      start(method);
kpeter@875
   519
      return OPTIMAL;
kpeter@875
   520
    }
kpeter@875
   521
kpeter@875
   522
    /// \brief Reset all the parameters that have been given before.
kpeter@875
   523
    ///
kpeter@875
   524
    /// This function resets all the paramaters that have been given
kpeter@875
   525
    /// before using functions \ref lowerMap(), \ref upperMap(),
kpeter@875
   526
    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
kpeter@875
   527
    ///
kpeter@898
   528
    /// It is useful for multiple \ref run() calls. Basically, all the given
kpeter@898
   529
    /// parameters are kept for the next \ref run() call, unless
kpeter@898
   530
    /// \ref resetParams() or \ref reset() is used.
kpeter@898
   531
    /// If the underlying digraph was also modified after the construction
kpeter@898
   532
    /// of the class or the last \ref reset() call, then the \ref reset()
kpeter@898
   533
    /// function must be used, otherwise \ref resetParams() is sufficient.
kpeter@875
   534
    ///
kpeter@875
   535
    /// For example,
kpeter@875
   536
    /// \code
kpeter@875
   537
    ///   CostScaling<ListDigraph> cs(graph);
kpeter@875
   538
    ///
kpeter@875
   539
    ///   // First run
kpeter@875
   540
    ///   cs.lowerMap(lower).upperMap(upper).costMap(cost)
kpeter@875
   541
    ///     .supplyMap(sup).run();
kpeter@875
   542
    ///
kpeter@898
   543
    ///   // Run again with modified cost map (resetParams() is not called,
kpeter@875
   544
    ///   // so only the cost map have to be set again)
kpeter@875
   545
    ///   cost[e] += 100;
kpeter@875
   546
    ///   cs.costMap(cost).run();
kpeter@875
   547
    ///
kpeter@898
   548
    ///   // Run again from scratch using resetParams()
kpeter@875
   549
    ///   // (the lower bounds will be set to zero on all arcs)
kpeter@898
   550
    ///   cs.resetParams();
kpeter@875
   551
    ///   cs.upperMap(capacity).costMap(cost)
kpeter@875
   552
    ///     .supplyMap(sup).run();
kpeter@875
   553
    /// \endcode
kpeter@875
   554
    ///
kpeter@875
   555
    /// \return <tt>(*this)</tt>
kpeter@898
   556
    ///
kpeter@898
   557
    /// \see reset(), run()
kpeter@898
   558
    CostScaling& resetParams() {
kpeter@875
   559
      for (int i = 0; i != _res_node_num; ++i) {
kpeter@875
   560
        _supply[i] = 0;
kpeter@874
   561
      }
kpeter@875
   562
      int limit = _first_out[_root];
kpeter@875
   563
      for (int j = 0; j != limit; ++j) {
kpeter@875
   564
        _lower[j] = 0;
kpeter@875
   565
        _upper[j] = INF;
kpeter@875
   566
        _scost[j] = _forward[j] ? 1 : -1;
kpeter@875
   567
      }
kpeter@875
   568
      for (int j = limit; j != _res_arc_num; ++j) {
kpeter@875
   569
        _lower[j] = 0;
kpeter@875
   570
        _upper[j] = INF;
kpeter@875
   571
        _scost[j] = 0;
kpeter@875
   572
        _scost[_reverse[j]] = 0;
alpar@956
   573
      }
kpeter@875
   574
      _have_lower = false;
kpeter@875
   575
      return *this;
kpeter@874
   576
    }
kpeter@874
   577
kpeter@898
   578
    /// \brief Reset all the parameters that have been given before.
kpeter@898
   579
    ///
kpeter@898
   580
    /// This function resets all the paramaters that have been given
kpeter@898
   581
    /// before using functions \ref lowerMap(), \ref upperMap(),
kpeter@898
   582
    /// \ref costMap(), \ref supplyMap(), \ref stSupply().
kpeter@898
   583
    ///
kpeter@898
   584
    /// It is useful for multiple run() calls. If this function is not
kpeter@898
   585
    /// used, all the parameters given before are kept for the next
kpeter@898
   586
    /// \ref run() call.
kpeter@898
   587
    /// However, the underlying digraph must not be modified after this
kpeter@898
   588
    /// class have been constructed, since it copies and extends the graph.
kpeter@898
   589
    /// \return <tt>(*this)</tt>
kpeter@898
   590
    CostScaling& reset() {
kpeter@898
   591
      // Resize vectors
kpeter@898
   592
      _node_num = countNodes(_graph);
kpeter@898
   593
      _arc_num = countArcs(_graph);
kpeter@898
   594
      _res_node_num = _node_num + 1;
kpeter@898
   595
      _res_arc_num = 2 * (_arc_num + _node_num);
kpeter@898
   596
      _root = _node_num;
kpeter@898
   597
kpeter@898
   598
      _first_out.resize(_res_node_num + 1);
kpeter@898
   599
      _forward.resize(_res_arc_num);
kpeter@898
   600
      _source.resize(_res_arc_num);
kpeter@898
   601
      _target.resize(_res_arc_num);
kpeter@898
   602
      _reverse.resize(_res_arc_num);
kpeter@898
   603
kpeter@898
   604
      _lower.resize(_res_arc_num);
kpeter@898
   605
      _upper.resize(_res_arc_num);
kpeter@898
   606
      _scost.resize(_res_arc_num);
kpeter@898
   607
      _supply.resize(_res_node_num);
alpar@956
   608
kpeter@898
   609
      _res_cap.resize(_res_arc_num);
kpeter@898
   610
      _cost.resize(_res_arc_num);
kpeter@898
   611
      _pi.resize(_res_node_num);
kpeter@898
   612
      _excess.resize(_res_node_num);
kpeter@898
   613
      _next_out.resize(_res_node_num);
kpeter@898
   614
kpeter@898
   615
      _arc_vec.reserve(_res_arc_num);
kpeter@898
   616
      _cost_vec.reserve(_res_arc_num);
kpeter@898
   617
kpeter@898
   618
      // Copy the graph
kpeter@898
   619
      int i = 0, j = 0, k = 2 * _arc_num + _node_num;
kpeter@898
   620
      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
kpeter@898
   621
        _node_id[n] = i;
kpeter@898
   622
      }
kpeter@898
   623
      i = 0;
kpeter@898
   624
      for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
kpeter@898
   625
        _first_out[i] = j;
kpeter@898
   626
        for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
kpeter@898
   627
          _arc_idf[a] = j;
kpeter@898
   628
          _forward[j] = true;
kpeter@898
   629
          _source[j] = i;
kpeter@898
   630
          _target[j] = _node_id[_graph.runningNode(a)];
kpeter@898
   631
        }
kpeter@898
   632
        for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
kpeter@898
   633
          _arc_idb[a] = j;
kpeter@898
   634
          _forward[j] = false;
kpeter@898
   635
          _source[j] = i;
kpeter@898
   636
          _target[j] = _node_id[_graph.runningNode(a)];
kpeter@898
   637
        }
kpeter@898
   638
        _forward[j] = false;
kpeter@898
   639
        _source[j] = i;
kpeter@898
   640
        _target[j] = _root;
kpeter@898
   641
        _reverse[j] = k;
kpeter@898
   642
        _forward[k] = true;
kpeter@898
   643
        _source[k] = _root;
kpeter@898
   644
        _target[k] = i;
kpeter@898
   645
        _reverse[k] = j;
kpeter@898
   646
        ++j; ++k;
kpeter@898
   647
      }
kpeter@898
   648
      _first_out[i] = j;
kpeter@898
   649
      _first_out[_res_node_num] = k;
kpeter@898
   650
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@898
   651
        int fi = _arc_idf[a];
kpeter@898
   652
        int bi = _arc_idb[a];
kpeter@898
   653
        _reverse[fi] = bi;
kpeter@898
   654
        _reverse[bi] = fi;
kpeter@898
   655
      }
alpar@956
   656
kpeter@898
   657
      // Reset parameters
kpeter@898
   658
      resetParams();
kpeter@898
   659
      return *this;
kpeter@898
   660
    }
kpeter@898
   661
kpeter@874
   662
    /// @}
kpeter@874
   663
kpeter@874
   664
    /// \name Query Functions
kpeter@875
   665
    /// The results of the algorithm can be obtained using these
kpeter@874
   666
    /// functions.\n
kpeter@875
   667
    /// The \ref run() function must be called before using them.
kpeter@874
   668
kpeter@874
   669
    /// @{
kpeter@874
   670
kpeter@875
   671
    /// \brief Return the total cost of the found flow.
kpeter@874
   672
    ///
kpeter@875
   673
    /// This function returns the total cost of the found flow.
kpeter@875
   674
    /// Its complexity is O(e).
kpeter@875
   675
    ///
kpeter@875
   676
    /// \note The return type of the function can be specified as a
kpeter@875
   677
    /// template parameter. For example,
kpeter@875
   678
    /// \code
kpeter@875
   679
    ///   cs.totalCost<double>();
kpeter@875
   680
    /// \endcode
kpeter@875
   681
    /// It is useful if the total cost cannot be stored in the \c Cost
kpeter@875
   682
    /// type of the algorithm, which is the default return type of the
kpeter@875
   683
    /// function.
kpeter@874
   684
    ///
kpeter@874
   685
    /// \pre \ref run() must be called before using this function.
kpeter@875
   686
    template <typename Number>
kpeter@875
   687
    Number totalCost() const {
kpeter@875
   688
      Number c = 0;
kpeter@875
   689
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   690
        int i = _arc_idb[a];
kpeter@875
   691
        c += static_cast<Number>(_res_cap[i]) *
kpeter@875
   692
             (-static_cast<Number>(_scost[i]));
kpeter@875
   693
      }
kpeter@875
   694
      return c;
kpeter@874
   695
    }
kpeter@874
   696
kpeter@875
   697
#ifndef DOXYGEN
kpeter@875
   698
    Cost totalCost() const {
kpeter@875
   699
      return totalCost<Cost>();
kpeter@874
   700
    }
kpeter@875
   701
#endif
kpeter@874
   702
kpeter@874
   703
    /// \brief Return the flow on the given arc.
kpeter@874
   704
    ///
kpeter@875
   705
    /// This function returns the flow on the given arc.
kpeter@874
   706
    ///
kpeter@874
   707
    /// \pre \ref run() must be called before using this function.
kpeter@875
   708
    Value flow(const Arc& a) const {
kpeter@875
   709
      return _res_cap[_arc_idb[a]];
kpeter@874
   710
    }
kpeter@874
   711
kpeter@875
   712
    /// \brief Return the flow map (the primal solution).
kpeter@874
   713
    ///
kpeter@875
   714
    /// This function copies the flow value on each arc into the given
kpeter@875
   715
    /// map. The \c Value type of the algorithm must be convertible to
kpeter@875
   716
    /// the \c Value type of the map.
kpeter@874
   717
    ///
kpeter@874
   718
    /// \pre \ref run() must be called before using this function.
kpeter@875
   719
    template <typename FlowMap>
kpeter@875
   720
    void flowMap(FlowMap &map) const {
kpeter@875
   721
      for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   722
        map.set(a, _res_cap[_arc_idb[a]]);
kpeter@875
   723
      }
kpeter@874
   724
    }
kpeter@874
   725
kpeter@875
   726
    /// \brief Return the potential (dual value) of the given node.
kpeter@874
   727
    ///
kpeter@875
   728
    /// This function returns the potential (dual value) of the
kpeter@875
   729
    /// given node.
kpeter@874
   730
    ///
kpeter@874
   731
    /// \pre \ref run() must be called before using this function.
kpeter@875
   732
    Cost potential(const Node& n) const {
kpeter@875
   733
      return static_cast<Cost>(_pi[_node_id[n]]);
kpeter@875
   734
    }
kpeter@875
   735
kpeter@875
   736
    /// \brief Return the potential map (the dual solution).
kpeter@875
   737
    ///
kpeter@875
   738
    /// This function copies the potential (dual value) of each node
kpeter@875
   739
    /// into the given map.
kpeter@875
   740
    /// The \c Cost type of the algorithm must be convertible to the
kpeter@875
   741
    /// \c Value type of the map.
kpeter@875
   742
    ///
kpeter@875
   743
    /// \pre \ref run() must be called before using this function.
kpeter@875
   744
    template <typename PotentialMap>
kpeter@875
   745
    void potentialMap(PotentialMap &map) const {
kpeter@875
   746
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@875
   747
        map.set(n, static_cast<Cost>(_pi[_node_id[n]]));
kpeter@875
   748
      }
kpeter@874
   749
    }
kpeter@874
   750
kpeter@874
   751
    /// @}
kpeter@874
   752
kpeter@874
   753
  private:
kpeter@874
   754
kpeter@875
   755
    // Initialize the algorithm
kpeter@875
   756
    ProblemType init() {
kpeter@887
   757
      if (_res_node_num <= 1) return INFEASIBLE;
kpeter@875
   758
kpeter@875
   759
      // Check the sum of supply values
kpeter@875
   760
      _sum_supply = 0;
kpeter@875
   761
      for (int i = 0; i != _root; ++i) {
kpeter@875
   762
        _sum_supply += _supply[i];
kpeter@874
   763
      }
kpeter@875
   764
      if (_sum_supply > 0) return INFEASIBLE;
alpar@956
   765
kpeter@875
   766
kpeter@875
   767
      // Initialize vectors
kpeter@875
   768
      for (int i = 0; i != _res_node_num; ++i) {
kpeter@875
   769
        _pi[i] = 0;
kpeter@875
   770
        _excess[i] = _supply[i];
kpeter@875
   771
      }
alpar@956
   772
kpeter@875
   773
      // Remove infinite upper bounds and check negative arcs
kpeter@875
   774
      const Value MAX = std::numeric_limits<Value>::max();
kpeter@875
   775
      int last_out;
kpeter@875
   776
      if (_have_lower) {
kpeter@875
   777
        for (int i = 0; i != _root; ++i) {
kpeter@875
   778
          last_out = _first_out[i+1];
kpeter@875
   779
          for (int j = _first_out[i]; j != last_out; ++j) {
kpeter@875
   780
            if (_forward[j]) {
kpeter@875
   781
              Value c = _scost[j] < 0 ? _upper[j] : _lower[j];
kpeter@875
   782
              if (c >= MAX) return UNBOUNDED;
kpeter@875
   783
              _excess[i] -= c;
kpeter@875
   784
              _excess[_target[j]] += c;
kpeter@875
   785
            }
kpeter@875
   786
          }
kpeter@875
   787
        }
kpeter@875
   788
      } else {
kpeter@875
   789
        for (int i = 0; i != _root; ++i) {
kpeter@875
   790
          last_out = _first_out[i+1];
kpeter@875
   791
          for (int j = _first_out[i]; j != last_out; ++j) {
kpeter@875
   792
            if (_forward[j] && _scost[j] < 0) {
kpeter@875
   793
              Value c = _upper[j];
kpeter@875
   794
              if (c >= MAX) return UNBOUNDED;
kpeter@875
   795
              _excess[i] -= c;
kpeter@875
   796
              _excess[_target[j]] += c;
kpeter@875
   797
            }
kpeter@875
   798
          }
kpeter@875
   799
        }
kpeter@875
   800
      }
kpeter@875
   801
      Value ex, max_cap = 0;
kpeter@875
   802
      for (int i = 0; i != _res_node_num; ++i) {
kpeter@875
   803
        ex = _excess[i];
kpeter@875
   804
        _excess[i] = 0;
kpeter@875
   805
        if (ex < 0) max_cap -= ex;
kpeter@875
   806
      }
kpeter@875
   807
      for (int j = 0; j != _res_arc_num; ++j) {
kpeter@875
   808
        if (_upper[j] >= MAX) _upper[j] = max_cap;
kpeter@874
   809
      }
kpeter@874
   810
kpeter@875
   811
      // Initialize the large cost vector and the epsilon parameter
kpeter@875
   812
      _epsilon = 0;
kpeter@875
   813
      LargeCost lc;
kpeter@875
   814
      for (int i = 0; i != _root; ++i) {
kpeter@875
   815
        last_out = _first_out[i+1];
kpeter@875
   816
        for (int j = _first_out[i]; j != last_out; ++j) {
kpeter@875
   817
          lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha;
kpeter@875
   818
          _cost[j] = lc;
kpeter@875
   819
          if (lc > _epsilon) _epsilon = lc;
kpeter@875
   820
        }
kpeter@875
   821
      }
kpeter@875
   822
      _epsilon /= _alpha;
kpeter@874
   823
kpeter@875
   824
      // Initialize maps for Circulation and remove non-zero lower bounds
kpeter@875
   825
      ConstMap<Arc, Value> low(0);
kpeter@875
   826
      typedef typename Digraph::template ArcMap<Value> ValueArcMap;
kpeter@875
   827
      typedef typename Digraph::template NodeMap<Value> ValueNodeMap;
kpeter@875
   828
      ValueArcMap cap(_graph), flow(_graph);
kpeter@875
   829
      ValueNodeMap sup(_graph);
kpeter@875
   830
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@875
   831
        sup[n] = _supply[_node_id[n]];
kpeter@874
   832
      }
kpeter@875
   833
      if (_have_lower) {
kpeter@875
   834
        for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   835
          int j = _arc_idf[a];
kpeter@875
   836
          Value c = _lower[j];
kpeter@875
   837
          cap[a] = _upper[j] - c;
kpeter@875
   838
          sup[_graph.source(a)] -= c;
kpeter@875
   839
          sup[_graph.target(a)] += c;
kpeter@875
   840
        }
kpeter@875
   841
      } else {
kpeter@875
   842
        for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   843
          cap[a] = _upper[_arc_idf[a]];
kpeter@875
   844
        }
kpeter@875
   845
      }
kpeter@874
   846
kpeter@910
   847
      _sup_node_num = 0;
kpeter@910
   848
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@910
   849
        if (sup[n] > 0) ++_sup_node_num;
kpeter@910
   850
      }
kpeter@910
   851
kpeter@874
   852
      // Find a feasible flow using Circulation
kpeter@875
   853
      Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap>
kpeter@875
   854
        circ(_graph, low, cap, sup);
kpeter@875
   855
      if (!circ.flowMap(flow).run()) return INFEASIBLE;
kpeter@875
   856
kpeter@875
   857
      // Set residual capacities and handle GEQ supply type
kpeter@875
   858
      if (_sum_supply < 0) {
kpeter@875
   859
        for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   860
          Value fa = flow[a];
kpeter@875
   861
          _res_cap[_arc_idf[a]] = cap[a] - fa;
kpeter@875
   862
          _res_cap[_arc_idb[a]] = fa;
kpeter@875
   863
          sup[_graph.source(a)] -= fa;
kpeter@875
   864
          sup[_graph.target(a)] += fa;
kpeter@875
   865
        }
kpeter@875
   866
        for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@875
   867
          _excess[_node_id[n]] = sup[n];
kpeter@875
   868
        }
kpeter@875
   869
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
kpeter@875
   870
          int u = _target[a];
kpeter@875
   871
          int ra = _reverse[a];
kpeter@875
   872
          _res_cap[a] = -_sum_supply + 1;
kpeter@875
   873
          _res_cap[ra] = -_excess[u];
kpeter@875
   874
          _cost[a] = 0;
kpeter@875
   875
          _cost[ra] = 0;
kpeter@875
   876
          _excess[u] = 0;
kpeter@875
   877
        }
kpeter@875
   878
      } else {
kpeter@875
   879
        for (ArcIt a(_graph); a != INVALID; ++a) {
kpeter@875
   880
          Value fa = flow[a];
kpeter@875
   881
          _res_cap[_arc_idf[a]] = cap[a] - fa;
kpeter@875
   882
          _res_cap[_arc_idb[a]] = fa;
kpeter@875
   883
        }
kpeter@875
   884
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
kpeter@875
   885
          int ra = _reverse[a];
kpeter@910
   886
          _res_cap[a] = 0;
kpeter@875
   887
          _res_cap[ra] = 0;
kpeter@875
   888
          _cost[a] = 0;
kpeter@875
   889
          _cost[ra] = 0;
kpeter@875
   890
        }
kpeter@875
   891
      }
alpar@956
   892
kpeter@875
   893
      return OPTIMAL;
kpeter@875
   894
    }
kpeter@875
   895
kpeter@875
   896
    // Execute the algorithm and transform the results
kpeter@876
   897
    void start(Method method) {
kpeter@876
   898
      // Maximum path length for partial augment
kpeter@876
   899
      const int MAX_PATH_LENGTH = 4;
kpeter@910
   900
alpar@956
   901
      // Initialize data structures for buckets
kpeter@910
   902
      _max_rank = _alpha * _res_node_num;
kpeter@910
   903
      _buckets.resize(_max_rank);
kpeter@910
   904
      _bucket_next.resize(_res_node_num + 1);
kpeter@910
   905
      _bucket_prev.resize(_res_node_num + 1);
kpeter@910
   906
      _rank.resize(_res_node_num + 1);
alpar@956
   907
kpeter@875
   908
      // Execute the algorithm
kpeter@876
   909
      switch (method) {
kpeter@876
   910
        case PUSH:
kpeter@876
   911
          startPush();
kpeter@876
   912
          break;
kpeter@876
   913
        case AUGMENT:
kpeter@876
   914
          startAugment();
kpeter@876
   915
          break;
kpeter@876
   916
        case PARTIAL_AUGMENT:
kpeter@876
   917
          startAugment(MAX_PATH_LENGTH);
kpeter@876
   918
          break;
kpeter@875
   919
      }
kpeter@875
   920
kpeter@875
   921
      // Compute node potentials for the original costs
kpeter@875
   922
      _arc_vec.clear();
kpeter@875
   923
      _cost_vec.clear();
kpeter@875
   924
      for (int j = 0; j != _res_arc_num; ++j) {
kpeter@875
   925
        if (_res_cap[j] > 0) {
kpeter@875
   926
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
kpeter@875
   927
          _cost_vec.push_back(_scost[j]);
kpeter@875
   928
        }
kpeter@875
   929
      }
kpeter@875
   930
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
kpeter@875
   931
kpeter@875
   932
      typename BellmanFord<StaticDigraph, LargeCostArcMap>
kpeter@875
   933
        ::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map);
kpeter@875
   934
      bf.distMap(_pi_map);
kpeter@875
   935
      bf.init(0);
kpeter@875
   936
      bf.start();
kpeter@875
   937
kpeter@875
   938
      // Handle non-zero lower bounds
kpeter@875
   939
      if (_have_lower) {
kpeter@875
   940
        int limit = _first_out[_root];
kpeter@875
   941
        for (int j = 0; j != limit; ++j) {
kpeter@875
   942
          if (!_forward[j]) _res_cap[j] += _lower[j];
kpeter@875
   943
        }
kpeter@875
   944
      }
kpeter@874
   945
    }
alpar@956
   946
kpeter@910
   947
    // Initialize a cost scaling phase
kpeter@910
   948
    void initPhase() {
kpeter@910
   949
      // Saturate arcs not satisfying the optimality condition
kpeter@910
   950
      for (int u = 0; u != _res_node_num; ++u) {
kpeter@910
   951
        int last_out = _first_out[u+1];
kpeter@910
   952
        LargeCost pi_u = _pi[u];
kpeter@910
   953
        for (int a = _first_out[u]; a != last_out; ++a) {
kpeter@910
   954
          int v = _target[a];
kpeter@910
   955
          if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) {
kpeter@910
   956
            Value delta = _res_cap[a];
kpeter@910
   957
            _excess[u] -= delta;
kpeter@910
   958
            _excess[v] += delta;
kpeter@910
   959
            _res_cap[a] = 0;
kpeter@910
   960
            _res_cap[_reverse[a]] += delta;
kpeter@910
   961
          }
kpeter@910
   962
        }
kpeter@910
   963
      }
alpar@956
   964
kpeter@910
   965
      // Find active nodes (i.e. nodes with positive excess)
kpeter@910
   966
      for (int u = 0; u != _res_node_num; ++u) {
kpeter@910
   967
        if (_excess[u] > 0) _active_nodes.push_back(u);
kpeter@910
   968
      }
kpeter@910
   969
kpeter@910
   970
      // Initialize the next arcs
kpeter@910
   971
      for (int u = 0; u != _res_node_num; ++u) {
kpeter@910
   972
        _next_out[u] = _first_out[u];
kpeter@910
   973
      }
kpeter@910
   974
    }
alpar@956
   975
kpeter@910
   976
    // Early termination heuristic
kpeter@910
   977
    bool earlyTermination() {
kpeter@910
   978
      const double EARLY_TERM_FACTOR = 3.0;
kpeter@910
   979
kpeter@910
   980
      // Build a static residual graph
kpeter@910
   981
      _arc_vec.clear();
kpeter@910
   982
      _cost_vec.clear();
kpeter@910
   983
      for (int j = 0; j != _res_arc_num; ++j) {
kpeter@910
   984
        if (_res_cap[j] > 0) {
kpeter@910
   985
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
kpeter@910
   986
          _cost_vec.push_back(_cost[j] + 1);
kpeter@910
   987
        }
kpeter@910
   988
      }
kpeter@910
   989
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
kpeter@910
   990
kpeter@910
   991
      // Run Bellman-Ford algorithm to check if the current flow is optimal
kpeter@910
   992
      BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map);
kpeter@910
   993
      bf.init(0);
kpeter@910
   994
      bool done = false;
kpeter@910
   995
      int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num)));
kpeter@910
   996
      for (int i = 0; i < K && !done; ++i) {
kpeter@910
   997
        done = bf.processNextWeakRound();
kpeter@910
   998
      }
kpeter@910
   999
      return done;
kpeter@910
  1000
    }
kpeter@910
  1001
kpeter@910
  1002
    // Global potential update heuristic
kpeter@910
  1003
    void globalUpdate() {
kpeter@910
  1004
      int bucket_end = _root + 1;
alpar@956
  1005
kpeter@910
  1006
      // Initialize buckets
kpeter@910
  1007
      for (int r = 0; r != _max_rank; ++r) {
kpeter@910
  1008
        _buckets[r] = bucket_end;
kpeter@910
  1009
      }
kpeter@910
  1010
      Value total_excess = 0;
kpeter@910
  1011
      for (int i = 0; i != _res_node_num; ++i) {
kpeter@910
  1012
        if (_excess[i] < 0) {
kpeter@910
  1013
          _rank[i] = 0;
kpeter@910
  1014
          _bucket_next[i] = _buckets[0];
kpeter@910
  1015
          _bucket_prev[_buckets[0]] = i;
kpeter@910
  1016
          _buckets[0] = i;
kpeter@910
  1017
        } else {
kpeter@910
  1018
          total_excess += _excess[i];
kpeter@910
  1019
          _rank[i] = _max_rank;
kpeter@910
  1020
        }
kpeter@910
  1021
      }
kpeter@910
  1022
      if (total_excess == 0) return;
kpeter@910
  1023
kpeter@910
  1024
      // Search the buckets
kpeter@910
  1025
      int r = 0;
kpeter@910
  1026
      for ( ; r != _max_rank; ++r) {
kpeter@910
  1027
        while (_buckets[r] != bucket_end) {
kpeter@910
  1028
          // Remove the first node from the current bucket
kpeter@910
  1029
          int u = _buckets[r];
kpeter@910
  1030
          _buckets[r] = _bucket_next[u];
alpar@956
  1031
kpeter@910
  1032
          // Search the incomming arcs of u
kpeter@910
  1033
          LargeCost pi_u = _pi[u];
kpeter@910
  1034
          int last_out = _first_out[u+1];
kpeter@910
  1035
          for (int a = _first_out[u]; a != last_out; ++a) {
kpeter@910
  1036
            int ra = _reverse[a];
kpeter@910
  1037
            if (_res_cap[ra] > 0) {
kpeter@910
  1038
              int v = _source[ra];
kpeter@910
  1039
              int old_rank_v = _rank[v];
kpeter@910
  1040
              if (r < old_rank_v) {
kpeter@910
  1041
                // Compute the new rank of v
kpeter@910
  1042
                LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon;
kpeter@910
  1043
                int new_rank_v = old_rank_v;
kpeter@910
  1044
                if (nrc < LargeCost(_max_rank))
kpeter@910
  1045
                  new_rank_v = r + 1 + int(nrc);
alpar@956
  1046
kpeter@910
  1047
                // Change the rank of v
kpeter@910
  1048
                if (new_rank_v < old_rank_v) {
kpeter@910
  1049
                  _rank[v] = new_rank_v;
kpeter@910
  1050
                  _next_out[v] = _first_out[v];
alpar@956
  1051
kpeter@910
  1052
                  // Remove v from its old bucket
kpeter@910
  1053
                  if (old_rank_v < _max_rank) {
kpeter@910
  1054
                    if (_buckets[old_rank_v] == v) {
kpeter@910
  1055
                      _buckets[old_rank_v] = _bucket_next[v];
kpeter@910
  1056
                    } else {
kpeter@910
  1057
                      _bucket_next[_bucket_prev[v]] = _bucket_next[v];
kpeter@910
  1058
                      _bucket_prev[_bucket_next[v]] = _bucket_prev[v];
kpeter@910
  1059
                    }
kpeter@910
  1060
                  }
alpar@956
  1061
kpeter@910
  1062
                  // Insert v to its new bucket
kpeter@910
  1063
                  _bucket_next[v] = _buckets[new_rank_v];
kpeter@910
  1064
                  _bucket_prev[_buckets[new_rank_v]] = v;
kpeter@910
  1065
                  _buckets[new_rank_v] = v;
kpeter@910
  1066
                }
kpeter@910
  1067
              }
kpeter@910
  1068
            }
kpeter@910
  1069
          }
kpeter@910
  1070
kpeter@910
  1071
          // Finish search if there are no more active nodes
kpeter@910
  1072
          if (_excess[u] > 0) {
kpeter@910
  1073
            total_excess -= _excess[u];
kpeter@910
  1074
            if (total_excess <= 0) break;
kpeter@910
  1075
          }
kpeter@910
  1076
        }
kpeter@910
  1077
        if (total_excess <= 0) break;
kpeter@910
  1078
      }
alpar@956
  1079
kpeter@910
  1080
      // Relabel nodes
kpeter@910
  1081
      for (int u = 0; u != _res_node_num; ++u) {
kpeter@910
  1082
        int k = std::min(_rank[u], r);
kpeter@910
  1083
        if (k > 0) {
kpeter@910
  1084
          _pi[u] -= _epsilon * k;
kpeter@910
  1085
          _next_out[u] = _first_out[u];
kpeter@910
  1086
        }
kpeter@910
  1087
      }
kpeter@910
  1088
    }
kpeter@874
  1089
kpeter@876
  1090
    /// Execute the algorithm performing augment and relabel operations
kpeter@876
  1091
    void startAugment(int max_length = std::numeric_limits<int>::max()) {
kpeter@874
  1092
      // Paramters for heuristics
kpeter@910
  1093
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
kpeter@910
  1094
      const double GLOBAL_UPDATE_FACTOR = 3.0;
kpeter@874
  1095
kpeter@910
  1096
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
kpeter@910
  1097
        (_res_node_num + _sup_node_num * _sup_node_num));
kpeter@910
  1098
      int next_update_limit = global_update_freq;
alpar@956
  1099
kpeter@910
  1100
      int relabel_cnt = 0;
alpar@956
  1101
kpeter@875
  1102
      // Perform cost scaling phases
kpeter@910
  1103
      std::vector<int> path;
kpeter@874
  1104
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
kpeter@874
  1105
                                        1 : _epsilon / _alpha )
kpeter@874
  1106
      {
kpeter@910
  1107
        // Early termination heuristic
kpeter@910
  1108
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
kpeter@910
  1109
          if (earlyTermination()) break;
kpeter@874
  1110
        }
alpar@956
  1111
kpeter@910
  1112
        // Initialize current phase
kpeter@910
  1113
        initPhase();
alpar@956
  1114
kpeter@874
  1115
        // Perform partial augment and relabel operations
kpeter@875
  1116
        while (true) {
kpeter@874
  1117
          // Select an active node (FIFO selection)
kpeter@875
  1118
          while (_active_nodes.size() > 0 &&
kpeter@875
  1119
                 _excess[_active_nodes.front()] <= 0) {
kpeter@875
  1120
            _active_nodes.pop_front();
kpeter@874
  1121
          }
kpeter@875
  1122
          if (_active_nodes.size() == 0) break;
kpeter@875
  1123
          int start = _active_nodes.front();
kpeter@874
  1124
kpeter@874
  1125
          // Find an augmenting path from the start node
kpeter@910
  1126
          path.clear();
kpeter@875
  1127
          int tip = start;
kpeter@910
  1128
          while (_excess[tip] >= 0 && int(path.size()) < max_length) {
kpeter@875
  1129
            int u;
kpeter@910
  1130
            LargeCost min_red_cost, rc, pi_tip = _pi[tip];
kpeter@910
  1131
            int last_out = _first_out[tip+1];
kpeter@875
  1132
            for (int a = _next_out[tip]; a != last_out; ++a) {
kpeter@910
  1133
              u = _target[a];
kpeter@910
  1134
              if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) {
kpeter@910
  1135
                path.push_back(a);
kpeter@875
  1136
                _next_out[tip] = a;
kpeter@874
  1137
                tip = u;
kpeter@874
  1138
                goto next_step;
kpeter@874
  1139
              }
kpeter@874
  1140
            }
kpeter@874
  1141
kpeter@874
  1142
            // Relabel tip node
kpeter@910
  1143
            min_red_cost = std::numeric_limits<LargeCost>::max();
kpeter@910
  1144
            if (tip != start) {
kpeter@910
  1145
              int ra = _reverse[path.back()];
kpeter@910
  1146
              min_red_cost = _cost[ra] + pi_tip - _pi[_target[ra]];
kpeter@910
  1147
            }
kpeter@875
  1148
            for (int a = _first_out[tip]; a != last_out; ++a) {
kpeter@910
  1149
              rc = _cost[a] + pi_tip - _pi[_target[a]];
kpeter@875
  1150
              if (_res_cap[a] > 0 && rc < min_red_cost) {
kpeter@875
  1151
                min_red_cost = rc;
kpeter@875
  1152
              }
kpeter@874
  1153
            }
kpeter@875
  1154
            _pi[tip] -= min_red_cost + _epsilon;
kpeter@875
  1155
            _next_out[tip] = _first_out[tip];
kpeter@910
  1156
            ++relabel_cnt;
kpeter@874
  1157
kpeter@874
  1158
            // Step back
kpeter@874
  1159
            if (tip != start) {
kpeter@910
  1160
              tip = _source[path.back()];
kpeter@910
  1161
              path.pop_back();
kpeter@874
  1162
            }
kpeter@874
  1163
kpeter@875
  1164
          next_step: ;
kpeter@874
  1165
          }
kpeter@874
  1166
kpeter@874
  1167
          // Augment along the found path (as much flow as possible)
kpeter@875
  1168
          Value delta;
kpeter@910
  1169
          int pa, u, v = start;
kpeter@910
  1170
          for (int i = 0; i != int(path.size()); ++i) {
kpeter@910
  1171
            pa = path[i];
kpeter@875
  1172
            u = v;
kpeter@910
  1173
            v = _target[pa];
kpeter@875
  1174
            delta = std::min(_res_cap[pa], _excess[u]);
kpeter@875
  1175
            _res_cap[pa] -= delta;
kpeter@875
  1176
            _res_cap[_reverse[pa]] += delta;
kpeter@875
  1177
            _excess[u] -= delta;
kpeter@875
  1178
            _excess[v] += delta;
kpeter@875
  1179
            if (_excess[v] > 0 && _excess[v] <= delta)
kpeter@875
  1180
              _active_nodes.push_back(v);
kpeter@874
  1181
          }
kpeter@910
  1182
kpeter@910
  1183
          // Global update heuristic
kpeter@910
  1184
          if (relabel_cnt >= next_update_limit) {
kpeter@910
  1185
            globalUpdate();
kpeter@910
  1186
            next_update_limit += global_update_freq;
kpeter@910
  1187
          }
kpeter@874
  1188
        }
kpeter@874
  1189
      }
kpeter@874
  1190
    }
kpeter@874
  1191
kpeter@875
  1192
    /// Execute the algorithm performing push and relabel operations
kpeter@876
  1193
    void startPush() {
kpeter@874
  1194
      // Paramters for heuristics
kpeter@910
  1195
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
kpeter@910
  1196
      const double GLOBAL_UPDATE_FACTOR = 2.0;
kpeter@874
  1197
kpeter@910
  1198
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
kpeter@910
  1199
        (_res_node_num + _sup_node_num * _sup_node_num));
kpeter@910
  1200
      int next_update_limit = global_update_freq;
kpeter@910
  1201
kpeter@910
  1202
      int relabel_cnt = 0;
alpar@956
  1203
kpeter@875
  1204
      // Perform cost scaling phases
kpeter@875
  1205
      BoolVector hyper(_res_node_num, false);
kpeter@910
  1206
      LargeCostVector hyper_cost(_res_node_num);
kpeter@874
  1207
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
kpeter@874
  1208
                                        1 : _epsilon / _alpha )
kpeter@874
  1209
      {
kpeter@910
  1210
        // Early termination heuristic
kpeter@910
  1211
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
kpeter@910
  1212
          if (earlyTermination()) break;
kpeter@874
  1213
        }
alpar@956
  1214
kpeter@910
  1215
        // Initialize current phase
kpeter@910
  1216
        initPhase();
kpeter@874
  1217
kpeter@874
  1218
        // Perform push and relabel operations
kpeter@875
  1219
        while (_active_nodes.size() > 0) {
kpeter@910
  1220
          LargeCost min_red_cost, rc, pi_n;
kpeter@875
  1221
          Value delta;
kpeter@875
  1222
          int n, t, a, last_out = _res_arc_num;
kpeter@875
  1223
kpeter@910
  1224
        next_node:
kpeter@874
  1225
          // Select an active node (FIFO selection)
kpeter@875
  1226
          n = _active_nodes.front();
kpeter@910
  1227
          last_out = _first_out[n+1];
kpeter@910
  1228
          pi_n = _pi[n];
alpar@956
  1229
kpeter@874
  1230
          // Perform push operations if there are admissible arcs
kpeter@875
  1231
          if (_excess[n] > 0) {
kpeter@875
  1232
            for (a = _next_out[n]; a != last_out; ++a) {
kpeter@875
  1233
              if (_res_cap[a] > 0 &&
kpeter@910
  1234
                  _cost[a] + pi_n - _pi[_target[a]] < 0) {
kpeter@875
  1235
                delta = std::min(_res_cap[a], _excess[n]);
kpeter@875
  1236
                t = _target[a];
kpeter@874
  1237
kpeter@874
  1238
                // Push-look-ahead heuristic
kpeter@875
  1239
                Value ahead = -_excess[t];
kpeter@910
  1240
                int last_out_t = _first_out[t+1];
kpeter@910
  1241
                LargeCost pi_t = _pi[t];
kpeter@875
  1242
                for (int ta = _next_out[t]; ta != last_out_t; ++ta) {
alpar@956
  1243
                  if (_res_cap[ta] > 0 &&
kpeter@910
  1244
                      _cost[ta] + pi_t - _pi[_target[ta]] < 0)
kpeter@875
  1245
                    ahead += _res_cap[ta];
kpeter@875
  1246
                  if (ahead >= delta) break;
kpeter@874
  1247
                }
kpeter@874
  1248
                if (ahead < 0) ahead = 0;
kpeter@874
  1249
kpeter@874
  1250
                // Push flow along the arc
kpeter@910
  1251
                if (ahead < delta && !hyper[t]) {
kpeter@875
  1252
                  _res_cap[a] -= ahead;
kpeter@875
  1253
                  _res_cap[_reverse[a]] += ahead;
kpeter@874
  1254
                  _excess[n] -= ahead;
kpeter@874
  1255
                  _excess[t] += ahead;
kpeter@875
  1256
                  _active_nodes.push_front(t);
kpeter@874
  1257
                  hyper[t] = true;
kpeter@910
  1258
                  hyper_cost[t] = _cost[a] + pi_n - pi_t;
kpeter@875
  1259
                  _next_out[n] = a;
kpeter@875
  1260
                  goto next_node;
kpeter@874
  1261
                } else {
kpeter@875
  1262
                  _res_cap[a] -= delta;
kpeter@875
  1263
                  _res_cap[_reverse[a]] += delta;
kpeter@874
  1264
                  _excess[n] -= delta;
kpeter@874
  1265
                  _excess[t] += delta;
kpeter@874
  1266
                  if (_excess[t] > 0 && _excess[t] <= delta)
kpeter@875
  1267
                    _active_nodes.push_back(t);
kpeter@874
  1268
                }
kpeter@874
  1269
kpeter@875
  1270
                if (_excess[n] == 0) {
kpeter@875
  1271
                  _next_out[n] = a;
kpeter@875
  1272
                  goto remove_nodes;
kpeter@875
  1273
                }
kpeter@874
  1274
              }
kpeter@874
  1275
            }
kpeter@875
  1276
            _next_out[n] = a;
kpeter@874
  1277
          }
kpeter@874
  1278
kpeter@874
  1279
          // Relabel the node if it is still active (or hyper)
kpeter@875
  1280
          if (_excess[n] > 0 || hyper[n]) {
kpeter@910
  1281
             min_red_cost = hyper[n] ? -hyper_cost[n] :
kpeter@910
  1282
               std::numeric_limits<LargeCost>::max();
kpeter@875
  1283
            for (int a = _first_out[n]; a != last_out; ++a) {
kpeter@910
  1284
              rc = _cost[a] + pi_n - _pi[_target[a]];
kpeter@875
  1285
              if (_res_cap[a] > 0 && rc < min_red_cost) {
kpeter@875
  1286
                min_red_cost = rc;
kpeter@875
  1287
              }
kpeter@874
  1288
            }
kpeter@875
  1289
            _pi[n] -= min_red_cost + _epsilon;
kpeter@910
  1290
            _next_out[n] = _first_out[n];
kpeter@874
  1291
            hyper[n] = false;
kpeter@910
  1292
            ++relabel_cnt;
kpeter@874
  1293
          }
alpar@956
  1294
kpeter@874
  1295
          // Remove nodes that are not active nor hyper
kpeter@875
  1296
        remove_nodes:
kpeter@875
  1297
          while ( _active_nodes.size() > 0 &&
kpeter@875
  1298
                  _excess[_active_nodes.front()] <= 0 &&
kpeter@875
  1299
                  !hyper[_active_nodes.front()] ) {
kpeter@875
  1300
            _active_nodes.pop_front();
kpeter@874
  1301
          }
alpar@956
  1302
kpeter@910
  1303
          // Global update heuristic
kpeter@910
  1304
          if (relabel_cnt >= next_update_limit) {
kpeter@910
  1305
            globalUpdate();
kpeter@910
  1306
            for (int u = 0; u != _res_node_num; ++u)
kpeter@910
  1307
              hyper[u] = false;
kpeter@910
  1308
            next_update_limit += global_update_freq;
kpeter@910
  1309
          }
kpeter@874
  1310
        }
kpeter@874
  1311
      }
kpeter@874
  1312
    }
kpeter@874
  1313
kpeter@874
  1314
  }; //class CostScaling
kpeter@874
  1315
kpeter@874
  1316
  ///@}
kpeter@874
  1317
kpeter@874
  1318
} //namespace lemon
kpeter@874
  1319
kpeter@874
  1320
#endif //LEMON_COST_SCALING_H