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