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