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