src/work/jacint/max_flow.h
author athos
Tue, 11 May 2004 16:38:17 +0000
changeset 611 83530dad618a
parent 586 04fdffd38e89
child 615 b6b31b75b522
permissions -rw-r--r--
Some modifications and another testfile.
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@602
    47
#include <bfs_dfs.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@602
    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@602
    91
    //   protected:
marci@602
    92
    //     MaxFlow() { }
marci@602
    93
    //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, 
marci@602
    94
    // 	     FlowMap& _flow) 
marci@602
    95
    //       {
marci@602
    96
    // 	g=&_G; 
marci@602
    97
    // 	s=_s; 
marci@602
    98
    // 	t=_t; 
marci@602
    99
    // 	capacity=&_capacity;
marci@602
   100
    // 	flow=&_flow;
marci@602
   101
    // 	n=_G.nodeNum;
marci@602
   102
    // 	level.set (_G); //kellene vmi ilyesmi fv 
marci@602
   103
    // 	excess(_G,0); //itt is
marci@602
   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@602
   365
			  VecNode& level_list, NNMap& left, NNMap& right ) 
marci@602
   366
    {
marci@478
   367
marci@602
   368
      std::queue<Node> bfs_queue;
marci@478
   369
      
marci@602
   370
      switch ( fe ) {
marci@602
   371
      case ZERO_FLOW: 
marci@602
   372
	{
marci@602
   373
	  //Reverse_bfs from t, to find the starting level.
marci@602
   374
	  level.set(t,0);
marci@602
   375
	  bfs_queue.push(t);
marci@478
   376
	
marci@602
   377
	  while (!bfs_queue.empty()) {
marci@478
   378
	    
marci@602
   379
	    Node v=bfs_queue.front();	
marci@602
   380
	    bfs_queue.pop();
marci@602
   381
	    int l=level[v]+1;
marci@478
   382
	    
marci@602
   383
	    InEdgeIt e;
marci@602
   384
	    for(g->first(e,v); g->valid(e); g->next(e)) {
marci@602
   385
	      Node w=g->tail(e);
marci@602
   386
	      if ( level[w] == n && w != s ) {
marci@602
   387
		bfs_queue.push(w);
marci@602
   388
		Node first=level_list[l];
marci@602
   389
		if ( g->valid(first) ) left.set(first,w);
marci@602
   390
		right.set(w,first);
marci@602
   391
		level_list[l]=w;
marci@602
   392
		level.set(w, l);
marci@602
   393
	      }
marci@602
   394
	    }
marci@602
   395
	  }
marci@478
   396
	  
marci@602
   397
	  //the starting flow
marci@602
   398
	  OutEdgeIt e;
marci@602
   399
	  for(g->first(e,s); g->valid(e); g->next(e)) 
marci@602
   400
	    {
marci@602
   401
	      Num c=(*capacity)[e];
marci@602
   402
	      if ( c <= 0 ) continue;
marci@602
   403
	      Node w=g->head(e);
marci@602
   404
	      if ( level[w] < n ) {	  
marci@602
   405
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@602
   406
		flow->set(e, c); 
marci@602
   407
		excess.set(w, excess[w]+c);
marci@602
   408
	      }
marci@602
   409
	    }
marci@602
   410
	  break;
marci@602
   411
	}
marci@478
   412
	
marci@602
   413
      case GEN_FLOW:
marci@602
   414
      case PREFLOW:
marci@602
   415
	{
marci@602
   416
	  //Reverse_bfs from t in the residual graph, 
marci@602
   417
	  //to find the starting level.
marci@602
   418
	  level.set(t,0);
marci@602
   419
	  bfs_queue.push(t);
marci@478
   420
	  
marci@602
   421
	  while (!bfs_queue.empty()) {
marci@478
   422
	    
marci@602
   423
	    Node v=bfs_queue.front();	
marci@602
   424
	    bfs_queue.pop();
marci@602
   425
	    int l=level[v]+1;
marci@478
   426
	    
marci@602
   427
	    InEdgeIt e;
marci@602
   428
	    for(g->first(e,v); g->valid(e); g->next(e)) {
marci@602
   429
	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@602
   430
	      Node w=g->tail(e);
marci@602
   431
	      if ( level[w] == n && w != s ) {
marci@602
   432
		bfs_queue.push(w);
marci@602
   433
		Node first=level_list[l];
marci@602
   434
		if ( g->valid(first) ) left.set(first,w);
marci@602
   435
		right.set(w,first);
marci@602
   436
		level_list[l]=w;
marci@602
   437
		level.set(w, l);
marci@602
   438
	      }
marci@602
   439
	    }
marci@478
   440
	    
marci@602
   441
	    OutEdgeIt f;
marci@602
   442
	    for(g->first(f,v); g->valid(f); g->next(f)) {
marci@602
   443
	      if ( 0 >= (*flow)[f] ) continue;
marci@602
   444
	      Node w=g->head(f);
marci@602
   445
	      if ( level[w] == n && w != s ) {
marci@602
   446
		bfs_queue.push(w);
marci@602
   447
		Node first=level_list[l];
marci@602
   448
		if ( g->valid(first) ) left.set(first,w);
marci@602
   449
		right.set(w,first);
marci@602
   450
		level_list[l]=w;
marci@602
   451
		level.set(w, l);
marci@602
   452
	      }
marci@602
   453
	    }
marci@602
   454
	  }
marci@478
   455
	  
marci@478
   456
	  
marci@602
   457
	  //the starting flow
marci@602
   458
	  OutEdgeIt e;
marci@602
   459
	  for(g->first(e,s); g->valid(e); g->next(e)) 
marci@602
   460
	    {
marci@602
   461
	      Num rem=(*capacity)[e]-(*flow)[e];
marci@602
   462
	      if ( rem <= 0 ) continue;
marci@602
   463
	      Node w=g->head(e);
marci@602
   464
	      if ( level[w] < n ) {	  
marci@602
   465
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@602
   466
		flow->set(e, (*capacity)[e]); 
marci@602
   467
		excess.set(w, excess[w]+rem);
marci@602
   468
	      }
marci@602
   469
	    }
marci@478
   470
	  
marci@602
   471
	  InEdgeIt f;
marci@602
   472
	  for(g->first(f,s); g->valid(f); g->next(f)) 
marci@602
   473
	    {
marci@602
   474
	      if ( (*flow)[f] <= 0 ) continue;
marci@602
   475
	      Node w=g->tail(f);
marci@602
   476
	      if ( level[w] < n ) {	  
marci@602
   477
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@602
   478
		excess.set(w, excess[w]+(*flow)[f]);
marci@602
   479
		flow->set(f, 0); 
marci@602
   480
	      }
marci@602
   481
	    }  
marci@602
   482
	  break;
marci@602
   483
	} //case PREFLOW
marci@602
   484
      }
marci@602
   485
    } //preflowPreproc
marci@478
   486
marci@478
   487
marci@478
   488
marci@478
   489
    void relabel(Node w, int newlevel, VecStack& active,  
marci@478
   490
		 VecNode& level_list, NNMap& left, 
marci@478
   491
		 NNMap& right, int& b, int& k, bool what_heur ) 
marci@478
   492
    {
marci@478
   493
marci@478
   494
      Num lev=level[w];	
marci@478
   495
      
marci@478
   496
      Node right_n=right[w];
marci@478
   497
      Node left_n=left[w];
marci@478
   498
      
marci@478
   499
      //unlacing starts
marci@478
   500
      if ( g->valid(right_n) ) {
marci@478
   501
	if ( g->valid(left_n) ) {
marci@478
   502
	  right.set(left_n, right_n);
marci@478
   503
	  left.set(right_n, left_n);
marci@478
   504
	} else {
marci@478
   505
	  level_list[lev]=right_n;   
marci@478
   506
	  left.set(right_n, INVALID);
marci@478
   507
	} 
marci@478
   508
      } else {
marci@478
   509
	if ( g->valid(left_n) ) {
marci@478
   510
	  right.set(left_n, INVALID);
marci@478
   511
	} else { 
marci@478
   512
	  level_list[lev]=INVALID;   
marci@478
   513
	} 
marci@478
   514
      } 
marci@478
   515
      //unlacing ends
marci@478
   516
		
marci@478
   517
      if ( !g->valid(level_list[lev]) ) {
marci@478
   518
	      
marci@478
   519
	//gapping starts
marci@478
   520
	for (int i=lev; i!=k ; ) {
marci@478
   521
	  Node v=level_list[++i];
marci@478
   522
	  while ( g->valid(v) ) {
marci@478
   523
	    level.set(v,n);
marci@478
   524
	    v=right[v];
marci@478
   525
	  }
marci@478
   526
	  level_list[i]=INVALID;
marci@478
   527
	  if ( !what_heur ) {
marci@478
   528
	    while ( !active[i].empty() ) {
marci@478
   529
	      active[i].pop();    //FIXME: ezt szebben kene
marci@478
   530
	    }
marci@478
   531
	  }	     
marci@478
   532
	}
marci@478
   533
	
marci@478
   534
	level.set(w,n);
marci@478
   535
	b=lev-1;
marci@478
   536
	k=b;
marci@478
   537
	//gapping ends
marci@478
   538
	
marci@478
   539
      } else {
marci@478
   540
	
marci@478
   541
	if ( newlevel == n ) level.set(w,n); 
marci@478
   542
	else {
marci@478
   543
	  level.set(w,++newlevel);
marci@478
   544
	  active[newlevel].push(w);
marci@478
   545
	  if ( what_heur ) b=newlevel;
marci@478
   546
	  if ( k < newlevel ) ++k;      //now k=newlevel
marci@478
   547
	  Node first=level_list[newlevel];
marci@478
   548
	  if ( g->valid(first) ) left.set(first,w);
marci@478
   549
	  right.set(w,first);
marci@478
   550
	  left.set(w,INVALID);
marci@478
   551
	  level_list[newlevel]=w;
marci@478
   552
	}
marci@478
   553
      }
marci@478
   554
      
marci@478
   555
    } //relabel
marci@478
   556
marci@478
   557
marci@478
   558
    template<typename MapGraphWrapper> 
marci@478
   559
    class DistanceMap {
marci@478
   560
    protected:
marci@478
   561
      const MapGraphWrapper* g;
marci@478
   562
      typename MapGraphWrapper::template NodeMap<int> dist; 
marci@478
   563
    public:
marci@478
   564
      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
marci@478
   565
      void set(const typename MapGraphWrapper::Node& n, int a) { 
marci@478
   566
	dist.set(n, a); 
marci@478
   567
      }
marci@478
   568
      int operator[](const typename MapGraphWrapper::Node& n) 
marci@485
   569
      { return dist[n]; }
marci@485
   570
      //       int get(const typename MapGraphWrapper::Node& n) const { 
marci@485
   571
      // 	return dist[n]; }
marci@485
   572
      //       bool get(const typename MapGraphWrapper::Edge& e) const { 
marci@485
   573
      // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
marci@478
   574
      bool operator[](const typename MapGraphWrapper::Edge& e) const { 
marci@478
   575
	return (dist[g->tail(e)]<dist[g->head(e)]); 
marci@478
   576
      }
marci@478
   577
    };
marci@478
   578
    
marci@478
   579
  };
marci@478
   580
marci@478
   581
marci@478
   582
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   583
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe ) 
marci@478
   584
  {
marci@478
   585
      
marci@485
   586
    int heur0=(int)(H0*n);  //time while running 'bound decrease' 
marci@485
   587
    int heur1=(int)(H1*n);  //time while running 'highest label'
marci@485
   588
    int heur=heur1;         //starting time interval (#of relabels)
marci@485
   589
    int numrelabel=0;
marci@478
   590
     
marci@485
   591
    bool what_heur=1;       
marci@485
   592
    //It is 0 in case 'bound decrease' and 1 in case 'highest label'
marci@478
   593
marci@485
   594
    bool end=false;     
marci@485
   595
    //Needed for 'bound decrease', true means no active nodes are above bound b.
marci@478
   596
marci@485
   597
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   598
    int b=k;    //bound on the highest level under n of an active node
marci@478
   599
      
marci@485
   600
    VecStack active(n);
marci@478
   601
      
marci@485
   602
    NNMap left(*g, INVALID);
marci@485
   603
    NNMap right(*g, INVALID);
marci@485
   604
    VecNode level_list(n,INVALID);
marci@485
   605
    //List of the nodes in level i<n, set to n.
marci@478
   606
marci@485
   607
    NodeIt v;
marci@485
   608
    for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
marci@485
   609
    //setting each node to level n
marci@478
   610
      
marci@485
   611
    switch ( fe ) {
marci@485
   612
    case PREFLOW:
marci@485
   613
      {
marci@485
   614
	//counting the excess
marci@485
   615
	NodeIt v;
marci@485
   616
	for(g->first(v); g->valid(v); g->next(v)) {
marci@478
   617
	  Num exc=0;
marci@478
   618
	  
marci@478
   619
	  InEdgeIt e;
marci@485
   620
	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@478
   621
	  OutEdgeIt f;
marci@485
   622
	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@485
   623
	    
marci@485
   624
	  excess.set(v,exc);	  
marci@485
   625
	    
marci@485
   626
	  //putting the active nodes into the stack
marci@485
   627
	  int lev=level[v];
marci@485
   628
	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
marci@478
   629
	}
marci@478
   630
	break;
marci@478
   631
      }
marci@485
   632
    case GEN_FLOW:
marci@485
   633
      {
marci@485
   634
	//Counting the excess of t
marci@485
   635
	Num exc=0;
marci@485
   636
	  
marci@485
   637
	InEdgeIt e;
marci@485
   638
	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@485
   639
	OutEdgeIt f;
marci@485
   640
	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@485
   641
	  
marci@485
   642
	excess.set(t,exc);	
marci@485
   643
	  
marci@485
   644
	break;
marci@485
   645
      }
marci@485
   646
    default:
marci@485
   647
      break;
marci@485
   648
    }
marci@478
   649
      
marci@485
   650
    preflowPreproc( fe, active, level_list, left, right );
marci@485
   651
    //End of preprocessing 
marci@478
   652
      
marci@478
   653
      
marci@485
   654
    //Push/relabel on the highest level active nodes.
marci@485
   655
    while ( true ) {
marci@485
   656
      if ( b == 0 ) {
marci@485
   657
	if ( !what_heur && !end && k > 0 ) {
marci@485
   658
	  b=k;
marci@485
   659
	  end=true;
marci@485
   660
	} else break;
marci@485
   661
      }
marci@485
   662
	
marci@485
   663
      if ( active[b].empty() ) --b; 
marci@485
   664
      else {
marci@485
   665
	end=false;  
marci@485
   666
	Node w=active[b].top();
marci@485
   667
	active[b].pop();
marci@485
   668
	int newlevel=push(w,active);
marci@485
   669
	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list, 
marci@485
   670
				     left, right, b, k, what_heur);
marci@485
   671
	  
marci@485
   672
	++numrelabel; 
marci@485
   673
	if ( numrelabel >= heur ) {
marci@485
   674
	  numrelabel=0;
marci@485
   675
	  if ( what_heur ) {
marci@485
   676
	    what_heur=0;
marci@485
   677
	    heur=heur0;
marci@485
   678
	    end=false;
marci@485
   679
	  } else {
marci@485
   680
	    what_heur=1;
marci@485
   681
	    heur=heur1;
marci@485
   682
	    b=k; 
marci@485
   683
	  }
marci@478
   684
	}
marci@478
   685
      } 
marci@485
   686
    } 
marci@485
   687
  }
marci@478
   688
marci@478
   689
marci@478
   690
marci@478
   691
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   692
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1() 
marci@478
   693
  {
marci@478
   694
      
marci@485
   695
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   696
    int b=k;    //bound on the highest level under n of an active node
marci@478
   697
      
marci@485
   698
    VecStack active(n);
marci@485
   699
    level.set(s,0);
marci@485
   700
    std::queue<Node> bfs_queue;
marci@485
   701
    bfs_queue.push(s);
marci@478
   702
	    
marci@485
   703
    while (!bfs_queue.empty()) {
marci@478
   704
	
marci@485
   705
      Node v=bfs_queue.front();	
marci@485
   706
      bfs_queue.pop();
marci@485
   707
      int l=level[v]+1;
marci@478
   708
	      
marci@485
   709
      InEdgeIt e;
marci@485
   710
      for(g->first(e,v); g->valid(e); g->next(e)) {
marci@485
   711
	if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@485
   712
	Node u=g->tail(e);
marci@485
   713
	if ( level[u] >= n ) { 
marci@485
   714
	  bfs_queue.push(u);
marci@485
   715
	  level.set(u, l);
marci@485
   716
	  if ( excess[u] > 0 ) active[l].push(u);
marci@478
   717
	}
marci@478
   718
      }
marci@485
   719
	
marci@485
   720
      OutEdgeIt f;
marci@485
   721
      for(g->first(f,v); g->valid(f); g->next(f)) {
marci@485
   722
	if ( 0 >= (*flow)[f] ) continue;
marci@485
   723
	Node u=g->head(f);
marci@485
   724
	if ( level[u] >= n ) { 
marci@485
   725
	  bfs_queue.push(u);
marci@485
   726
	  level.set(u, l);
marci@485
   727
	  if ( excess[u] > 0 ) active[l].push(u);
marci@485
   728
	}
marci@485
   729
      }
marci@485
   730
    }
marci@485
   731
    b=n-2;
marci@478
   732
marci@485
   733
    while ( true ) {
marci@478
   734
	
marci@485
   735
      if ( b == 0 ) break;
marci@478
   736
marci@485
   737
      if ( active[b].empty() ) --b; 
marci@485
   738
      else {
marci@485
   739
	Node w=active[b].top();
marci@485
   740
	active[b].pop();
marci@485
   741
	int newlevel=push(w,active);	  
marci@478
   742
marci@485
   743
	//relabel
marci@485
   744
	if ( excess[w] > 0 ) {
marci@485
   745
	  level.set(w,++newlevel);
marci@485
   746
	  active[newlevel].push(w);
marci@485
   747
	  b=newlevel;
marci@485
   748
	}
marci@485
   749
      }  // if stack[b] is nonempty
marci@485
   750
    } // while(true)
marci@485
   751
  }
marci@478
   752
marci@478
   753
marci@478
   754
marci@478
   755
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   756
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath() 
marci@478
   757
  {
marci@485
   758
    ResGW res_graph(*g, *capacity, *flow);
marci@485
   759
    bool _augment=false;
marci@478
   760
      
marci@485
   761
    //ReachedMap level(res_graph);
marci@485
   762
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   763
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   764
    bfs.pushAndSetReached(s);
marci@478
   765
	
marci@485
   766
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph); 
marci@485
   767
    pred.set(s, INVALID);
marci@478
   768
      
marci@485
   769
    typename ResGW::template NodeMap<Num> free(res_graph);
marci@478
   770
	
marci@485
   771
    //searching for augmenting path
marci@485
   772
    while ( !bfs.finished() ) { 
marci@485
   773
      ResGWOutEdgeIt e=bfs;
marci@485
   774
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
   775
	Node v=res_graph.tail(e);
marci@485
   776
	Node w=res_graph.head(e);
marci@485
   777
	pred.set(w, e);
marci@485
   778
	if (res_graph.valid(pred[v])) {
marci@485
   779
	  free.set(w, std::min(free[v], res_graph.resCap(e)));
marci@485
   780
	} else {
marci@485
   781
	  free.set(w, res_graph.resCap(e)); 
marci@478
   782
	}
marci@485
   783
	if (res_graph.head(e)==t) { _augment=true; break; }
marci@485
   784
      }
marci@478
   785
	
marci@485
   786
      ++bfs;
marci@485
   787
    } //end of searching augmenting path
marci@478
   788
marci@485
   789
    if (_augment) {
marci@485
   790
      Node n=t;
marci@485
   791
      Num augment_value=free[t];
marci@485
   792
      while (res_graph.valid(pred[n])) { 
marci@485
   793
	ResGWEdge e=pred[n];
marci@485
   794
	res_graph.augment(e, augment_value); 
marci@485
   795
	n=res_graph.tail(e);
marci@478
   796
      }
marci@485
   797
    }
marci@478
   798
marci@485
   799
    return _augment;
marci@485
   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
marci@478
   810
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   811
  template<typename MutableGraph> 
marci@478
   812
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow() 
marci@478
   813
  {      
marci@485
   814
    typedef MutableGraph MG;
marci@485
   815
    bool _augment=false;
marci@478
   816
marci@485
   817
    ResGW res_graph(*g, *capacity, *flow);
marci@478
   818
marci@485
   819
    //bfs for distances on the residual graph
marci@485
   820
    //ReachedMap level(res_graph);
marci@485
   821
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   822
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   823
    bfs.pushAndSetReached(s);
marci@485
   824
    typename ResGW::template NodeMap<int> 
marci@485
   825
      dist(res_graph); //filled up with 0's
marci@478
   826
marci@485
   827
    //F will contain the physical copy of the residual graph
marci@485
   828
    //with the set of edges which are on shortest paths
marci@485
   829
    MG F;
marci@485
   830
    typename ResGW::template NodeMap<typename MG::Node> 
marci@485
   831
      res_graph_to_F(res_graph);
marci@485
   832
    {
marci@485
   833
      typename ResGW::NodeIt n;
marci@485
   834
      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
marci@485
   835
	res_graph_to_F.set(n, F.addNode());
marci@478
   836
      }
marci@485
   837
    }
marci@478
   838
marci@485
   839
    typename MG::Node sF=res_graph_to_F[s];
marci@485
   840
    typename MG::Node tF=res_graph_to_F[t];
marci@485
   841
    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
marci@485
   842
    typename MG::template EdgeMap<Num> residual_capacity(F);
marci@478
   843
marci@485
   844
    while ( !bfs.finished() ) { 
marci@485
   845
      ResGWOutEdgeIt e=bfs;
marci@485
   846
      if (res_graph.valid(e)) {
marci@485
   847
	if (bfs.isBNodeNewlyReached()) {
marci@485
   848
	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@485
   849
	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
marci@485
   850
	  original_edge.update();
marci@485
   851
	  original_edge.set(f, e);
marci@485
   852
	  residual_capacity.update();
marci@485
   853
	  residual_capacity.set(f, res_graph.resCap(e));
marci@485
   854
	} else {
marci@485
   855
	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
marci@478
   856
	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
marci@478
   857
	    original_edge.update();
marci@478
   858
	    original_edge.set(f, e);
marci@478
   859
	    residual_capacity.update();
marci@478
   860
	    residual_capacity.set(f, res_graph.resCap(e));
marci@478
   861
	  }
marci@478
   862
	}
marci@485
   863
      }
marci@485
   864
      ++bfs;
marci@485
   865
    } //computing distances from s in the residual graph
marci@478
   866
marci@485
   867
    bool __augment=true;
marci@478
   868
marci@485
   869
    while (__augment) {
marci@485
   870
      __augment=false;
marci@485
   871
      //computing blocking flow with dfs
marci@485
   872
      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
marci@485
   873
      typename MG::template NodeMap<typename MG::Edge> pred(F);
marci@485
   874
      pred.set(sF, INVALID);
marci@485
   875
      //invalid iterators for sources
marci@478
   876
marci@485
   877
      typename MG::template NodeMap<Num> free(F);
marci@478
   878
marci@485
   879
      dfs.pushAndSetReached(sF);      
marci@485
   880
      while (!dfs.finished()) {
marci@485
   881
	++dfs;
marci@485
   882
	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
marci@485
   883
	  if (dfs.isBNodeNewlyReached()) {
marci@485
   884
	    typename MG::Node v=F.aNode(dfs);
marci@485
   885
	    typename MG::Node w=F.bNode(dfs);
marci@485
   886
	    pred.set(w, dfs);
marci@485
   887
	    if (F.valid(pred[v])) {
marci@485
   888
	      free.set(w, std::min(free[v], residual_capacity[dfs]));
marci@485
   889
	    } else {
marci@485
   890
	      free.set(w, residual_capacity[dfs]); 
marci@485
   891
	    }
marci@485
   892
	    if (w==tF) { 
marci@485
   893
	      __augment=true; 
marci@485
   894
	      _augment=true;
marci@485
   895
	      break; 
marci@485
   896
	    }
marci@478
   897
	      
marci@485
   898
	  } else {
marci@485
   899
	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
marci@485
   900
	  }
marci@485
   901
	} 
marci@485
   902
      }
marci@485
   903
marci@485
   904
      if (__augment) {
marci@485
   905
	typename MG::Node n=tF;
marci@485
   906
	Num augment_value=free[tF];
marci@485
   907
	while (F.valid(pred[n])) { 
marci@485
   908
	  typename MG::Edge e=pred[n];
marci@485
   909
	  res_graph.augment(original_edge[e], augment_value); 
marci@485
   910
	  n=F.tail(e);
marci@485
   911
	  if (residual_capacity[e]==augment_value) 
marci@485
   912
	    F.erase(e); 
marci@485
   913
	  else 
marci@485
   914
	    residual_capacity.set(e, residual_capacity[e]-augment_value);
marci@478
   915
	}
marci@485
   916
      }
marci@478
   917
	
marci@485
   918
    }
marci@478
   919
            
marci@485
   920
    return _augment;
marci@485
   921
  }
marci@478
   922
marci@478
   923
marci@478
   924
marci@478
   925
marci@478
   926
marci@478
   927
marci@478
   928
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@478
   929
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2() 
marci@478
   930
  {
marci@485
   931
    bool _augment=false;
marci@478
   932
marci@485
   933
    ResGW res_graph(*g, *capacity, *flow);
marci@478
   934
      
marci@485
   935
    //ReachedMap level(res_graph);
marci@485
   936
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   937
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@478
   938
marci@485
   939
    bfs.pushAndSetReached(s);
marci@485
   940
    DistanceMap<ResGW> dist(res_graph);
marci@485
   941
    while ( !bfs.finished() ) { 
marci@485
   942
      ResGWOutEdgeIt e=bfs;
marci@485
   943
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
   944
	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@485
   945
      }
marci@485
   946
      ++bfs;
marci@485
   947
    } //computing distances from s in the residual graph
marci@478
   948
marci@478
   949
      //Subgraph containing the edges on some shortest paths
marci@485
   950
    ConstMap<typename ResGW::Node, bool> true_map(true);
marci@485
   951
    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>, 
marci@485
   952
      DistanceMap<ResGW> > FilterResGW;
marci@485
   953
    FilterResGW filter_res_graph(res_graph, true_map, dist);
marci@478
   954
marci@485
   955
    //Subgraph, which is able to delete edges which are already 
marci@485
   956
    //met by the dfs
marci@485
   957
    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt> 
marci@485
   958
      first_out_edges(filter_res_graph);
marci@485
   959
    typename FilterResGW::NodeIt v;
marci@485
   960
    for(filter_res_graph.first(v); filter_res_graph.valid(v); 
marci@485
   961
	filter_res_graph.next(v)) 
marci@478
   962
      {
marci@478
   963
 	typename FilterResGW::OutEdgeIt e;
marci@478
   964
 	filter_res_graph.first(e, v);
marci@478
   965
 	first_out_edges.set(v, e);
marci@478
   966
      }
marci@485
   967
    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
marci@485
   968
      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
marci@485
   969
    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
marci@478
   970
marci@485
   971
    bool __augment=true;
marci@478
   972
marci@485
   973
    while (__augment) {
marci@478
   974
marci@485
   975
      __augment=false;
marci@485
   976
      //computing blocking flow with dfs
marci@485
   977
      DfsIterator< ErasingResGW, 
marci@485
   978
	typename ErasingResGW::template NodeMap<bool> > 
marci@485
   979
	dfs(erasing_res_graph);
marci@485
   980
      typename ErasingResGW::
marci@485
   981
	template NodeMap<typename ErasingResGW::OutEdgeIt> 
marci@485
   982
	pred(erasing_res_graph); 
marci@485
   983
      pred.set(s, INVALID);
marci@485
   984
      //invalid iterators for sources
marci@478
   985
marci@485
   986
      typename ErasingResGW::template NodeMap<Num> 
marci@485
   987
	free1(erasing_res_graph);
marci@478
   988
marci@485
   989
      dfs.pushAndSetReached(
marci@485
   990
			    typename ErasingResGW::Node(
marci@485
   991
							typename FilterResGW::Node(
marci@485
   992
										   typename ResGW::Node(s)
marci@485
   993
										   )
marci@485
   994
							)
marci@485
   995
			    );
marci@485
   996
      while (!dfs.finished()) {
marci@485
   997
	++dfs;
marci@485
   998
	if (erasing_res_graph.valid(
marci@485
   999
				    typename ErasingResGW::OutEdgeIt(dfs))) 
marci@478
  1000
 	  { 
marci@478
  1001
  	    if (dfs.isBNodeNewlyReached()) {
marci@478
  1002
	  
marci@478
  1003
 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
marci@478
  1004
 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
marci@478
  1005
marci@478
  1006
 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
marci@478
  1007
 	      if (erasing_res_graph.valid(pred[v])) {
marci@478
  1008
 		free1.set(w, std::min(free1[v], res_graph.resCap(
marci@485
  1009
								 typename ErasingResGW::OutEdgeIt(dfs))));
marci@478
  1010
 	      } else {
marci@478
  1011
 		free1.set(w, res_graph.resCap(
marci@485
  1012
					      typename ErasingResGW::OutEdgeIt(dfs))); 
marci@478
  1013
 	      }
marci@478
  1014
	      
marci@478
  1015
 	      if (w==t) { 
marci@478
  1016
 		__augment=true; 
marci@478
  1017
 		_augment=true;
marci@478
  1018
 		break; 
marci@478
  1019
 	      }
marci@478
  1020
 	    } else {
marci@478
  1021
 	      erasing_res_graph.erase(dfs);
marci@478
  1022
	    }
marci@478
  1023
	  }
marci@485
  1024
      }	
marci@478
  1025
marci@485
  1026
      if (__augment) {
marci@485
  1027
	typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
marci@485
  1028
	// 	  typename ResGW::NodeMap<Num> a(res_graph);
marci@485
  1029
	// 	  typename ResGW::Node b;
marci@485
  1030
	// 	  Num j=a[b];
marci@485
  1031
	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
marci@485
  1032
	// 	  typename FilterResGW::Node b1;
marci@485
  1033
	// 	  Num j1=a1[b1];
marci@485
  1034
	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
marci@485
  1035
	// 	  typename ErasingResGW::Node b2;
marci@485
  1036
	// 	  Num j2=a2[b2];
marci@485
  1037
	Num augment_value=free1[n];
marci@485
  1038
	while (erasing_res_graph.valid(pred[n])) { 
marci@485
  1039
	  typename ErasingResGW::OutEdgeIt e=pred[n];
marci@485
  1040
	  res_graph.augment(e, augment_value);
marci@485
  1041
	  n=erasing_res_graph.tail(e);
marci@485
  1042
	  if (res_graph.resCap(e)==0)
marci@485
  1043
	    erasing_res_graph.erase(e);
marci@478
  1044
	}
marci@478
  1045
      }
marci@478
  1046
      
marci@485
  1047
    } //while (__augment) 
marci@478
  1048
            
marci@485
  1049
    return _augment;
marci@485
  1050
  }
marci@478
  1051
marci@478
  1052
marci@478
  1053
marci@478
  1054
marci@478
  1055
} //namespace hugo
marci@478
  1056
marci@480
  1057
#endif //HUGO_MAX_FLOW_H
marci@478
  1058
marci@478
  1059
marci@478
  1060
marci@478
  1061