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