lemon/edmonds_karp.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 25 Feb 2021 09:46:12 +0100
changeset 1209 4a170261cc54
parent 1074 97d978243703
permissions -rw-r--r--
Merge #638
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
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
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
alpar@1092
    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
    ///
alpar@1092
    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
alpar@1074
    83
  /// digraph \cite clrs01algorithms, \cite amo93networkflows,
alpar@1074
    84
  /// \cite 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
alpar@1092
    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>
alpar@1092
   107
#else
thoneyvazul@1056
   108
  template <typename GR,
alpar@1092
   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
alpar@1074
   115
    /// \brief The \ref lemon::EdmondsKarpDefaultTraits "traits class"
alpar@1074
   116
    /// of the algorithm.
thoneyvazul@1056
   117
    typedef TR Traits;
kpeter@1057
   118
    /// The type of the digraph the algorithm runs on.
thoneyvazul@1056
   119
    typedef typename Traits::Digraph Digraph;
kpeter@1057
   120
    /// The type of the capacity map.
thoneyvazul@1056
   121
    typedef typename Traits::CapacityMap CapacityMap;
kpeter@1057
   122
    /// The type of the flow values.
alpar@1092
   123
    typedef typename Traits::Value Value;
thoneyvazul@1056
   124
kpeter@1057
   125
    /// The type of the flow map.
thoneyvazul@1056
   126
    typedef typename Traits::FlowMap FlowMap;
kpeter@1057
   127
    /// The type of the tolerance.
thoneyvazul@1056
   128
    typedef typename Traits::Tolerance Tolerance;
thoneyvazul@1056
   129
thoneyvazul@1056
   130
  private:
thoneyvazul@1056
   131
thoneyvazul@1056
   132
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
thoneyvazul@1056
   133
    typedef typename Digraph::template NodeMap<Arc> PredMap;
alpar@1092
   134
thoneyvazul@1056
   135
    const Digraph& _graph;
thoneyvazul@1056
   136
    const CapacityMap* _capacity;
thoneyvazul@1056
   137
thoneyvazul@1056
   138
    Node _source, _target;
thoneyvazul@1056
   139
thoneyvazul@1056
   140
    FlowMap* _flow;
thoneyvazul@1056
   141
    bool _local_flow;
thoneyvazul@1056
   142
thoneyvazul@1056
   143
    PredMap* _pred;
thoneyvazul@1056
   144
    std::vector<Node> _queue;
alpar@1092
   145
thoneyvazul@1056
   146
    Tolerance _tolerance;
thoneyvazul@1056
   147
    Value _flow_value;
thoneyvazul@1056
   148
thoneyvazul@1056
   149
    void createStructures() {
thoneyvazul@1056
   150
      if (!_flow) {
alpar@1092
   151
        _flow = Traits::createFlowMap(_graph);
alpar@1092
   152
        _local_flow = true;
thoneyvazul@1056
   153
      }
thoneyvazul@1056
   154
      if (!_pred) {
alpar@1092
   155
        _pred = new PredMap(_graph);
thoneyvazul@1056
   156
      }
thoneyvazul@1056
   157
      _queue.resize(countNodes(_graph));
thoneyvazul@1056
   158
    }
thoneyvazul@1056
   159
thoneyvazul@1056
   160
    void destroyStructures() {
thoneyvazul@1056
   161
      if (_local_flow) {
alpar@1092
   162
        delete _flow;
thoneyvazul@1056
   163
      }
thoneyvazul@1056
   164
      if (_pred) {
alpar@1092
   165
        delete _pred;
thoneyvazul@1056
   166
      }
thoneyvazul@1056
   167
    }
alpar@1092
   168
thoneyvazul@1056
   169
  public:
thoneyvazul@1056
   170
kpeter@1060
   171
    typedef EdmondsKarp Create;
kpeter@1060
   172
thoneyvazul@1056
   173
    ///\name Named template parameters
thoneyvazul@1056
   174
thoneyvazul@1056
   175
    ///@{
thoneyvazul@1056
   176
thoneyvazul@1056
   177
    template <typename T>
kpeter@1058
   178
    struct SetFlowMapTraits : public Traits {
thoneyvazul@1056
   179
      typedef T FlowMap;
thoneyvazul@1056
   180
      static FlowMap *createFlowMap(const Digraph&) {
alpar@1092
   181
        LEMON_ASSERT(false, "FlowMap is not initialized");
thoneyvazul@1056
   182
        return 0;
thoneyvazul@1056
   183
      }
thoneyvazul@1056
   184
    };
thoneyvazul@1056
   185
thoneyvazul@1056
   186
    /// \brief \ref named-templ-param "Named parameter" for setting
thoneyvazul@1056
   187
    /// FlowMap type
thoneyvazul@1056
   188
    ///
thoneyvazul@1056
   189
    /// \ref named-templ-param "Named parameter" for setting FlowMap
thoneyvazul@1056
   190
    /// type
thoneyvazul@1056
   191
    template <typename T>
alpar@1092
   192
    struct SetFlowMap
kpeter@1058
   193
      : public EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > {
kpeter@1058
   194
      typedef EdmondsKarp<Digraph, CapacityMap, SetFlowMapTraits<T> > Create;
thoneyvazul@1056
   195
    };
thoneyvazul@1056
   196
thoneyvazul@1056
   197
    /// @}
thoneyvazul@1056
   198
thoneyvazul@1056
   199
  protected:
alpar@1092
   200
thoneyvazul@1056
   201
    EdmondsKarp() {}
thoneyvazul@1056
   202
thoneyvazul@1056
   203
  public:
thoneyvazul@1056
   204
thoneyvazul@1056
   205
    /// \brief The constructor of the class.
thoneyvazul@1056
   206
    ///
alpar@1092
   207
    /// The constructor of the class.
alpar@1092
   208
    /// \param digraph The digraph the algorithm runs on.
alpar@1092
   209
    /// \param capacity The capacity of the arcs.
thoneyvazul@1056
   210
    /// \param source The source node.
thoneyvazul@1056
   211
    /// \param target The target node.
thoneyvazul@1056
   212
    EdmondsKarp(const Digraph& digraph, const CapacityMap& capacity,
alpar@1092
   213
                Node source, Node target)
thoneyvazul@1056
   214
      : _graph(digraph), _capacity(&capacity), _source(source), _target(target),
alpar@1092
   215
        _flow(0), _local_flow(false), _pred(0), _tolerance(), _flow_value()
thoneyvazul@1056
   216
    {
kpeter@1057
   217
      LEMON_ASSERT(_source != _target,
kpeter@1057
   218
                   "Flow source and target are the same nodes.");
thoneyvazul@1056
   219
    }
thoneyvazul@1056
   220
thoneyvazul@1056
   221
    /// \brief Destructor.
thoneyvazul@1056
   222
    ///
thoneyvazul@1056
   223
    /// Destructor.
thoneyvazul@1056
   224
    ~EdmondsKarp() {
thoneyvazul@1056
   225
      destroyStructures();
thoneyvazul@1056
   226
    }
thoneyvazul@1056
   227
thoneyvazul@1056
   228
    /// \brief Sets the capacity map.
thoneyvazul@1056
   229
    ///
thoneyvazul@1056
   230
    /// Sets the capacity map.
kpeter@1057
   231
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   232
    EdmondsKarp& capacityMap(const CapacityMap& map) {
thoneyvazul@1056
   233
      _capacity = &map;
thoneyvazul@1056
   234
      return *this;
thoneyvazul@1056
   235
    }
thoneyvazul@1056
   236
thoneyvazul@1056
   237
    /// \brief Sets the flow map.
thoneyvazul@1056
   238
    ///
thoneyvazul@1056
   239
    /// Sets the flow map.
kpeter@1057
   240
    /// If you don't use this function before calling \ref run() or
kpeter@1057
   241
    /// \ref init(), an instance will be allocated automatically.
kpeter@1057
   242
    /// The destructor deallocates this automatically allocated map,
kpeter@1057
   243
    /// of course.
kpeter@1057
   244
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   245
    EdmondsKarp& flowMap(FlowMap& map) {
thoneyvazul@1056
   246
      if (_local_flow) {
alpar@1092
   247
        delete _flow;
alpar@1092
   248
        _local_flow = false;
thoneyvazul@1056
   249
      }
thoneyvazul@1056
   250
      _flow = &map;
thoneyvazul@1056
   251
      return *this;
thoneyvazul@1056
   252
    }
thoneyvazul@1056
   253
thoneyvazul@1056
   254
    /// \brief Sets the source node.
thoneyvazul@1056
   255
    ///
thoneyvazul@1056
   256
    /// Sets the source node.
kpeter@1057
   257
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   258
    EdmondsKarp& source(const Node& node) {
thoneyvazul@1056
   259
      _source = node;
thoneyvazul@1056
   260
      return *this;
thoneyvazul@1056
   261
    }
thoneyvazul@1056
   262
thoneyvazul@1056
   263
    /// \brief Sets the target node.
thoneyvazul@1056
   264
    ///
thoneyvazul@1056
   265
    /// Sets the target node.
kpeter@1057
   266
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   267
    EdmondsKarp& target(const Node& node) {
thoneyvazul@1056
   268
      _target = node;
thoneyvazul@1056
   269
      return *this;
thoneyvazul@1056
   270
    }
thoneyvazul@1056
   271
thoneyvazul@1056
   272
    /// \brief Sets the tolerance used by algorithm.
thoneyvazul@1056
   273
    ///
thoneyvazul@1056
   274
    /// Sets the tolerance used by algorithm.
kpeter@1057
   275
    /// \return <tt>(*this)</tt>
thoneyvazul@1056
   276
    EdmondsKarp& tolerance(const Tolerance& tolerance) {
thoneyvazul@1056
   277
      _tolerance = tolerance;
thoneyvazul@1056
   278
      return *this;
alpar@1092
   279
    }
thoneyvazul@1056
   280
kpeter@1057
   281
    /// \brief Returns a const reference to the tolerance.
thoneyvazul@1056
   282
    ///
kpeter@1057
   283
    /// Returns a const reference to the tolerance object used by
kpeter@1057
   284
    /// the algorithm.
thoneyvazul@1056
   285
    const Tolerance& tolerance() const {
thoneyvazul@1056
   286
      return _tolerance;
alpar@1092
   287
    }
thoneyvazul@1056
   288
thoneyvazul@1056
   289
    /// \name Execution control
kpeter@1057
   290
    /// The simplest way to execute the algorithm is to use \ref run().\n
kpeter@1057
   291
    /// If you need better control on the initial solution or the execution,
kpeter@1057
   292
    /// you have to call one of the \ref init() functions first, then
kpeter@1057
   293
    /// \ref start() or multiple times the \ref augment() function.
alpar@1092
   294
thoneyvazul@1056
   295
    ///@{
thoneyvazul@1056
   296
kpeter@1057
   297
    /// \brief Initializes the algorithm.
kpeter@1057
   298
    ///
kpeter@1057
   299
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   300
    /// flow to zero on each arc.
thoneyvazul@1056
   301
    void init() {
thoneyvazul@1056
   302
      createStructures();
thoneyvazul@1056
   303
      for (ArcIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   304
        _flow->set(it, 0);
thoneyvazul@1056
   305
      }
thoneyvazul@1056
   306
      _flow_value = 0;
thoneyvazul@1056
   307
    }
alpar@1092
   308
kpeter@1057
   309
    /// \brief Initializes the algorithm using the given flow map.
kpeter@1057
   310
    ///
kpeter@1057
   311
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   312
    /// flow to the given \c flowMap. The \c flowMap should
kpeter@1057
   313
    /// contain a feasible flow, i.e. at each node excluding the source
kpeter@1057
   314
    /// and the target, the incoming flow should be equal to the
thoneyvazul@1056
   315
    /// outgoing flow.
thoneyvazul@1056
   316
    template <typename FlowMap>
kpeter@1059
   317
    void init(const FlowMap& flowMap) {
thoneyvazul@1056
   318
      createStructures();
thoneyvazul@1056
   319
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@1092
   320
        _flow->set(e, flowMap[e]);
thoneyvazul@1056
   321
      }
thoneyvazul@1056
   322
      _flow_value = 0;
thoneyvazul@1056
   323
      for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   324
        _flow_value += (*_flow)[jt];
thoneyvazul@1056
   325
      }
thoneyvazul@1056
   326
      for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   327
        _flow_value -= (*_flow)[jt];
thoneyvazul@1056
   328
      }
thoneyvazul@1056
   329
    }
thoneyvazul@1056
   330
kpeter@1057
   331
    /// \brief Initializes the algorithm using the given flow map.
kpeter@1057
   332
    ///
kpeter@1057
   333
    /// Initializes the internal data structures and sets the initial
kpeter@1057
   334
    /// flow to the given \c flowMap. The \c flowMap should
kpeter@1057
   335
    /// contain a feasible flow, i.e. at each node excluding the source
kpeter@1057
   336
    /// and the target, the incoming flow should be equal to the
alpar@1092
   337
    /// outgoing flow.
kpeter@1057
   338
    /// \return \c false when the given \c flowMap does not contain a
thoneyvazul@1056
   339
    /// feasible flow.
thoneyvazul@1056
   340
    template <typename FlowMap>
kpeter@1059
   341
    bool checkedInit(const FlowMap& flowMap) {
thoneyvazul@1056
   342
      createStructures();
thoneyvazul@1056
   343
      for (ArcIt e(_graph); e != INVALID; ++e) {
alpar@1092
   344
        _flow->set(e, flowMap[e]);
thoneyvazul@1056
   345
      }
thoneyvazul@1056
   346
      for (NodeIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   347
        if (it == _source || it == _target) continue;
thoneyvazul@1056
   348
        Value outFlow = 0;
thoneyvazul@1056
   349
        for (OutArcIt jt(_graph, it); jt != INVALID; ++jt) {
thoneyvazul@1056
   350
          outFlow += (*_flow)[jt];
thoneyvazul@1056
   351
        }
thoneyvazul@1056
   352
        Value inFlow = 0;
thoneyvazul@1056
   353
        for (InArcIt jt(_graph, it); jt != INVALID; ++jt) {
thoneyvazul@1056
   354
          inFlow += (*_flow)[jt];
thoneyvazul@1056
   355
        }
thoneyvazul@1056
   356
        if (_tolerance.different(outFlow, inFlow)) {
thoneyvazul@1056
   357
          return false;
thoneyvazul@1056
   358
        }
thoneyvazul@1056
   359
      }
thoneyvazul@1056
   360
      for (ArcIt it(_graph); it != INVALID; ++it) {
thoneyvazul@1056
   361
        if (_tolerance.less((*_flow)[it], 0)) return false;
thoneyvazul@1056
   362
        if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false;
thoneyvazul@1056
   363
      }
thoneyvazul@1056
   364
      _flow_value = 0;
thoneyvazul@1056
   365
      for (OutArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   366
        _flow_value += (*_flow)[jt];
thoneyvazul@1056
   367
      }
thoneyvazul@1056
   368
      for (InArcIt jt(_graph, _source); jt != INVALID; ++jt) {
thoneyvazul@1056
   369
        _flow_value -= (*_flow)[jt];
thoneyvazul@1056
   370
      }
thoneyvazul@1056
   371
      return true;
thoneyvazul@1056
   372
    }
thoneyvazul@1056
   373
kpeter@1057
   374
    /// \brief Augments the solution along a shortest path.
alpar@1092
   375
    ///
kpeter@1057
   376
    /// Augments the solution along a shortest path. This function searches a
kpeter@1057
   377
    /// shortest path between the source and the target
kpeter@1057
   378
    /// in the residual digraph by the Bfs algoritm.
thoneyvazul@1056
   379
    /// Then it increases the flow on this path with the minimal residual
kpeter@1057
   380
    /// capacity on the path. If there is no such path, it gives back
thoneyvazul@1056
   381
    /// false.
kpeter@1057
   382
    /// \return \c false when the augmenting did not success, i.e. the
thoneyvazul@1056
   383
    /// current flow is a feasible and optimal solution.
thoneyvazul@1056
   384
    bool augment() {
thoneyvazul@1056
   385
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@1092
   386
        _pred->set(n, INVALID);
thoneyvazul@1056
   387
      }
alpar@1092
   388
thoneyvazul@1056
   389
      int first = 0, last = 1;
alpar@1092
   390
thoneyvazul@1056
   391
      _queue[0] = _source;
thoneyvazul@1056
   392
      _pred->set(_source, OutArcIt(_graph, _source));
thoneyvazul@1056
   393
thoneyvazul@1056
   394
      while (first != last && (*_pred)[_target] == INVALID) {
alpar@1092
   395
        Node n = _queue[first++];
alpar@1092
   396
alpar@1092
   397
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
alpar@1092
   398
          Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@1092
   399
          Node t = _graph.target(e);
alpar@1092
   400
          if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
alpar@1092
   401
            _pred->set(t, e);
alpar@1092
   402
            _queue[last++] = t;
alpar@1092
   403
          }
alpar@1092
   404
        }
alpar@1092
   405
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
alpar@1092
   406
          Value rem = (*_flow)[e];
alpar@1092
   407
          Node t = _graph.source(e);
alpar@1092
   408
          if (_tolerance.positive(rem) && (*_pred)[t] == INVALID) {
alpar@1092
   409
            _pred->set(t, e);
alpar@1092
   410
            _queue[last++] = t;
alpar@1092
   411
          }
alpar@1092
   412
        }
thoneyvazul@1056
   413
      }
thoneyvazul@1056
   414
thoneyvazul@1056
   415
      if ((*_pred)[_target] != INVALID) {
alpar@1092
   416
        Node n = _target;
alpar@1092
   417
        Arc e = (*_pred)[n];
thoneyvazul@1056
   418
alpar@1092
   419
        Value prem = (*_capacity)[e] - (*_flow)[e];
alpar@1092
   420
        n = _graph.source(e);
alpar@1092
   421
        while (n != _source) {
alpar@1092
   422
          e = (*_pred)[n];
alpar@1092
   423
          if (_graph.target(e) == n) {
alpar@1092
   424
            Value rem = (*_capacity)[e] - (*_flow)[e];
alpar@1092
   425
            if (rem < prem) prem = rem;
alpar@1092
   426
            n = _graph.source(e);
alpar@1092
   427
          } else {
alpar@1092
   428
            Value rem = (*_flow)[e];
alpar@1092
   429
            if (rem < prem) prem = rem;
alpar@1092
   430
            n = _graph.target(e);
alpar@1092
   431
          }
alpar@1092
   432
        }
thoneyvazul@1056
   433
alpar@1092
   434
        n = _target;
alpar@1092
   435
        e = (*_pred)[n];
thoneyvazul@1056
   436
alpar@1092
   437
        _flow->set(e, (*_flow)[e] + prem);
alpar@1092
   438
        n = _graph.source(e);
alpar@1092
   439
        while (n != _source) {
alpar@1092
   440
          e = (*_pred)[n];
alpar@1092
   441
          if (_graph.target(e) == n) {
alpar@1092
   442
            _flow->set(e, (*_flow)[e] + prem);
alpar@1092
   443
            n = _graph.source(e);
alpar@1092
   444
          } else {
alpar@1092
   445
            _flow->set(e, (*_flow)[e] - prem);
alpar@1092
   446
            n = _graph.target(e);
alpar@1092
   447
          }
alpar@1092
   448
        }
thoneyvazul@1056
   449
alpar@1092
   450
        _flow_value += prem;
alpar@1092
   451
        return true;
thoneyvazul@1056
   452
      } else {
alpar@1092
   453
        return false;
thoneyvazul@1056
   454
      }
thoneyvazul@1056
   455
    }
thoneyvazul@1056
   456
thoneyvazul@1056
   457
    /// \brief Executes the algorithm
thoneyvazul@1056
   458
    ///
kpeter@1057
   459
    /// Executes the algorithm by performing augmenting phases until the
alpar@1092
   460
    /// optimal solution is reached.
kpeter@1057
   461
    /// \pre One of the \ref init() functions must be called before
kpeter@1057
   462
    /// using this function.
thoneyvazul@1056
   463
    void start() {
thoneyvazul@1056
   464
      while (augment()) {}
thoneyvazul@1056
   465
    }
thoneyvazul@1056
   466
thoneyvazul@1056
   467
    /// \brief Runs the algorithm.
alpar@1092
   468
    ///
kpeter@1057
   469
    /// Runs the Edmonds-Karp algorithm.
kpeter@1057
   470
    /// \note ek.run() is just a shortcut of the following code.
alpar@1092
   471
    ///\code
thoneyvazul@1056
   472
    /// ek.init();
thoneyvazul@1056
   473
    /// ek.start();
thoneyvazul@1056
   474
    ///\endcode
thoneyvazul@1056
   475
    void run() {
thoneyvazul@1056
   476
      init();
thoneyvazul@1056
   477
      start();
thoneyvazul@1056
   478
    }
thoneyvazul@1056
   479
thoneyvazul@1056
   480
    /// @}
thoneyvazul@1056
   481
thoneyvazul@1056
   482
    /// \name Query Functions
thoneyvazul@1056
   483
    /// The result of the Edmonds-Karp algorithm can be obtained using these
thoneyvazul@1056
   484
    /// functions.\n
kpeter@1057
   485
    /// Either \ref run() or \ref start() should be called before using them.
alpar@1092
   486
thoneyvazul@1056
   487
    ///@{
thoneyvazul@1056
   488
thoneyvazul@1056
   489
    /// \brief Returns the value of the maximum flow.
thoneyvazul@1056
   490
    ///
kpeter@1057
   491
    /// Returns the value of the maximum flow found by the algorithm.
kpeter@1057
   492
    ///
kpeter@1057
   493
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   494
    /// using this function.
thoneyvazul@1056
   495
    Value flowValue() const {
thoneyvazul@1056
   496
      return _flow_value;
thoneyvazul@1056
   497
    }
thoneyvazul@1056
   498
kpeter@1057
   499
    /// \brief Returns the flow value on the given arc.
thoneyvazul@1056
   500
    ///
kpeter@1057
   501
    /// Returns the flow value on the given arc.
kpeter@1057
   502
    ///
kpeter@1057
   503
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   504
    /// using this function.
thoneyvazul@1056
   505
    Value flow(const Arc& arc) const {
thoneyvazul@1056
   506
      return (*_flow)[arc];
thoneyvazul@1056
   507
    }
thoneyvazul@1056
   508
kpeter@1057
   509
    /// \brief Returns a const reference to the flow map.
thoneyvazul@1056
   510
    ///
kpeter@1057
   511
    /// Returns a const reference to the arc map storing the found flow.
kpeter@1057
   512
    ///
kpeter@1057
   513
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   514
    /// using this function.
kpeter@1057
   515
    const FlowMap& flowMap() const {
kpeter@1057
   516
      return *_flow;
kpeter@1057
   517
    }
thoneyvazul@1056
   518
kpeter@1057
   519
    /// \brief Returns \c true when the node is on the source side of the
kpeter@1057
   520
    /// minimum cut.
kpeter@1057
   521
    ///
kpeter@1057
   522
    /// Returns true when the node is on the source side of the found
kpeter@1057
   523
    /// minimum cut.
kpeter@1057
   524
    ///
kpeter@1057
   525
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   526
    /// using this function.
thoneyvazul@1056
   527
    bool minCut(const Node& node) const {
kpeter@1061
   528
      return ((*_pred)[node] != INVALID) || node == _source;
thoneyvazul@1056
   529
    }
thoneyvazul@1056
   530
kpeter@1057
   531
    /// \brief Gives back a minimum value cut.
thoneyvazul@1056
   532
    ///
kpeter@1057
   533
    /// Sets \c cutMap to the characteristic vector of a minimum value
kpeter@1057
   534
    /// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
kpeter@1057
   535
    /// node map with \c bool (or convertible) value type.
kpeter@1057
   536
    ///
kpeter@1057
   537
    /// \note This function calls \ref minCut() for each node, so it runs in
kpeter@1057
   538
    /// O(n) time.
kpeter@1057
   539
    ///
kpeter@1057
   540
    /// \pre Either \ref run() or \ref init() must be called before
kpeter@1057
   541
    /// using this function.
thoneyvazul@1056
   542
    template <typename CutMap>
thoneyvazul@1056
   543
    void minCutMap(CutMap& cutMap) const {
thoneyvazul@1056
   544
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@1092
   545
        cutMap.set(n, (*_pred)[n] != INVALID);
thoneyvazul@1056
   546
      }
thoneyvazul@1056
   547
      cutMap.set(_source, true);
alpar@1092
   548
    }
thoneyvazul@1056
   549
thoneyvazul@1056
   550
    /// @}
thoneyvazul@1056
   551
thoneyvazul@1056
   552
  };
thoneyvazul@1056
   553
thoneyvazul@1056
   554
}
thoneyvazul@1056
   555
thoneyvazul@1056
   556
#endif