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