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