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