lemon/preflow.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 21 Nov 2008 14:11:29 +0000
changeset 404 660db48f324f
child 405 53c5277ba294
permissions -rw-r--r--
Port preflow push max flow alg. from svn -r3516 (#176)
Namely,
- port the files
- apply the migrate script
- apply the unify script
- break the long lines in lemon/preflow.h
- convert the .dim test file to .lgf
- fix compilation problems
alpar@404
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@404
     2
 *
alpar@404
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@404
     4
 *
alpar@404
     5
 * Copyright (C) 2003-2008
alpar@404
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@404
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@404
     8
 *
alpar@404
     9
 * Permission to use, modify and distribute this software is granted
alpar@404
    10
 * provided that this copyright notice appears in all copies. For
alpar@404
    11
 * precise terms see the accompanying LICENSE file.
alpar@404
    12
 *
alpar@404
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@404
    14
 * express or implied, and with no claim as to its suitability for any
alpar@404
    15
 * purpose.
alpar@404
    16
 *
alpar@404
    17
 */
alpar@404
    18
alpar@404
    19
#ifndef LEMON_PREFLOW_H
alpar@404
    20
#define LEMON_PREFLOW_H
alpar@404
    21
alpar@404
    22
#include <lemon/error.h>
alpar@404
    23
#include <lemon/tolerance.h>
alpar@404
    24
#include <lemon/elevator.h>
alpar@404
    25
alpar@404
    26
/// \file
alpar@404
    27
/// \ingroup max_flow
alpar@404
    28
/// \brief Implementation of the preflow algorithm.
alpar@404
    29
alpar@404
    30
namespace lemon {
alpar@404
    31
alpar@404
    32
  /// \brief Default traits class of Preflow class.
alpar@404
    33
  ///
alpar@404
    34
  /// Default traits class of Preflow class.
alpar@404
    35
  /// \param _Graph Digraph type.
alpar@404
    36
  /// \param _CapacityMap Type of capacity map.
alpar@404
    37
  template <typename _Graph, typename _CapacityMap>
alpar@404
    38
  struct PreflowDefaultTraits {
alpar@404
    39
alpar@404
    40
    /// \brief The digraph type the algorithm runs on.
alpar@404
    41
    typedef _Graph Digraph;
alpar@404
    42
alpar@404
    43
    /// \brief The type of the map that stores the arc capacities.
alpar@404
    44
    ///
alpar@404
    45
    /// The type of the map that stores the arc capacities.
alpar@404
    46
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@404
    47
    typedef _CapacityMap CapacityMap;
alpar@404
    48
alpar@404
    49
    /// \brief The type of the length of the arcs.
alpar@404
    50
    typedef typename CapacityMap::Value Value;
alpar@404
    51
alpar@404
    52
    /// \brief The map type that stores the flow values.
alpar@404
    53
    ///
alpar@404
    54
    /// The map type that stores the flow values.
alpar@404
    55
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@404
    56
    typedef typename Digraph::template ArcMap<Value> FlowMap;
alpar@404
    57
alpar@404
    58
    /// \brief Instantiates a FlowMap.
alpar@404
    59
    ///
alpar@404
    60
    /// This function instantiates a \ref FlowMap.
alpar@404
    61
    /// \param digraph The digraph, to which we would like to define
alpar@404
    62
    /// the flow map.
alpar@404
    63
    static FlowMap* createFlowMap(const Digraph& digraph) {
alpar@404
    64
      return new FlowMap(digraph);
alpar@404
    65
    }
alpar@404
    66
alpar@404
    67
    /// \brief The eleavator type used by Preflow algorithm.
alpar@404
    68
    ///
alpar@404
    69
    /// The elevator type used by Preflow algorithm.
alpar@404
    70
    ///
alpar@404
    71
    /// \sa Elevator
alpar@404
    72
    /// \sa LinkedElevator
alpar@404
    73
    typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator;
alpar@404
    74
alpar@404
    75
    /// \brief Instantiates an Elevator.
alpar@404
    76
    ///
alpar@404
    77
    /// This function instantiates a \ref Elevator.
alpar@404
    78
    /// \param digraph The digraph, to which we would like to define
alpar@404
    79
    /// the elevator.
alpar@404
    80
    /// \param max_level The maximum level of the elevator.
alpar@404
    81
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
alpar@404
    82
      return new Elevator(digraph, max_level);
alpar@404
    83
    }
alpar@404
    84
alpar@404
    85
    /// \brief The tolerance used by the algorithm
alpar@404
    86
    ///
alpar@404
    87
    /// The tolerance used by the algorithm to handle inexact computation.
alpar@404
    88
    typedef lemon::Tolerance<Value> Tolerance;
alpar@404
    89
alpar@404
    90
  };
alpar@404
    91
alpar@404
    92
alpar@404
    93
  /// \ingroup max_flow
alpar@404
    94
  ///
alpar@404
    95
  /// \brief %Preflow algorithms class.
alpar@404
    96
  ///
alpar@404
    97
  /// This class provides an implementation of the Goldberg's \e
alpar@404
    98
  /// preflow \e algorithm producing a flow of maximum value in a
alpar@404
    99
  /// digraph. The preflow algorithms are the fastest known max
alpar@404
   100
  /// flow algorithms. The current implementation use a mixture of the
alpar@404
   101
  /// \e "highest label" and the \e "bound decrease" heuristics.
alpar@404
   102
  /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
alpar@404
   103
  ///
alpar@404
   104
  /// The algorithm consists from two phases. After the first phase
alpar@404
   105
  /// the maximal flow value and the minimum cut can be obtained. The
alpar@404
   106
  /// second phase constructs the feasible maximum flow on each arc.
alpar@404
   107
  ///
alpar@404
   108
  /// \param _Graph The digraph type the algorithm runs on.
alpar@404
   109
  /// \param _CapacityMap The flow map type.
alpar@404
   110
  /// \param _Traits Traits class to set various data types used by
alpar@404
   111
  /// the algorithm.  The default traits class is \ref
alpar@404
   112
  /// PreflowDefaultTraits.  See \ref PreflowDefaultTraits for the
alpar@404
   113
  /// documentation of a %Preflow traits class.
alpar@404
   114
  ///
alpar@404
   115
  ///\author Jacint Szabo and Balazs Dezso
alpar@404
   116
#ifdef DOXYGEN
alpar@404
   117
  template <typename _Graph, typename _CapacityMap, typename _Traits>
alpar@404
   118
#else
alpar@404
   119
  template <typename _Graph,
alpar@404
   120
            typename _CapacityMap = typename _Graph::template ArcMap<int>,
alpar@404
   121
            typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> >
alpar@404
   122
#endif
alpar@404
   123
  class Preflow {
alpar@404
   124
  public:
alpar@404
   125
alpar@404
   126
    typedef _Traits Traits;
alpar@404
   127
    typedef typename Traits::Digraph Digraph;
alpar@404
   128
    typedef typename Traits::CapacityMap CapacityMap;
alpar@404
   129
    typedef typename Traits::Value Value;
alpar@404
   130
alpar@404
   131
    typedef typename Traits::FlowMap FlowMap;
alpar@404
   132
    typedef typename Traits::Elevator Elevator;
alpar@404
   133
    typedef typename Traits::Tolerance Tolerance;
alpar@404
   134
alpar@404
   135
    /// \brief \ref Exception for uninitialized parameters.
alpar@404
   136
    ///
alpar@404
   137
    /// This error represents problems in the initialization
alpar@404
   138
    /// of the parameters of the algorithms.
alpar@404
   139
    class UninitializedParameter : public lemon::Exception {
alpar@404
   140
    public:
alpar@404
   141
      virtual const char* what() const throw() {
alpar@404
   142
        return "lemon::Preflow::UninitializedParameter";
alpar@404
   143
      }
alpar@404
   144
    };
alpar@404
   145
alpar@404
   146
  private:
alpar@404
   147
alpar@404
   148
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@404
   149
alpar@404
   150
    const Digraph& _graph;
alpar@404
   151
    const CapacityMap* _capacity;
alpar@404
   152
alpar@404
   153
    int _node_num;
alpar@404
   154
alpar@404
   155
    Node _source, _target;
alpar@404
   156
alpar@404
   157
    FlowMap* _flow;
alpar@404
   158
    bool _local_flow;
alpar@404
   159
alpar@404
   160
    Elevator* _level;
alpar@404
   161
    bool _local_level;
alpar@404
   162
alpar@404
   163
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
alpar@404
   164
    ExcessMap* _excess;
alpar@404
   165
alpar@404
   166
    Tolerance _tolerance;
alpar@404
   167
alpar@404
   168
    bool _phase;
alpar@404
   169
alpar@404
   170
alpar@404
   171
    void createStructures() {
alpar@404
   172
      _node_num = countNodes(_graph);
alpar@404
   173
alpar@404
   174
      if (!_flow) {
alpar@404
   175
        _flow = Traits::createFlowMap(_graph);
alpar@404
   176
        _local_flow = true;
alpar@404
   177
      }
alpar@404
   178
      if (!_level) {
alpar@404
   179
        _level = Traits::createElevator(_graph, _node_num);
alpar@404
   180
        _local_level = true;
alpar@404
   181
      }
alpar@404
   182
      if (!_excess) {
alpar@404
   183
        _excess = new ExcessMap(_graph);
alpar@404
   184
      }
alpar@404
   185
    }
alpar@404
   186
alpar@404
   187
    void destroyStructures() {
alpar@404
   188
      if (_local_flow) {
alpar@404
   189
        delete _flow;
alpar@404
   190
      }
alpar@404
   191
      if (_local_level) {
alpar@404
   192
        delete _level;
alpar@404
   193
      }
alpar@404
   194
      if (_excess) {
alpar@404
   195
        delete _excess;
alpar@404
   196
      }
alpar@404
   197
    }
alpar@404
   198
alpar@404
   199
  public:
alpar@404
   200
alpar@404
   201
    typedef Preflow Create;
alpar@404
   202
alpar@404
   203
    ///\name Named template parameters
alpar@404
   204
alpar@404
   205
    ///@{
alpar@404
   206
alpar@404
   207
    template <typename _FlowMap>
alpar@404
   208
    struct DefFlowMapTraits : public Traits {
alpar@404
   209
      typedef _FlowMap FlowMap;
alpar@404
   210
      static FlowMap *createFlowMap(const Digraph&) {
alpar@404
   211
        throw UninitializedParameter();
alpar@404
   212
      }
alpar@404
   213
    };
alpar@404
   214
alpar@404
   215
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@404
   216
    /// FlowMap type
alpar@404
   217
    ///
alpar@404
   218
    /// \ref named-templ-param "Named parameter" for setting FlowMap
alpar@404
   219
    /// type
alpar@404
   220
    template <typename _FlowMap>
alpar@404
   221
    struct DefFlowMap
alpar@404
   222
      : public Preflow<Digraph, CapacityMap, DefFlowMapTraits<_FlowMap> > {
alpar@404
   223
      typedef Preflow<Digraph, CapacityMap,
alpar@404
   224
                      DefFlowMapTraits<_FlowMap> > Create;
alpar@404
   225
    };
alpar@404
   226
alpar@404
   227
    template <typename _Elevator>
alpar@404
   228
    struct DefElevatorTraits : public Traits {
alpar@404
   229
      typedef _Elevator Elevator;
alpar@404
   230
      static Elevator *createElevator(const Digraph&, int) {
alpar@404
   231
        throw UninitializedParameter();
alpar@404
   232
      }
alpar@404
   233
    };
alpar@404
   234
alpar@404
   235
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@404
   236
    /// Elevator type
alpar@404
   237
    ///
alpar@404
   238
    /// \ref named-templ-param "Named parameter" for setting Elevator
alpar@404
   239
    /// type
alpar@404
   240
    template <typename _Elevator>
alpar@404
   241
    struct DefElevator
alpar@404
   242
      : public Preflow<Digraph, CapacityMap, DefElevatorTraits<_Elevator> > {
alpar@404
   243
      typedef Preflow<Digraph, CapacityMap,
alpar@404
   244
                      DefElevatorTraits<_Elevator> > Create;
alpar@404
   245
    };
alpar@404
   246
alpar@404
   247
    template <typename _Elevator>
alpar@404
   248
    struct DefStandardElevatorTraits : public Traits {
alpar@404
   249
      typedef _Elevator Elevator;
alpar@404
   250
      static Elevator *createElevator(const Digraph& digraph, int max_level) {
alpar@404
   251
        return new Elevator(digraph, max_level);
alpar@404
   252
      }
alpar@404
   253
    };
alpar@404
   254
alpar@404
   255
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@404
   256
    /// Elevator type
alpar@404
   257
    ///
alpar@404
   258
    /// \ref named-templ-param "Named parameter" for setting Elevator
alpar@404
   259
    /// type. The Elevator should be standard constructor interface, ie.
alpar@404
   260
    /// the digraph and the maximum level should be passed to it.
alpar@404
   261
    template <typename _Elevator>
alpar@404
   262
    struct DefStandardElevator
alpar@404
   263
      : public Preflow<Digraph, CapacityMap,
alpar@404
   264
                       DefStandardElevatorTraits<_Elevator> > {
alpar@404
   265
      typedef Preflow<Digraph, CapacityMap,
alpar@404
   266
                      DefStandardElevatorTraits<_Elevator> > Create;
alpar@404
   267
    };
alpar@404
   268
alpar@404
   269
    /// @}
alpar@404
   270
alpar@404
   271
  protected:
alpar@404
   272
alpar@404
   273
    Preflow() {}
alpar@404
   274
alpar@404
   275
  public:
alpar@404
   276
alpar@404
   277
alpar@404
   278
    /// \brief The constructor of the class.
alpar@404
   279
    ///
alpar@404
   280
    /// The constructor of the class.
alpar@404
   281
    /// \param digraph The digraph the algorithm runs on.
alpar@404
   282
    /// \param capacity The capacity of the arcs.
alpar@404
   283
    /// \param source The source node.
alpar@404
   284
    /// \param target The target node.
alpar@404
   285
    Preflow(const Digraph& digraph, const CapacityMap& capacity,
alpar@404
   286
               Node source, Node target)
alpar@404
   287
      : _graph(digraph), _capacity(&capacity),
alpar@404
   288
        _node_num(0), _source(source), _target(target),
alpar@404
   289
        _flow(0), _local_flow(false),
alpar@404
   290
        _level(0), _local_level(false),
alpar@404
   291
        _excess(0), _tolerance(), _phase() {}
alpar@404
   292
alpar@404
   293
    /// \brief Destrcutor.
alpar@404
   294
    ///
alpar@404
   295
    /// Destructor.
alpar@404
   296
    ~Preflow() {
alpar@404
   297
      destroyStructures();
alpar@404
   298
    }
alpar@404
   299
alpar@404
   300
    /// \brief Sets the capacity map.
alpar@404
   301
    ///
alpar@404
   302
    /// Sets the capacity map.
alpar@404
   303
    /// \return \c (*this)
alpar@404
   304
    Preflow& capacityMap(const CapacityMap& map) {
alpar@404
   305
      _capacity = &map;
alpar@404
   306
      return *this;
alpar@404
   307
    }
alpar@404
   308
alpar@404
   309
    /// \brief Sets the flow map.
alpar@404
   310
    ///
alpar@404
   311
    /// Sets the flow map.
alpar@404
   312
    /// \return \c (*this)
alpar@404
   313
    Preflow& flowMap(FlowMap& map) {
alpar@404
   314
      if (_local_flow) {
alpar@404
   315
        delete _flow;
alpar@404
   316
        _local_flow = false;
alpar@404
   317
      }
alpar@404
   318
      _flow = &map;
alpar@404
   319
      return *this;
alpar@404
   320
    }
alpar@404
   321
alpar@404
   322
    /// \brief Returns the flow map.
alpar@404
   323
    ///
alpar@404
   324
    /// \return The flow map.
alpar@404
   325
    const FlowMap& flowMap() {
alpar@404
   326
      return *_flow;
alpar@404
   327
    }
alpar@404
   328
alpar@404
   329
    /// \brief Sets the elevator.
alpar@404
   330
    ///
alpar@404
   331
    /// Sets the elevator.
alpar@404
   332
    /// \return \c (*this)
alpar@404
   333
    Preflow& elevator(Elevator& elevator) {
alpar@404
   334
      if (_local_level) {
alpar@404
   335
        delete _level;
alpar@404
   336
        _local_level = false;
alpar@404
   337
      }
alpar@404
   338
      _level = &elevator;
alpar@404
   339
      return *this;
alpar@404
   340
    }
alpar@404
   341
alpar@404
   342
    /// \brief Returns the elevator.
alpar@404
   343
    ///
alpar@404
   344
    /// \return The elevator.
alpar@404
   345
    const Elevator& elevator() {
alpar@404
   346
      return *_level;
alpar@404
   347
    }
alpar@404
   348
alpar@404
   349
    /// \brief Sets the source node.
alpar@404
   350
    ///
alpar@404
   351
    /// Sets the source node.
alpar@404
   352
    /// \return \c (*this)
alpar@404
   353
    Preflow& source(const Node& node) {
alpar@404
   354
      _source = node;
alpar@404
   355
      return *this;
alpar@404
   356
    }
alpar@404
   357
alpar@404
   358
    /// \brief Sets the target node.
alpar@404
   359
    ///
alpar@404
   360
    /// Sets the target node.
alpar@404
   361
    /// \return \c (*this)
alpar@404
   362
    Preflow& target(const Node& node) {
alpar@404
   363
      _target = node;
alpar@404
   364
      return *this;
alpar@404
   365
    }
alpar@404
   366
alpar@404
   367
    /// \brief Sets the tolerance used by algorithm.
alpar@404
   368
    ///
alpar@404
   369
    /// Sets the tolerance used by algorithm.
alpar@404
   370
    Preflow& tolerance(const Tolerance& tolerance) const {
alpar@404
   371
      _tolerance = tolerance;
alpar@404
   372
      return *this;
alpar@404
   373
    }
alpar@404
   374
alpar@404
   375
    /// \brief Returns the tolerance used by algorithm.
alpar@404
   376
    ///
alpar@404
   377
    /// Returns the tolerance used by algorithm.
alpar@404
   378
    const Tolerance& tolerance() const {
alpar@404
   379
      return tolerance;
alpar@404
   380
    }
alpar@404
   381
alpar@404
   382
    /// \name Execution control The simplest way to execute the
alpar@404
   383
    /// algorithm is to use one of the member functions called \c
alpar@404
   384
    /// run().
alpar@404
   385
    /// \n
alpar@404
   386
    /// If you need more control on initial solution or
alpar@404
   387
    /// execution then you have to call one \ref init() function and then
alpar@404
   388
    /// the startFirstPhase() and if you need the startSecondPhase().
alpar@404
   389
alpar@404
   390
    ///@{
alpar@404
   391
alpar@404
   392
    /// \brief Initializes the internal data structures.
alpar@404
   393
    ///
alpar@404
   394
    /// Initializes the internal data structures.
alpar@404
   395
    ///
alpar@404
   396
    void init() {
alpar@404
   397
      createStructures();
alpar@404
   398
alpar@404
   399
      _phase = true;
alpar@404
   400
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@404
   401
        _excess->set(n, 0);
alpar@404
   402
      }
alpar@404
   403
alpar@404
   404
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@404
   405
        _flow->set(e, 0);
alpar@404
   406
      }
alpar@404
   407
alpar@404
   408
      typename Digraph::template NodeMap<bool> reached(_graph, false);
alpar@404
   409
alpar@404
   410
      _level->initStart();
alpar@404
   411
      _level->initAddItem(_target);
alpar@404
   412
alpar@404
   413
      std::vector<Node> queue;
alpar@404
   414
      reached.set(_source, true);
alpar@404
   415
alpar@404
   416
      queue.push_back(_target);
alpar@404
   417
      reached.set(_target, true);
alpar@404
   418
      while (!queue.empty()) {
alpar@404
   419
        _level->initNewLevel();
alpar@404
   420
        std::vector<Node> nqueue;
alpar@404
   421
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@404
   422
          Node n = queue[i];
alpar@404
   423
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   424
            Node u = _graph.source(e);
alpar@404
   425
            if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
alpar@404
   426
              reached.set(u, true);
alpar@404
   427
              _level->initAddItem(u);
alpar@404
   428
              nqueue.push_back(u);
alpar@404
   429
            }
alpar@404
   430
          }
alpar@404
   431
        }
alpar@404
   432
        queue.swap(nqueue);
alpar@404
   433
      }
alpar@404
   434
      _level->initFinish();
alpar@404
   435
alpar@404
   436
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@404
   437
        if (_tolerance.positive((*_capacity)[e])) {
alpar@404
   438
          Node u = _graph.target(e);
alpar@404
   439
          if ((*_level)[u] == _level->maxLevel()) continue;
alpar@404
   440
          _flow->set(e, (*_capacity)[e]);
alpar@404
   441
          _excess->set(u, (*_excess)[u] + (*_capacity)[e]);
alpar@404
   442
          if (u != _target && !_level->active(u)) {
alpar@404
   443
            _level->activate(u);
alpar@404
   444
          }
alpar@404
   445
        }
alpar@404
   446
      }
alpar@404
   447
    }
alpar@404
   448
alpar@404
   449
    /// \brief Initializes the internal data structures.
alpar@404
   450
    ///
alpar@404
   451
    /// Initializes the internal data structures and sets the initial
alpar@404
   452
    /// flow to the given \c flowMap. The \c flowMap should contain a
alpar@404
   453
    /// flow or at least a preflow, ie. in each node excluding the
alpar@404
   454
    /// target the incoming flow should greater or equal to the
alpar@404
   455
    /// outgoing flow.
alpar@404
   456
    /// \return %False when the given \c flowMap is not a preflow.
alpar@404
   457
    template <typename FlowMap>
alpar@404
   458
    bool flowInit(const FlowMap& flowMap) {
alpar@404
   459
      createStructures();
alpar@404
   460
alpar@404
   461
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@404
   462
        _flow->set(e, flowMap[e]);
alpar@404
   463
      }
alpar@404
   464
alpar@404
   465
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@404
   466
        Value excess = 0;
alpar@404
   467
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   468
          excess += (*_flow)[e];
alpar@404
   469
        }
alpar@404
   470
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   471
          excess -= (*_flow)[e];
alpar@404
   472
        }
alpar@404
   473
        if (excess < 0 && n != _source) return false;
alpar@404
   474
        _excess->set(n, excess);
alpar@404
   475
      }
alpar@404
   476
alpar@404
   477
      typename Digraph::template NodeMap<bool> reached(_graph, false);
alpar@404
   478
alpar@404
   479
      _level->initStart();
alpar@404
   480
      _level->initAddItem(_target);
alpar@404
   481
alpar@404
   482
      std::vector<Node> queue;
alpar@404
   483
      reached.set(_source, true);
alpar@404
   484
alpar@404
   485
      queue.push_back(_target);
alpar@404
   486
      reached.set(_target, true);
alpar@404
   487
      while (!queue.empty()) {
alpar@404
   488
        _level->initNewLevel();
alpar@404
   489
        std::vector<Node> nqueue;
alpar@404
   490
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@404
   491
          Node n = queue[i];
alpar@404
   492
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   493
            Node u = _graph.source(e);
alpar@404
   494
            if (!reached[u] &&
alpar@404
   495
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
alpar@404
   496
              reached.set(u, true);
alpar@404
   497
              _level->initAddItem(u);
alpar@404
   498
              nqueue.push_back(u);
alpar@404
   499
            }
alpar@404
   500
          }
alpar@404
   501
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   502
            Node v = _graph.target(e);
alpar@404
   503
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
alpar@404
   504
              reached.set(v, true);
alpar@404
   505
              _level->initAddItem(v);
alpar@404
   506
              nqueue.push_back(v);
alpar@404
   507
            }
alpar@404
   508
          }
alpar@404
   509
        }
alpar@404
   510
        queue.swap(nqueue);
alpar@404
   511
      }
alpar@404
   512
      _level->initFinish();
alpar@404
   513
alpar@404
   514
      for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@404
   515
        Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@404
   516
        if (_tolerance.positive(rem)) {
alpar@404
   517
          Node u = _graph.target(e);
alpar@404
   518
          if ((*_level)[u] == _level->maxLevel()) continue;
alpar@404
   519
          _flow->set(e, (*_capacity)[e]);
alpar@404
   520
          _excess->set(u, (*_excess)[u] + rem);
alpar@404
   521
          if (u != _target && !_level->active(u)) {
alpar@404
   522
            _level->activate(u);
alpar@404
   523
          }
alpar@404
   524
        }
alpar@404
   525
      }
alpar@404
   526
      for (InArcIt e(_graph, _source); e != INVALID; ++e) {
alpar@404
   527
        Value rem = (*_flow)[e];
alpar@404
   528
        if (_tolerance.positive(rem)) {
alpar@404
   529
          Node v = _graph.source(e);
alpar@404
   530
          if ((*_level)[v] == _level->maxLevel()) continue;
alpar@404
   531
          _flow->set(e, 0);
alpar@404
   532
          _excess->set(v, (*_excess)[v] + rem);
alpar@404
   533
          if (v != _target && !_level->active(v)) {
alpar@404
   534
            _level->activate(v);
alpar@404
   535
          }
alpar@404
   536
        }
alpar@404
   537
      }
alpar@404
   538
      return true;
alpar@404
   539
    }
alpar@404
   540
alpar@404
   541
    /// \brief Starts the first phase of the preflow algorithm.
alpar@404
   542
    ///
alpar@404
   543
    /// The preflow algorithm consists of two phases, this method runs
alpar@404
   544
    /// the first phase. After the first phase the maximum flow value
alpar@404
   545
    /// and a minimum value cut can already be computed, although a
alpar@404
   546
    /// maximum flow is not yet obtained. So after calling this method
alpar@404
   547
    /// \ref flowValue() returns the value of a maximum flow and \ref
alpar@404
   548
    /// minCut() returns a minimum cut.
alpar@404
   549
    /// \pre One of the \ref init() functions should be called.
alpar@404
   550
    void startFirstPhase() {
alpar@404
   551
      _phase = true;
alpar@404
   552
alpar@404
   553
      Node n = _level->highestActive();
alpar@404
   554
      int level = _level->highestActiveLevel();
alpar@404
   555
      while (n != INVALID) {
alpar@404
   556
        int num = _node_num;
alpar@404
   557
alpar@404
   558
        while (num > 0 && n != INVALID) {
alpar@404
   559
          Value excess = (*_excess)[n];
alpar@404
   560
          int new_level = _level->maxLevel();
alpar@404
   561
alpar@404
   562
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   563
            Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@404
   564
            if (!_tolerance.positive(rem)) continue;
alpar@404
   565
            Node v = _graph.target(e);
alpar@404
   566
            if ((*_level)[v] < level) {
alpar@404
   567
              if (!_level->active(v) && v != _target) {
alpar@404
   568
                _level->activate(v);
alpar@404
   569
              }
alpar@404
   570
              if (!_tolerance.less(rem, excess)) {
alpar@404
   571
                _flow->set(e, (*_flow)[e] + excess);
alpar@404
   572
                _excess->set(v, (*_excess)[v] + excess);
alpar@404
   573
                excess = 0;
alpar@404
   574
                goto no_more_push_1;
alpar@404
   575
              } else {
alpar@404
   576
                excess -= rem;
alpar@404
   577
                _excess->set(v, (*_excess)[v] + rem);
alpar@404
   578
                _flow->set(e, (*_capacity)[e]);
alpar@404
   579
              }
alpar@404
   580
            } else if (new_level > (*_level)[v]) {
alpar@404
   581
              new_level = (*_level)[v];
alpar@404
   582
            }
alpar@404
   583
          }
alpar@404
   584
alpar@404
   585
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   586
            Value rem = (*_flow)[e];
alpar@404
   587
            if (!_tolerance.positive(rem)) continue;
alpar@404
   588
            Node v = _graph.source(e);
alpar@404
   589
            if ((*_level)[v] < level) {
alpar@404
   590
              if (!_level->active(v) && v != _target) {
alpar@404
   591
                _level->activate(v);
alpar@404
   592
              }
alpar@404
   593
              if (!_tolerance.less(rem, excess)) {
alpar@404
   594
                _flow->set(e, (*_flow)[e] - excess);
alpar@404
   595
                _excess->set(v, (*_excess)[v] + excess);
alpar@404
   596
                excess = 0;
alpar@404
   597
                goto no_more_push_1;
alpar@404
   598
              } else {
alpar@404
   599
                excess -= rem;
alpar@404
   600
                _excess->set(v, (*_excess)[v] + rem);
alpar@404
   601
                _flow->set(e, 0);
alpar@404
   602
              }
alpar@404
   603
            } else if (new_level > (*_level)[v]) {
alpar@404
   604
              new_level = (*_level)[v];
alpar@404
   605
            }
alpar@404
   606
          }
alpar@404
   607
alpar@404
   608
        no_more_push_1:
alpar@404
   609
alpar@404
   610
          _excess->set(n, excess);
alpar@404
   611
alpar@404
   612
          if (excess != 0) {
alpar@404
   613
            if (new_level + 1 < _level->maxLevel()) {
alpar@404
   614
              _level->liftHighestActive(new_level + 1);
alpar@404
   615
            } else {
alpar@404
   616
              _level->liftHighestActiveToTop();
alpar@404
   617
            }
alpar@404
   618
            if (_level->emptyLevel(level)) {
alpar@404
   619
              _level->liftToTop(level);
alpar@404
   620
            }
alpar@404
   621
          } else {
alpar@404
   622
            _level->deactivate(n);
alpar@404
   623
          }
alpar@404
   624
alpar@404
   625
          n = _level->highestActive();
alpar@404
   626
          level = _level->highestActiveLevel();
alpar@404
   627
          --num;
alpar@404
   628
        }
alpar@404
   629
alpar@404
   630
        num = _node_num * 20;
alpar@404
   631
        while (num > 0 && n != INVALID) {
alpar@404
   632
          Value excess = (*_excess)[n];
alpar@404
   633
          int new_level = _level->maxLevel();
alpar@404
   634
alpar@404
   635
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   636
            Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@404
   637
            if (!_tolerance.positive(rem)) continue;
alpar@404
   638
            Node v = _graph.target(e);
alpar@404
   639
            if ((*_level)[v] < level) {
alpar@404
   640
              if (!_level->active(v) && v != _target) {
alpar@404
   641
                _level->activate(v);
alpar@404
   642
              }
alpar@404
   643
              if (!_tolerance.less(rem, excess)) {
alpar@404
   644
                _flow->set(e, (*_flow)[e] + excess);
alpar@404
   645
                _excess->set(v, (*_excess)[v] + excess);
alpar@404
   646
                excess = 0;
alpar@404
   647
                goto no_more_push_2;
alpar@404
   648
              } else {
alpar@404
   649
                excess -= rem;
alpar@404
   650
                _excess->set(v, (*_excess)[v] + rem);
alpar@404
   651
                _flow->set(e, (*_capacity)[e]);
alpar@404
   652
              }
alpar@404
   653
            } else if (new_level > (*_level)[v]) {
alpar@404
   654
              new_level = (*_level)[v];
alpar@404
   655
            }
alpar@404
   656
          }
alpar@404
   657
alpar@404
   658
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   659
            Value rem = (*_flow)[e];
alpar@404
   660
            if (!_tolerance.positive(rem)) continue;
alpar@404
   661
            Node v = _graph.source(e);
alpar@404
   662
            if ((*_level)[v] < level) {
alpar@404
   663
              if (!_level->active(v) && v != _target) {
alpar@404
   664
                _level->activate(v);
alpar@404
   665
              }
alpar@404
   666
              if (!_tolerance.less(rem, excess)) {
alpar@404
   667
                _flow->set(e, (*_flow)[e] - excess);
alpar@404
   668
                _excess->set(v, (*_excess)[v] + excess);
alpar@404
   669
                excess = 0;
alpar@404
   670
                goto no_more_push_2;
alpar@404
   671
              } else {
alpar@404
   672
                excess -= rem;
alpar@404
   673
                _excess->set(v, (*_excess)[v] + rem);
alpar@404
   674
                _flow->set(e, 0);
alpar@404
   675
              }
alpar@404
   676
            } else if (new_level > (*_level)[v]) {
alpar@404
   677
              new_level = (*_level)[v];
alpar@404
   678
            }
alpar@404
   679
          }
alpar@404
   680
alpar@404
   681
        no_more_push_2:
alpar@404
   682
alpar@404
   683
          _excess->set(n, excess);
alpar@404
   684
alpar@404
   685
          if (excess != 0) {
alpar@404
   686
            if (new_level + 1 < _level->maxLevel()) {
alpar@404
   687
              _level->liftActiveOn(level, new_level + 1);
alpar@404
   688
            } else {
alpar@404
   689
              _level->liftActiveToTop(level);
alpar@404
   690
            }
alpar@404
   691
            if (_level->emptyLevel(level)) {
alpar@404
   692
              _level->liftToTop(level);
alpar@404
   693
            }
alpar@404
   694
          } else {
alpar@404
   695
            _level->deactivate(n);
alpar@404
   696
          }
alpar@404
   697
alpar@404
   698
          while (level >= 0 && _level->activeFree(level)) {
alpar@404
   699
            --level;
alpar@404
   700
          }
alpar@404
   701
          if (level == -1) {
alpar@404
   702
            n = _level->highestActive();
alpar@404
   703
            level = _level->highestActiveLevel();
alpar@404
   704
          } else {
alpar@404
   705
            n = _level->activeOn(level);
alpar@404
   706
          }
alpar@404
   707
          --num;
alpar@404
   708
        }
alpar@404
   709
      }
alpar@404
   710
    }
alpar@404
   711
alpar@404
   712
    /// \brief Starts the second phase of the preflow algorithm.
alpar@404
   713
    ///
alpar@404
   714
    /// The preflow algorithm consists of two phases, this method runs
alpar@404
   715
    /// the second phase. After calling \ref init() and \ref
alpar@404
   716
    /// startFirstPhase() and then \ref startSecondPhase(), \ref
alpar@404
   717
    /// flowMap() return a maximum flow, \ref flowValue() returns the
alpar@404
   718
    /// value of a maximum flow, \ref minCut() returns a minimum cut
alpar@404
   719
    /// \pre The \ref init() and startFirstPhase() functions should be
alpar@404
   720
    /// called before.
alpar@404
   721
    void startSecondPhase() {
alpar@404
   722
      _phase = false;
alpar@404
   723
alpar@404
   724
      typename Digraph::template NodeMap<bool> reached(_graph);
alpar@404
   725
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@404
   726
        reached.set(n, (*_level)[n] < _level->maxLevel());
alpar@404
   727
      }
alpar@404
   728
alpar@404
   729
      _level->initStart();
alpar@404
   730
      _level->initAddItem(_source);
alpar@404
   731
alpar@404
   732
      std::vector<Node> queue;
alpar@404
   733
      queue.push_back(_source);
alpar@404
   734
      reached.set(_source, true);
alpar@404
   735
alpar@404
   736
      while (!queue.empty()) {
alpar@404
   737
        _level->initNewLevel();
alpar@404
   738
        std::vector<Node> nqueue;
alpar@404
   739
        for (int i = 0; i < int(queue.size()); ++i) {
alpar@404
   740
          Node n = queue[i];
alpar@404
   741
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   742
            Node v = _graph.target(e);
alpar@404
   743
            if (!reached[v] && _tolerance.positive((*_flow)[e])) {
alpar@404
   744
              reached.set(v, true);
alpar@404
   745
              _level->initAddItem(v);
alpar@404
   746
              nqueue.push_back(v);
alpar@404
   747
            }
alpar@404
   748
          }
alpar@404
   749
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   750
            Node u = _graph.source(e);
alpar@404
   751
            if (!reached[u] &&
alpar@404
   752
                _tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
alpar@404
   753
              reached.set(u, true);
alpar@404
   754
              _level->initAddItem(u);
alpar@404
   755
              nqueue.push_back(u);
alpar@404
   756
            }
alpar@404
   757
          }
alpar@404
   758
        }
alpar@404
   759
        queue.swap(nqueue);
alpar@404
   760
      }
alpar@404
   761
      _level->initFinish();
alpar@404
   762
alpar@404
   763
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@404
   764
        if (!reached[n]) {
alpar@404
   765
          _level->dirtyTopButOne(n);
alpar@404
   766
        } else if ((*_excess)[n] > 0 && _target != n) {
alpar@404
   767
          _level->activate(n);
alpar@404
   768
        }
alpar@404
   769
      }
alpar@404
   770
alpar@404
   771
      Node n;
alpar@404
   772
      while ((n = _level->highestActive()) != INVALID) {
alpar@404
   773
        Value excess = (*_excess)[n];
alpar@404
   774
        int level = _level->highestActiveLevel();
alpar@404
   775
        int new_level = _level->maxLevel();
alpar@404
   776
alpar@404
   777
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   778
          Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@404
   779
          if (!_tolerance.positive(rem)) continue;
alpar@404
   780
          Node v = _graph.target(e);
alpar@404
   781
          if ((*_level)[v] < level) {
alpar@404
   782
            if (!_level->active(v) && v != _source) {
alpar@404
   783
              _level->activate(v);
alpar@404
   784
            }
alpar@404
   785
            if (!_tolerance.less(rem, excess)) {
alpar@404
   786
              _flow->set(e, (*_flow)[e] + excess);
alpar@404
   787
              _excess->set(v, (*_excess)[v] + excess);
alpar@404
   788
              excess = 0;
alpar@404
   789
              goto no_more_push;
alpar@404
   790
            } else {
alpar@404
   791
              excess -= rem;
alpar@404
   792
              _excess->set(v, (*_excess)[v] + rem);
alpar@404
   793
              _flow->set(e, (*_capacity)[e]);
alpar@404
   794
            }
alpar@404
   795
          } else if (new_level > (*_level)[v]) {
alpar@404
   796
            new_level = (*_level)[v];
alpar@404
   797
          }
alpar@404
   798
        }
alpar@404
   799
alpar@404
   800
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@404
   801
          Value rem = (*_flow)[e];
alpar@404
   802
          if (!_tolerance.positive(rem)) continue;
alpar@404
   803
          Node v = _graph.source(e);
alpar@404
   804
          if ((*_level)[v] < level) {
alpar@404
   805
            if (!_level->active(v) && v != _source) {
alpar@404
   806
              _level->activate(v);
alpar@404
   807
            }
alpar@404
   808
            if (!_tolerance.less(rem, excess)) {
alpar@404
   809
              _flow->set(e, (*_flow)[e] - excess);
alpar@404
   810
              _excess->set(v, (*_excess)[v] + excess);
alpar@404
   811
              excess = 0;
alpar@404
   812
              goto no_more_push;
alpar@404
   813
            } else {
alpar@404
   814
              excess -= rem;
alpar@404
   815
              _excess->set(v, (*_excess)[v] + rem);
alpar@404
   816
              _flow->set(e, 0);
alpar@404
   817
            }
alpar@404
   818
          } else if (new_level > (*_level)[v]) {
alpar@404
   819
            new_level = (*_level)[v];
alpar@404
   820
          }
alpar@404
   821
        }
alpar@404
   822
alpar@404
   823
      no_more_push:
alpar@404
   824
alpar@404
   825
        _excess->set(n, excess);
alpar@404
   826
alpar@404
   827
        if (excess != 0) {
alpar@404
   828
          if (new_level + 1 < _level->maxLevel()) {
alpar@404
   829
            _level->liftHighestActive(new_level + 1);
alpar@404
   830
          } else {
alpar@404
   831
            // Calculation error
alpar@404
   832
            _level->liftHighestActiveToTop();
alpar@404
   833
          }
alpar@404
   834
          if (_level->emptyLevel(level)) {
alpar@404
   835
            // Calculation error
alpar@404
   836
            _level->liftToTop(level);
alpar@404
   837
          }
alpar@404
   838
        } else {
alpar@404
   839
          _level->deactivate(n);
alpar@404
   840
        }
alpar@404
   841
alpar@404
   842
      }
alpar@404
   843
    }
alpar@404
   844
alpar@404
   845
    /// \brief Runs the preflow algorithm.
alpar@404
   846
    ///
alpar@404
   847
    /// Runs the preflow algorithm.
alpar@404
   848
    /// \note pf.run() is just a shortcut of the following code.
alpar@404
   849
    /// \code
alpar@404
   850
    ///   pf.init();
alpar@404
   851
    ///   pf.startFirstPhase();
alpar@404
   852
    ///   pf.startSecondPhase();
alpar@404
   853
    /// \endcode
alpar@404
   854
    void run() {
alpar@404
   855
      init();
alpar@404
   856
      startFirstPhase();
alpar@404
   857
      startSecondPhase();
alpar@404
   858
    }
alpar@404
   859
alpar@404
   860
    /// \brief Runs the preflow algorithm to compute the minimum cut.
alpar@404
   861
    ///
alpar@404
   862
    /// Runs the preflow algorithm to compute the minimum cut.
alpar@404
   863
    /// \note pf.runMinCut() is just a shortcut of the following code.
alpar@404
   864
    /// \code
alpar@404
   865
    ///   pf.init();
alpar@404
   866
    ///   pf.startFirstPhase();
alpar@404
   867
    /// \endcode
alpar@404
   868
    void runMinCut() {
alpar@404
   869
      init();
alpar@404
   870
      startFirstPhase();
alpar@404
   871
    }
alpar@404
   872
alpar@404
   873
    /// @}
alpar@404
   874
alpar@404
   875
    /// \name Query Functions
alpar@404
   876
    /// The result of the %Preflow algorithm can be obtained using these
alpar@404
   877
    /// functions.\n
alpar@404
   878
    /// Before the use of these functions,
alpar@404
   879
    /// either run() or start() must be called.
alpar@404
   880
alpar@404
   881
    ///@{
alpar@404
   882
alpar@404
   883
    /// \brief Returns the value of the maximum flow.
alpar@404
   884
    ///
alpar@404
   885
    /// Returns the value of the maximum flow by returning the excess
alpar@404
   886
    /// of the target node \c t. This value equals to the value of
alpar@404
   887
    /// the maximum flow already after the first phase.
alpar@404
   888
    Value flowValue() const {
alpar@404
   889
      return (*_excess)[_target];
alpar@404
   890
    }
alpar@404
   891
alpar@404
   892
    /// \brief Returns true when the node is on the source side of minimum cut.
alpar@404
   893
    ///
alpar@404
   894
    /// Returns true when the node is on the source side of minimum
alpar@404
   895
    /// cut. This method can be called both after running \ref
alpar@404
   896
    /// startFirstPhase() and \ref startSecondPhase().
alpar@404
   897
    bool minCut(const Node& node) const {
alpar@404
   898
      return ((*_level)[node] == _level->maxLevel()) == _phase;
alpar@404
   899
    }
alpar@404
   900
alpar@404
   901
    /// \brief Returns a minimum value cut.
alpar@404
   902
    ///
alpar@404
   903
    /// Sets the \c cutMap to the characteristic vector of a minimum value
alpar@404
   904
    /// cut. This method can be called both after running \ref
alpar@404
   905
    /// startFirstPhase() and \ref startSecondPhase(). The result after second
alpar@404
   906
    /// phase could be changed slightly if inexact computation is used.
alpar@404
   907
    /// \pre The \c cutMap should be a bool-valued node-map.
alpar@404
   908
    template <typename CutMap>
alpar@404
   909
    void minCutMap(CutMap& cutMap) const {
alpar@404
   910
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@404
   911
        cutMap.set(n, minCut(n));
alpar@404
   912
      }
alpar@404
   913
    }
alpar@404
   914
alpar@404
   915
    /// \brief Returns the flow on the arc.
alpar@404
   916
    ///
alpar@404
   917
    /// Sets the \c flowMap to the flow on the arcs. This method can
alpar@404
   918
    /// be called after the second phase of algorithm.
alpar@404
   919
    Value flow(const Arc& arc) const {
alpar@404
   920
      return (*_flow)[arc];
alpar@404
   921
    }
alpar@404
   922
alpar@404
   923
    /// @}
alpar@404
   924
  };
alpar@404
   925
}
alpar@404
   926
alpar@404
   927
#endif