lemon/preflow.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 10 Oct 2009 08:18:46 +0200
changeset 802 134852d7fb0a
parent 762 ece80147fb08
child 835 c92296660262
permissions -rw-r--r--
Insert citations into the doc (#184)

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