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