lemon/edmonds_karp.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 28 Feb 2013 18:05:56 +0100
changeset 1058 2f00ef323c2e
parent 1057 6a8a688eacf6
child 1059 08f2dc76e82e
permissions -rw-r--r--
Rename DefFlowMap named parameter to SetFlowMap (#177)
in EdmondsKarp according to Preflow
thoneyvazul@1056
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
thoneyvazul@1056
     2
 *
thoneyvazul@1056
     3
 * This file is a part of LEMON, a generic C++ optimization library.
thoneyvazul@1056
     4
 *
thoneyvazul@1056
     5
 * Copyright (C) 2003-2010
thoneyvazul@1056
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
thoneyvazul@1056
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
thoneyvazul@1056
     8
 *
thoneyvazul@1056
     9
 * Permission to use, modify and distribute this software is granted
thoneyvazul@1056
    10
 * provided that this copyright notice appears in all copies. For
thoneyvazul@1056
    11
 * precise terms see the accompanying LICENSE file.
thoneyvazul@1056
    12
 *
thoneyvazul@1056
    13
 * This software is provided "AS IS" with no warranty of any kind,
thoneyvazul@1056
    14
 * express or implied, and with no claim as to its suitability for any
thoneyvazul@1056
    15
 * purpose.
thoneyvazul@1056
    16
 *
thoneyvazul@1056
    17
 */
thoneyvazul@1056
    18
thoneyvazul@1056
    19
#ifndef LEMON_EDMONDS_KARP_H
thoneyvazul@1056
    20
#define LEMON_EDMONDS_KARP_H
thoneyvazul@1056
    21
thoneyvazul@1056
    22
/// \file
thoneyvazul@1056
    23
/// \ingroup max_flow
thoneyvazul@1056
    24
/// \brief Implementation of the Edmonds-Karp algorithm.
thoneyvazul@1056
    25
thoneyvazul@1056
    26
#include <lemon/tolerance.h>
thoneyvazul@1056
    27
#include <vector>
thoneyvazul@1056
    28
thoneyvazul@1056
    29
namespace lemon {
thoneyvazul@1056
    30
thoneyvazul@1056
    31
  /// \brief Default traits class of EdmondsKarp class.
thoneyvazul@1056
    32
  ///
thoneyvazul@1056
    33
  /// Default traits class of EdmondsKarp class.
thoneyvazul@1056
    34
  /// \param GR Digraph type.
thoneyvazul@1056
    35
  /// \param CAP Type of capacity map.
thoneyvazul@1056
    36
  template <typename GR, typename CAP>
thoneyvazul@1056
    37
  struct EdmondsKarpDefaultTraits {
thoneyvazul@1056
    38
thoneyvazul@1056
    39
    /// \brief The digraph type the algorithm runs on. 
thoneyvazul@1056
    40
    typedef GR Digraph;
thoneyvazul@1056
    41
thoneyvazul@1056
    42
    /// \brief The type of the map that stores the arc capacities.
thoneyvazul@1056
    43
    ///
thoneyvazul@1056
    44
    /// The type of the map that stores the arc capacities.
thoneyvazul@1056
    45
    /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
thoneyvazul@1056
    46
    typedef CAP CapacityMap;
thoneyvazul@1056
    47
kpeter@1057
    48
    /// \brief The type of the flow values.
thoneyvazul@1056
    49
    typedef typename CapacityMap::Value Value;
thoneyvazul@1056
    50
kpeter@1057
    51
    /// \brief The type of the map that stores the flow values.
thoneyvazul@1056
    52
    ///
kpeter@1057
    53
    /// The type of the map that stores the flow values.
thoneyvazul@1056
    54
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
kpeter@1057
    55
#ifdef DOXYGEN
kpeter@1057
    56
    typedef GR::ArcMap<Value> FlowMap;
kpeter@1057
    57
#else
thoneyvazul@1056
    58
    typedef typename Digraph::template ArcMap<Value> FlowMap;
kpeter@1057
    59
#endif
thoneyvazul@1056
    60
thoneyvazul@1056
    61
    /// \brief Instantiates a FlowMap.
thoneyvazul@1056
    62
    ///
thoneyvazul@1056
    63
    /// This function instantiates a \ref FlowMap. 
kpeter@1057
    64
    /// \param digraph The digraph for which we would like to define
kpeter@1057
    65
    /// the flow map.
thoneyvazul@1056
    66
    static FlowMap* createFlowMap(const Digraph& digraph) {
thoneyvazul@1056
    67
      return new FlowMap(digraph);
thoneyvazul@1056
    68
    }
thoneyvazul@1056
    69
thoneyvazul@1056
    70
    /// \brief The tolerance used by the algorithm
thoneyvazul@1056
    71
    ///
thoneyvazul@1056
    72
    /// The tolerance used by the algorithm to handle inexact computation.
thoneyvazul@1056
    73
    typedef lemon::Tolerance<Value> Tolerance;
thoneyvazul@1056
    74
thoneyvazul@1056
    75
  };
thoneyvazul@1056
    76
thoneyvazul@1056
    77
  /// \ingroup max_flow
thoneyvazul@1056
    78
  ///
thoneyvazul@1056
    79
  /// \brief Edmonds-Karp algorithms class.
thoneyvazul@1056
    80
  ///
thoneyvazul@1056
    81
  /// This class provides an implementation of the \e Edmonds-Karp \e
kpeter@1057
    82
  /// algorithm producing a \ref max_flow "flow of maximum value" in a
kpeter@1057
    83
  /// digraph \ref clrs01algorithms, \ref amo93networkflows,
kpeter@1057
    84
  /// \ref edmondskarp72theoretical.
kpeter@1057
    85
  /// The Edmonds-Karp algorithm is slower than the Preflow
kpeter@1057
    86
  /// algorithm, but it has an advantage of the step-by-step execution
thoneyvazul@1056
    87
  /// control with feasible flow solutions. The \e source node, the \e
thoneyvazul@1056
    88
  /// target node, the \e capacity of the arcs and the \e starting \e
thoneyvazul@1056
    89
  /// flow value of the arcs should be passed to the algorithm
thoneyvazul@1056
    90
  /// through the constructor.
thoneyvazul@1056
    91
  ///
thoneyvazul@1056
    92
  /// The time complexity of the algorithm is \f$ O(nm^2) \f$ in
kpeter@1057
    93
  /// worst case. Always try the Preflow algorithm instead of this if
thoneyvazul@1056
    94
  /// you just want to compute the optimal flow.
thoneyvazul@1056
    95
  ///
kpeter@1057
    96
  /// \tparam GR The type of the digraph the algorithm runs on.
kpeter@1057
    97
  /// \tparam CAP The type of the capacity map. The default map
kpeter@1057
    98
  /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". 
kpeter@1057
    99
  /// \tparam TR The traits class that defines various types used by the
kpeter@1057
   100
  /// algorithm. By default, it is \ref EdmondsKarpDefaultTraits
kpeter@1057
   101
  /// "EdmondsKarpDefaultTraits<GR, CAP>".
kpeter@1057
   102
  /// In most cases, this parameter should not be set directly,
kpeter@1057
   103
  /// consider to use the named template parameters instead.
thoneyvazul@1056
   104
thoneyvazul@1056
   105
#ifdef DOXYGEN
thoneyvazul@1056
   106
  template <typename GR, typename CAP, typename TR>
thoneyvazul@1056
   107
#else 
thoneyvazul@1056
   108
  template <typename GR,
thoneyvazul@1056
   109
	    typename CAP = typename GR::template ArcMap<int>,
thoneyvazul@1056
   110
            typename TR = EdmondsKarpDefaultTraits<GR, CAP> >
thoneyvazul@1056
   111
#endif
thoneyvazul@1056
   112
  class EdmondsKarp {
thoneyvazul@1056
   113
  public:
thoneyvazul@1056
   114
kpeter@1057
   115
    /// The \ref EdmondsKarpDefaultTraits "traits class" of the algorithm.
thoneyvazul@1056
   116
    typedef TR Traits;
kpeter@1057
   117
    /// The type of the digraph the algorithm runs on.
thoneyvazul@1056
   118
    typedef typename Traits::Digraph Digraph;
kpeter@1057
   119
    /// The type of the capacity map.
thoneyvazul@1056
   120
    typedef typename Traits::CapacityMap CapacityMap;
kpeter@1057
   121
    /// The type of the flow values.
thoneyvazul@1056
   122
    typedef typename Traits::Value Value; 
thoneyvazul@1056
   123
kpeter@1057
   124
    /// The type of the flow map.
thoneyvazul@1056
   125
    typedef typename Traits::FlowMap FlowMap;
kpeter@1057
   126
    /// The type of the tolerance.
thoneyvazul@1056
   127
    typedef typename Traits::Tolerance Tolerance;
thoneyvazul@1056
   128
thoneyvazul@1056
   129
  private:
thoneyvazul@1056
   130
thoneyvazul@1056
   131
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
thoneyvazul@1056
   132
    typedef typename Digraph::template NodeMap<Arc> PredMap;
thoneyvazul@1056
   133
    
thoneyvazul@1056
   134
    const Digraph& _graph;
thoneyvazul@1056
   135
    const CapacityMap* _capacity;
thoneyvazul@1056
   136
thoneyvazul@1056
   137
    Node _source, _target;
thoneyvazul@1056
   138
thoneyvazul@1056
   139
    FlowMap* _flow;
thoneyvazul@1056
   140
    bool _local_flow;
thoneyvazul@1056
   141
thoneyvazul@1056
   142
    PredMap* _pred;
thoneyvazul@1056
   143
    std::vector<Node> _queue;
thoneyvazul@1056
   144
    
thoneyvazul@1056
   145
    Tolerance _tolerance;
thoneyvazul@1056
   146
    Value _flow_value;
thoneyvazul@1056
   147
thoneyvazul@1056
   148
    void createStructures() {
thoneyvazul@1056
   149
      if (!_flow) {
thoneyvazul@1056
   150
	_flow = Traits::createFlowMap(_graph);
thoneyvazul@1056
   151
	_local_flow = true;
thoneyvazul@1056
   152
      }
thoneyvazul@1056
   153
      if (!_pred) {
thoneyvazul@1056
   154
	_pred = new PredMap(_graph);
thoneyvazul@1056
   155
      }
thoneyvazul@1056
   156
      _queue.resize(countNodes(_graph));
thoneyvazul@1056
   157
    }
thoneyvazul@1056
   158
thoneyvazul@1056
   159
    void destroyStructures() {
thoneyvazul@1056
   160
      if (_local_flow) {
thoneyvazul@1056
   161
	delete _flow;
thoneyvazul@1056
   162
      }
thoneyvazul@1056
   163
      if (_pred) {
thoneyvazul@1056
   164
	delete _pred;
thoneyvazul@1056
   165
      }
thoneyvazul@1056
   166
    }
thoneyvazul@1056
   167
    
thoneyvazul@1056
   168
  public:
thoneyvazul@1056
   169
thoneyvazul@1056
   170
    ///\name Named template parameters
thoneyvazul@1056
   171
thoneyvazul@1056
   172
    ///@{
thoneyvazul@1056
   173
thoneyvazul@1056
   174
    template <typename T>
kpeter@1058
   175
    struct SetFlowMapTraits : public Traits {
thoneyvazul@1056
   176
      typedef T FlowMap;
thoneyvazul@1056
   177
      static FlowMap *createFlowMap(const Digraph&) {
kpeter@1057
   178
	LEMON_ASSERT(false, "FlowMap is not initialized");
thoneyvazul@1056
   179
        return 0;
thoneyvazul@1056
   180
      }
thoneyvazul@1056
   181
    };
thoneyvazul@1056
   182
thoneyvazul@1056
   183
    /// \brief \ref named-templ-param "Named parameter" for setting
thoneyvazul@1056
   184
    /// FlowMap type
thoneyvazul@1056
   185
    ///
thoneyvazul@1056
   186
    /// \ref named-templ-param "Named parameter" for setting FlowMap
thoneyvazul@1056
   187
    /// type
thoneyvazul@1056
   188
    template <typename T>
kpeter@1058
   189
    struct SetFlowMap 
kpeter@1058
   190
      : public EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > {
kpeter@1058
   191
      typedef EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > Create;
thoneyvazul@1056
   192
    };
thoneyvazul@1056
   193
thoneyvazul@1056
   194
    /// @}
thoneyvazul@1056
   195
thoneyvazul@1056
   196
  protected:
thoneyvazul@1056
   197
    
thoneyvazul@1056
   198
    EdmondsKarp() {}
thoneyvazul@1056
   199
thoneyvazul@1056
   200
  public:
thoneyvazul@1056
   201
thoneyvazul@1056
   202
    /// \brief The constructor of the class.
thoneyvazul@1056
   203
    ///
thoneyvazul@1056
   204
    /// The constructor of the class. 
thoneyvazul@1056
   205
    /// \param digraph The digraph the algorithm runs on. 
thoneyvazul@1056
   206
    /// \param capacity The capacity of the arcs. 
thoneyvazul@1056
   207
    /// \param source The source node.
thoneyvazul@1056
   208
    /// \param target The target node.
thoneyvazul@1056
   209
    EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity,
thoneyvazul@1056
   210
		Node source, Node target)
thoneyvazul@1056
   211
      : _graph(digraph), _capacity(&capacity), _source(source), _target(target),
thoneyvazul@1056
   212
	_flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value()
thoneyvazul@1056
   213
    {
kpeter@1057
   214
      LEMON_ASSERT(_source != _target,
kpeter@1057
   215
                   "Flow source and target are the same nodes.");
thoneyvazul@1056
   216
    }
thoneyvazul@1056
   217
thoneyvazul@1056
   218
    /// \brief Destructor.
thoneyvazul@1056
   219
    ///
thoneyvazul@1056
   220
    /// Destructor.
thoneyvazul@1056
   221
    ~EdmondsKarp() {
thoneyvazul@1056
   222
      destroyStructures();
thoneyvazul@1056
   223
    }
thoneyvazul@1056
   224
thoneyvazul@1056
   225
    /// \brief Sets the capacity map.
thoneyvazul@1056
   226
    ///
thoneyvazul@1056
   227
    /// Sets the capacity map.
kpeter@1057
   228
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   229
    EdmondsKarp& capacityMap(const CapacityMap& map) {
thoneyvazul@1056
   230
      _capacity = &map;
thoneyvazul@1056
   231
      return *this;
thoneyvazul@1056
   232
    }
thoneyvazul@1056
   233
thoneyvazul@1056
   234
    /// \brief Sets the flow map.
thoneyvazul@1056
   235
    ///
thoneyvazul@1056
   236
    /// Sets the flow map.
kpeter@1057
   237
    /// If you don't use this function before calling \ref run() or
kpeter@1057
   238
    /// \ref init(), an instance will be allocated automatically.
kpeter@1057
   239
    /// The destructor deallocates this automatically allocated map,
kpeter@1057
   240
    /// of course.
kpeter@1057
   241
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   242
    EdmondsKarp& flowMap(FlowMap& map) {
thoneyvazul@1056
   243
      if (_local_flow) {
thoneyvazul@1056
   244
	delete _flow;
thoneyvazul@1056
   245
	_local_flow = false;
thoneyvazul@1056
   246
      }
thoneyvazul@1056
   247
      _flow = &map;
thoneyvazul@1056
   248
      return *this;
thoneyvazul@1056
   249
    }
thoneyvazul@1056
   250
thoneyvazul@1056
   251
    /// \brief Sets the source node.
thoneyvazul@1056
   252
    ///
thoneyvazul@1056
   253
    /// Sets the source node.
kpeter@1057
   254
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   255
    EdmondsKarp& source(const Node& node) {
thoneyvazul@1056
   256
      _source = node;
thoneyvazul@1056
   257
      return *this;
thoneyvazul@1056
   258
    }
thoneyvazul@1056
   259
thoneyvazul@1056
   260
    /// \brief Sets the target node.
thoneyvazul@1056
   261
    ///
thoneyvazul@1056
   262
    /// Sets the target node.
kpeter@1057
   263
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   264
    EdmondsKarp& target(const Node& node) {
thoneyvazul@1056
   265
      _target = node;
thoneyvazul@1056
   266
      return *this;
thoneyvazul@1056
   267
    }
thoneyvazul@1056
   268
thoneyvazul@1056
   269
    /// \brief Sets the tolerance used by algorithm.
thoneyvazul@1056
   270
    ///
thoneyvazul@1056
   271
    /// Sets the tolerance used by algorithm.
kpeter@1057
   272
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   273
    EdmondsKarp& tolerance(const Tolerance& tolerance) {
thoneyvazul@1056
   274
      _tolerance = tolerance;
thoneyvazul@1056
   275
      return *this;
thoneyvazul@1056
   276
    } 
thoneyvazul@1056
   277
kpeter@1057
   278
    /// \brief Returns a const reference to the tolerance.
thoneyvazul@1056
   279
    ///
kpeter@1057
   280
    /// Returns a const reference to the tolerance object used by
kpeter@1057
   281
    /// the algorithm.
thoneyvazul@1056
   282
    const Tolerance& tolerance() const {
thoneyvazul@1056
   283
      return _tolerance;
thoneyvazul@1056
   284
    } 
thoneyvazul@1056
   285
thoneyvazul@1056
   286
    /// \name Execution control
kpeter@1057
   287
    /// The simplest way to execute the algorithm is to use \ref run().\n
kpeter@1057
   288
    /// If you need better control on the initial solution or the execution,
kpeter@1057
   289
    /// you have to call one of the \ref init() functions first, then
kpeter@1057
   290
    /// \ref start() or multiple times the \ref augment() function.
thoneyvazul@1056
   291
    
thoneyvazul@1056
   292
    ///@{
thoneyvazul@1056
   293
kpeter@1057
   294
    /// \brief Initializes the algorithm.
kpeter@1057
   295
    ///
kpeter@1057
   296
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   297
    /// flow to zero on each arc.
thoneyvazul@1056
   298
    void init() {
thoneyvazul@1056
   299
      createStructures();
thoneyvazul@1056
   300
      for (ArcIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   301
        _flow->set(it, 0);
thoneyvazul@1056
   302
      }
thoneyvazul@1056
   303
      _flow_value = 0;
thoneyvazul@1056
   304
    }
thoneyvazul@1056
   305
    
kpeter@1057
   306
    /// \brief Initializes the algorithm using the given flow map.
kpeter@1057
   307
    ///
kpeter@1057
   308
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   309
    /// flow to the given \c flowMap. The \c flowMap should
kpeter@1057
   310
    /// contain a feasible flow, i.e. at each node excluding the source
kpeter@1057
   311
    /// and the target, the incoming flow should be equal to the
thoneyvazul@1056
   312
    /// outgoing flow.
thoneyvazul@1056
   313
    template <typename FlowMap>
thoneyvazul@1056
   314
    void flowInit(const FlowMap& flowMap) {
thoneyvazul@1056
   315
      createStructures();
thoneyvazul@1056
   316
      for (ArcIt e(_graph); e != INVALID; ++e) {
thoneyvazul@1056
   317
	_flow->set(e, flowMap[e]);
thoneyvazul@1056
   318
      }
thoneyvazul@1056
   319
      _flow_value = 0;
thoneyvazul@1056
   320
      for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   321
        _flow_value += (*_flow)[jt];
thoneyvazul@1056
   322
      }
thoneyvazul@1056
   323
      for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   324
        _flow_value -= (*_flow)[jt];
thoneyvazul@1056
   325
      }
thoneyvazul@1056
   326
    }
thoneyvazul@1056
   327
kpeter@1057
   328
    /// \brief Initializes the algorithm using the given flow map.
kpeter@1057
   329
    ///
kpeter@1057
   330
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   331
    /// flow to the given \c flowMap. The \c flowMap should
kpeter@1057
   332
    /// contain a feasible flow, i.e. at each node excluding the source
kpeter@1057
   333
    /// and the target, the incoming flow should be equal to the
kpeter@1057
   334
    /// outgoing flow. 
kpeter@1057
   335
    /// \return \c false when the given \c flowMap does not contain a
thoneyvazul@1056
   336
    /// feasible flow.
thoneyvazul@1056
   337
    template <typename FlowMap>
thoneyvazul@1056
   338
    bool checkedFlowInit(const FlowMap& flowMap) {
thoneyvazul@1056
   339
      createStructures();
thoneyvazul@1056
   340
      for (ArcIt e(_graph); e != INVALID; ++e) {
thoneyvazul@1056
   341
	_flow->set(e, flowMap[e]);
thoneyvazul@1056
   342
      }
thoneyvazul@1056
   343
      for (NodeIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   344
        if (it == _source || it == _target) continue;
thoneyvazul@1056
   345
        Value outFlow = 0;
thoneyvazul@1056
   346
        for (OutArcIt jt(_graph, it); jt != INVALID; ++jt) {
thoneyvazul@1056
   347
          outFlow += (*_flow)[jt];
thoneyvazul@1056
   348
        }
thoneyvazul@1056
   349
        Value inFlow = 0;
thoneyvazul@1056
   350
        for (InArcIt jt(_graph, it); jt != INVALID; ++jt) {
thoneyvazul@1056
   351
          inFlow += (*_flow)[jt];
thoneyvazul@1056
   352
        }
thoneyvazul@1056
   353
        if (_tolerance.different(outFlow, inFlow)) {
thoneyvazul@1056
   354
          return false;
thoneyvazul@1056
   355
        }
thoneyvazul@1056
   356
      }
thoneyvazul@1056
   357
      for (ArcIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   358
        if (_tolerance.less((*_flow)[it], 0)) return false;
thoneyvazul@1056
   359
        if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false;
thoneyvazul@1056
   360
      }
thoneyvazul@1056
   361
      _flow_value = 0;
thoneyvazul@1056
   362
      for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   363
        _flow_value += (*_flow)[jt];
thoneyvazul@1056
   364
      }
thoneyvazul@1056
   365
      for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   366
        _flow_value -= (*_flow)[jt];
thoneyvazul@1056
   367
      }
thoneyvazul@1056
   368
      return true;
thoneyvazul@1056
   369
    }
thoneyvazul@1056
   370
kpeter@1057
   371
    /// \brief Augments the solution along a shortest path.
thoneyvazul@1056
   372
    /// 
kpeter@1057
   373
    /// Augments the solution along a shortest path. This function searches a
kpeter@1057
   374
    /// shortest path between the source and the target
kpeter@1057
   375
    /// in the residual digraph by the Bfs algoritm.
thoneyvazul@1056
   376
    /// Then it increases the flow on this path with the minimal residual
kpeter@1057
   377
    /// capacity on the path. If there is no such path, it gives back
thoneyvazul@1056
   378
    /// false.
kpeter@1057
   379
    /// \return \c false when the augmenting did not success, i.e. the
thoneyvazul@1056
   380
    /// current flow is a feasible and optimal solution.
thoneyvazul@1056
   381
    bool augment() {
thoneyvazul@1056
   382
      for (NodeIt n(_graph); n != INVALID; ++n) {
thoneyvazul@1056
   383
	_pred->set(n, INVALID);
thoneyvazul@1056
   384
      }
thoneyvazul@1056
   385
      
thoneyvazul@1056
   386
      int first = 0, last = 1;
thoneyvazul@1056
   387
      
thoneyvazul@1056
   388
      _queue[0] = _source;
thoneyvazul@1056
   389
      _pred->set(_source, OutArcIt(_graph, _source));
thoneyvazul@1056
   390
thoneyvazul@1056
   391
      while (first != last && (*_pred)[_target] == INVALID) {
thoneyvazul@1056
   392
	Node n = _queue[first++];
thoneyvazul@1056
   393
	
thoneyvazul@1056
   394
	for (OutArcIt e(_graph, n); e != INVALID; ++e) {
thoneyvazul@1056
   395
	  Value rem = (*_capacity)[e] - (*_flow)[e];
thoneyvazul@1056
   396
	  Node t = _graph.target(e);
thoneyvazul@1056
   397
	  if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
thoneyvazul@1056
   398
	    _pred->set(t, e);
thoneyvazul@1056
   399
	    _queue[last++] = t;
thoneyvazul@1056
   400
	  }
thoneyvazul@1056
   401
	}
thoneyvazul@1056
   402
	for (InArcIt e(_graph, n); e != INVALID; ++e) {
thoneyvazul@1056
   403
	  Value rem = (*_flow)[e];
thoneyvazul@1056
   404
	  Node t = _graph.source(e);
thoneyvazul@1056
   405
	  if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
thoneyvazul@1056
   406
	    _pred->set(t, e);
thoneyvazul@1056
   407
	    _queue[last++] = t;
thoneyvazul@1056
   408
	  }
thoneyvazul@1056
   409
	}
thoneyvazul@1056
   410
      }
thoneyvazul@1056
   411
thoneyvazul@1056
   412
      if ((*_pred)[_target] != INVALID) {
thoneyvazul@1056
   413
	Node n = _target;
thoneyvazul@1056
   414
	Arc e = (*_pred)[n];
thoneyvazul@1056
   415
thoneyvazul@1056
   416
	Value prem = (*_capacity)[e] - (*_flow)[e];
thoneyvazul@1056
   417
	n = _graph.source(e);
thoneyvazul@1056
   418
	while (n != _source) {
thoneyvazul@1056
   419
	  e = (*_pred)[n];
thoneyvazul@1056
   420
	  if (_graph.target(e) == n) {
thoneyvazul@1056
   421
	    Value rem = (*_capacity)[e] - (*_flow)[e];
thoneyvazul@1056
   422
	    if (rem < prem) prem = rem;
thoneyvazul@1056
   423
	    n = _graph.source(e);
thoneyvazul@1056
   424
	  } else {
thoneyvazul@1056
   425
	    Value rem = (*_flow)[e];
thoneyvazul@1056
   426
	    if (rem < prem) prem = rem;
thoneyvazul@1056
   427
	    n = _graph.target(e);   
thoneyvazul@1056
   428
	  } 
thoneyvazul@1056
   429
	}
thoneyvazul@1056
   430
thoneyvazul@1056
   431
	n = _target;
thoneyvazul@1056
   432
	e = (*_pred)[n];
thoneyvazul@1056
   433
thoneyvazul@1056
   434
	_flow->set(e, (*_flow)[e] + prem);
thoneyvazul@1056
   435
	n = _graph.source(e);
thoneyvazul@1056
   436
	while (n != _source) {
thoneyvazul@1056
   437
	  e = (*_pred)[n];
thoneyvazul@1056
   438
	  if (_graph.target(e) == n) {
thoneyvazul@1056
   439
	    _flow->set(e, (*_flow)[e] + prem);
thoneyvazul@1056
   440
	    n = _graph.source(e);
thoneyvazul@1056
   441
	  } else {
thoneyvazul@1056
   442
	    _flow->set(e, (*_flow)[e] - prem);
thoneyvazul@1056
   443
	    n = _graph.target(e);   
thoneyvazul@1056
   444
	  } 
thoneyvazul@1056
   445
	}
thoneyvazul@1056
   446
thoneyvazul@1056
   447
	_flow_value += prem;	
thoneyvazul@1056
   448
	return true;
thoneyvazul@1056
   449
      } else {
thoneyvazul@1056
   450
	return false;
thoneyvazul@1056
   451
      }
thoneyvazul@1056
   452
    }
thoneyvazul@1056
   453
thoneyvazul@1056
   454
    /// \brief Executes the algorithm
thoneyvazul@1056
   455
    ///
kpeter@1057
   456
    /// Executes the algorithm by performing augmenting phases until the
kpeter@1057
   457
    /// optimal solution is reached. 
kpeter@1057
   458
    /// \pre One of the \ref init() functions must be called before
kpeter@1057
   459
    /// using this function.
thoneyvazul@1056
   460
    void start() {
thoneyvazul@1056
   461
      while (augment()) {}
thoneyvazul@1056
   462
    }
thoneyvazul@1056
   463
thoneyvazul@1056
   464
    /// \brief Runs the algorithm.
thoneyvazul@1056
   465
    /// 
kpeter@1057
   466
    /// Runs the Edmonds-Karp algorithm.
kpeter@1057
   467
    /// \note ek.run() is just a shortcut of the following code.
thoneyvazul@1056
   468
    ///\code 
thoneyvazul@1056
   469
    /// ek.init();
thoneyvazul@1056
   470
    /// ek.start();
thoneyvazul@1056
   471
    ///\endcode
thoneyvazul@1056
   472
    void run() {
thoneyvazul@1056
   473
      init();
thoneyvazul@1056
   474
      start();
thoneyvazul@1056
   475
    }
thoneyvazul@1056
   476
thoneyvazul@1056
   477
    /// @}
thoneyvazul@1056
   478
thoneyvazul@1056
   479
    /// \name Query Functions
thoneyvazul@1056
   480
    /// The result of the Edmonds-Karp algorithm can be obtained using these
thoneyvazul@1056
   481
    /// functions.\n
kpeter@1057
   482
    /// Either \ref run() or \ref start() should be called before using them.
thoneyvazul@1056
   483
    
thoneyvazul@1056
   484
    ///@{
thoneyvazul@1056
   485
thoneyvazul@1056
   486
    /// \brief Returns the value of the maximum flow.
thoneyvazul@1056
   487
    ///
kpeter@1057
   488
    /// Returns the value of the maximum flow found by the algorithm.
kpeter@1057
   489
    ///
kpeter@1057
   490
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   491
    /// using this function.
thoneyvazul@1056
   492
    Value flowValue() const {
thoneyvazul@1056
   493
      return _flow_value;
thoneyvazul@1056
   494
    }
thoneyvazul@1056
   495
kpeter@1057
   496
    /// \brief Returns the flow value on the given arc.
thoneyvazul@1056
   497
    ///
kpeter@1057
   498
    /// Returns the flow value on the given arc.
kpeter@1057
   499
    ///
kpeter@1057
   500
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   501
    /// using this function.
thoneyvazul@1056
   502
    Value flow(const Arc& arc) const {
thoneyvazul@1056
   503
      return (*_flow)[arc];
thoneyvazul@1056
   504
    }
thoneyvazul@1056
   505
kpeter@1057
   506
    /// \brief Returns a const reference to the flow map.
thoneyvazul@1056
   507
    ///
kpeter@1057
   508
    /// Returns a const reference to the arc map storing the found flow.
kpeter@1057
   509
    ///
kpeter@1057
   510
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   511
    /// using this function.
kpeter@1057
   512
    const FlowMap& flowMap() const {
kpeter@1057
   513
      return *_flow;
kpeter@1057
   514
    }
thoneyvazul@1056
   515
kpeter@1057
   516
    /// \brief Returns \c true when the node is on the source side of the
kpeter@1057
   517
    /// minimum cut.
kpeter@1057
   518
    ///
kpeter@1057
   519
    /// Returns true when the node is on the source side of the found
kpeter@1057
   520
    /// minimum cut.
kpeter@1057
   521
    ///
kpeter@1057
   522
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   523
    /// using this function.
thoneyvazul@1056
   524
    bool minCut(const Node& node) const {
thoneyvazul@1056
   525
      return ((*_pred)[node] != INVALID) or node == _source;
thoneyvazul@1056
   526
    }
thoneyvazul@1056
   527
kpeter@1057
   528
    /// \brief Gives back a minimum value cut.
thoneyvazul@1056
   529
    ///
kpeter@1057
   530
    /// Sets \c cutMap to the characteristic vector of a minimum value
kpeter@1057
   531
    /// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
kpeter@1057
   532
    /// node map with \c bool (or convertible) value type.
kpeter@1057
   533
    ///
kpeter@1057
   534
    /// \note This function calls \ref minCut() for each node, so it runs in
kpeter@1057
   535
    /// O(n) time.
kpeter@1057
   536
    ///
kpeter@1057
   537
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   538
    /// using this function.
thoneyvazul@1056
   539
    template <typename CutMap>
thoneyvazul@1056
   540
    void minCutMap(CutMap& cutMap) const {
thoneyvazul@1056
   541
      for (NodeIt n(_graph); n != INVALID; ++n) {
thoneyvazul@1056
   542
	cutMap.set(n, (*_pred)[n] != INVALID);
thoneyvazul@1056
   543
      }
thoneyvazul@1056
   544
      cutMap.set(_source, true);
thoneyvazul@1056
   545
    }    
thoneyvazul@1056
   546
thoneyvazul@1056
   547
    /// @}
thoneyvazul@1056
   548
thoneyvazul@1056
   549
  };
thoneyvazul@1056
   550
thoneyvazul@1056
   551
}
thoneyvazul@1056
   552
thoneyvazul@1056
   553
#endif