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