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