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