lemon/circulation.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 26 Apr 2009 16:44:53 +0200
changeset 672 029a48052c67
parent 628 aa1804409f29
parent 657 dacc2cee2b4c
child 669 28f58740b6f8
permissions -rw-r--r--
Modify the interface of MinCostArborescence + improvements (#267)

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