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