lemon/circulation.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 24 Jul 2009 10:27:40 +0200
changeset 713 4ac30454f1c1
parent 641 756a5ec551c8
child 715 ece80147fb08
permissions -rw-r--r--
Small doc improvements
alpar@399
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@399
     2
 *
alpar@399
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@399
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@399
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@399
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@399
     8
 *
alpar@399
     9
 * Permission to use, modify and distribute this software is granted
alpar@399
    10
 * provided that this copyright notice appears in all copies. For
alpar@399
    11
 * precise terms see the accompanying LICENSE file.
alpar@399
    12
 *
alpar@399
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@399
    14
 * express or implied, and with no claim as to its suitability for any
alpar@399
    15
 * purpose.
alpar@399
    16
 *
alpar@399
    17
 */
alpar@399
    18
alpar@399
    19
#ifndef LEMON_CIRCULATION_H
alpar@399
    20
#define LEMON_CIRCULATION_H
alpar@399
    21
alpar@399
    22
#include <lemon/tolerance.h>
alpar@399
    23
#include <lemon/elevator.h>
kpeter@622
    24
#include <limits>
alpar@399
    25
alpar@399
    26
///\ingroup max_flow
alpar@399
    27
///\file
kpeter@402
    28
///\brief Push-relabel algorithm for finding a feasible circulation.
alpar@399
    29
///
alpar@399
    30
namespace lemon {
alpar@399
    31
alpar@399
    32
  /// \brief Default traits class of Circulation class.
alpar@399
    33
  ///
alpar@399
    34
  /// Default traits class of Circulation class.
kpeter@610
    35
  ///
kpeter@610
    36
  /// \tparam GR Type of the digraph the algorithm runs on.
kpeter@610
    37
  /// \tparam LM The type of the lower bound map.
kpeter@610
    38
  /// \tparam UM The type of the upper bound (capacity) map.
kpeter@610
    39
  /// \tparam SM The type of the supply map.
kpeter@492
    40
  template <typename GR, typename LM,
kpeter@610
    41
            typename UM, typename SM>
alpar@399
    42
  struct CirculationDefaultTraits {
alpar@399
    43
kpeter@402
    44
    /// \brief The type of the digraph the algorithm runs on.
kpeter@492
    45
    typedef GR Digraph;
alpar@399
    46
kpeter@610
    47
    /// \brief The type of the lower bound map.
alpar@399
    48
    ///
kpeter@610
    49
    /// The type of the map that stores the lower bounds on the arcs.
kpeter@610
    50
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
kpeter@610
    51
    typedef LM LowerMap;
alpar@399
    52
kpeter@610
    53
    /// \brief The type of the upper bound (capacity) map.
alpar@399
    54
    ///
kpeter@610
    55
    /// The type of the map that stores the upper bounds (capacities)
kpeter@610
    56
    /// on the arcs.
kpeter@610
    57
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
kpeter@610
    58
    typedef UM UpperMap;
alpar@399
    59
kpeter@610
    60
    /// \brief The type of supply map.
alpar@399
    61
    ///
kpeter@610
    62
    /// The type of the map that stores the signed supply values of the 
kpeter@610
    63
    /// nodes. 
kpeter@610
    64
    /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
kpeter@610
    65
    typedef SM SupplyMap;
alpar@399
    66
kpeter@641
    67
    /// \brief The type of the flow and supply values.
kpeter@641
    68
    typedef typename SupplyMap::Value Value;
alpar@399
    69
kpeter@402
    70
    /// \brief The type of the map that stores the flow values.
alpar@399
    71
    ///
kpeter@402
    72
    /// The type of the map that stores the flow values.
kpeter@610
    73
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@610
    74
    /// concept.
kpeter@713
    75
#ifdef DOXYGEN
kpeter@713
    76
    typedef GR::ArcMap<Value> FlowMap;
kpeter@713
    77
#else
kpeter@641
    78
    typedef typename Digraph::template ArcMap<Value> FlowMap;
kpeter@713
    79
#endif
alpar@399
    80
alpar@399
    81
    /// \brief Instantiates a FlowMap.
alpar@399
    82
    ///
alpar@399
    83
    /// This function instantiates a \ref FlowMap.
kpeter@610
    84
    /// \param digraph The digraph for which we would like to define
alpar@399
    85
    /// the flow map.
alpar@399
    86
    static FlowMap* createFlowMap(const Digraph& digraph) {
alpar@399
    87
      return new FlowMap(digraph);
alpar@399
    88
    }
alpar@399
    89
kpeter@402
    90
    /// \brief The elevator type used by the algorithm.
alpar@399
    91
    ///
kpeter@402
    92
    /// The elevator type used by the algorithm.
alpar@399
    93
    ///
kpeter@713
    94
    /// \sa Elevator, LinkedElevator
kpeter@713
    95
#ifdef DOXYGEN
kpeter@713
    96
    typedef lemon::Elevator<GR, GR::Node> Elevator;
kpeter@713
    97
#else
alpar@399
    98
    typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
kpeter@713
    99
#endif
alpar@399
   100
alpar@399
   101
    /// \brief Instantiates an Elevator.
alpar@399
   102
    ///
kpeter@402
   103
    /// This function instantiates an \ref Elevator.
kpeter@610
   104
    /// \param digraph The digraph for which we would like to define
alpar@399
   105
    /// the elevator.
alpar@399
   106
    /// \param max_level The maximum level of the elevator.
alpar@399
   107
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
alpar@399
   108
      return new Elevator(digraph, max_level);
alpar@399
   109
    }
alpar@399
   110
alpar@399
   111
    /// \brief The tolerance used by the algorithm
alpar@399
   112
    ///
alpar@399
   113
    /// The tolerance used by the algorithm to handle inexact computation.
kpeter@641
   114
    typedef lemon::Tolerance<Value> Tolerance;
alpar@399
   115
alpar@399
   116
  };
alpar@399
   117
kpeter@402
   118
  /**
kpeter@402
   119
     \brief Push-relabel algorithm for the network circulation problem.
alpar@399
   120
alpar@399
   121
     \ingroup max_flow
kpeter@610
   122
     This class implements a push-relabel algorithm for the \e network
kpeter@610
   123
     \e circulation problem.
kpeter@402
   124
     It is to find a feasible circulation when lower and upper bounds
kpeter@610
   125
     are given for the flow values on the arcs and lower bounds are
kpeter@610
   126
     given for the difference between the outgoing and incoming flow
kpeter@610
   127
     at the nodes.
kpeter@402
   128
alpar@399
   129
     The exact formulation of this problem is the following.
kpeter@622
   130
     Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
kpeter@622
   131
     \f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
kpeter@622
   132
     upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$
kpeter@610
   133
     holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
kpeter@610
   134
     denotes the signed supply values of the nodes.
kpeter@610
   135
     If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$
kpeter@610
   136
     supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with
kpeter@610
   137
     \f$-sup(u)\f$ demand.
kpeter@622
   138
     A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
kpeter@610
   139
     solution of the following problem.
kpeter@402
   140
kpeter@610
   141
     \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
kpeter@610
   142
     \geq sup(u) \quad \forall u\in V, \f]
kpeter@610
   143
     \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f]
kpeter@610
   144
     
kpeter@610
   145
     The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
kpeter@610
   146
     zero or negative in order to have a feasible solution (since the sum
kpeter@610
   147
     of the expressions on the left-hand side of the inequalities is zero).
kpeter@610
   148
     It means that the total demand must be greater or equal to the total
kpeter@610
   149
     supply and all the supplies have to be carried out from the supply nodes,
kpeter@610
   150
     but there could be demands that are not satisfied.
kpeter@610
   151
     If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
kpeter@610
   152
     constraints have to be satisfied with equality, i.e. all demands
kpeter@610
   153
     have to be satisfied and all supplies have to be used.
kpeter@610
   154
     
kpeter@610
   155
     If you need the opposite inequalities in the supply/demand constraints
kpeter@610
   156
     (i.e. the total demand is less than the total supply and all the demands
kpeter@610
   157
     have to be satisfied while there could be supplies that are not used),
kpeter@610
   158
     then you could easily transform the problem to the above form by reversing
kpeter@610
   159
     the direction of the arcs and taking the negative of the supply values
kpeter@610
   160
     (e.g. using \ref ReverseDigraph and \ref NegMap adaptors).
kpeter@610
   161
kpeter@622
   162
     This algorithm either calculates a feasible circulation, or provides
kpeter@622
   163
     a \ref barrier() "barrier", which prooves that a feasible soultion
kpeter@622
   164
     cannot exist.
kpeter@622
   165
kpeter@610
   166
     Note that this algorithm also provides a feasible solution for the
kpeter@610
   167
     \ref min_cost_flow "minimum cost flow problem".
kpeter@402
   168
kpeter@492
   169
     \tparam GR The type of the digraph the algorithm runs on.
kpeter@610
   170
     \tparam LM The type of the lower bound map. The default
kpeter@492
   171
     map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
kpeter@610
   172
     \tparam UM The type of the upper bound (capacity) map.
kpeter@610
   173
     The default map type is \c LM.
kpeter@610
   174
     \tparam SM The type of the supply map. The default map type is
kpeter@492
   175
     \ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>".
alpar@399
   176
  */
kpeter@402
   177
#ifdef DOXYGEN
kpeter@492
   178
template< typename GR,
kpeter@492
   179
          typename LM,
kpeter@492
   180
          typename UM,
kpeter@610
   181
          typename SM,
kpeter@492
   182
          typename TR >
kpeter@402
   183
#else
kpeter@492
   184
template< typename GR,
kpeter@492
   185
          typename LM = typename GR::template ArcMap<int>,
kpeter@492
   186
          typename UM = LM,
kpeter@610
   187
          typename SM = typename GR::template NodeMap<typename UM::Value>,
kpeter@610
   188
          typename TR = CirculationDefaultTraits<GR, LM, UM, SM> >
kpeter@402
   189
#endif
alpar@399
   190
  class Circulation {
kpeter@402
   191
  public:
alpar@399
   192
kpeter@402
   193
    ///The \ref CirculationDefaultTraits "traits class" of the algorithm.
kpeter@492
   194
    typedef TR Traits;
kpeter@402
   195
    ///The type of the digraph the algorithm runs on.
alpar@399
   196
    typedef typename Traits::Digraph Digraph;
kpeter@641
   197
    ///The type of the flow and supply values.
kpeter@641
   198
    typedef typename Traits::Value Value;
alpar@399
   199
kpeter@610
   200
    ///The type of the lower bound map.
kpeter@610
   201
    typedef typename Traits::LowerMap LowerMap;
kpeter@610
   202
    ///The type of the upper bound (capacity) map.
kpeter@610
   203
    typedef typename Traits::UpperMap UpperMap;
kpeter@610
   204
    ///The type of the supply map.
kpeter@610
   205
    typedef typename Traits::SupplyMap SupplyMap;
kpeter@402
   206
    ///The type of the flow map.
alpar@399
   207
    typedef typename Traits::FlowMap FlowMap;
kpeter@402
   208
kpeter@402
   209
    ///The type of the elevator.
alpar@399
   210
    typedef typename Traits::Elevator Elevator;
kpeter@402
   211
    ///The type of the tolerance.
alpar@399
   212
    typedef typename Traits::Tolerance Tolerance;
alpar@399
   213
kpeter@402
   214
  private:
kpeter@402
   215
kpeter@402
   216
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@399
   217
alpar@399
   218
    const Digraph &_g;
alpar@399
   219
    int _node_num;
alpar@399
   220
kpeter@610
   221
    const LowerMap *_lo;
kpeter@610
   222
    const UpperMap *_up;
kpeter@610
   223
    const SupplyMap *_supply;
alpar@399
   224
alpar@399
   225
    FlowMap *_flow;
alpar@399
   226
    bool _local_flow;
alpar@399
   227
alpar@399
   228
    Elevator* _level;
alpar@399
   229
    bool _local_level;
alpar@399
   230
kpeter@641
   231
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
alpar@399
   232
    ExcessMap* _excess;
alpar@399
   233
alpar@399
   234
    Tolerance _tol;
alpar@399
   235
    int _el;
alpar@399
   236
alpar@399
   237
  public:
alpar@399
   238
alpar@399
   239
    typedef Circulation Create;
alpar@399
   240
kpeter@402
   241
    ///\name Named Template Parameters
alpar@399
   242
alpar@399
   243
    ///@{
alpar@399
   244
kpeter@559
   245
    template <typename T>
alpar@401
   246
    struct SetFlowMapTraits : public Traits {
kpeter@559
   247
      typedef T FlowMap;
alpar@399
   248
      static FlowMap *createFlowMap(const Digraph&) {
alpar@399
   249
        LEMON_ASSERT(false, "FlowMap is not initialized");
alpar@399
   250
        return 0; // ignore warnings
alpar@399
   251
      }
alpar@399
   252
    };
alpar@399
   253
alpar@399
   254
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@399
   255
    /// FlowMap type
alpar@399
   256
    ///
alpar@399
   257
    /// \ref named-templ-param "Named parameter" for setting FlowMap
kpeter@402
   258
    /// type.
kpeter@559
   259
    template <typename T>
alpar@401
   260
    struct SetFlowMap
kpeter@610
   261
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   262
                           SetFlowMapTraits<T> > {
kpeter@610
   263
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   264
                          SetFlowMapTraits<T> > Create;
alpar@399
   265
    };
alpar@399
   266
kpeter@559
   267
    template <typename T>
alpar@401
   268
    struct SetElevatorTraits : public Traits {
kpeter@559
   269
      typedef T Elevator;
alpar@399
   270
      static Elevator *createElevator(const Digraph&, int) {
alpar@399
   271
        LEMON_ASSERT(false, "Elevator is not initialized");
alpar@399
   272
        return 0; // ignore warnings
alpar@399
   273
      }
alpar@399
   274
    };
alpar@399
   275
alpar@399
   276
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@399
   277
    /// Elevator type
alpar@399
   278
    ///
alpar@399
   279
    /// \ref named-templ-param "Named parameter" for setting Elevator
kpeter@402
   280
    /// type. If this named parameter is used, then an external
kpeter@402
   281
    /// elevator object must be passed to the algorithm using the
kpeter@402
   282
    /// \ref elevator(Elevator&) "elevator()" function before calling
kpeter@402
   283
    /// \ref run() or \ref init().
kpeter@402
   284
    /// \sa SetStandardElevator
kpeter@559
   285
    template <typename T>
alpar@401
   286
    struct SetElevator
kpeter@610
   287
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   288
                           SetElevatorTraits<T> > {
kpeter@610
   289
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   290
                          SetElevatorTraits<T> > Create;
alpar@399
   291
    };
alpar@399
   292
kpeter@559
   293
    template <typename T>
alpar@401
   294
    struct SetStandardElevatorTraits : public Traits {
kpeter@559
   295
      typedef T Elevator;
alpar@399
   296
      static Elevator *createElevator(const Digraph& digraph, int max_level) {
alpar@399
   297
        return new Elevator(digraph, max_level);
alpar@399
   298
      }
alpar@399
   299
    };
alpar@399
   300
alpar@399
   301
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@402
   302
    /// Elevator type with automatic allocation
alpar@399
   303
    ///
alpar@399
   304
    /// \ref named-templ-param "Named parameter" for setting Elevator
kpeter@402
   305
    /// type with automatic allocation.
kpeter@402
   306
    /// The Elevator should have standard constructor interface to be
kpeter@402
   307
    /// able to automatically created by the algorithm (i.e. the
kpeter@402
   308
    /// digraph and the maximum level should be passed to it).
kpeter@402
   309
    /// However an external elevator object could also be passed to the
kpeter@402
   310
    /// algorithm with the \ref elevator(Elevator&) "elevator()" function
kpeter@402
   311
    /// before calling \ref run() or \ref init().
kpeter@402
   312
    /// \sa SetElevator
kpeter@559
   313
    template <typename T>
alpar@401
   314
    struct SetStandardElevator
kpeter@610
   315
      : public Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   316
                       SetStandardElevatorTraits<T> > {
kpeter@610
   317
      typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap,
kpeter@559
   318
                      SetStandardElevatorTraits<T> > Create;
alpar@399
   319
    };
alpar@399
   320
alpar@399
   321
    /// @}
alpar@399
   322
alpar@399
   323
  protected:
alpar@399
   324
alpar@399
   325
    Circulation() {}
alpar@399
   326
alpar@399
   327
  public:
alpar@399
   328
kpeter@610
   329
    /// Constructor.
alpar@399
   330
alpar@399
   331
    /// The constructor of the class.
kpeter@610
   332
    ///
kpeter@610
   333
    /// \param graph The digraph the algorithm runs on.
kpeter@610
   334
    /// \param lower The lower bounds for the flow values on the arcs.
kpeter@610
   335
    /// \param upper The upper bounds (capacities) for the flow values 
kpeter@610
   336
    /// on the arcs.
kpeter@610
   337
    /// \param supply The signed supply values of the nodes.
kpeter@610
   338
    Circulation(const Digraph &graph, const LowerMap &lower,
kpeter@610
   339
                const UpperMap &upper, const SupplyMap &supply)
kpeter@610
   340
      : _g(graph), _lo(&lower), _up(&upper), _supply(&supply),
kpeter@610
   341
        _flow(NULL), _local_flow(false), _level(NULL), _local_level(false),
kpeter@610
   342
        _excess(NULL) {}
alpar@399
   343
kpeter@402
   344
    /// Destructor.
alpar@399
   345
    ~Circulation() {
alpar@399
   346
      destroyStructures();
alpar@399
   347
    }
alpar@399
   348
kpeter@402
   349
alpar@399
   350
  private:
alpar@399
   351
kpeter@622
   352
    bool checkBoundMaps() {
kpeter@622
   353
      for (ArcIt e(_g);e!=INVALID;++e) {
kpeter@622
   354
        if (_tol.less((*_up)[e], (*_lo)[e])) return false;
kpeter@622
   355
      }
kpeter@622
   356
      return true;
kpeter@622
   357
    }
kpeter@622
   358
alpar@399
   359
    void createStructures() {
alpar@399
   360
      _node_num = _el = countNodes(_g);
alpar@399
   361
alpar@399
   362
      if (!_flow) {
alpar@399
   363
        _flow = Traits::createFlowMap(_g);
alpar@399
   364
        _local_flow = true;
alpar@399
   365
      }
alpar@399
   366
      if (!_level) {
alpar@399
   367
        _level = Traits::createElevator(_g, _node_num);
alpar@399
   368
        _local_level = true;
alpar@399
   369
      }
alpar@399
   370
      if (!_excess) {
alpar@399
   371
        _excess = new ExcessMap(_g);
alpar@399
   372
      }
alpar@399
   373
    }
alpar@399
   374
alpar@399
   375
    void destroyStructures() {
alpar@399
   376
      if (_local_flow) {
alpar@399
   377
        delete _flow;
alpar@399
   378
      }
alpar@399
   379
      if (_local_level) {
alpar@399
   380
        delete _level;
alpar@399
   381
      }
alpar@399
   382
      if (_excess) {
alpar@399
   383
        delete _excess;
alpar@399
   384
      }
alpar@399
   385
    }
alpar@399
   386
alpar@399
   387
  public:
alpar@399
   388
kpeter@610
   389
    /// Sets the lower bound map.
alpar@399
   390
kpeter@610
   391
    /// Sets the lower bound map.
kpeter@402
   392
    /// \return <tt>(*this)</tt>
kpeter@610
   393
    Circulation& lowerMap(const LowerMap& map) {
alpar@399
   394
      _lo = &map;
alpar@399
   395
      return *this;
alpar@399
   396
    }
alpar@399
   397
kpeter@610
   398
    /// Sets the upper bound (capacity) map.
alpar@399
   399
kpeter@610
   400
    /// Sets the upper bound (capacity) map.
kpeter@402
   401
    /// \return <tt>(*this)</tt>
kpeter@622
   402
    Circulation& upperMap(const UpperMap& map) {
alpar@399
   403
      _up = &map;
alpar@399
   404
      return *this;
alpar@399
   405
    }
alpar@399
   406
kpeter@610
   407
    /// Sets the supply map.
alpar@399
   408
kpeter@610
   409
    /// Sets the supply map.
kpeter@402
   410
    /// \return <tt>(*this)</tt>
kpeter@610
   411
    Circulation& supplyMap(const SupplyMap& map) {
kpeter@610
   412
      _supply = &map;
alpar@399
   413
      return *this;
alpar@399
   414
    }
alpar@399
   415
kpeter@402
   416
    /// \brief Sets the flow map.
kpeter@402
   417
    ///
alpar@399
   418
    /// Sets the flow map.
kpeter@402
   419
    /// If you don't use this function before calling \ref run() or
kpeter@402
   420
    /// \ref init(), an instance will be allocated automatically.
kpeter@402
   421
    /// The destructor deallocates this automatically allocated map,
kpeter@402
   422
    /// of course.
kpeter@402
   423
    /// \return <tt>(*this)</tt>
alpar@399
   424
    Circulation& flowMap(FlowMap& map) {
alpar@399
   425
      if (_local_flow) {
alpar@399
   426
        delete _flow;
alpar@399
   427
        _local_flow = false;
alpar@399
   428
      }
alpar@399
   429
      _flow = &map;
alpar@399
   430
      return *this;
alpar@399
   431
    }
alpar@399
   432
kpeter@402
   433
    /// \brief Sets the elevator used by algorithm.
alpar@399
   434
    ///
kpeter@402
   435
    /// Sets the elevator used by algorithm.
kpeter@402
   436
    /// If you don't use this function before calling \ref run() or
kpeter@402
   437
    /// \ref init(), an instance will be allocated automatically.
kpeter@402
   438
    /// The destructor deallocates this automatically allocated elevator,
kpeter@402
   439
    /// of course.
kpeter@402
   440
    /// \return <tt>(*this)</tt>
alpar@399
   441
    Circulation& elevator(Elevator& elevator) {
alpar@399
   442
      if (_local_level) {
alpar@399
   443
        delete _level;
alpar@399
   444
        _local_level = false;
alpar@399
   445
      }
alpar@399
   446
      _level = &elevator;
alpar@399
   447
      return *this;
alpar@399
   448
    }
alpar@399
   449
kpeter@402
   450
    /// \brief Returns a const reference to the elevator.
alpar@399
   451
    ///
kpeter@402
   452
    /// Returns a const reference to the elevator.
kpeter@402
   453
    ///
kpeter@402
   454
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@402
   455
    /// using this function.
kpeter@420
   456
    const Elevator& elevator() const {
alpar@399
   457
      return *_level;
alpar@399
   458
    }
alpar@399
   459
kpeter@402
   460
    /// \brief Sets the tolerance used by algorithm.
kpeter@402
   461
    ///
alpar@399
   462
    /// Sets the tolerance used by algorithm.
alpar@399
   463
    Circulation& tolerance(const Tolerance& tolerance) const {
alpar@399
   464
      _tol = tolerance;
alpar@399
   465
      return *this;
alpar@399
   466
    }
alpar@399
   467
kpeter@402
   468
    /// \brief Returns a const reference to the tolerance.
alpar@399
   469
    ///
kpeter@402
   470
    /// Returns a const reference to the tolerance.
alpar@399
   471
    const Tolerance& tolerance() const {
alpar@399
   472
      return tolerance;
alpar@399
   473
    }
alpar@399
   474
kpeter@402
   475
    /// \name Execution Control
kpeter@402
   476
    /// The simplest way to execute the algorithm is to call \ref run().\n
kpeter@713
   477
    /// If you need better control on the initial solution or the execution,
kpeter@713
   478
    /// you have to call one of the \ref init() functions first, then
kpeter@402
   479
    /// the \ref start() function.
alpar@399
   480
alpar@399
   481
    ///@{
alpar@399
   482
alpar@399
   483
    /// Initializes the internal data structures.
alpar@399
   484
kpeter@402
   485
    /// Initializes the internal data structures and sets all flow values
kpeter@402
   486
    /// to the lower bound.
alpar@399
   487
    void init()
alpar@399
   488
    {
kpeter@622
   489
      LEMON_DEBUG(checkBoundMaps(),
kpeter@622
   490
        "Upper bounds must be greater or equal to the lower bounds");
kpeter@622
   491
alpar@399
   492
      createStructures();
alpar@399
   493
alpar@399
   494
      for(NodeIt n(_g);n!=INVALID;++n) {
alpar@611
   495
        (*_excess)[n] = (*_supply)[n];
alpar@399
   496
      }
alpar@399
   497
alpar@399
   498
      for (ArcIt e(_g);e!=INVALID;++e) {
alpar@399
   499
        _flow->set(e, (*_lo)[e]);
kpeter@581
   500
        (*_excess)[_g.target(e)] += (*_flow)[e];
kpeter@581
   501
        (*_excess)[_g.source(e)] -= (*_flow)[e];
alpar@399
   502
      }
alpar@399
   503
alpar@399
   504
      // global relabeling tested, but in general case it provides
alpar@399
   505
      // worse performance for random digraphs
alpar@399
   506
      _level->initStart();
alpar@399
   507
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   508
        _level->initAddItem(n);
alpar@399
   509
      _level->initFinish();
alpar@399
   510
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   511
        if(_tol.positive((*_excess)[n]))
alpar@399
   512
          _level->activate(n);
alpar@399
   513
    }
alpar@399
   514
kpeter@402
   515
    /// Initializes the internal data structures using a greedy approach.
alpar@399
   516
kpeter@402
   517
    /// Initializes the internal data structures using a greedy approach
kpeter@402
   518
    /// to construct the initial solution.
alpar@399
   519
    void greedyInit()
alpar@399
   520
    {
kpeter@622
   521
      LEMON_DEBUG(checkBoundMaps(),
kpeter@622
   522
        "Upper bounds must be greater or equal to the lower bounds");
kpeter@622
   523
alpar@399
   524
      createStructures();
alpar@399
   525
alpar@399
   526
      for(NodeIt n(_g);n!=INVALID;++n) {
alpar@611
   527
        (*_excess)[n] = (*_supply)[n];
alpar@399
   528
      }
alpar@399
   529
alpar@399
   530
      for (ArcIt e(_g);e!=INVALID;++e) {
kpeter@622
   531
        if (!_tol.less(-(*_excess)[_g.target(e)], (*_up)[e])) {
alpar@399
   532
          _flow->set(e, (*_up)[e]);
kpeter@581
   533
          (*_excess)[_g.target(e)] += (*_up)[e];
kpeter@581
   534
          (*_excess)[_g.source(e)] -= (*_up)[e];
kpeter@622
   535
        } else if (_tol.less(-(*_excess)[_g.target(e)], (*_lo)[e])) {
alpar@399
   536
          _flow->set(e, (*_lo)[e]);
kpeter@581
   537
          (*_excess)[_g.target(e)] += (*_lo)[e];
kpeter@581
   538
          (*_excess)[_g.source(e)] -= (*_lo)[e];
alpar@399
   539
        } else {
kpeter@641
   540
          Value fc = -(*_excess)[_g.target(e)];
alpar@399
   541
          _flow->set(e, fc);
kpeter@581
   542
          (*_excess)[_g.target(e)] = 0;
kpeter@581
   543
          (*_excess)[_g.source(e)] -= fc;
alpar@399
   544
        }
alpar@399
   545
      }
alpar@399
   546
alpar@399
   547
      _level->initStart();
alpar@399
   548
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   549
        _level->initAddItem(n);
alpar@399
   550
      _level->initFinish();
alpar@399
   551
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   552
        if(_tol.positive((*_excess)[n]))
alpar@399
   553
          _level->activate(n);
alpar@399
   554
    }
alpar@399
   555
kpeter@402
   556
    ///Executes the algorithm
alpar@399
   557
kpeter@402
   558
    ///This function executes the algorithm.
kpeter@402
   559
    ///
kpeter@402
   560
    ///\return \c true if a feasible circulation is found.
alpar@399
   561
    ///
alpar@399
   562
    ///\sa barrier()
kpeter@402
   563
    ///\sa barrierMap()
alpar@399
   564
    bool start()
alpar@399
   565
    {
alpar@399
   566
alpar@399
   567
      Node act;
alpar@399
   568
      Node bact=INVALID;
alpar@399
   569
      Node last_activated=INVALID;
alpar@399
   570
      while((act=_level->highestActive())!=INVALID) {
alpar@399
   571
        int actlevel=(*_level)[act];
alpar@399
   572
        int mlevel=_node_num;
kpeter@641
   573
        Value exc=(*_excess)[act];
alpar@399
   574
alpar@399
   575
        for(OutArcIt e(_g,act);e!=INVALID; ++e) {
alpar@399
   576
          Node v = _g.target(e);
kpeter@641
   577
          Value fc=(*_up)[e]-(*_flow)[e];
alpar@399
   578
          if(!_tol.positive(fc)) continue;
alpar@399
   579
          if((*_level)[v]<actlevel) {
alpar@399
   580
            if(!_tol.less(fc, exc)) {
alpar@399
   581
              _flow->set(e, (*_flow)[e] + exc);
kpeter@581
   582
              (*_excess)[v] += exc;
alpar@399
   583
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
alpar@399
   584
                _level->activate(v);
kpeter@581
   585
              (*_excess)[act] = 0;
alpar@399
   586
              _level->deactivate(act);
alpar@399
   587
              goto next_l;
alpar@399
   588
            }
alpar@399
   589
            else {
alpar@399
   590
              _flow->set(e, (*_up)[e]);
kpeter@581
   591
              (*_excess)[v] += fc;
alpar@399
   592
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
alpar@399
   593
                _level->activate(v);
alpar@399
   594
              exc-=fc;
alpar@399
   595
            }
alpar@399
   596
          }
alpar@399
   597
          else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
alpar@399
   598
        }
alpar@399
   599
        for(InArcIt e(_g,act);e!=INVALID; ++e) {
alpar@399
   600
          Node v = _g.source(e);
kpeter@641
   601
          Value fc=(*_flow)[e]-(*_lo)[e];
alpar@399
   602
          if(!_tol.positive(fc)) continue;
alpar@399
   603
          if((*_level)[v]<actlevel) {
alpar@399
   604
            if(!_tol.less(fc, exc)) {
alpar@399
   605
              _flow->set(e, (*_flow)[e] - exc);
kpeter@581
   606
              (*_excess)[v] += exc;
alpar@399
   607
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
alpar@399
   608
                _level->activate(v);
kpeter@581
   609
              (*_excess)[act] = 0;
alpar@399
   610
              _level->deactivate(act);
alpar@399
   611
              goto next_l;
alpar@399
   612
            }
alpar@399
   613
            else {
alpar@399
   614
              _flow->set(e, (*_lo)[e]);
kpeter@581
   615
              (*_excess)[v] += fc;
alpar@399
   616
              if(!_level->active(v) && _tol.positive((*_excess)[v]))
alpar@399
   617
                _level->activate(v);
alpar@399
   618
              exc-=fc;
alpar@399
   619
            }
alpar@399
   620
          }
alpar@399
   621
          else if((*_level)[v]<mlevel) mlevel=(*_level)[v];
alpar@399
   622
        }
alpar@399
   623
kpeter@581
   624
        (*_excess)[act] = exc;
alpar@399
   625
        if(!_tol.positive(exc)) _level->deactivate(act);
alpar@399
   626
        else if(mlevel==_node_num) {
alpar@399
   627
          _level->liftHighestActiveToTop();
alpar@399
   628
          _el = _node_num;
alpar@399
   629
          return false;
alpar@399
   630
        }
alpar@399
   631
        else {
alpar@399
   632
          _level->liftHighestActive(mlevel+1);
alpar@399
   633
          if(_level->onLevel(actlevel)==0) {
alpar@399
   634
            _el = actlevel;
alpar@399
   635
            return false;
alpar@399
   636
          }
alpar@399
   637
        }
alpar@399
   638
      next_l:
alpar@399
   639
        ;
alpar@399
   640
      }
alpar@399
   641
      return true;
alpar@399
   642
    }
alpar@399
   643
kpeter@402
   644
    /// Runs the algorithm.
alpar@399
   645
kpeter@402
   646
    /// This function runs the algorithm.
kpeter@402
   647
    ///
kpeter@402
   648
    /// \return \c true if a feasible circulation is found.
kpeter@402
   649
    ///
kpeter@402
   650
    /// \note Apart from the return value, c.run() is just a shortcut of
kpeter@402
   651
    /// the following code.
alpar@399
   652
    /// \code
kpeter@402
   653
    ///   c.greedyInit();
kpeter@402
   654
    ///   c.start();
alpar@399
   655
    /// \endcode
alpar@399
   656
    bool run() {
alpar@399
   657
      greedyInit();
alpar@399
   658
      return start();
alpar@399
   659
    }
alpar@399
   660
alpar@399
   661
    /// @}
alpar@399
   662
alpar@399
   663
    /// \name Query Functions
kpeter@402
   664
    /// The results of the circulation algorithm can be obtained using
kpeter@402
   665
    /// these functions.\n
kpeter@402
   666
    /// Either \ref run() or \ref start() should be called before
kpeter@402
   667
    /// using them.
alpar@399
   668
alpar@399
   669
    ///@{
alpar@399
   670
kpeter@641
   671
    /// \brief Returns the flow value on the given arc.
kpeter@402
   672
    ///
kpeter@641
   673
    /// Returns the flow value on the given arc.
kpeter@402
   674
    ///
kpeter@402
   675
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@402
   676
    /// using this function.
kpeter@641
   677
    Value flow(const Arc& arc) const {
kpeter@402
   678
      return (*_flow)[arc];
kpeter@402
   679
    }
kpeter@402
   680
kpeter@402
   681
    /// \brief Returns a const reference to the flow map.
kpeter@402
   682
    ///
kpeter@402
   683
    /// Returns a const reference to the arc map storing the found flow.
kpeter@402
   684
    ///
kpeter@402
   685
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@402
   686
    /// using this function.
kpeter@420
   687
    const FlowMap& flowMap() const {
kpeter@402
   688
      return *_flow;
kpeter@402
   689
    }
kpeter@402
   690
alpar@399
   691
    /**
kpeter@402
   692
       \brief Returns \c true if the given node is in a barrier.
kpeter@402
   693
alpar@399
   694
       Barrier is a set \e B of nodes for which
kpeter@402
   695
kpeter@610
   696
       \f[ \sum_{uv\in A: u\in B} upper(uv) -
kpeter@610
   697
           \sum_{uv\in A: v\in B} lower(uv) < \sum_{v\in B} sup(v) \f]
kpeter@402
   698
kpeter@402
   699
       holds. The existence of a set with this property prooves that a
kpeter@402
   700
       feasible circualtion cannot exist.
kpeter@402
   701
kpeter@402
   702
       This function returns \c true if the given node is in the found
kpeter@402
   703
       barrier. If a feasible circulation is found, the function
kpeter@402
   704
       gives back \c false for every node.
kpeter@402
   705
kpeter@402
   706
       \pre Either \ref run() or \ref init() must be called before
kpeter@402
   707
       using this function.
kpeter@402
   708
kpeter@402
   709
       \sa barrierMap()
alpar@399
   710
       \sa checkBarrier()
alpar@399
   711
    */
kpeter@420
   712
    bool barrier(const Node& node) const
kpeter@402
   713
    {
kpeter@402
   714
      return (*_level)[node] >= _el;
kpeter@402
   715
    }
kpeter@402
   716
kpeter@402
   717
    /// \brief Gives back a barrier.
kpeter@402
   718
    ///
kpeter@402
   719
    /// This function sets \c bar to the characteristic vector of the
kpeter@402
   720
    /// found barrier. \c bar should be a \ref concepts::WriteMap "writable"
kpeter@402
   721
    /// node map with \c bool (or convertible) value type.
kpeter@402
   722
    ///
kpeter@402
   723
    /// If a feasible circulation is found, the function gives back an
kpeter@402
   724
    /// empty set, so \c bar[v] will be \c false for all nodes \c v.
kpeter@402
   725
    ///
kpeter@402
   726
    /// \note This function calls \ref barrier() for each node,
kpeter@559
   727
    /// so it runs in O(n) time.
kpeter@402
   728
    ///
kpeter@402
   729
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@402
   730
    /// using this function.
kpeter@402
   731
    ///
kpeter@402
   732
    /// \sa barrier()
kpeter@402
   733
    /// \sa checkBarrier()
kpeter@402
   734
    template<class BarrierMap>
kpeter@420
   735
    void barrierMap(BarrierMap &bar) const
alpar@399
   736
    {
alpar@399
   737
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   738
        bar.set(n, (*_level)[n] >= _el);
alpar@399
   739
    }
alpar@399
   740
alpar@399
   741
    /// @}
alpar@399
   742
alpar@399
   743
    /// \name Checker Functions
kpeter@402
   744
    /// The feasibility of the results can be checked using
kpeter@402
   745
    /// these functions.\n
kpeter@402
   746
    /// Either \ref run() or \ref start() should be called before
kpeter@402
   747
    /// using them.
alpar@399
   748
alpar@399
   749
    ///@{
alpar@399
   750
kpeter@402
   751
    ///Check if the found flow is a feasible circulation
kpeter@402
   752
kpeter@402
   753
    ///Check if the found flow is a feasible circulation,
kpeter@402
   754
    ///
kpeter@420
   755
    bool checkFlow() const {
alpar@399
   756
      for(ArcIt e(_g);e!=INVALID;++e)
alpar@399
   757
        if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false;
alpar@399
   758
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   759
        {
kpeter@641
   760
          Value dif=-(*_supply)[n];
alpar@399
   761
          for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e];
alpar@399
   762
          for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e];
alpar@399
   763
          if(_tol.negative(dif)) return false;
alpar@399
   764
        }
alpar@399
   765
      return true;
alpar@399
   766
    }
alpar@399
   767
alpar@399
   768
    ///Check whether or not the last execution provides a barrier
alpar@399
   769
kpeter@402
   770
    ///Check whether or not the last execution provides a barrier.
alpar@399
   771
    ///\sa barrier()
kpeter@402
   772
    ///\sa barrierMap()
kpeter@420
   773
    bool checkBarrier() const
alpar@399
   774
    {
kpeter@641
   775
      Value delta=0;
kpeter@641
   776
      Value inf_cap = std::numeric_limits<Value>::has_infinity ?
kpeter@641
   777
        std::numeric_limits<Value>::infinity() :
kpeter@641
   778
        std::numeric_limits<Value>::max();
alpar@399
   779
      for(NodeIt n(_g);n!=INVALID;++n)
alpar@399
   780
        if(barrier(n))
kpeter@610
   781
          delta-=(*_supply)[n];
alpar@399
   782
      for(ArcIt e(_g);e!=INVALID;++e)
alpar@399
   783
        {
alpar@399
   784
          Node s=_g.source(e);
alpar@399
   785
          Node t=_g.target(e);
kpeter@622
   786
          if(barrier(s)&&!barrier(t)) {
kpeter@622
   787
            if (_tol.less(inf_cap - (*_up)[e], delta)) return false;
kpeter@622
   788
            delta+=(*_up)[e];
kpeter@622
   789
          }
alpar@399
   790
          else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e];
alpar@399
   791
        }
alpar@399
   792
      return _tol.negative(delta);
alpar@399
   793
    }
alpar@399
   794
alpar@399
   795
    /// @}
alpar@399
   796
alpar@399
   797
  };
alpar@399
   798
alpar@399
   799
}
alpar@399
   800
alpar@399
   801
#endif