lemon/preflow.h
author alpar
Tue, 13 Mar 2007 12:33:40 +0000
changeset 2406 0ffc78641b34
parent 2387 317b9a88c350
child 2473 9ffff9051a4b
permissions -rw-r--r--
Better doc.
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_PREFLOW_H
alpar@921
    20
#define LEMON_PREFLOW_H
jacint@836
    21
jacint@836
    22
#include <vector>
jacint@836
    23
#include <queue>
jacint@836
    24
jacint@1762
    25
#include <lemon/error.h>
deba@1993
    26
#include <lemon/bits/invalid.h>
alpar@1835
    27
#include <lemon/tolerance.h>
alpar@921
    28
#include <lemon/maps.h>
klao@977
    29
#include <lemon/graph_utils.h>
jacint@836
    30
jacint@836
    31
/// \file
deba@2376
    32
/// \ingroup max_flow
deba@1742
    33
/// \brief Implementation of the preflow algorithm.
jacint@836
    34
alpar@921
    35
namespace lemon {
jacint@836
    36
deba@2376
    37
  ///\ingroup max_flow
deba@1792
    38
  ///\brief %Preflow algorithms class.
deba@1792
    39
  ///
jacint@836
    40
  ///This class provides an implementation of the \e preflow \e
jacint@836
    41
  ///algorithm producing a flow of maximum value in a directed
jacint@2024
    42
  ///graph. The preflow algorithms are the fastest known max flow algorithms. 
jacint@2024
    43
  ///The \e source node, the \e target node, the \e
jacint@836
    44
  ///capacity of the edges and the \e starting \e flow value of the
jacint@836
    45
  ///edges should be passed to the algorithm through the
jacint@836
    46
  ///constructor. It is possible to change these quantities using the
zsuzska@1285
    47
  ///functions \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285
    48
  ///flowMap.
jacint@836
    49
  ///
alpar@921
    50
  ///After running \ref lemon::Preflow::phase1() "phase1()"
alpar@921
    51
  ///or \ref lemon::Preflow::run() "run()", the maximal flow
jacint@836
    52
  ///value can be obtained by calling \ref flowValue(). The minimum
alpar@851
    53
  ///value cut can be written into a <tt>bool</tt> node map by
alpar@851
    54
  ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
jacint@836
    55
  ///the inclusionwise minimum and maximum of the minimum value cuts,
jacint@836
    56
  ///resp.)
jacint@836
    57
  ///
jacint@836
    58
  ///\param Graph The directed graph type the algorithm runs on.
jacint@836
    59
  ///\param Num The number type of the capacities and the flow values.
alpar@1222
    60
  ///\param CapacityMap The capacity map type.
jacint@836
    61
  ///\param FlowMap The flow map type.
alpar@2387
    62
  ///\param Tol The tolerance type. 
jacint@836
    63
  ///
jacint@836
    64
  ///\author Jacint Szabo 
alpar@1227
    65
  ///\todo Second template parameter is superfluous
jacint@836
    66
  template <typename Graph, typename Num,
alpar@1222
    67
	    typename CapacityMap=typename Graph::template EdgeMap<Num>,
alpar@1835
    68
            typename FlowMap=typename Graph::template EdgeMap<Num>,
alpar@2387
    69
	    typename Tol=Tolerance<Num> >
jacint@836
    70
  class Preflow {
jacint@836
    71
  protected:
jacint@836
    72
    typedef typename Graph::Node Node;
jacint@836
    73
    typedef typename Graph::NodeIt NodeIt;
jacint@836
    74
    typedef typename Graph::EdgeIt EdgeIt;
jacint@836
    75
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@836
    76
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@836
    77
jacint@836
    78
    typedef typename Graph::template NodeMap<Node> NNMap;
jacint@836
    79
    typedef typename std::vector<Node> VecNode;
jacint@836
    80
alpar@1222
    81
    const Graph* _g;
alpar@1222
    82
    Node _source;
alpar@1222
    83
    Node _target;
alpar@1222
    84
    const CapacityMap* _capacity;
alpar@1222
    85
    FlowMap* _flow;
alpar@1835
    86
alpar@2387
    87
    Tol _surely;
alpar@1835
    88
    
alpar@1222
    89
    int _node_num;      //the number of nodes of G
jacint@836
    90
    
jacint@836
    91
    typename Graph::template NodeMap<int> level;  
jacint@836
    92
    typename Graph::template NodeMap<Num> excess;
jacint@836
    93
jacint@836
    94
    // constants used for heuristics
jacint@836
    95
    static const int H0=20;
jacint@836
    96
    static const int H1=1;
jacint@836
    97
jacint@1762
    98
  public:
jacint@1762
    99
jacint@1762
   100
    ///\ref Exception for the case when s=t.
jacint@1762
   101
jacint@1762
   102
    ///\ref Exception for the case when the source equals the target.
jacint@1762
   103
    class InvalidArgument : public lemon::LogicError {
jacint@836
   104
    public:
alpar@2151
   105
      virtual const char* what() const throw() {
jacint@1762
   106
	return "lemon::Preflow::InvalidArgument";
jacint@1762
   107
      }
jacint@1762
   108
    };
jacint@1762
   109
    
jacint@1762
   110
    
jacint@836
   111
    ///Indicates the property of the starting flow map.
jacint@1762
   112
    
alpar@1222
   113
    ///Indicates the property of the starting flow map.
jacint@836
   114
    ///
jacint@836
   115
    enum FlowEnum{
alpar@1898
   116
      ///indicates an unspecified edge map. \c flow will be 
alpar@1898
   117
      ///set to the constant zero flow in the beginning of
alpar@1898
   118
      ///the algorithm in this case.
jacint@836
   119
      NO_FLOW,
alpar@1898
   120
      ///constant zero flow
jacint@836
   121
      ZERO_FLOW,
alpar@1898
   122
      ///any flow, i.e. the sum of the in-flows equals to
alpar@1898
   123
      ///the sum of the out-flows in every node except the \c source and
alpar@1898
   124
      ///the \c target.
jacint@836
   125
      GEN_FLOW,
alpar@1898
   126
      ///any preflow, i.e. the sum of the in-flows is at 
alpar@1898
   127
      ///least the sum of the out-flows in every node except the \c source.
jacint@836
   128
      PRE_FLOW
jacint@836
   129
    };
jacint@836
   130
jacint@836
   131
    ///Indicates the state of the preflow algorithm.
jacint@836
   132
alpar@1222
   133
    ///Indicates the state of the preflow algorithm.
jacint@836
   134
    ///
jacint@836
   135
    enum StatusEnum {
alpar@1898
   136
      ///before running the algorithm or
alpar@1898
   137
      ///at an unspecified state.
jacint@836
   138
      AFTER_NOTHING,
alpar@1898
   139
      ///right after running \ref phase1()
jacint@836
   140
      AFTER_PREFLOW_PHASE_1,      
alpar@1898
   141
      ///after running \ref phase2()
jacint@836
   142
      AFTER_PREFLOW_PHASE_2
jacint@836
   143
    };
jacint@836
   144
    
jacint@1762
   145
  protected: 
jacint@1762
   146
    FlowEnum flow_prop;
jacint@836
   147
    StatusEnum status; // Do not needle this flag only if necessary.
jacint@836
   148
    
jacint@836
   149
  public: 
jacint@836
   150
    ///The constructor of the class.
jacint@836
   151
jacint@836
   152
    ///The constructor of the class. 
zsuzska@1285
   153
    ///\param _gr The directed graph the algorithm runs on. 
jacint@836
   154
    ///\param _s The source node.
jacint@836
   155
    ///\param _t The target node.
alpar@1222
   156
    ///\param _cap The capacity of the edges. 
alpar@1222
   157
    ///\param _f The flow of the edges. 
alpar@2387
   158
    ///\param _sr Tol class.
jacint@836
   159
    ///Except the graph, all of these parameters can be reset by
zsuzska@1285
   160
    ///calling \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285
   161
    ///flowMap, resp.
deba@2033
   162
    Preflow(const Graph& _gr, Node _s, Node _t, 
deba@2033
   163
            const CapacityMap& _cap, FlowMap& _f,
alpar@2387
   164
            const Tol &_sr=Tol()) :
alpar@1222
   165
	_g(&_gr), _source(_s), _target(_t), _capacity(&_cap),
deba@2019
   166
	_flow(&_f), _surely(_sr),
alpar@1835
   167
	_node_num(countNodes(_gr)), level(_gr), excess(_gr,0), 
jacint@1762
   168
	flow_prop(NO_FLOW), status(AFTER_NOTHING) { 
jacint@1762
   169
	if ( _source==_target )
jacint@1762
   170
	  throw InvalidArgument();
deba@2033
   171
    }
jacint@1762
   172
    
alpar@1898
   173
    ///Give a reference to the tolerance handler class
jacint@836
   174
alpar@1898
   175
    ///Give a reference to the tolerance handler class
alpar@1898
   176
    ///\sa Tolerance
alpar@2387
   177
    Tol &tolerance() { return _surely; }
alpar@1898
   178
jacint@836
   179
    ///Runs the preflow algorithm.  
jacint@836
   180
alpar@851
   181
    ///Runs the preflow algorithm.
alpar@851
   182
    ///
jacint@836
   183
    void run() {
jacint@836
   184
      phase1(flow_prop);
jacint@836
   185
      phase2();
jacint@836
   186
    }
jacint@836
   187
    
jacint@836
   188
    ///Runs the preflow algorithm.  
jacint@836
   189
    
jacint@836
   190
    ///Runs the preflow algorithm. 
jacint@836
   191
    ///\pre The starting flow map must be
jacint@836
   192
    /// - a constant zero flow if \c fp is \c ZERO_FLOW,
jacint@836
   193
    /// - an arbitrary flow if \c fp is \c GEN_FLOW,
jacint@836
   194
    /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
jacint@836
   195
    /// - any map if \c fp is NO_FLOW.
jacint@836
   196
    ///If the starting flow map is a flow or a preflow then 
jacint@836
   197
    ///the algorithm terminates faster.
jacint@836
   198
    void run(FlowEnum fp) {
jacint@836
   199
      flow_prop=fp;
jacint@836
   200
      run();
jacint@836
   201
    }
jacint@836
   202
      
jacint@836
   203
    ///Runs the first phase of the preflow algorithm.
jacint@836
   204
jacint@920
   205
    ///The preflow algorithm consists of two phases, this method runs
jacint@920
   206
    ///the first phase. After the first phase the maximum flow value
zsuzska@1285
   207
    ///and a minimum value cut can already be computed, although a
jacint@920
   208
    ///maximum flow is not yet obtained. So after calling this method
jacint@920
   209
    ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920
   210
    ///minCut returns a minimum cut.     
jacint@920
   211
    ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
jacint@920
   212
    ///value cuts unless calling \ref phase2.  
jacint@920
   213
    ///\pre The starting flow must be 
jacint@920
   214
    ///- a constant zero flow if \c fp is \c ZERO_FLOW, 
jacint@920
   215
    ///- an arbitary flow if \c fp is \c GEN_FLOW, 
jacint@920
   216
    ///- an arbitary preflow if \c fp is \c PRE_FLOW, 
jacint@920
   217
    ///- any map if \c fp is NO_FLOW.
jacint@836
   218
    void phase1(FlowEnum fp)
jacint@836
   219
    {
jacint@836
   220
      flow_prop=fp;
jacint@836
   221
      phase1();
jacint@836
   222
    }
jacint@836
   223
jacint@836
   224
    
jacint@836
   225
    ///Runs the first phase of the preflow algorithm.
jacint@836
   226
jacint@920
   227
    ///The preflow algorithm consists of two phases, this method runs
jacint@920
   228
    ///the first phase. After the first phase the maximum flow value
zsuzska@1285
   229
    ///and a minimum value cut can already be computed, although a
jacint@920
   230
    ///maximum flow is not yet obtained. So after calling this method
jacint@920
   231
    ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920
   232
    ///minCut returns a minimum cut.
deba@1786
   233
    ///\warning \ref minMinCut() and \ref maxMinCut() do not
alpar@911
   234
    ///give minimum value cuts unless calling \ref phase2().
jacint@836
   235
    void phase1()
jacint@836
   236
    {
deba@2386
   237
      int heur0=int(H0*_node_num);  //time while running 'bound decrease'
deba@2386
   238
      int heur1=int(H1*_node_num);  //time while running 'highest label'
jacint@836
   239
      int heur=heur1;         //starting time interval (#of relabels)
jacint@836
   240
      int numrelabel=0;
jacint@836
   241
jacint@836
   242
      bool what_heur=1;
jacint@836
   243
      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
jacint@836
   244
jacint@836
   245
      bool end=false;
jacint@836
   246
      //Needed for 'bound decrease', true means no active 
jacint@836
   247
      //nodes are above bound b.
jacint@836
   248
alpar@1222
   249
      int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@2024
   250
      int b=k;    //bound on the highest level under n containing an active node
jacint@836
   251
alpar@1222
   252
      VecNode first(_node_num, INVALID);
alpar@1222
   253
      NNMap next(*_g, INVALID);
jacint@836
   254
alpar@1222
   255
      NNMap left(*_g, INVALID);
alpar@1222
   256
      NNMap right(*_g, INVALID);
alpar@1222
   257
      VecNode level_list(_node_num,INVALID);
jacint@836
   258
      //List of the nodes in level i<n, set to n.
jacint@836
   259
jacint@836
   260
      preflowPreproc(first, next, level_list, left, right);
jacint@836
   261
jacint@836
   262
      //Push/relabel on the highest level active nodes.
jacint@836
   263
      while ( true ) {
jacint@836
   264
	if ( b == 0 ) {
jacint@836
   265
	  if ( !what_heur && !end && k > 0 ) {
jacint@836
   266
	    b=k;
jacint@836
   267
	    end=true;
jacint@836
   268
	  } else break;
jacint@836
   269
	}
jacint@836
   270
jacint@836
   271
	if ( first[b]==INVALID ) --b;
jacint@836
   272
	else {
jacint@836
   273
	  end=false;
jacint@836
   274
	  Node w=first[b];
jacint@836
   275
	  first[b]=next[w];
jacint@836
   276
	  int newlevel=push(w, next, first);
deba@2330
   277
	  if ( excess[w] != 0 ) {
deba@2330
   278
            relabel(w, newlevel, first, next, level_list, 
deba@2330
   279
                    left, right, b, k, what_heur);
deba@2330
   280
          }
jacint@836
   281
jacint@836
   282
	  ++numrelabel;
jacint@836
   283
	  if ( numrelabel >= heur ) {
jacint@836
   284
	    numrelabel=0;
jacint@836
   285
	    if ( what_heur ) {
jacint@836
   286
	      what_heur=0;
jacint@836
   287
	      heur=heur0;
jacint@836
   288
	      end=false;
jacint@836
   289
	    } else {
jacint@836
   290
	      what_heur=1;
jacint@836
   291
	      heur=heur1;
jacint@836
   292
	      b=k;
jacint@836
   293
	    }
jacint@836
   294
	  }
jacint@836
   295
	}
jacint@836
   296
      }
jacint@836
   297
      flow_prop=PRE_FLOW;
jacint@836
   298
      status=AFTER_PREFLOW_PHASE_1;
jacint@836
   299
    }
jacint@836
   300
    // Heuristics:
jacint@836
   301
    //   2 phase
jacint@836
   302
    //   gap
jacint@836
   303
    //   list 'level_list' on the nodes on level i implemented by hand
jacint@836
   304
    //   stack 'active' on the active nodes on level i      
jacint@836
   305
    //   runs heuristic 'highest label' for H1*n relabels
alpar@1222
   306
    //   runs heuristic 'bound decrease' for H0*n relabels,
alpar@1222
   307
    //        starts with 'highest label'
jacint@836
   308
    //   Parameters H0 and H1 are initialized to 20 and 1.
jacint@836
   309
jacint@836
   310
jacint@836
   311
    ///Runs the second phase of the preflow algorithm.
jacint@836
   312
jacint@836
   313
    ///The preflow algorithm consists of two phases, this method runs
alpar@1631
   314
    ///the second phase. After calling \ref phase1() and then
alpar@1631
   315
    ///\ref phase2(),
alpar@1631
   316
    /// \ref flowMap() return a maximum flow, \ref flowValue
jacint@920
   317
    ///returns the value of a maximum flow, \ref minCut returns a
jacint@920
   318
    ///minimum cut, while the methods \ref minMinCut and \ref
jacint@920
   319
    ///maxMinCut return the inclusionwise minimum and maximum cuts of
jacint@920
   320
    ///minimum value, resp.  \pre \ref phase1 must be called before.
deba@2330
   321
    ///
deba@2330
   322
    /// \todo The inexact computation can cause positive excess on a set of 
deba@2330
   323
    /// unpushable nodes. We may have to watch the empty level in this case 
deba@2330
   324
    /// due to avoid the terrible long running time.
jacint@836
   325
    void phase2()
jacint@836
   326
    {
jacint@836
   327
alpar@1222
   328
      int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@836
   329
      int b=k;    //bound on the highest level under n of an active node
jacint@836
   330
jacint@836
   331
    
alpar@1222
   332
      VecNode first(_node_num, INVALID);
alpar@1222
   333
      NNMap next(*_g, INVALID); 
alpar@1222
   334
      level.set(_source,0);
jacint@836
   335
      std::queue<Node> bfs_queue;
alpar@1222
   336
      bfs_queue.push(_source);
jacint@836
   337
jacint@836
   338
      while ( !bfs_queue.empty() ) {
jacint@836
   339
jacint@836
   340
	Node v=bfs_queue.front();
jacint@836
   341
	bfs_queue.pop();
jacint@836
   342
	int l=level[v]+1;
jacint@836
   343
alpar@1222
   344
	for(InEdgeIt e(*_g,v); e!=INVALID; ++e) {
deba@2330
   345
	  if ( !_surely.positive((*_capacity)[e] - (*_flow)[e])) continue;
alpar@1222
   346
	  Node u=_g->source(e);
alpar@1222
   347
	  if ( level[u] >= _node_num ) {
jacint@836
   348
	    bfs_queue.push(u);
jacint@836
   349
	    level.set(u, l);
deba@2330
   350
	    if ( excess[u] != 0 ) {
jacint@836
   351
	      next.set(u,first[l]);
jacint@836
   352
	      first[l]=u;
jacint@836
   353
	    }
jacint@836
   354
	  }
jacint@836
   355
	}
jacint@836
   356
alpar@1222
   357
	for(OutEdgeIt e(*_g,v); e!=INVALID; ++e) {
jacint@2024
   358
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   359
	  Node u=_g->target(e);
alpar@1222
   360
	  if ( level[u] >= _node_num ) {
jacint@836
   361
	    bfs_queue.push(u);
jacint@836
   362
	    level.set(u, l);
deba@2330
   363
	    if ( excess[u] != 0 ) {
jacint@836
   364
	      next.set(u,first[l]);
jacint@836
   365
	      first[l]=u;
jacint@836
   366
	    }
jacint@836
   367
	  }
jacint@836
   368
	}
jacint@836
   369
      }
alpar@1222
   370
      b=_node_num-2;
jacint@836
   371
jacint@836
   372
      while ( true ) {
jacint@836
   373
jacint@836
   374
	if ( b == 0 ) break;
jacint@836
   375
	if ( first[b]==INVALID ) --b;
jacint@836
   376
	else {
jacint@836
   377
	  Node w=first[b];
jacint@836
   378
	  first[b]=next[w];
jacint@836
   379
	  int newlevel=push(w,next, first);
jacint@836
   380
	  
deba@2330
   381
          if ( newlevel == _node_num) {
deba@2330
   382
            excess.set(w, 0);
deba@2330
   383
	    level.set(w,_node_num);
deba@2330
   384
          }
jacint@836
   385
	  //relabel
deba@2330
   386
	  if ( excess[w] != 0 ) {
jacint@836
   387
	    level.set(w,++newlevel);
jacint@836
   388
	    next.set(w,first[newlevel]);
jacint@836
   389
	    first[newlevel]=w;
jacint@836
   390
	    b=newlevel;
jacint@836
   391
	  }
jacint@836
   392
	} 
jacint@836
   393
      } // while(true)
jacint@836
   394
      flow_prop=GEN_FLOW;
jacint@836
   395
      status=AFTER_PREFLOW_PHASE_2;
jacint@836
   396
    }
jacint@836
   397
jacint@836
   398
    /// Returns the value of the maximum flow.
jacint@836
   399
jacint@836
   400
    /// Returns the value of the maximum flow by returning the excess
alpar@911
   401
    /// of the target node \c t. This value equals to the value of
jacint@836
   402
    /// the maximum flow already after running \ref phase1.
jacint@836
   403
    Num flowValue() const {
alpar@1222
   404
      return excess[_target];
jacint@836
   405
    }
jacint@836
   406
jacint@836
   407
jacint@836
   408
    ///Returns a minimum value cut.
jacint@836
   409
jacint@836
   410
    ///Sets \c M to the characteristic vector of a minimum value
jacint@836
   411
    ///cut. This method can be called both after running \ref
jacint@836
   412
    ///phase1 and \ref phase2. It is much faster after
marci@849
   413
    ///\ref phase1.  \pre M should be a bool-valued node-map. \pre
alpar@911
   414
    ///If \ref minCut() is called after \ref phase2() then M should
jacint@836
   415
    ///be initialized to false.
jacint@836
   416
    template<typename _CutMap>
jacint@836
   417
    void minCut(_CutMap& M) const {
jacint@836
   418
      switch ( status ) {
jacint@836
   419
	case AFTER_PREFLOW_PHASE_1:
alpar@1222
   420
	for(NodeIt v(*_g); v!=INVALID; ++v) {
alpar@1222
   421
	  if (level[v] < _node_num) {
jacint@836
   422
	    M.set(v, false);
jacint@836
   423
	  } else {
jacint@836
   424
	    M.set(v, true);
jacint@836
   425
	  }
jacint@836
   426
	}
jacint@836
   427
	break;
jacint@836
   428
	case AFTER_PREFLOW_PHASE_2:
jacint@836
   429
	minMinCut(M);
jacint@836
   430
	break;
jacint@836
   431
	case AFTER_NOTHING:
jacint@836
   432
	break;
jacint@836
   433
      }
jacint@836
   434
    }
jacint@836
   435
jacint@836
   436
    ///Returns the inclusionwise minimum of the minimum value cuts.
jacint@836
   437
jacint@836
   438
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836
   439
    ///which is inclusionwise minimum. It is computed by processing a
jacint@836
   440
    ///bfs from the source node \c s in the residual graph.  \pre M
jacint@836
   441
    ///should be a node map of bools initialized to false.  \pre \ref
jacint@836
   442
    ///phase2 should already be run.
jacint@836
   443
    template<typename _CutMap>
jacint@836
   444
    void minMinCut(_CutMap& M) const {
jacint@836
   445
jacint@836
   446
      std::queue<Node> queue;
alpar@1222
   447
      M.set(_source,true);
alpar@1227
   448
      queue.push(_source);
jacint@836
   449
      
jacint@836
   450
      while (!queue.empty()) {
jacint@836
   451
	Node w=queue.front();
jacint@836
   452
	queue.pop();
jacint@836
   453
	
alpar@1222
   454
	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   455
	  Node v=_g->target(e);
deba@2330
   456
	  if (!M[v] && _surely.positive((*_capacity)[e] -(*_flow)[e]) ) {
jacint@836
   457
	    queue.push(v);
jacint@836
   458
	    M.set(v, true);
jacint@836
   459
	  }
jacint@836
   460
	}
jacint@836
   461
	
alpar@1222
   462
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   463
	  Node v=_g->source(e);
jacint@2024
   464
	  if (!M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836
   465
	    queue.push(v);
jacint@836
   466
	    M.set(v, true);
jacint@836
   467
	  }
jacint@836
   468
	}
jacint@836
   469
      }
jacint@836
   470
    }
jacint@836
   471
    
jacint@836
   472
    ///Returns the inclusionwise maximum of the minimum value cuts.
jacint@836
   473
jacint@836
   474
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836
   475
    ///which is inclusionwise maximum. It is computed by processing a
jacint@836
   476
    ///backward bfs from the target node \c t in the residual graph.
alpar@911
   477
    ///\pre \ref phase2() or run() should already be run.
jacint@836
   478
    template<typename _CutMap>
jacint@836
   479
    void maxMinCut(_CutMap& M) const {
jacint@836
   480
alpar@1222
   481
      for(NodeIt v(*_g) ; v!=INVALID; ++v) M.set(v, true);
jacint@836
   482
jacint@836
   483
      std::queue<Node> queue;
jacint@836
   484
alpar@1222
   485
      M.set(_target,false);
alpar@1222
   486
      queue.push(_target);
jacint@836
   487
jacint@836
   488
      while (!queue.empty()) {
jacint@836
   489
        Node w=queue.front();
jacint@836
   490
	queue.pop();
jacint@836
   491
alpar@1222
   492
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   493
	  Node v=_g->source(e);
deba@2330
   494
	  if (M[v] && _surely.positive((*_capacity)[e] - (*_flow)[e]) ) {
jacint@836
   495
	    queue.push(v);
jacint@836
   496
	    M.set(v, false);
jacint@836
   497
	  }
jacint@836
   498
	}
jacint@836
   499
alpar@1222
   500
	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   501
	  Node v=_g->target(e);
jacint@2024
   502
	  if (M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836
   503
	    queue.push(v);
jacint@836
   504
	    M.set(v, false);
jacint@836
   505
	  }
jacint@836
   506
	}
jacint@836
   507
      }
jacint@836
   508
    }
jacint@836
   509
jacint@836
   510
    ///Sets the source node to \c _s.
jacint@836
   511
jacint@836
   512
    ///Sets the source node to \c _s.
jacint@836
   513
    /// 
alpar@1222
   514
    void source(Node _s) { 
alpar@1222
   515
      _source=_s; 
jacint@836
   516
      if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
jacint@836
   517
      status=AFTER_NOTHING; 
jacint@836
   518
    }
jacint@836
   519
alpar@1222
   520
    ///Returns the source node.
alpar@1222
   521
alpar@1222
   522
    ///Returns the source node.
alpar@1222
   523
    /// 
alpar@1222
   524
    Node source() const { 
alpar@1222
   525
      return _source;
alpar@1222
   526
    }
alpar@1222
   527
jacint@836
   528
    ///Sets the target node to \c _t.
jacint@836
   529
jacint@836
   530
    ///Sets the target node to \c _t.
jacint@836
   531
    ///
alpar@1222
   532
    void target(Node _t) { 
alpar@1222
   533
      _target=_t; 
jacint@836
   534
      if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
jacint@836
   535
      status=AFTER_NOTHING; 
jacint@836
   536
    }
jacint@836
   537
alpar@1222
   538
    ///Returns the target node.
alpar@1222
   539
alpar@1222
   540
    ///Returns the target node.
alpar@1222
   541
    /// 
alpar@1222
   542
    Node target() const { 
alpar@1222
   543
      return _target;
alpar@1222
   544
    }
alpar@1222
   545
jacint@836
   546
    /// Sets the edge map of the capacities to _cap.
jacint@836
   547
jacint@836
   548
    /// Sets the edge map of the capacities to _cap.
jacint@836
   549
    /// 
alpar@1222
   550
    void capacityMap(const CapacityMap& _cap) { 
alpar@1222
   551
      _capacity=&_cap; 
jacint@836
   552
      status=AFTER_NOTHING; 
jacint@836
   553
    }
zsuzska@1285
   554
    /// Returns a reference to capacity map.
alpar@1222
   555
zsuzska@1285
   556
    /// Returns a reference to capacity map.
alpar@1222
   557
    /// 
alpar@1222
   558
    const CapacityMap &capacityMap() const { 
alpar@1222
   559
      return *_capacity;
alpar@1222
   560
    }
jacint@836
   561
jacint@836
   562
    /// Sets the edge map of the flows to _flow.
jacint@836
   563
jacint@836
   564
    /// Sets the edge map of the flows to _flow.
jacint@836
   565
    /// 
alpar@1222
   566
    void flowMap(FlowMap& _f) { 
alpar@1222
   567
      _flow=&_f; 
jacint@836
   568
      flow_prop=NO_FLOW;
jacint@836
   569
      status=AFTER_NOTHING; 
jacint@836
   570
    }
alpar@1222
   571
     
zsuzska@1285
   572
    /// Returns a reference to flow map.
jacint@836
   573
zsuzska@1285
   574
    /// Returns a reference to flow map.
alpar@1222
   575
    /// 
alpar@1222
   576
    const FlowMap &flowMap() const { 
alpar@1222
   577
      return *_flow;
alpar@1222
   578
    }
jacint@836
   579
jacint@836
   580
  private:
jacint@836
   581
jacint@836
   582
    int push(Node w, NNMap& next, VecNode& first) {
jacint@836
   583
jacint@836
   584
      int lev=level[w];
jacint@836
   585
      Num exc=excess[w];
alpar@1222
   586
      int newlevel=_node_num;       //bound on the next level of w
jacint@836
   587
alpar@1222
   588
      for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
deba@2330
   589
	if ( !_surely.positive((*_capacity)[e] - (*_flow)[e])) continue;
alpar@1222
   590
	Node v=_g->target(e);
jacint@2024
   591
	
jacint@836
   592
	if( lev > level[v] ) { //Push is allowed now
jacint@836
   593
	  
deba@2330
   594
	  if ( excess[v] == 0 && v!=_target && v!=_source ) {
jacint@836
   595
	    next.set(v,first[level[v]]);
jacint@836
   596
	    first[level[v]]=v;
jacint@836
   597
	  }
jacint@836
   598
alpar@1222
   599
	  Num cap=(*_capacity)[e];
alpar@1222
   600
	  Num flo=(*_flow)[e];
jacint@836
   601
	  Num remcap=cap-flo;
jacint@836
   602
	  
jacint@2024
   603
	  if ( ! _surely.less(remcap, exc) ) { //A nonsaturating push.
jacint@836
   604
	    
alpar@1222
   605
	    _flow->set(e, flo+exc);
jacint@836
   606
	    excess.set(v, excess[v]+exc);
jacint@836
   607
	    exc=0;
jacint@836
   608
	    break;
jacint@836
   609
jacint@836
   610
	  } else { //A saturating push.
alpar@1222
   611
	    _flow->set(e, cap);
jacint@836
   612
	    excess.set(v, excess[v]+remcap);
jacint@836
   613
	    exc-=remcap;
jacint@836
   614
	  }
jacint@836
   615
	} else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836
   616
      } //for out edges wv
jacint@836
   617
deba@2330
   618
      if ( exc != 0 ) {
alpar@1222
   619
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
jacint@836
   620
	  
jacint@2024
   621
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   622
	  Node v=_g->source(e);
jacint@2024
   623
	  
jacint@836
   624
	  if( lev > level[v] ) { //Push is allowed now
jacint@836
   625
deba@2330
   626
	    if ( excess[v] == 0 && v!=_target && v!=_source ) {
jacint@836
   627
	      next.set(v,first[level[v]]);
jacint@836
   628
	      first[level[v]]=v;
jacint@836
   629
	    }
jacint@836
   630
alpar@1222
   631
	    Num flo=(*_flow)[e];
jacint@836
   632
jacint@2024
   633
	    if ( !_surely.less(flo, exc) ) { //A nonsaturating push.
jacint@836
   634
alpar@1222
   635
	      _flow->set(e, flo-exc);
jacint@836
   636
	      excess.set(v, excess[v]+exc);
jacint@836
   637
	      exc=0;
jacint@836
   638
	      break;
jacint@836
   639
	    } else {  //A saturating push.
jacint@836
   640
jacint@836
   641
	      excess.set(v, excess[v]+flo);
jacint@836
   642
	      exc-=flo;
alpar@1222
   643
	      _flow->set(e,0);
jacint@836
   644
	    }
jacint@836
   645
	  } else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836
   646
	} //for in edges vw
jacint@836
   647
jacint@836
   648
      } // if w still has excess after the out edge for cycle
jacint@836
   649
jacint@836
   650
      excess.set(w, exc);
jacint@836
   651
      
jacint@836
   652
      return newlevel;
jacint@836
   653
    }
jacint@836
   654
    
jacint@836
   655
    
jacint@836
   656
    
jacint@836
   657
    void preflowPreproc(VecNode& first, NNMap& next, 
jacint@836
   658
			VecNode& level_list, NNMap& left, NNMap& right)
jacint@836
   659
    {
alpar@1222
   660
      for(NodeIt v(*_g); v!=INVALID; ++v) level.set(v,_node_num);
jacint@836
   661
      std::queue<Node> bfs_queue;
jacint@836
   662
      
jacint@836
   663
      if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
jacint@836
   664
	//Reverse_bfs from t in the residual graph,
jacint@836
   665
	//to find the starting level.
alpar@1222
   666
	level.set(_target,0);
alpar@1222
   667
	bfs_queue.push(_target);
jacint@836
   668
	
jacint@836
   669
	while ( !bfs_queue.empty() ) {
jacint@836
   670
	  
jacint@836
   671
	  Node v=bfs_queue.front();
jacint@836
   672
	  bfs_queue.pop();
jacint@836
   673
	  int l=level[v]+1;
jacint@836
   674
	  
alpar@1222
   675
	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
deba@2330
   676
	    if ( !_surely.positive((*_capacity)[e] - (*_flow)[e] )) continue;
alpar@1222
   677
	    Node w=_g->source(e);
alpar@1222
   678
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   679
	      bfs_queue.push(w);
jacint@836
   680
	      Node z=level_list[l];
jacint@836
   681
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   682
	      right.set(w,z);
jacint@836
   683
	      level_list[l]=w;
jacint@836
   684
	      level.set(w, l);
jacint@836
   685
	    }
jacint@836
   686
	  }
jacint@836
   687
	  
alpar@1222
   688
	  for(OutEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
jacint@2024
   689
	    if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   690
	    Node w=_g->target(e);
alpar@1222
   691
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   692
	      bfs_queue.push(w);
jacint@836
   693
	      Node z=level_list[l];
jacint@836
   694
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   695
	      right.set(w,z);
jacint@836
   696
	      level_list[l]=w;
jacint@836
   697
	      level.set(w, l);
jacint@836
   698
	    }
jacint@836
   699
	  }
jacint@836
   700
	} //while
jacint@836
   701
      } //if
jacint@836
   702
jacint@836
   703
jacint@836
   704
      switch (flow_prop) {
jacint@836
   705
	case NO_FLOW:  
alpar@1222
   706
	for(EdgeIt e(*_g); e!=INVALID; ++e) _flow->set(e,0);
jacint@836
   707
	case ZERO_FLOW:
alpar@1222
   708
	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836
   709
	
jacint@836
   710
	//Reverse_bfs from t, to find the starting level.
alpar@1222
   711
	level.set(_target,0);
alpar@1222
   712
	bfs_queue.push(_target);
jacint@836
   713
	
jacint@836
   714
	while ( !bfs_queue.empty() ) {
jacint@836
   715
	  
jacint@836
   716
	  Node v=bfs_queue.front();
jacint@836
   717
	  bfs_queue.pop();
jacint@836
   718
	  int l=level[v]+1;
jacint@836
   719
	  
alpar@1222
   720
	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
alpar@1222
   721
	    Node w=_g->source(e);
alpar@1222
   722
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   723
	      bfs_queue.push(w);
jacint@836
   724
	      Node z=level_list[l];
jacint@836
   725
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   726
	      right.set(w,z);
jacint@836
   727
	      level_list[l]=w;
jacint@836
   728
	      level.set(w, l);
jacint@836
   729
	    }
jacint@836
   730
	  }
jacint@836
   731
	}
jacint@836
   732
	
jacint@836
   733
	//the starting flow
alpar@1222
   734
	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222
   735
	  Num c=(*_capacity)[e];
jacint@2024
   736
	  if ( !_surely.positive(c) ) continue;
alpar@1222
   737
	  Node w=_g->target(e);
alpar@1222
   738
	  if ( level[w] < _node_num ) {
deba@2330
   739
	    if ( excess[w] == 0 && w!=_target ) { //putting into the stack
jacint@836
   740
	      next.set(w,first[level[w]]);
jacint@836
   741
	      first[level[w]]=w;
jacint@836
   742
	    }
alpar@1222
   743
	    _flow->set(e, c);
jacint@836
   744
	    excess.set(w, excess[w]+c);
jacint@836
   745
	  }
jacint@836
   746
	}
jacint@836
   747
	break;
jacint@836
   748
jacint@836
   749
	case GEN_FLOW:
alpar@1222
   750
	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836
   751
	{
jacint@836
   752
	  Num exc=0;
alpar@1222
   753
	  for(InEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222
   754
	  for(OutEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc-=(*_flow)[e];
deba@2330
   755
          if (!_surely.positive(exc)) {
deba@2330
   756
            exc = 0;
deba@2330
   757
          }
deba@2330
   758
          excess.set(_target,exc);
jacint@836
   759
	}
jacint@836
   760
jacint@836
   761
	//the starting flow
alpar@1222
   762
	for(OutEdgeIt e(*_g,_source); e!=INVALID; ++e)	{
alpar@1222
   763
	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024
   764
	  if ( !_surely.positive(rem) ) continue;
alpar@1222
   765
	  Node w=_g->target(e);
alpar@1222
   766
	  if ( level[w] < _node_num ) {
deba@2330
   767
	    if ( excess[w] == 0 && w!=_target ) { //putting into the stack
jacint@836
   768
	      next.set(w,first[level[w]]);
jacint@836
   769
	      first[level[w]]=w;
jacint@836
   770
	    }   
alpar@1222
   771
	    _flow->set(e, (*_capacity)[e]);
jacint@836
   772
	    excess.set(w, excess[w]+rem);
jacint@836
   773
	  }
jacint@836
   774
	}
jacint@836
   775
	
alpar@1222
   776
	for(InEdgeIt e(*_g,_source); e!=INVALID; ++e) {
jacint@2024
   777
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   778
	  Node w=_g->source(e);
alpar@1222
   779
	  if ( level[w] < _node_num ) {
deba@2330
   780
	    if ( excess[w] == 0 && w!=_target ) {
jacint@836
   781
	      next.set(w,first[level[w]]);
jacint@836
   782
	      first[level[w]]=w;
jacint@836
   783
	    }  
alpar@1222
   784
	    excess.set(w, excess[w]+(*_flow)[e]);
alpar@1222
   785
	    _flow->set(e, 0);
jacint@836
   786
	  }
jacint@836
   787
	}
jacint@836
   788
	break;
jacint@836
   789
jacint@836
   790
	case PRE_FLOW:	
jacint@836
   791
	//the starting flow
alpar@1222
   792
	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222
   793
	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024
   794
	  if ( !_surely.positive(rem) ) continue;
alpar@1222
   795
	  Node w=_g->target(e);
alpar@1222
   796
	  if ( level[w] < _node_num ) _flow->set(e, (*_capacity)[e]);
jacint@836
   797
	}
jacint@836
   798
	
alpar@1222
   799
	for(InEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
jacint@2024
   800
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   801
	  Node w=_g->source(e);
alpar@1222
   802
	  if ( level[w] < _node_num ) _flow->set(e, 0);
jacint@836
   803
	}
jacint@836
   804
	
jacint@836
   805
	//computing the excess
alpar@1222
   806
	for(NodeIt w(*_g); w!=INVALID; ++w) {
jacint@836
   807
	  Num exc=0;
alpar@1222
   808
	  for(InEdgeIt e(*_g,w); e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222
   809
	  for(OutEdgeIt e(*_g,w); e!=INVALID; ++e) exc-=(*_flow)[e];
deba@2330
   810
          if (!_surely.positive(exc)) {
deba@2330
   811
            exc = 0;
deba@2330
   812
          }
jacint@836
   813
	  excess.set(w,exc);
jacint@836
   814
	  
jacint@836
   815
	  //putting the active nodes into the stack
jacint@836
   816
	  int lev=level[w];
deba@2330
   817
	    if ( exc != 0 && lev < _node_num && Node(w) != _target ) {
jacint@836
   818
	      next.set(w,first[lev]);
jacint@836
   819
	      first[lev]=w;
jacint@836
   820
	    }
jacint@836
   821
	}
jacint@836
   822
	break;
jacint@836
   823
      } //switch
jacint@836
   824
    } //preflowPreproc
jacint@836
   825
jacint@836
   826
jacint@836
   827
    void relabel(Node w, int newlevel, VecNode& first, NNMap& next, 
jacint@836
   828
		 VecNode& level_list, NNMap& left,
jacint@836
   829
		 NNMap& right, int& b, int& k, bool what_heur )
jacint@836
   830
    {
jacint@836
   831
jacint@836
   832
      int lev=level[w];
jacint@836
   833
jacint@836
   834
      Node right_n=right[w];
jacint@836
   835
      Node left_n=left[w];
jacint@836
   836
jacint@836
   837
      //unlacing starts
jacint@836
   838
      if ( right_n!=INVALID ) {
jacint@836
   839
	if ( left_n!=INVALID ) {
jacint@836
   840
	  right.set(left_n, right_n);
jacint@836
   841
	  left.set(right_n, left_n);
jacint@836
   842
	} else {
jacint@836
   843
	  level_list[lev]=right_n;
jacint@836
   844
	  left.set(right_n, INVALID);
jacint@836
   845
	}
jacint@836
   846
      } else {
jacint@836
   847
	if ( left_n!=INVALID ) {
jacint@836
   848
	  right.set(left_n, INVALID);
jacint@836
   849
	} else {
jacint@836
   850
	  level_list[lev]=INVALID;
jacint@836
   851
	}
jacint@836
   852
      }
jacint@836
   853
      //unlacing ends
jacint@836
   854
jacint@836
   855
      if ( level_list[lev]==INVALID ) {
jacint@836
   856
jacint@836
   857
	//gapping starts
jacint@836
   858
	for (int i=lev; i!=k ; ) {
jacint@836
   859
	  Node v=level_list[++i];
jacint@836
   860
	  while ( v!=INVALID ) {
alpar@1222
   861
	    level.set(v,_node_num);
jacint@836
   862
	    v=right[v];
jacint@836
   863
	  }
jacint@836
   864
	  level_list[i]=INVALID;
jacint@836
   865
	  if ( !what_heur ) first[i]=INVALID;
jacint@836
   866
	}
jacint@836
   867
alpar@1222
   868
	level.set(w,_node_num);
jacint@836
   869
	b=lev-1;
jacint@836
   870
	k=b;
jacint@836
   871
	//gapping ends
jacint@836
   872
jacint@836
   873
      } else {
jacint@836
   874
alpar@1222
   875
	if ( newlevel == _node_num ) level.set(w,_node_num);
jacint@836
   876
	else {
jacint@836
   877
	  level.set(w,++newlevel);
jacint@836
   878
	  next.set(w,first[newlevel]);
jacint@836
   879
	  first[newlevel]=w;
jacint@836
   880
	  if ( what_heur ) b=newlevel;
jacint@836
   881
	  if ( k < newlevel ) ++k;      //now k=newlevel
jacint@836
   882
	  Node z=level_list[newlevel];
jacint@836
   883
	  if ( z!=INVALID ) left.set(z,w);
jacint@836
   884
	  right.set(w,z);
jacint@836
   885
	  left.set(w,INVALID);
jacint@836
   886
	  level_list[newlevel]=w;
jacint@836
   887
	}
jacint@836
   888
      }
jacint@836
   889
    } //relabel
jacint@836
   890
jacint@836
   891
  }; 
alpar@1227
   892
deba@2376
   893
  ///\ingroup max_flow
deba@1792
   894
  ///\brief Function type interface for Preflow algorithm.
deba@1792
   895
  ///
alpar@1227
   896
  ///Function type interface for Preflow algorithm.
alpar@1227
   897
  ///\sa Preflow
alpar@1227
   898
  template<class GR, class CM, class FM>
alpar@1227
   899
  Preflow<GR,typename CM::Value,CM,FM> preflow(const GR &g,
alpar@1227
   900
			    typename GR::Node source,
alpar@1227
   901
			    typename GR::Node target,
alpar@1227
   902
			    const CM &cap,
alpar@1227
   903
			    FM &flow
alpar@1227
   904
			    )
alpar@1227
   905
  {
alpar@1227
   906
    return Preflow<GR,typename CM::Value,CM,FM>(g,source,target,cap,flow);
alpar@1227
   907
  }
alpar@1227
   908
alpar@921
   909
} //namespace lemon
jacint@836
   910
alpar@921
   911
#endif //LEMON_PREFLOW_H