src/work/jacint/max_flow.h
author alpar
Sun, 09 May 2004 16:21:56 +0000
changeset 590 5c1465127b79
parent 557 9c0ce0a1f000
child 602 580b329c2a0c
permissions -rw-r--r--
Changes in the Maps' copy constructors.
marci@478
     1
// -*- C++ -*-
marci@478
     2
marci@478
     3
/*
marci@485
     4
  Heuristics: 
marci@485
     5
  2 phase
marci@485
     6
  gap
marci@485
     7
  list 'level_list' on the nodes on level i implemented by hand
marci@485
     8
  stack 'active' on the active nodes on level i
marci@485
     9
  runs heuristic 'highest label' for H1*n relabels
marci@485
    10
  runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
marci@478
    11
 
marci@485
    12
  Parameters H0 and H1 are initialized to 20 and 1.
marci@478
    13
marci@485
    14
  Constructors:
marci@478
    15
marci@485
    16
  Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if 
marci@485
    17
  FlowMap is not constant zero, and should be true if it is
marci@478
    18
marci@485
    19
  Members:
marci@478
    20
marci@485
    21
  void run()
marci@478
    22
marci@485
    23
  Num flowValue() : returns the value of a maximum flow
marci@478
    24
marci@485
    25
  void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
marci@485
    26
  minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
marci@478
    27
marci@485
    28
  void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
marci@485
    29
  maximum min cut. M should be a map of bools initialized to false.
marci@478
    30
marci@485
    31
  void minCut(CutMap& M) : sets M to the characteristic vector of 
marci@485
    32
  a min cut. M should be a map of bools initialized to false.
marci@478
    33
marci@478
    34
*/
marci@478
    35
marci@480
    36
#ifndef HUGO_MAX_FLOW_H
marci@480
    37
#define HUGO_MAX_FLOW_H
marci@478
    38
marci@478
    39
#define H0 20
marci@478
    40
#define H1 1
marci@478
    41
marci@478
    42
#include <vector>
marci@478
    43
#include <queue>
marci@478
    44
#include <stack>
marci@478
    45
marci@557
    46
#include <hugo/graph_wrapper.h>
marci@478
    47
#include <bfs_iterator.h>
marci@555
    48
#include <hugo/invalid.h>
marci@555
    49
#include <hugo/maps.h>
marci@478
    50
#include <for_each_macros.h>
marci@478
    51
marci@488
    52
/// \file
marci@488
    53
/// \brief Dimacs file format reader.
marci@478
    54
marci@478
    55
namespace hugo {
marci@478
    56
marci@488
    57
marci@488
    58
//  ///\author Marton Makai, Jacint Szabo
marci@488
    59
  /// A class for computing max flows and related quantities.
marci@478
    60
  template <typename Graph, typename Num, 
marci@478
    61
	    typename CapMap=typename Graph::template EdgeMap<Num>, 
marci@478
    62
            typename FlowMap=typename Graph::template EdgeMap<Num> >
marci@478
    63
  class MaxFlow {
marci@478
    64
    
marci@478
    65
    typedef typename Graph::Node Node;
marci@478
    66
    typedef typename Graph::NodeIt NodeIt;
marci@478
    67
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@478
    68
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@478
    69
marci@478
    70
    typedef typename std::vector<std::stack<Node> > VecStack;
marci@478
    71
    typedef typename Graph::template NodeMap<Node> NNMap;
marci@478
    72
    typedef typename std::vector<Node> VecNode;
marci@478
    73
marci@478
    74
    const Graph* g;
marci@478
    75
    Node s;
marci@478
    76
    Node t;
marci@478
    77
    const CapMap* capacity;  
marci@478
    78
    FlowMap* flow;
marci@478
    79
    int n;      //the number of nodes of G
marci@478
    80
    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
marci@478
    81
    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
marci@478
    82
    typedef typename ResGW::Edge ResGWEdge;
marci@478
    83
    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
marci@478
    84
    typedef typename Graph::template NodeMap<int> ReachedMap;
marci@478
    85
    ReachedMap level;
marci@478
    86
    //level works as a bool map in augmenting path algorithms 
marci@478
    87
    //and is used by bfs for storing reached information.
marci@478
    88
    //In preflow, it shows levels of nodes.
marci@478
    89
    //typename Graph::template NodeMap<int> level;    
marci@478
    90
    typename Graph::template NodeMap<Num> excess; 
marci@510
    91
//   protected:
marci@510
    92
//     MaxFlow() { }
marci@510
    93
//     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, 
marci@510
    94
// 	     FlowMap& _flow) 
marci@510
    95
//       {
marci@510
    96
// 	g=&_G; 
marci@510
    97
// 	s=_s; 
marci@510
    98
// 	t=_t; 
marci@510
    99
// 	capacity=&_capacity;
marci@510
   100
// 	flow=&_flow;
marci@510
   101
// 	n=_G.nodeNum;
marci@510
   102
// 	level.set (_G); //kellene vmi ilyesmi fv 
marci@510
   103
// 	excess(_G,0); //itt is
marci@510
   104
//       }
marci@478
   105
marci@478
   106
  public:
marci@478
   107
 
alpar@586
   108
    ///\todo Document this.
alpar@586
   109
    ///\todo Maybe, it should be PRE_FLOW instead.
alpar@586
   110
    ///- \c ZERO_FLOW means something,
alpar@586
   111
    ///- \c GEN_FLOW means something else,
alpar@586
   112
    ///- \c PREFLOW is something different.
marci@478
   113
    enum flowEnum{
marci@478
   114
      ZERO_FLOW=0,
marci@478
   115
      GEN_FLOW=1,
marci@478
   116
      PREFLOW=2
marci@478
   117
    };
marci@478
   118
marci@478
   119
    MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, 
marci@478
   120
	    FlowMap& _flow) :
marci@478
   121
      g(&_G), s(_s), t(_t), capacity(&_capacity), 
marci@478
   122
      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
marci@478
   123
marci@485
   124
    /// A max flow algorithm is run.
marci@485
   125
    ///\pre the flow have to be 0 at the beginning.
marci@478
   126
    void run() {
marci@488
   127
      preflow(ZERO_FLOW);
marci@478
   128
    }
marci@478
   129
    
marci@487
   130
    /// A preflow algorithm is run. 
marci@487
   131
    ///\pre The initial edge-map have to be a 
marci@487
   132
    /// zero flow if \c fe is \c ZERO_FLOW,
marci@487
   133
    /// a flow if \c fe is \c GEN_FLOW, 
marci@487
   134
    /// and a pre-flow it is \c PREFLOW.
marci@488
   135
    void preflow(flowEnum fe) {
marci@478
   136
      preflowPhase0(fe);
marci@478
   137
      preflowPhase1();
marci@478
   138
    }
marci@478
   139
marci@485
   140
    /// Run the first phase of preflow, starting from a 0 flow, from a flow, 
marci@485
   141
    /// or from a preflow, according to \c fe.
marci@478
   142
    void preflowPhase0( flowEnum fe );
marci@478
   143
marci@485
   144
    /// Second phase of preflow.
marci@478
   145
    void preflowPhase1();
marci@478
   146
marci@485
   147
    /// Starting from a flow, this method searches for an augmenting path 
marci@485
   148
    /// according to the Edmonds-Karp algorithm 
marci@485
   149
    /// and augments the flow on if any. 
marci@487
   150
    /// The return value shows if the augmentation was succesful.
marci@478
   151
    bool augmentOnShortestPath();
marci@478
   152
marci@485
   153
    /// Starting from a flow, this method searches for an augmenting blockin 
marci@485
   154
    /// flow according to Dinits' algorithm and augments the flow on if any. 
marci@485
   155
    /// The blocking flow is computed in a physically constructed 
marci@485
   156
    /// residual graph of type \c Mutablegraph.
marci@487
   157
    /// The return value show sif the augmentation was succesful.
marci@478
   158
    template<typename MutableGraph> bool augmentOnBlockingFlow();
marci@478
   159
marci@485
   160
    /// The same as \c augmentOnBlockingFlow<MutableGraph> but the 
marci@485
   161
    /// residual graph is not constructed physically.
marci@487
   162
    /// The return value shows if the augmentation was succesful.
marci@478
   163
    bool augmentOnBlockingFlow2();
marci@478
   164
marci@478
   165
    /// Returns the actual flow value.
marci@478
   166
    /// More precisely, it returns the negative excess of s, thus 
marci@478
   167
    /// this works also for preflows.
marci@478
   168
    Num flowValue() { 
marci@478
   169
      Num a=0;
marci@478
   170
      FOR_EACH_INC_LOC(OutEdgeIt, e, *g, s) a+=(*flow)[e];
marci@478
   171
      FOR_EACH_INC_LOC(InEdgeIt, e, *g, s) a-=(*flow)[e];
marci@478
   172
      return a;
marci@478
   173
    }
marci@478
   174
marci@485
   175
    /// Should be used between preflowPhase0 and preflowPhase1.
marci@485
   176
    ///\todo We have to make some status variable which shows the actual state 
marci@485
   177
    /// of the class. This enables us to determine which methods are valid 
marci@485
   178
    /// for MinCut computation
marci@478
   179
    template<typename _CutMap>
marci@478
   180
    void actMinCut(_CutMap& M) {
marci@478
   181
      NodeIt v;
marci@485
   182
      for(g->first(v); g->valid(v); g->next(v)) {
marci@485
   183
	if ( level[v] < n ) {
marci@485
   184
	  M.set(v,false);
marci@485
   185
	} else {
marci@485
   186
	  M.set(v,true);
marci@485
   187
	}
marci@478
   188
      }
marci@478
   189
    }
marci@478
   190
marci@485
   191
    /// The unique inclusionwise minimum cut is computed by 
marci@485
   192
    /// processing a bfs from s in the residual graph.
marci@485
   193
    ///\pre flow have to be a max flow otherwise it will the whole node-set.
marci@478
   194
    template<typename _CutMap>
marci@478
   195
    void minMinCut(_CutMap& M) {
marci@478
   196
    
marci@478
   197
      std::queue<Node> queue;
marci@478
   198
      
marci@478
   199
      M.set(s,true);      
marci@478
   200
      queue.push(s);
marci@478
   201
marci@478
   202
      while (!queue.empty()) {
marci@478
   203
        Node w=queue.front();
marci@478
   204
	queue.pop();
marci@478
   205
marci@478
   206
	OutEdgeIt e;
marci@478
   207
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
marci@478
   208
	  Node v=g->head(e);
marci@478
   209
	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
marci@478
   210
	    queue.push(v);
marci@478
   211
	    M.set(v, true);
marci@478
   212
	  }
marci@478
   213
	} 
marci@478
   214
marci@478
   215
	InEdgeIt f;
marci@478
   216
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
marci@478
   217
	  Node v=g->tail(f);
marci@478
   218
	  if (!M[v] && (*flow)[f] > 0 ) {
marci@478
   219
	    queue.push(v);
marci@478
   220
	    M.set(v, true);
marci@478
   221
	  }
marci@478
   222
	} 
marci@478
   223
      }
marci@478
   224
    }
marci@478
   225
marci@478
   226
marci@485
   227
    /// The unique inclusionwise maximum cut is computed by 
marci@485
   228
    /// processing a reverse bfs from t in the residual graph.
marci@485
   229
    ///\pre flow have to be a max flow otherwise it will be empty.
marci@478
   230
    template<typename _CutMap>
marci@478
   231
    void maxMinCut(_CutMap& M) {
marci@478
   232
marci@478
   233
      NodeIt v;
marci@478
   234
      for(g->first(v) ; g->valid(v); g->next(v)) {
marci@478
   235
	M.set(v, true);
marci@478
   236
      }
marci@478
   237
marci@478
   238
      std::queue<Node> queue;
marci@478
   239
      
marci@478
   240
      M.set(t,false);        
marci@478
   241
      queue.push(t);
marci@478
   242
marci@478
   243
      while (!queue.empty()) {
marci@478
   244
        Node w=queue.front();
marci@478
   245
	queue.pop();
marci@478
   246
marci@478
   247
marci@478
   248
	InEdgeIt e;
marci@478
   249
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
marci@478
   250
	  Node v=g->tail(e);
marci@478
   251
	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
marci@478
   252
	    queue.push(v);
marci@478
   253
	    M.set(v, false);
marci@478
   254
	  }
marci@478
   255
	}
marci@478
   256
	
marci@478
   257
	OutEdgeIt f;
marci@478
   258
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
marci@478
   259
	  Node v=g->head(f);
marci@478
   260
	  if (M[v] && (*flow)[f] > 0 ) {
marci@478
   261
	    queue.push(v);
marci@478
   262
	    M.set(v, false);
marci@478
   263
	  }
marci@478
   264
	}
marci@478
   265
      }
marci@478
   266
    }
marci@478
   267
marci@478
   268
marci@485
   269
    /// A minimum cut is computed.
marci@478
   270
    template<typename CutMap>
marci@485
   271
    void minCut(CutMap& M) { minMinCut(M); }
marci@478
   272
marci@485
   273
    ///
marci@487
   274
    void resetSource(Node _s) { s=_s; }
marci@487
   275
    ///
marci@487
   276
    void resetTarget(Node _t) { t=_t; }
marci@478
   277
   
marci@485
   278
    /// capacity-map is changed.
marci@485
   279
    void resetCap(const CapMap& _cap) { capacity=&_cap; }
marci@478
   280
    
marci@485
   281
    /// flow-map is changed. 
marci@485
   282
    void resetFlow(FlowMap& _flow) { flow=&_flow; }
marci@478
   283
marci@478
   284
marci@478
   285
  private:
marci@478
   286
marci@478
   287
    int push(Node w, VecStack& active) {
marci@478
   288
      
marci@478
   289
      int lev=level[w];
marci@478
   290
      Num exc=excess[w];
marci@478
   291
      int newlevel=n;       //bound on the next level of w
marci@478
   292
	  
marci@478
   293
      OutEdgeIt e;
marci@478
   294
      for(g->first(e,w); g->valid(e); g->next(e)) {
marci@478
   295
	    
marci@478
   296
	if ( (*flow)[e] >= (*capacity)[e] ) continue; 
marci@478
   297
	Node v=g->head(e);            
marci@478
   298
	    
marci@478
   299
	if( lev > level[v] ) { //Push is allowed now
marci@478
   300
	  
marci@478
   301
	  if ( excess[v]<=0 && v!=t && v!=s ) {
marci@478
   302
	    int lev_v=level[v];
marci@478
   303
	    active[lev_v].push(v);
marci@478
   304
	  }
marci@478
   305
	  
marci@478
   306
	  Num cap=(*capacity)[e];
marci@478
   307
	  Num flo=(*flow)[e];
marci@478
   308
	  Num remcap=cap-flo;
marci@478
   309
	  
marci@478
   310
	  if ( remcap >= exc ) { //A nonsaturating push.
marci@478
   311
	    
marci@478
   312
	    flow->set(e, flo+exc);
marci@478
   313
	    excess.set(v, excess[v]+exc);
marci@478
   314
	    exc=0;
marci@478
   315
	    break; 
marci@478
   316
	    
marci@478
   317
	  } else { //A saturating push.
marci@478
   318
	    flow->set(e, cap);
marci@478
   319
	    excess.set(v, excess[v]+remcap);
marci@478
   320
	    exc-=remcap;
marci@478
   321
	  }
marci@478
   322
	} else if ( newlevel > level[v] ) newlevel = level[v];
marci@478
   323
      } //for out edges wv 
marci@478
   324
      
marci@478
   325
      if ( exc > 0 ) {	
marci@478
   326
	InEdgeIt e;
marci@478
   327
	for(g->first(e,w); g->valid(e); g->next(e)) {
marci@478
   328
	  
marci@478
   329
	  if( (*flow)[e] <= 0 ) continue; 
marci@478
   330
	  Node v=g->tail(e); 
marci@478
   331
	  
marci@478
   332
	  if( lev > level[v] ) { //Push is allowed now
marci@478
   333
	    
marci@478
   334
	    if ( excess[v]<=0 && v!=t && v!=s ) {
marci@478
   335
	      int lev_v=level[v];
marci@478
   336
	      active[lev_v].push(v);
marci@478
   337
	    }
marci@478
   338
	    
marci@478
   339
	    Num flo=(*flow)[e];
marci@478
   340
	    
marci@478
   341
	    if ( flo >= exc ) { //A nonsaturating push.
marci@478
   342
	      
marci@478
   343
	      flow->set(e, flo-exc);
marci@478
   344
	      excess.set(v, excess[v]+exc);
marci@478
   345
	      exc=0;
marci@478
   346
	      break; 
marci@478
   347
	    } else {  //A saturating push.
marci@478
   348
	      
marci@478
   349
	      excess.set(v, excess[v]+flo);
marci@478
   350
	      exc-=flo;
marci@478
   351
	      flow->set(e,0);
marci@478
   352
	    }  
marci@478
   353
	  } else if ( newlevel > level[v] ) newlevel = level[v];
marci@478
   354
	} //for in edges vw
marci@478
   355
	
marci@478
   356
      } // if w still has excess after the out edge for cycle
marci@478
   357
      
marci@478
   358
      excess.set(w, exc);
marci@478
   359
      
marci@478
   360
      return newlevel;
marci@485
   361
    }
marci@478
   362
marci@478
   363
marci@478
   364
    void preflowPreproc ( flowEnum fe, VecStack& active, 
marci@478
   365
			  VecNode& level_list, NNMap& left, NNMap& right ) {
marci@478
   366
marci@485
   367
			    std::queue<Node> bfs_queue;
marci@478
   368
      
marci@485
   369
			    switch ( fe ) {
marci@485
   370
			    case ZERO_FLOW: 
marci@485
   371
			      {
marci@485
   372
				//Reverse_bfs from t, to find the starting level.
marci@485
   373
				level.set(t,0);
marci@485
   374
				bfs_queue.push(t);
marci@478
   375
	
marci@485
   376
				while (!bfs_queue.empty()) {
marci@478
   377
	    
marci@485
   378
				  Node v=bfs_queue.front();	
marci@485
   379
				  bfs_queue.pop();
marci@485
   380
				  int l=level[v]+1;
marci@478
   381
	    
marci@485
   382
				  InEdgeIt e;
marci@485
   383
				  for(g->first(e,v); g->valid(e); g->next(e)) {
marci@485
   384
				    Node w=g->tail(e);
marci@485
   385
				    if ( level[w] == n && w != s ) {
marci@485
   386
				      bfs_queue.push(w);
marci@485
   387
				      Node first=level_list[l];
marci@485
   388
				      if ( g->valid(first) ) left.set(first,w);
marci@485
   389
				      right.set(w,first);
marci@485
   390
				      level_list[l]=w;
marci@485
   391
				      level.set(w, l);
marci@485
   392
				    }
marci@485
   393
				  }
marci@485
   394
				}
marci@478
   395
	  
marci@485
   396
				//the starting flow
marci@485
   397
				OutEdgeIt e;
marci@485
   398
				for(g->first(e,s); g->valid(e); g->next(e)) 
marci@485
   399
				  {
marci@485
   400
				    Num c=(*capacity)[e];
marci@485
   401
				    if ( c <= 0 ) continue;
marci@485
   402
				    Node w=g->head(e);
marci@485
   403
				    if ( level[w] < n ) {	  
marci@485
   404
				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@485
   405
				      flow->set(e, c); 
marci@485
   406
				      excess.set(w, excess[w]+c);
marci@485
   407
				    }
marci@485
   408
				  }
marci@485
   409
				break;
marci@485
   410
			      }
marci@478
   411
	
marci@485
   412
			    case GEN_FLOW:
marci@485
   413
			    case PREFLOW:
marci@485
   414
			      {
marci@485
   415
				//Reverse_bfs from t in the residual graph, 
marci@485
   416
				//to find the starting level.
marci@485
   417
				level.set(t,0);
marci@485
   418
				bfs_queue.push(t);
marci@478
   419
	  
marci@485
   420
				while (!bfs_queue.empty()) {
marci@478
   421
	    
marci@485
   422
				  Node v=bfs_queue.front();	
marci@485
   423
				  bfs_queue.pop();
marci@485
   424
				  int l=level[v]+1;
marci@478
   425
	    
marci@485
   426
				  InEdgeIt e;
marci@485
   427
				  for(g->first(e,v); g->valid(e); g->next(e)) {
marci@485
   428
				    if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@485
   429
				    Node w=g->tail(e);
marci@485
   430
				    if ( level[w] == n && w != s ) {
marci@485
   431
				      bfs_queue.push(w);
marci@485
   432
				      Node first=level_list[l];
marci@485
   433
				      if ( g->valid(first) ) left.set(first,w);
marci@485
   434
				      right.set(w,first);
marci@485
   435
				      level_list[l]=w;
marci@485
   436
				      level.set(w, l);
marci@485
   437
				    }
marci@485
   438
				  }
marci@478
   439
	    
marci@485
   440
				  OutEdgeIt f;
marci@485
   441
				  for(g->first(f,v); g->valid(f); g->next(f)) {
marci@485
   442
				    if ( 0 >= (*flow)[f] ) continue;
marci@485
   443
				    Node w=g->head(f);
marci@485
   444
				    if ( level[w] == n && w != s ) {
marci@485
   445
				      bfs_queue.push(w);
marci@485
   446
				      Node first=level_list[l];
marci@485
   447
				      if ( g->valid(first) ) left.set(first,w);
marci@485
   448
				      right.set(w,first);
marci@485
   449
				      level_list[l]=w;
marci@485
   450
				      level.set(w, l);
marci@485
   451
				    }
marci@485
   452
				  }
marci@485
   453
				}
marci@478
   454
	  
marci@478
   455
	  
marci@485
   456
				//the starting flow
marci@485
   457
				OutEdgeIt e;
marci@485
   458
				for(g->first(e,s); g->valid(e); g->next(e)) 
marci@485
   459
				  {
marci@485
   460
				    Num rem=(*capacity)[e]-(*flow)[e];
marci@485
   461
				    if ( rem <= 0 ) continue;
marci@485
   462
				    Node w=g->head(e);
marci@485
   463
				    if ( level[w] < n ) {	  
marci@485
   464
				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@485
   465
				      flow->set(e, (*capacity)[e]); 
marci@485
   466
				      excess.set(w, excess[w]+rem);
marci@485
   467
				    }
marci@485
   468
				  }
marci@478
   469
	  
marci@485
   470
				InEdgeIt f;
marci@485
   471
				for(g->first(f,s); g->valid(f); g->next(f)) 
marci@485
   472
				  {
marci@485
   473
				    if ( (*flow)[f] <= 0 ) continue;
marci@485
   474
				    Node w=g->tail(f);
marci@485
   475
				    if ( level[w] < n ) {	  
marci@485
   476
				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@485
   477
				      excess.set(w, excess[w]+(*flow)[f]);
marci@485
   478
				      flow->set(f, 0); 
marci@485
   479
				    }
marci@485
   480
				  }  
marci@485
   481
				break;
marci@485
   482
			      } //case PREFLOW
marci@485
   483
			    }
marci@485
   484
			  } //preflowPreproc
marci@478
   485
marci@478
   486
marci@478
   487
marci@478
   488
    void relabel(Node w, int newlevel, VecStack& active,  
marci@478
   489
		 VecNode& level_list, NNMap& left, 
marci@478
   490
		 NNMap& right, int& b, int& k, bool what_heur ) 
marci@478
   491
    {
marci@478
   492
marci@478
   493
      Num lev=level[w];	
marci@478
   494
      
marci@478
   495
      Node right_n=right[w];
marci@478
   496
      Node left_n=left[w];
marci@478
   497
      
marci@478
   498
      //unlacing starts
marci@478
   499
      if ( g->valid(right_n) ) {
marci@478
   500
	if ( g->valid(left_n) ) {
marci@478
   501
	  right.set(left_n, right_n);
marci@478
   502
	  left.set(right_n, left_n);
marci@478
   503
	} else {
marci@478
   504
	  level_list[lev]=right_n;   
marci@478
   505
	  left.set(right_n, INVALID);
marci@478
   506
	} 
marci@478
   507
      } else {
marci@478
   508
	if ( g->valid(left_n) ) {
marci@478
   509
	  right.set(left_n, INVALID);
marci@478
   510
	} else { 
marci@478
   511
	  level_list[lev]=INVALID;   
marci@478
   512
	} 
marci@478
   513
      } 
marci@478
   514
      //unlacing ends
marci@478
   515
		
marci@478
   516
      if ( !g->valid(level_list[lev]) ) {
marci@478
   517
	      
marci@478
   518
	//gapping starts
marci@478
   519
	for (int i=lev; i!=k ; ) {
marci@478
   520
	  Node v=level_list[++i];
marci@478
   521
	  while ( g->valid(v) ) {
marci@478
   522
	    level.set(v,n);
marci@478
   523
	    v=right[v];
marci@478
   524
	  }
marci@478
   525
	  level_list[i]=INVALID;
marci@478
   526
	  if ( !what_heur ) {
marci@478
   527
	    while ( !active[i].empty() ) {
marci@478
   528
	      active[i].pop();    //FIXME: ezt szebben kene
marci@478
   529
	    }
marci@478
   530
	  }	     
marci@478
   531
	}
marci@478
   532
	
marci@478
   533
	level.set(w,n);
marci@478
   534
	b=lev-1;
marci@478
   535
	k=b;
marci@478
   536
	//gapping ends
marci@478
   537
	
marci@478
   538
      } else {
marci@478
   539
	
marci@478
   540
	if ( newlevel == n ) level.set(w,n); 
marci@478
   541
	else {
marci@478
   542
	  level.set(w,++newlevel);
marci@478
   543
	  active[newlevel].push(w);
marci@478
   544
	  if ( what_heur ) b=newlevel;
marci@478
   545
	  if ( k < newlevel ) ++k;      //now k=newlevel
marci@478
   546
	  Node first=level_list[newlevel];
marci@478
   547
	  if ( g->valid(first) ) left.set(first,w);
marci@478
   548
	  right.set(w,first);
marci@478
   549
	  left.set(w,INVALID);
marci@478
   550
	  level_list[newlevel]=w;
marci@478
   551
	}
marci@478
   552
      }
marci@478
   553
      
marci@478
   554
    } //relabel
marci@478
   555
marci@478
   556
marci@478
   557
    template<typename MapGraphWrapper> 
marci@478
   558
    class DistanceMap {
marci@478
   559
    protected:
marci@478
   560
      const MapGraphWrapper* g;
marci@478
   561
      typename MapGraphWrapper::template NodeMap<int> dist; 
marci@478
   562
    public:
marci@478
   563
      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
marci@478
   564
      void set(const typename MapGraphWrapper::Node& n, int a) { 
marci@478
   565
	dist.set(n, a); 
marci@478
   566
      }
marci@478
   567
      int operator[](const typename MapGraphWrapper::Node& n) 
marci@485
   568
      { return dist[n]; }
marci@485
   569
      //       int get(const typename MapGraphWrapper::Node& n) const { 
marci@485
   570
      // 	return dist[n]; }
marci@485
   571
      //       bool get(const typename MapGraphWrapper::Edge& e) const { 
marci@485
   572
      // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
marci@478
   573
      bool operator[](const typename MapGraphWrapper::Edge& e) const { 
marci@478
   574
	return (dist[g->tail(e)]<dist[g->head(e)]); 
marci@478
   575
      }
marci@478
   576
    };
marci@478
   577
    
marci@478
   578
  };
marci@478
   579
marci@478
   580
marci@478
   581
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   582
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe ) 
marci@478
   583
  {
marci@478
   584
      
marci@485
   585
    int heur0=(int)(H0*n);  //time while running 'bound decrease' 
marci@485
   586
    int heur1=(int)(H1*n);  //time while running 'highest label'
marci@485
   587
    int heur=heur1;         //starting time interval (#of relabels)
marci@485
   588
    int numrelabel=0;
marci@478
   589
     
marci@485
   590
    bool what_heur=1;       
marci@485
   591
    //It is 0 in case 'bound decrease' and 1 in case 'highest label'
marci@478
   592
marci@485
   593
    bool end=false;     
marci@485
   594
    //Needed for 'bound decrease', true means no active nodes are above bound b.
marci@478
   595
marci@485
   596
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   597
    int b=k;    //bound on the highest level under n of an active node
marci@478
   598
      
marci@485
   599
    VecStack active(n);
marci@478
   600
      
marci@485
   601
    NNMap left(*g, INVALID);
marci@485
   602
    NNMap right(*g, INVALID);
marci@485
   603
    VecNode level_list(n,INVALID);
marci@485
   604
    //List of the nodes in level i<n, set to n.
marci@478
   605
marci@485
   606
    NodeIt v;
marci@485
   607
    for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
marci@485
   608
    //setting each node to level n
marci@478
   609
      
marci@485
   610
    switch ( fe ) {
marci@485
   611
    case PREFLOW:
marci@485
   612
      {
marci@485
   613
	//counting the excess
marci@485
   614
	NodeIt v;
marci@485
   615
	for(g->first(v); g->valid(v); g->next(v)) {
marci@478
   616
	  Num exc=0;
marci@478
   617
	  
marci@478
   618
	  InEdgeIt e;
marci@485
   619
	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@478
   620
	  OutEdgeIt f;
marci@485
   621
	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@485
   622
	    
marci@485
   623
	  excess.set(v,exc);	  
marci@485
   624
	    
marci@485
   625
	  //putting the active nodes into the stack
marci@485
   626
	  int lev=level[v];
marci@485
   627
	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
marci@478
   628
	}
marci@478
   629
	break;
marci@478
   630
      }
marci@485
   631
    case GEN_FLOW:
marci@485
   632
      {
marci@485
   633
	//Counting the excess of t
marci@485
   634
	Num exc=0;
marci@485
   635
	  
marci@485
   636
	InEdgeIt e;
marci@485
   637
	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@485
   638
	OutEdgeIt f;
marci@485
   639
	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@485
   640
	  
marci@485
   641
	excess.set(t,exc);	
marci@485
   642
	  
marci@485
   643
	break;
marci@485
   644
      }
marci@485
   645
    default:
marci@485
   646
      break;
marci@485
   647
    }
marci@478
   648
      
marci@485
   649
    preflowPreproc( fe, active, level_list, left, right );
marci@485
   650
    //End of preprocessing 
marci@478
   651
      
marci@478
   652
      
marci@485
   653
    //Push/relabel on the highest level active nodes.
marci@485
   654
    while ( true ) {
marci@485
   655
      if ( b == 0 ) {
marci@485
   656
	if ( !what_heur && !end && k > 0 ) {
marci@485
   657
	  b=k;
marci@485
   658
	  end=true;
marci@485
   659
	} else break;
marci@485
   660
      }
marci@485
   661
	
marci@485
   662
      if ( active[b].empty() ) --b; 
marci@485
   663
      else {
marci@485
   664
	end=false;  
marci@485
   665
	Node w=active[b].top();
marci@485
   666
	active[b].pop();
marci@485
   667
	int newlevel=push(w,active);
marci@485
   668
	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list, 
marci@485
   669
				     left, right, b, k, what_heur);
marci@485
   670
	  
marci@485
   671
	++numrelabel; 
marci@485
   672
	if ( numrelabel >= heur ) {
marci@485
   673
	  numrelabel=0;
marci@485
   674
	  if ( what_heur ) {
marci@485
   675
	    what_heur=0;
marci@485
   676
	    heur=heur0;
marci@485
   677
	    end=false;
marci@485
   678
	  } else {
marci@485
   679
	    what_heur=1;
marci@485
   680
	    heur=heur1;
marci@485
   681
	    b=k; 
marci@485
   682
	  }
marci@478
   683
	}
marci@478
   684
      } 
marci@485
   685
    } 
marci@485
   686
  }
marci@478
   687
marci@478
   688
marci@478
   689
marci@478
   690
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   691
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1() 
marci@478
   692
  {
marci@478
   693
      
marci@485
   694
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   695
    int b=k;    //bound on the highest level under n of an active node
marci@478
   696
      
marci@485
   697
    VecStack active(n);
marci@485
   698
    level.set(s,0);
marci@485
   699
    std::queue<Node> bfs_queue;
marci@485
   700
    bfs_queue.push(s);
marci@478
   701
	    
marci@485
   702
    while (!bfs_queue.empty()) {
marci@478
   703
	
marci@485
   704
      Node v=bfs_queue.front();	
marci@485
   705
      bfs_queue.pop();
marci@485
   706
      int l=level[v]+1;
marci@478
   707
	      
marci@485
   708
      InEdgeIt e;
marci@485
   709
      for(g->first(e,v); g->valid(e); g->next(e)) {
marci@485
   710
	if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@485
   711
	Node u=g->tail(e);
marci@485
   712
	if ( level[u] >= n ) { 
marci@485
   713
	  bfs_queue.push(u);
marci@485
   714
	  level.set(u, l);
marci@485
   715
	  if ( excess[u] > 0 ) active[l].push(u);
marci@478
   716
	}
marci@478
   717
      }
marci@485
   718
	
marci@485
   719
      OutEdgeIt f;
marci@485
   720
      for(g->first(f,v); g->valid(f); g->next(f)) {
marci@485
   721
	if ( 0 >= (*flow)[f] ) continue;
marci@485
   722
	Node u=g->head(f);
marci@485
   723
	if ( level[u] >= n ) { 
marci@485
   724
	  bfs_queue.push(u);
marci@485
   725
	  level.set(u, l);
marci@485
   726
	  if ( excess[u] > 0 ) active[l].push(u);
marci@485
   727
	}
marci@485
   728
      }
marci@485
   729
    }
marci@485
   730
    b=n-2;
marci@478
   731
marci@485
   732
    while ( true ) {
marci@478
   733
	
marci@485
   734
      if ( b == 0 ) break;
marci@478
   735
marci@485
   736
      if ( active[b].empty() ) --b; 
marci@485
   737
      else {
marci@485
   738
	Node w=active[b].top();
marci@485
   739
	active[b].pop();
marci@485
   740
	int newlevel=push(w,active);	  
marci@478
   741
marci@485
   742
	//relabel
marci@485
   743
	if ( excess[w] > 0 ) {
marci@485
   744
	  level.set(w,++newlevel);
marci@485
   745
	  active[newlevel].push(w);
marci@485
   746
	  b=newlevel;
marci@485
   747
	}
marci@485
   748
      }  // if stack[b] is nonempty
marci@485
   749
    } // while(true)
marci@485
   750
  }
marci@478
   751
marci@478
   752
marci@478
   753
marci@478
   754
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   755
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath() 
marci@478
   756
  {
marci@485
   757
    ResGW res_graph(*g, *capacity, *flow);
marci@485
   758
    bool _augment=false;
marci@478
   759
      
marci@485
   760
    //ReachedMap level(res_graph);
marci@485
   761
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   762
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   763
    bfs.pushAndSetReached(s);
marci@478
   764
	
marci@485
   765
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph); 
marci@485
   766
    pred.set(s, INVALID);
marci@478
   767
      
marci@485
   768
    typename ResGW::template NodeMap<Num> free(res_graph);
marci@478
   769
	
marci@485
   770
    //searching for augmenting path
marci@485
   771
    while ( !bfs.finished() ) { 
marci@485
   772
      ResGWOutEdgeIt e=bfs;
marci@485
   773
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
   774
	Node v=res_graph.tail(e);
marci@485
   775
	Node w=res_graph.head(e);
marci@485
   776
	pred.set(w, e);
marci@485
   777
	if (res_graph.valid(pred[v])) {
marci@485
   778
	  free.set(w, std::min(free[v], res_graph.resCap(e)));
marci@485
   779
	} else {
marci@485
   780
	  free.set(w, res_graph.resCap(e)); 
marci@478
   781
	}
marci@485
   782
	if (res_graph.head(e)==t) { _augment=true; break; }
marci@485
   783
      }
marci@478
   784
	
marci@485
   785
      ++bfs;
marci@485
   786
    } //end of searching augmenting path
marci@478
   787
marci@485
   788
    if (_augment) {
marci@485
   789
      Node n=t;
marci@485
   790
      Num augment_value=free[t];
marci@485
   791
      while (res_graph.valid(pred[n])) { 
marci@485
   792
	ResGWEdge e=pred[n];
marci@485
   793
	res_graph.augment(e, augment_value); 
marci@485
   794
	n=res_graph.tail(e);
marci@478
   795
      }
marci@485
   796
    }
marci@478
   797
marci@485
   798
    return _augment;
marci@485
   799
  }
marci@478
   800
marci@478
   801
marci@478
   802
marci@478
   803
marci@478
   804
marci@478
   805
marci@478
   806
marci@478
   807
marci@478
   808
marci@478
   809
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   810
  template<typename MutableGraph> 
marci@478
   811
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow() 
marci@478
   812
  {      
marci@485
   813
    typedef MutableGraph MG;
marci@485
   814
    bool _augment=false;
marci@478
   815
marci@485
   816
    ResGW res_graph(*g, *capacity, *flow);
marci@478
   817
marci@485
   818
    //bfs for distances on the residual graph
marci@485
   819
    //ReachedMap level(res_graph);
marci@485
   820
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   821
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   822
    bfs.pushAndSetReached(s);
marci@485
   823
    typename ResGW::template NodeMap<int> 
marci@485
   824
      dist(res_graph); //filled up with 0's
marci@478
   825
marci@485
   826
    //F will contain the physical copy of the residual graph
marci@485
   827
    //with the set of edges which are on shortest paths
marci@485
   828
    MG F;
marci@485
   829
    typename ResGW::template NodeMap<typename MG::Node> 
marci@485
   830
      res_graph_to_F(res_graph);
marci@485
   831
    {
marci@485
   832
      typename ResGW::NodeIt n;
marci@485
   833
      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
marci@485
   834
	res_graph_to_F.set(n, F.addNode());
marci@478
   835
      }
marci@485
   836
    }
marci@478
   837
marci@485
   838
    typename MG::Node sF=res_graph_to_F[s];
marci@485
   839
    typename MG::Node tF=res_graph_to_F[t];
marci@485
   840
    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
marci@485
   841
    typename MG::template EdgeMap<Num> residual_capacity(F);
marci@478
   842
marci@485
   843
    while ( !bfs.finished() ) { 
marci@485
   844
      ResGWOutEdgeIt e=bfs;
marci@485
   845
      if (res_graph.valid(e)) {
marci@485
   846
	if (bfs.isBNodeNewlyReached()) {
marci@485
   847
	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@485
   848
	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
marci@485
   849
	  original_edge.update();
marci@485
   850
	  original_edge.set(f, e);
marci@485
   851
	  residual_capacity.update();
marci@485
   852
	  residual_capacity.set(f, res_graph.resCap(e));
marci@485
   853
	} else {
marci@485
   854
	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
marci@478
   855
	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
marci@478
   856
	    original_edge.update();
marci@478
   857
	    original_edge.set(f, e);
marci@478
   858
	    residual_capacity.update();
marci@478
   859
	    residual_capacity.set(f, res_graph.resCap(e));
marci@478
   860
	  }
marci@478
   861
	}
marci@485
   862
      }
marci@485
   863
      ++bfs;
marci@485
   864
    } //computing distances from s in the residual graph
marci@478
   865
marci@485
   866
    bool __augment=true;
marci@478
   867
marci@485
   868
    while (__augment) {
marci@485
   869
      __augment=false;
marci@485
   870
      //computing blocking flow with dfs
marci@485
   871
      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
marci@485
   872
      typename MG::template NodeMap<typename MG::Edge> pred(F);
marci@485
   873
      pred.set(sF, INVALID);
marci@485
   874
      //invalid iterators for sources
marci@478
   875
marci@485
   876
      typename MG::template NodeMap<Num> free(F);
marci@478
   877
marci@485
   878
      dfs.pushAndSetReached(sF);      
marci@485
   879
      while (!dfs.finished()) {
marci@485
   880
	++dfs;
marci@485
   881
	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
marci@485
   882
	  if (dfs.isBNodeNewlyReached()) {
marci@485
   883
	    typename MG::Node v=F.aNode(dfs);
marci@485
   884
	    typename MG::Node w=F.bNode(dfs);
marci@485
   885
	    pred.set(w, dfs);
marci@485
   886
	    if (F.valid(pred[v])) {
marci@485
   887
	      free.set(w, std::min(free[v], residual_capacity[dfs]));
marci@485
   888
	    } else {
marci@485
   889
	      free.set(w, residual_capacity[dfs]); 
marci@485
   890
	    }
marci@485
   891
	    if (w==tF) { 
marci@485
   892
	      __augment=true; 
marci@485
   893
	      _augment=true;
marci@485
   894
	      break; 
marci@485
   895
	    }
marci@478
   896
	      
marci@485
   897
	  } else {
marci@485
   898
	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
marci@485
   899
	  }
marci@485
   900
	} 
marci@485
   901
      }
marci@485
   902
marci@485
   903
      if (__augment) {
marci@485
   904
	typename MG::Node n=tF;
marci@485
   905
	Num augment_value=free[tF];
marci@485
   906
	while (F.valid(pred[n])) { 
marci@485
   907
	  typename MG::Edge e=pred[n];
marci@485
   908
	  res_graph.augment(original_edge[e], augment_value); 
marci@485
   909
	  n=F.tail(e);
marci@485
   910
	  if (residual_capacity[e]==augment_value) 
marci@485
   911
	    F.erase(e); 
marci@485
   912
	  else 
marci@485
   913
	    residual_capacity.set(e, residual_capacity[e]-augment_value);
marci@478
   914
	}
marci@485
   915
      }
marci@478
   916
	
marci@485
   917
    }
marci@478
   918
            
marci@485
   919
    return _augment;
marci@485
   920
  }
marci@478
   921
marci@478
   922
marci@478
   923
marci@478
   924
marci@478
   925
marci@478
   926
marci@478
   927
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   928
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2() 
marci@478
   929
  {
marci@485
   930
    bool _augment=false;
marci@478
   931
marci@485
   932
    ResGW res_graph(*g, *capacity, *flow);
marci@478
   933
      
marci@485
   934
    //ReachedMap level(res_graph);
marci@485
   935
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   936
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@478
   937
marci@485
   938
    bfs.pushAndSetReached(s);
marci@485
   939
    DistanceMap<ResGW> dist(res_graph);
marci@485
   940
    while ( !bfs.finished() ) { 
marci@485
   941
      ResGWOutEdgeIt e=bfs;
marci@485
   942
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
   943
	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@485
   944
      }
marci@485
   945
      ++bfs;
marci@485
   946
    } //computing distances from s in the residual graph
marci@478
   947
marci@478
   948
      //Subgraph containing the edges on some shortest paths
marci@485
   949
    ConstMap<typename ResGW::Node, bool> true_map(true);
marci@485
   950
    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>, 
marci@485
   951
      DistanceMap<ResGW> > FilterResGW;
marci@485
   952
    FilterResGW filter_res_graph(res_graph, true_map, dist);
marci@478
   953
marci@485
   954
    //Subgraph, which is able to delete edges which are already 
marci@485
   955
    //met by the dfs
marci@485
   956
    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt> 
marci@485
   957
      first_out_edges(filter_res_graph);
marci@485
   958
    typename FilterResGW::NodeIt v;
marci@485
   959
    for(filter_res_graph.first(v); filter_res_graph.valid(v); 
marci@485
   960
	filter_res_graph.next(v)) 
marci@478
   961
      {
marci@478
   962
 	typename FilterResGW::OutEdgeIt e;
marci@478
   963
 	filter_res_graph.first(e, v);
marci@478
   964
 	first_out_edges.set(v, e);
marci@478
   965
      }
marci@485
   966
    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
marci@485
   967
      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
marci@485
   968
    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
marci@478
   969
marci@485
   970
    bool __augment=true;
marci@478
   971
marci@485
   972
    while (__augment) {
marci@478
   973
marci@485
   974
      __augment=false;
marci@485
   975
      //computing blocking flow with dfs
marci@485
   976
      DfsIterator< ErasingResGW, 
marci@485
   977
	typename ErasingResGW::template NodeMap<bool> > 
marci@485
   978
	dfs(erasing_res_graph);
marci@485
   979
      typename ErasingResGW::
marci@485
   980
	template NodeMap<typename ErasingResGW::OutEdgeIt> 
marci@485
   981
	pred(erasing_res_graph); 
marci@485
   982
      pred.set(s, INVALID);
marci@485
   983
      //invalid iterators for sources
marci@478
   984
marci@485
   985
      typename ErasingResGW::template NodeMap<Num> 
marci@485
   986
	free1(erasing_res_graph);
marci@478
   987
marci@485
   988
      dfs.pushAndSetReached(
marci@485
   989
			    typename ErasingResGW::Node(
marci@485
   990
							typename FilterResGW::Node(
marci@485
   991
										   typename ResGW::Node(s)
marci@485
   992
										   )
marci@485
   993
							)
marci@485
   994
			    );
marci@485
   995
      while (!dfs.finished()) {
marci@485
   996
	++dfs;
marci@485
   997
	if (erasing_res_graph.valid(
marci@485
   998
				    typename ErasingResGW::OutEdgeIt(dfs))) 
marci@478
   999
 	  { 
marci@478
  1000
  	    if (dfs.isBNodeNewlyReached()) {
marci@478
  1001
	  
marci@478
  1002
 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
marci@478
  1003
 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
marci@478
  1004
marci@478
  1005
 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
marci@478
  1006
 	      if (erasing_res_graph.valid(pred[v])) {
marci@478
  1007
 		free1.set(w, std::min(free1[v], res_graph.resCap(
marci@485
  1008
								 typename ErasingResGW::OutEdgeIt(dfs))));
marci@478
  1009
 	      } else {
marci@478
  1010
 		free1.set(w, res_graph.resCap(
marci@485
  1011
					      typename ErasingResGW::OutEdgeIt(dfs))); 
marci@478
  1012
 	      }
marci@478
  1013
	      
marci@478
  1014
 	      if (w==t) { 
marci@478
  1015
 		__augment=true; 
marci@478
  1016
 		_augment=true;
marci@478
  1017
 		break; 
marci@478
  1018
 	      }
marci@478
  1019
 	    } else {
marci@478
  1020
 	      erasing_res_graph.erase(dfs);
marci@478
  1021
	    }
marci@478
  1022
	  }
marci@485
  1023
      }	
marci@478
  1024
marci@485
  1025
      if (__augment) {
marci@485
  1026
	typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
marci@485
  1027
	// 	  typename ResGW::NodeMap<Num> a(res_graph);
marci@485
  1028
	// 	  typename ResGW::Node b;
marci@485
  1029
	// 	  Num j=a[b];
marci@485
  1030
	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
marci@485
  1031
	// 	  typename FilterResGW::Node b1;
marci@485
  1032
	// 	  Num j1=a1[b1];
marci@485
  1033
	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
marci@485
  1034
	// 	  typename ErasingResGW::Node b2;
marci@485
  1035
	// 	  Num j2=a2[b2];
marci@485
  1036
	Num augment_value=free1[n];
marci@485
  1037
	while (erasing_res_graph.valid(pred[n])) { 
marci@485
  1038
	  typename ErasingResGW::OutEdgeIt e=pred[n];
marci@485
  1039
	  res_graph.augment(e, augment_value);
marci@485
  1040
	  n=erasing_res_graph.tail(e);
marci@485
  1041
	  if (res_graph.resCap(e)==0)
marci@485
  1042
	    erasing_res_graph.erase(e);
marci@478
  1043
	}
marci@478
  1044
      }
marci@478
  1045
      
marci@485
  1046
    } //while (__augment) 
marci@478
  1047
            
marci@485
  1048
    return _augment;
marci@485
  1049
  }
marci@478
  1050
marci@478
  1051
marci@478
  1052
marci@478
  1053
marci@478
  1054
} //namespace hugo
marci@478
  1055
marci@480
  1056
#endif //HUGO_MAX_FLOW_H
marci@478
  1057
marci@478
  1058
marci@478
  1059
marci@478
  1060