src/work/jacint/preflow_hl4.h
author jacint
Sun, 22 Feb 2004 12:17:16 +0000
changeset 113 cf7b01232d86
parent 105 a3c73e9b9b2e
permissions -rw-r--r--
*** empty log message ***
jacint@102
     1
// -*- C++ -*-
jacint@102
     2
/*
jacint@109
     3
preflow_h5.h
jacint@102
     4
by jacint. 
jacint@109
     5
Heuristics: 
jacint@109
     6
 2 phase
jacint@109
     7
 gap
jacint@109
     8
 list 'level_list' on the nodes on level i implemented by hand
jacint@109
     9
 highest label
jacint@109
    10
 relevel: in phase 0, after BFS*n relabels, it runs a reverse 
jacint@109
    11
   bfs from t in the res graph to relevel the nodes reachable from t.
jacint@109
    12
   BFS is initialized to 20
jacint@102
    13
jacint@109
    14
Due to the last heuristic, this algorithm is quite fast on very 
jacint@109
    15
sparse graphs, but relatively bad on even the dense graphs.
jacint@102
    16
jacint@109
    17
'NodeMap<bool> cut' is a member, in this way we can count it fast, after
jacint@109
    18
the algorithm was run.
jacint@102
    19
jacint@109
    20
The constructor runs the algorithm.
jacint@102
    21
jacint@109
    22
Members:
jacint@102
    23
jacint@109
    24
T maxFlow() : returns the value of a maximum flow
jacint@102
    25
jacint@109
    26
T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
jacint@102
    27
jacint@109
    28
FlowMap Flow() : returns the fixed maximum flow x
jacint@102
    29
jacint@109
    30
void Flow(FlowMap& _flow ) : returns the fixed maximum flow x
jacint@102
    31
jacint@109
    32
void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@102
    33
     minimum min cut. M should be a map of bools initialized to false.
jacint@102
    34
jacint@109
    35
void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@102
    36
     maximum min cut. M should be a map of bools initialized to false.
jacint@102
    37
jacint@109
    38
void minCut(CutMap& M) : fast function, sets M to the characteristic 
jacint@109
    39
     vector of a minimum cut. 
jacint@109
    40
jacint@109
    41
Different member from the other preflow_hl-s (here we have a member 
jacint@109
    42
'NodeMap<bool> cut').
jacint@109
    43
jacint@109
    44
CutMap minCut() : fast function, giving the characteristic 
jacint@109
    45
     vector of a minimum cut.
jacint@109
    46
jacint@109
    47
jacint@102
    48
*/
jacint@102
    49
jacint@102
    50
#ifndef PREFLOW_HL4_H
jacint@102
    51
#define PREFLOW_HL4_H
jacint@102
    52
jacint@109
    53
#define BFS 20
jacint@109
    54
jacint@102
    55
#include <vector>
jacint@102
    56
#include <queue>
jacint@102
    57
jacint@109
    58
#include <time_measure.h>  //for test
jacint@109
    59
alpar@105
    60
namespace hugo {
jacint@102
    61
jacint@102
    62
  template <typename Graph, typename T, 
jacint@102
    63
    typename FlowMap=typename Graph::EdgeMap<T>, 
jacint@109
    64
    typename CutMap=typename Graph::NodeMap<bool>,
jacint@102
    65
    typename CapMap=typename Graph::EdgeMap<T> >
jacint@102
    66
  class preflow_hl4 {
jacint@102
    67
    
jacint@102
    68
    typedef typename Graph::NodeIt NodeIt;
jacint@102
    69
    typedef typename Graph::EdgeIt EdgeIt;
jacint@102
    70
    typedef typename Graph::EachNodeIt EachNodeIt;
jacint@102
    71
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@102
    72
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@102
    73
    
jacint@102
    74
    Graph& G;
jacint@102
    75
    NodeIt s;
jacint@102
    76
    NodeIt t;
jacint@102
    77
    FlowMap flow;
jacint@102
    78
    CapMap& capacity;  
jacint@109
    79
    CutMap cut;
jacint@102
    80
    T value;
jacint@102
    81
    
jacint@102
    82
  public:
jacint@102
    83
jacint@109
    84
    double time;
jacint@109
    85
jacint@102
    86
    preflow_hl4(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) :
jacint@109
    87
      G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), 
jacint@109
    88
      cut(G, false) {
jacint@102
    89
jacint@109
    90
      bool phase=0;   //phase 0 is the 1st phase, phase 1 is the 2nd
jacint@102
    91
      int n=G.nodeNum(); 
jacint@109
    92
      int relabel=0;
jacint@109
    93
      int heur=(int)BFS*n;
jacint@102
    94
      int k=n-2;
jacint@102
    95
      int b=k;
jacint@102
    96
      /*
jacint@102
    97
	b is a bound on the highest level of the stack. 
jacint@102
    98
	k is a bound on the highest nonempty level i < n.
jacint@102
    99
      */
jacint@102
   100
jacint@102
   101
      typename Graph::NodeMap<int> level(G,n);      
jacint@102
   102
      typename Graph::NodeMap<T> excess(G); 
jacint@109
   103
      
jacint@109
   104
      std::vector<NodeIt> active(n);
jacint@109
   105
      typename Graph::NodeMap<NodeIt> next(G);
jacint@102
   106
      //Stack of the active nodes in level i < n.
jacint@102
   107
      //We use it in both phases.
jacint@102
   108
jacint@102
   109
      typename Graph::NodeMap<NodeIt> left(G);
jacint@102
   110
      typename Graph::NodeMap<NodeIt> right(G);
jacint@102
   111
      std::vector<NodeIt> level_list(n);
jacint@102
   112
      /*
jacint@102
   113
	Needed for the list of the nodes in level i.
jacint@102
   114
      */
jacint@102
   115
jacint@102
   116
      /*Reverse_bfs from t, to find the starting level.*/
jacint@102
   117
      level.set(t,0);
jacint@102
   118
      std::queue<NodeIt> bfs_queue;
jacint@102
   119
      bfs_queue.push(t);
jacint@102
   120
jacint@102
   121
      while (!bfs_queue.empty()) {
jacint@102
   122
jacint@102
   123
	NodeIt v=bfs_queue.front();	
jacint@102
   124
	bfs_queue.pop();
jacint@102
   125
	int l=level.get(v)+1;
jacint@102
   126
jacint@102
   127
	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@102
   128
	  NodeIt w=G.tail(e);
jacint@109
   129
	  if ( level.get(w) == n && w !=s ) {
jacint@102
   130
	    bfs_queue.push(w);
jacint@102
   131
	    NodeIt first=level_list[l];
jacint@102
   132
	    if ( first != 0 ) left.set(first,w);
jacint@102
   133
	    right.set(w,first);
jacint@102
   134
	    level_list[l]=w;
jacint@102
   135
	    level.set(w, l);
jacint@102
   136
	  }
jacint@102
   137
	}
jacint@102
   138
      }
jacint@102
   139
      
jacint@102
   140
      level.set(s,n);
jacint@102
   141
jacint@102
   142
jacint@102
   143
      /* Starting flow. It is everywhere 0 at the moment. */     
jacint@102
   144
      for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
jacint@102
   145
	{
jacint@102
   146
	  T c=capacity.get(e);
jacint@102
   147
	  if ( c == 0 ) continue;
jacint@102
   148
	  NodeIt w=G.head(e);
jacint@102
   149
	  if ( level.get(w) < n ) {	  
jacint@109
   150
	    if ( excess.get(w) == 0 && w!=t ) {
jacint@109
   151
	      next.set(w,active[level.get(w)]);
jacint@109
   152
	      active[level.get(w)]=w;
jacint@109
   153
	    }
jacint@102
   154
	    flow.set(e, c); 
jacint@102
   155
	    excess.set(w, excess.get(w)+c);
jacint@102
   156
	  }
jacint@102
   157
	}
jacint@102
   158
      /* 
jacint@102
   159
	 End of preprocessing 
jacint@102
   160
      */
jacint@102
   161
jacint@102
   162
jacint@102
   163
      /*
jacint@102
   164
	Push/relabel on the highest level active nodes.
jacint@102
   165
      */	
jacint@102
   166
      while ( true ) {
jacint@102
   167
jacint@102
   168
	if ( b == 0 ) {
jacint@102
   169
	  if ( phase ) break;
jacint@102
   170
	  
jacint@102
   171
	  /*
jacint@102
   172
	    In the end of phase 0 we apply a bfs from s in
jacint@102
   173
	    the residual graph.
jacint@102
   174
	  */
jacint@102
   175
	  phase=1;
jacint@109
   176
	  
jacint@109
   177
	  //Now have a min cut.
jacint@109
   178
	  for( EachNodeIt v=G.template first<EachNodeIt>(); 
jacint@109
   179
	       v.valid(); ++v) 
jacint@109
   180
	    if (level.get(v) >= n ) cut.set(v,true);
jacint@109
   181
	  
jacint@109
   182
	  time=currTime();
jacint@102
   183
	  level.set(s,0);
jacint@102
   184
	  std::queue<NodeIt> bfs_queue;
jacint@102
   185
	  bfs_queue.push(s);
jacint@102
   186
	  
jacint@102
   187
	  while (!bfs_queue.empty()) {
jacint@102
   188
	    
jacint@102
   189
	    NodeIt v=bfs_queue.front();	
jacint@102
   190
	    bfs_queue.pop();
jacint@102
   191
	    int l=level.get(v)+1;
jacint@102
   192
jacint@102
   193
	    for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@102
   194
	      if ( capacity.get(e) == flow.get(e) ) continue;
jacint@102
   195
	      NodeIt u=G.tail(e);
jacint@102
   196
	      if ( level.get(u) >= n ) { 
jacint@102
   197
		bfs_queue.push(u);
jacint@102
   198
		level.set(u, l);
jacint@109
   199
		if ( excess.get(u) > 0 ) {
jacint@109
   200
		    next.set(u,active[l]);
jacint@109
   201
		    active[l]=u;
jacint@109
   202
		}
jacint@102
   203
	      }
jacint@102
   204
	    }
jacint@102
   205
jacint@102
   206
	    for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
jacint@102
   207
	      if ( 0 == flow.get(e) ) continue;
jacint@102
   208
	      NodeIt u=G.head(e);
jacint@102
   209
	      if ( level.get(u) >= n ) { 
jacint@102
   210
		bfs_queue.push(u);
jacint@102
   211
		level.set(u, l);
jacint@109
   212
		if ( excess.get(u) > 0 ) {
jacint@109
   213
		    next.set(u,active[l]);
jacint@109
   214
		    active[l]=u;
jacint@109
   215
		}
jacint@102
   216
	      }
jacint@102
   217
	    }
jacint@102
   218
	  }
jacint@102
   219
	  b=n-2;
jacint@102
   220
	}
jacint@102
   221
jacint@102
   222
jacint@109
   223
	if ( active[b] == 0 ) --b;
jacint@102
   224
	else {
jacint@102
   225
	  
jacint@109
   226
	  NodeIt w=active[b];
jacint@109
   227
	  active[b]=next.get(w);
jacint@102
   228
	  int lev=level.get(w);
jacint@102
   229
	  T exc=excess.get(w);
jacint@109
   230
	  int newlevel=n;          //bound on the next level of w.
jacint@102
   231
	  
jacint@102
   232
	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
jacint@102
   233
	    
jacint@102
   234
	    if ( flow.get(e) == capacity.get(e) ) continue; 
jacint@102
   235
	    NodeIt v=G.head(e);            
jacint@102
   236
	    //e=wv	    
jacint@102
   237
	    
jacint@102
   238
	    if( lev > level.get(v) ) {      
jacint@102
   239
	      /*Push is allowed now*/
jacint@102
   240
	      
jacint@109
   241
	      if ( excess.get(v)==0 && v!=t && v!=s )  {
jacint@109
   242
		int lev_v=level.get(v);
jacint@109
   243
		next.set(v,active[lev_v]);
jacint@109
   244
		active[lev_v]=v;
jacint@109
   245
	      }
jacint@102
   246
	      
jacint@102
   247
	      T cap=capacity.get(e);
jacint@102
   248
	      T flo=flow.get(e);
jacint@102
   249
	      T remcap=cap-flo;
jacint@102
   250
	      
jacint@102
   251
	      if ( remcap >= exc ) {       
jacint@102
   252
		/*A nonsaturating push.*/
jacint@102
   253
		
jacint@102
   254
		flow.set(e, flo+exc);
jacint@102
   255
		excess.set(v, excess.get(v)+exc);
jacint@102
   256
		exc=0;
jacint@102
   257
		break; 
jacint@102
   258
		
jacint@102
   259
	      } else { 
jacint@102
   260
		/*A saturating push.*/
jacint@102
   261
		
jacint@102
   262
		flow.set(e, cap);
jacint@102
   263
		excess.set(v, excess.get(v)+remcap);
jacint@102
   264
		exc-=remcap;
jacint@102
   265
	      }
jacint@102
   266
	    } else if ( newlevel > level.get(v) ){
jacint@102
   267
	      newlevel = level.get(v);
jacint@102
   268
	    }	    
jacint@102
   269
	    
jacint@102
   270
	  } //for out edges wv 
jacint@102
   271
	
jacint@102
   272
	
jacint@102
   273
	if ( exc > 0 ) {	
jacint@102
   274
	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
jacint@102
   275
	    
jacint@102
   276
	    if( flow.get(e) == 0 ) continue; 
jacint@102
   277
	    NodeIt v=G.tail(e);  
jacint@102
   278
	    //e=vw
jacint@102
   279
	    
jacint@102
   280
	    if( lev > level.get(v) ) {  
jacint@102
   281
	      /*Push is allowed now*/
jacint@102
   282
	      
jacint@109
   283
	      if ( excess.get(v)==0 && v!=t && v!=s ) {
jacint@109
   284
		int lev_v=level.get(v);
jacint@109
   285
		next.set(v,active[lev_v]);
jacint@109
   286
		active[lev_v]=v;
jacint@109
   287
	      }
jacint@102
   288
	      
jacint@102
   289
	      T flo=flow.get(e);
jacint@102
   290
	      
jacint@102
   291
	      if ( flo >= exc ) { 
jacint@102
   292
		/*A nonsaturating push.*/
jacint@102
   293
		
jacint@102
   294
		flow.set(e, flo-exc);
jacint@102
   295
		excess.set(v, excess.get(v)+exc);
jacint@102
   296
		exc=0;
jacint@102
   297
		break; 
jacint@102
   298
	      } else {                                               
jacint@102
   299
		/*A saturating push.*/
jacint@102
   300
		
jacint@102
   301
		excess.set(v, excess.get(v)+flo);
jacint@102
   302
		exc-=flo;
jacint@102
   303
		flow.set(e,0);
jacint@102
   304
	      }  
jacint@102
   305
	    } else if ( newlevel > level.get(v) ) {
jacint@102
   306
	      newlevel = level.get(v);
jacint@102
   307
	    }	    
jacint@102
   308
	  } //for in edges vw
jacint@102
   309
	  
jacint@102
   310
	} // if w still has excess after the out edge for cycle
jacint@102
   311
	
jacint@102
   312
	excess.set(w, exc);
jacint@102
   313
	 
jacint@102
   314
	/*
jacint@102
   315
	  Relabel
jacint@102
   316
	*/
jacint@102
   317
	
jacint@102
   318
	if ( exc > 0 ) {
jacint@102
   319
	  //now 'lev' is the old level of w
jacint@102
   320
	
jacint@102
   321
	  if ( phase ) {
jacint@102
   322
	    level.set(w,++newlevel);
jacint@109
   323
	    next.set(w,active[newlevel]);
jacint@109
   324
	    active[newlevel]=w;
jacint@102
   325
	    b=newlevel;
jacint@102
   326
	  } else {
jacint@102
   327
	    //unlacing
jacint@102
   328
	    NodeIt right_n=right.get(w);
jacint@102
   329
	    NodeIt left_n=left.get(w);
jacint@102
   330
jacint@102
   331
	    if ( right_n != 0 ) {
jacint@102
   332
	      if ( left_n != 0 ) {
jacint@102
   333
		right.set(left_n, right_n);
jacint@102
   334
		left.set(right_n, left_n);
jacint@102
   335
	      } else {
jacint@102
   336
		level_list[lev]=right_n;
jacint@102
   337
		left.set(right_n, 0);
jacint@102
   338
	      } 
jacint@102
   339
	    } else {
jacint@102
   340
	      if ( left_n != 0 ) {
jacint@102
   341
		right.set(left_n, 0);
jacint@102
   342
	      } else { 
jacint@102
   343
		level_list[lev]=0;
jacint@102
   344
	      } 
jacint@102
   345
	    }
jacint@109
   346
	    //unlacing ends
jacint@102
   347
		
jacint@102
   348
jacint@102
   349
	    if ( level_list[lev]==0 ) {
jacint@102
   350
jacint@102
   351
	      for (int i=lev; i!=k ; ) {
jacint@102
   352
		NodeIt v=level_list[++i];
jacint@102
   353
		while ( v != 0 ) {
jacint@102
   354
		  level.set(v,n);
jacint@102
   355
		  v=right.get(v);
jacint@102
   356
		}
jacint@102
   357
		level_list[i]=0;
jacint@102
   358
	      }	     
jacint@102
   359
jacint@102
   360
	      level.set(w,n);
jacint@102
   361
jacint@102
   362
	      b=--lev;
jacint@102
   363
	      k=b;
jacint@102
   364
jacint@102
   365
	    } else {
jacint@102
   366
jacint@102
   367
	      if ( newlevel == n ) {
jacint@102
   368
		level.set(w,n);
jacint@102
   369
	      } else {
jacint@102
   370
		
jacint@102
   371
		level.set(w,++newlevel);
jacint@109
   372
		next.set(w,active[newlevel]);
jacint@109
   373
		active[newlevel]=w;
jacint@102
   374
		b=newlevel;
jacint@102
   375
		if ( k < newlevel ) ++k;
jacint@102
   376
		NodeIt first=level_list[newlevel];
jacint@102
   377
		if ( first != 0 ) left.set(first,w);
jacint@102
   378
		right.set(w,first);
jacint@102
   379
		left.set(w,0);
jacint@102
   380
		level_list[newlevel]=w;
jacint@102
   381
	      }
jacint@102
   382
	    }
jacint@109
   383
jacint@109
   384
	    ++relabel;
jacint@109
   385
	    if ( relabel >= heur ) {
jacint@109
   386
	      relabel=0;
jacint@109
   387
	      b=n-2;
jacint@109
   388
	      k=b;
jacint@109
   389
		
jacint@109
   390
	      for ( int i=1; i!=n; ++i ) { 
jacint@109
   391
		active[i]=0;
jacint@109
   392
		level_list[i]=0;
jacint@109
   393
	      }
jacint@109
   394
jacint@109
   395
	      //bfs from t in the res graph to relevel the nodes
jacint@109
   396
	      for( EachNodeIt v=G.template first<EachNodeIt>(); 
jacint@109
   397
		   v.valid(); ++v) level.set(v,n);
jacint@109
   398
jacint@109
   399
	      level.set(t,0);
jacint@109
   400
	      std::queue<NodeIt> bfs_queue;
jacint@109
   401
	      bfs_queue.push(t);
jacint@109
   402
	      
jacint@109
   403
	      while (!bfs_queue.empty()) {
jacint@109
   404
		
jacint@109
   405
		NodeIt v=bfs_queue.front();	
jacint@109
   406
		bfs_queue.pop();
jacint@109
   407
		int l=level.get(v)+1;
jacint@109
   408
		
jacint@109
   409
		for(InEdgeIt e=G.template first<InEdgeIt>(v); 
jacint@109
   410
		    e.valid(); ++e) {
jacint@109
   411
		  if ( capacity.get(e) == flow.get(e) ) continue;
jacint@109
   412
		  NodeIt u=G.tail(e);
jacint@109
   413
		  if ( level.get(u) == n ) { 
jacint@109
   414
		    bfs_queue.push(u);
jacint@109
   415
		    level.set(u, l);
jacint@109
   416
		    if ( excess.get(u) > 0 ) {
jacint@109
   417
		      next.set(u,active[l]);
jacint@109
   418
		      active[l]=u;
jacint@109
   419
		    }
jacint@109
   420
		    NodeIt first=level_list[l];
jacint@109
   421
		    if ( first != 0 ) left.set(first,w);
jacint@109
   422
		    right.set(w,first);
jacint@109
   423
		    left.set(w,0);
jacint@109
   424
		    level_list[l]=w;
jacint@109
   425
		  }
jacint@109
   426
		}
jacint@109
   427
		
jacint@109
   428
		
jacint@109
   429
		for(OutEdgeIt e=G.template first<OutEdgeIt>(v); 
jacint@109
   430
		    e.valid(); ++e) {
jacint@109
   431
		  if ( 0 == flow.get(e) ) continue;
jacint@109
   432
		  NodeIt u=G.head(e);
jacint@109
   433
		  if ( level.get(u) == n ) { 
jacint@109
   434
		    bfs_queue.push(u);
jacint@109
   435
		    level.set(u, l);
jacint@109
   436
		    if ( excess.get(u) > 0 ) {
jacint@109
   437
		      next.set(u,active[l]);
jacint@109
   438
		      active[l]=u;
jacint@109
   439
		    }
jacint@109
   440
		    NodeIt first=level_list[l];
jacint@109
   441
		    if ( first != 0 ) left.set(first,w);
jacint@109
   442
		    right.set(w,first);
jacint@109
   443
		    left.set(w,0);
jacint@109
   444
		    level_list[l]=w;
jacint@109
   445
		  }
jacint@109
   446
		}
jacint@109
   447
	      }
jacint@109
   448
	      
jacint@109
   449
	      level.set(s,n);
jacint@109
   450
	    }
jacint@109
   451
	  
jacint@102
   452
	  } //phase 0
jacint@102
   453
	} // if ( exc > 0 )
jacint@109
   454
	
jacint@102
   455
	
jacint@102
   456
	} // if stack[b] is nonempty
jacint@102
   457
	
jacint@102
   458
      } // while(true)
jacint@102
   459
jacint@102
   460
jacint@102
   461
      value = excess.get(t);
jacint@102
   462
      /*Max flow value.*/
jacint@102
   463
jacint@102
   464
jacint@102
   465
    } //void run()
jacint@102
   466
jacint@102
   467
jacint@102
   468
jacint@102
   469
jacint@102
   470
jacint@102
   471
    /*
jacint@102
   472
      Returns the maximum value of a flow.
jacint@102
   473
     */
jacint@102
   474
jacint@109
   475
    T maxFlow() {
jacint@102
   476
      return value;
jacint@102
   477
    }
jacint@102
   478
jacint@102
   479
jacint@102
   480
jacint@102
   481
    /*
jacint@102
   482
      For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). 
jacint@102
   483
    */
jacint@102
   484
jacint@109
   485
    T flowOnEdge(EdgeIt e) {
jacint@102
   486
      return flow.get(e);
jacint@102
   487
    }
jacint@102
   488
jacint@102
   489
jacint@102
   490
jacint@109
   491
    FlowMap Flow() {
jacint@102
   492
      return flow;
jacint@102
   493
    }
jacint@102
   494
jacint@102
   495
jacint@102
   496
    
jacint@109
   497
    void Flow(FlowMap& _flow ) {
jacint@102
   498
      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
jacint@102
   499
	_flow.set(v,flow.get(v));
jacint@102
   500
    }
jacint@102
   501
jacint@102
   502
jacint@102
   503
jacint@102
   504
    /*
jacint@102
   505
      Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@102
   506
    */
jacint@102
   507
    
jacint@109
   508
    template<typename _CutMap>
jacint@109
   509
    void minMinCut(_CutMap& M) {
jacint@102
   510
    
jacint@102
   511
      std::queue<NodeIt> queue;
jacint@102
   512
      
jacint@102
   513
      M.set(s,true);      
jacint@102
   514
      queue.push(s);
jacint@102
   515
jacint@102
   516
      while (!queue.empty()) {
jacint@102
   517
        NodeIt w=queue.front();
jacint@102
   518
	queue.pop();
jacint@102
   519
jacint@102
   520
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   521
	  NodeIt v=G.head(e);
jacint@102
   522
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@102
   523
	    queue.push(v);
jacint@102
   524
	    M.set(v, true);
jacint@102
   525
	  }
jacint@102
   526
	} 
jacint@102
   527
jacint@102
   528
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   529
	  NodeIt v=G.tail(e);
jacint@102
   530
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@102
   531
	    queue.push(v);
jacint@102
   532
	    M.set(v, true);
jacint@102
   533
	  }
jacint@102
   534
	} 
jacint@102
   535
jacint@102
   536
      }
jacint@102
   537
jacint@102
   538
    }
jacint@102
   539
jacint@102
   540
jacint@102
   541
jacint@102
   542
    /*
jacint@102
   543
      Returns the maximum min cut, by a reverse bfs 
jacint@102
   544
      from t in the residual graph.
jacint@102
   545
    */
jacint@102
   546
    
jacint@109
   547
    template<typename _CutMap>
jacint@109
   548
    void maxMinCut(_CutMap& M) {
jacint@102
   549
    
jacint@102
   550
      std::queue<NodeIt> queue;
jacint@102
   551
      
jacint@102
   552
      M.set(t,true);        
jacint@102
   553
      queue.push(t);
jacint@102
   554
jacint@102
   555
      while (!queue.empty()) {
jacint@102
   556
        NodeIt w=queue.front();
jacint@102
   557
	queue.pop();
jacint@102
   558
jacint@102
   559
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   560
	  NodeIt v=G.tail(e);
jacint@102
   561
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@102
   562
	    queue.push(v);
jacint@102
   563
	    M.set(v, true);
jacint@102
   564
	  }
jacint@102
   565
	}
jacint@102
   566
jacint@102
   567
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   568
	  NodeIt v=G.head(e);
jacint@102
   569
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@102
   570
	    queue.push(v);
jacint@102
   571
	    M.set(v, true);
jacint@102
   572
	  }
jacint@102
   573
	}
jacint@102
   574
      }
jacint@102
   575
jacint@102
   576
      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
jacint@102
   577
	M.set(v, !M.get(v));
jacint@102
   578
      }
jacint@102
   579
jacint@102
   580
    }
jacint@102
   581
jacint@102
   582
jacint@109
   583
    template<typename _CutMap>
jacint@109
   584
    void minCut(_CutMap& M) {
jacint@109
   585
      for( EachNodeIt v=G.template first<EachNodeIt>(); 
jacint@109
   586
	   v.valid(); ++v) 
jacint@109
   587
	M.set(v, cut.get(v));
jacint@102
   588
    }
jacint@102
   589
jacint@109
   590
   
jacint@109
   591
    CutMap minCut() {
jacint@109
   592
      return cut;
jacint@109
   593
    }
jacint@102
   594
jacint@102
   595
jacint@102
   596
  };
jacint@109
   597
}//namespace marci
jacint@102
   598
#endif 
jacint@102
   599
jacint@102
   600
jacint@102
   601
jacint@102
   602