src/work/jacint/preflow_hl4.h
author alpar
Fri, 20 Feb 2004 21:57:39 +0000
changeset 106 0508d63fcc96
parent 102 294cb99af985
child 109 fc5982b39e10
permissions -rw-r--r--
.
jacint@102
     1
// -*- C++ -*-
jacint@102
     2
/*
jacint@102
     3
preflow_hl4.h
jacint@102
     4
by jacint. 
jacint@102
     5
Runs the two phase highest label preflow push algorithm. In phase 0
jacint@102
     6
we maintain in a list the nodes in level i < n, and we maintain a 
jacint@102
     7
bound k on the max level i < n containing a node, so we can do
jacint@102
     8
the gap heuristic fast. Phase 1 is the same. (The algorithm is the 
jacint@102
     9
same as preflow.hl3, the only diff is that here we use the gap
jacint@102
    10
heuristic with the list of the nodes on level i, and not a bfs form the
jacint@102
    11
upgraded node.)
jacint@102
    12
jacint@102
    13
In phase 1 we shift everything downwards by n.
jacint@102
    14
jacint@102
    15
Member functions:
jacint@102
    16
jacint@102
    17
void run() : runs the algorithm
jacint@102
    18
jacint@102
    19
 The following functions should be used after run() was already run.
jacint@102
    20
jacint@102
    21
T maxflow() : returns the value of a maximum flow
jacint@102
    22
jacint@102
    23
T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
jacint@102
    24
jacint@102
    25
FlowMap allflow() : returns a maximum flow
jacint@102
    26
jacint@102
    27
void allflow(FlowMap& _flow ) : returns a maximum flow
jacint@102
    28
jacint@102
    29
void mincut(CutMap& M) : sets M to the characteristic vector of a 
jacint@102
    30
     minimum cut. M should be a map of bools initialized to false.
jacint@102
    31
jacint@102
    32
void min_mincut(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@102
    35
void max_mincut(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@102
    38
*/
jacint@102
    39
jacint@102
    40
#ifndef PREFLOW_HL4_H
jacint@102
    41
#define PREFLOW_HL4_H
jacint@102
    42
jacint@102
    43
#include <vector>
jacint@102
    44
#include <stack>
jacint@102
    45
#include <queue>
jacint@102
    46
alpar@105
    47
namespace hugo {
jacint@102
    48
jacint@102
    49
  template <typename Graph, typename T, 
jacint@102
    50
    typename FlowMap=typename Graph::EdgeMap<T>, 
jacint@102
    51
    typename CapMap=typename Graph::EdgeMap<T> >
jacint@102
    52
  class preflow_hl4 {
jacint@102
    53
    
jacint@102
    54
    typedef typename Graph::NodeIt NodeIt;
jacint@102
    55
    typedef typename Graph::EdgeIt EdgeIt;
jacint@102
    56
    typedef typename Graph::EachNodeIt EachNodeIt;
jacint@102
    57
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@102
    58
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@102
    59
    
jacint@102
    60
    Graph& G;
jacint@102
    61
    NodeIt s;
jacint@102
    62
    NodeIt t;
jacint@102
    63
    FlowMap flow;
jacint@102
    64
    CapMap& capacity;  
jacint@102
    65
    T value;
jacint@102
    66
    
jacint@102
    67
  public:
jacint@102
    68
jacint@102
    69
    preflow_hl4(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) :
jacint@102
    70
      G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { }
jacint@102
    71
jacint@102
    72
jacint@102
    73
    void run() {
jacint@102
    74
 
jacint@102
    75
      bool phase=0;
jacint@102
    76
      int n=G.nodeNum(); 
jacint@102
    77
      int k=n-2;
jacint@102
    78
      int b=k;
jacint@102
    79
      /*
jacint@102
    80
	b is a bound on the highest level of the stack. 
jacint@102
    81
	k is a bound on the highest nonempty level i < n.
jacint@102
    82
      */
jacint@102
    83
jacint@102
    84
      typename Graph::NodeMap<int> level(G,n);      
jacint@102
    85
      typename Graph::NodeMap<T> excess(G); 
jacint@102
    86
      std::vector<std::stack<NodeIt> > stack(n);    
jacint@102
    87
      //Stack of the active nodes in level i < n.
jacint@102
    88
      //We use it in both phases.
jacint@102
    89
jacint@102
    90
      typename Graph::NodeMap<NodeIt> left(G);
jacint@102
    91
      typename Graph::NodeMap<NodeIt> right(G);
jacint@102
    92
      std::vector<NodeIt> level_list(n);
jacint@102
    93
      /*
jacint@102
    94
	Needed for the list of the nodes in level i.
jacint@102
    95
      */
jacint@102
    96
jacint@102
    97
      /*Reverse_bfs from t, to find the starting level.*/
jacint@102
    98
      level.set(t,0);
jacint@102
    99
      std::queue<NodeIt> bfs_queue;
jacint@102
   100
      bfs_queue.push(t);
jacint@102
   101
jacint@102
   102
      while (!bfs_queue.empty()) {
jacint@102
   103
jacint@102
   104
	NodeIt v=bfs_queue.front();	
jacint@102
   105
	bfs_queue.pop();
jacint@102
   106
	int l=level.get(v)+1;
jacint@102
   107
jacint@102
   108
	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@102
   109
	  NodeIt w=G.tail(e);
jacint@102
   110
	  if ( level.get(w) == n ) {
jacint@102
   111
	    bfs_queue.push(w);
jacint@102
   112
	    NodeIt first=level_list[l];
jacint@102
   113
	    if ( first != 0 ) left.set(first,w);
jacint@102
   114
	    right.set(w,first);
jacint@102
   115
	    level_list[l]=w;
jacint@102
   116
	    level.set(w, l);
jacint@102
   117
	  }
jacint@102
   118
	}
jacint@102
   119
      }
jacint@102
   120
      
jacint@102
   121
      level.set(s,n);
jacint@102
   122
jacint@102
   123
jacint@102
   124
      /* Starting flow. It is everywhere 0 at the moment. */     
jacint@102
   125
      for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
jacint@102
   126
	{
jacint@102
   127
	  T c=capacity.get(e);
jacint@102
   128
	  if ( c == 0 ) continue;
jacint@102
   129
	  NodeIt w=G.head(e);
jacint@102
   130
	  if ( level.get(w) < n ) {	  
jacint@102
   131
	    if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w); 
jacint@102
   132
	    flow.set(e, c); 
jacint@102
   133
	    excess.set(w, excess.get(w)+c);
jacint@102
   134
	  }
jacint@102
   135
	}
jacint@102
   136
      /* 
jacint@102
   137
	 End of preprocessing 
jacint@102
   138
      */
jacint@102
   139
jacint@102
   140
jacint@102
   141
      /*
jacint@102
   142
	Push/relabel on the highest level active nodes.
jacint@102
   143
      */	
jacint@102
   144
      while ( true ) {
jacint@102
   145
jacint@102
   146
	if ( b == 0 ) {
jacint@102
   147
	  if ( phase ) break;
jacint@102
   148
	  
jacint@102
   149
	  /*
jacint@102
   150
	    In the end of phase 0 we apply a bfs from s in
jacint@102
   151
	    the residual graph.
jacint@102
   152
	  */
jacint@102
   153
	  phase=1;
jacint@102
   154
	  level.set(s,0);
jacint@102
   155
	  std::queue<NodeIt> bfs_queue;
jacint@102
   156
	  bfs_queue.push(s);
jacint@102
   157
	  
jacint@102
   158
	  while (!bfs_queue.empty()) {
jacint@102
   159
	    
jacint@102
   160
	    NodeIt v=bfs_queue.front();	
jacint@102
   161
	    bfs_queue.pop();
jacint@102
   162
	    int l=level.get(v)+1;
jacint@102
   163
jacint@102
   164
	    for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@102
   165
	      if ( capacity.get(e) == flow.get(e) ) continue;
jacint@102
   166
	      NodeIt u=G.tail(e);
jacint@102
   167
	      if ( level.get(u) >= n ) { 
jacint@102
   168
		bfs_queue.push(u);
jacint@102
   169
		level.set(u, l);
jacint@102
   170
		if ( excess.get(u) > 0 ) stack[l].push(u);
jacint@102
   171
	      }
jacint@102
   172
	    }
jacint@102
   173
jacint@102
   174
	    for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
jacint@102
   175
	      if ( 0 == flow.get(e) ) continue;
jacint@102
   176
	      NodeIt u=G.head(e);
jacint@102
   177
	      if ( level.get(u) >= n ) { 
jacint@102
   178
		bfs_queue.push(u);
jacint@102
   179
		level.set(u, l);
jacint@102
   180
		if ( excess.get(u) > 0 ) stack[l].push(u);
jacint@102
   181
	      }
jacint@102
   182
	    }
jacint@102
   183
	  }
jacint@102
   184
	  b=n-2;
jacint@102
   185
	}
jacint@102
   186
jacint@102
   187
jacint@102
   188
	if ( stack[b].empty() ) --b;
jacint@102
   189
	else {
jacint@102
   190
	  
jacint@102
   191
	  NodeIt w=stack[b].top();        //w is a highest label active node.
jacint@102
   192
	  stack[b].pop();           
jacint@102
   193
	  int lev=level.get(w);
jacint@102
   194
	  T exc=excess.get(w);
jacint@102
   195
	  int newlevel=n;          //In newlevel we bound the next level of w.
jacint@102
   196
	  
jacint@102
   197
	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
jacint@102
   198
	    
jacint@102
   199
	    if ( flow.get(e) == capacity.get(e) ) continue; 
jacint@102
   200
	    NodeIt v=G.head(e);            
jacint@102
   201
	    //e=wv	    
jacint@102
   202
	    
jacint@102
   203
	    if( lev > level.get(v) ) {      
jacint@102
   204
	      /*Push is allowed now*/
jacint@102
   205
	      
jacint@102
   206
	      if ( excess.get(v)==0 && v!=t && v!=s ) 
jacint@102
   207
		stack[level.get(v)].push(v); 
jacint@102
   208
	      /*v becomes active.*/
jacint@102
   209
	      
jacint@102
   210
	      T cap=capacity.get(e);
jacint@102
   211
	      T flo=flow.get(e);
jacint@102
   212
	      T remcap=cap-flo;
jacint@102
   213
	      
jacint@102
   214
	      if ( remcap >= exc ) {       
jacint@102
   215
		/*A nonsaturating push.*/
jacint@102
   216
		
jacint@102
   217
		flow.set(e, flo+exc);
jacint@102
   218
		excess.set(v, excess.get(v)+exc);
jacint@102
   219
		exc=0;
jacint@102
   220
		break; 
jacint@102
   221
		
jacint@102
   222
	      } else { 
jacint@102
   223
		/*A saturating push.*/
jacint@102
   224
		
jacint@102
   225
		flow.set(e, cap);
jacint@102
   226
		excess.set(v, excess.get(v)+remcap);
jacint@102
   227
		exc-=remcap;
jacint@102
   228
	      }
jacint@102
   229
	    } else if ( newlevel > level.get(v) ){
jacint@102
   230
	      newlevel = level.get(v);
jacint@102
   231
	    }	    
jacint@102
   232
	    
jacint@102
   233
	  } //for out edges wv 
jacint@102
   234
	
jacint@102
   235
	
jacint@102
   236
	if ( exc > 0 ) {	
jacint@102
   237
	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
jacint@102
   238
	    
jacint@102
   239
	    if( flow.get(e) == 0 ) continue; 
jacint@102
   240
	    NodeIt v=G.tail(e);  
jacint@102
   241
	    //e=vw
jacint@102
   242
	    
jacint@102
   243
	    if( lev > level.get(v) ) {  
jacint@102
   244
	      /*Push is allowed now*/
jacint@102
   245
	      
jacint@102
   246
	      if ( excess.get(v)==0 && v!=t && v!=s ) 
jacint@102
   247
		stack[level.get(v)].push(v); 
jacint@102
   248
	      /*v becomes active.*/
jacint@102
   249
	      
jacint@102
   250
	      T flo=flow.get(e);
jacint@102
   251
	      
jacint@102
   252
	      if ( flo >= exc ) { 
jacint@102
   253
		/*A nonsaturating push.*/
jacint@102
   254
		
jacint@102
   255
		flow.set(e, flo-exc);
jacint@102
   256
		excess.set(v, excess.get(v)+exc);
jacint@102
   257
		exc=0;
jacint@102
   258
		break; 
jacint@102
   259
	      } else {                                               
jacint@102
   260
		/*A saturating push.*/
jacint@102
   261
		
jacint@102
   262
		excess.set(v, excess.get(v)+flo);
jacint@102
   263
		exc-=flo;
jacint@102
   264
		flow.set(e,0);
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
	  } //for in edges vw
jacint@102
   270
	  
jacint@102
   271
	} // if w still has excess after the out edge for cycle
jacint@102
   272
	
jacint@102
   273
	excess.set(w, exc);
jacint@102
   274
	 
jacint@102
   275
	/*
jacint@102
   276
	  Relabel
jacint@102
   277
	*/
jacint@102
   278
	
jacint@102
   279
	if ( exc > 0 ) {
jacint@102
   280
	  //now 'lev' is the old level of w
jacint@102
   281
	
jacint@102
   282
	  if ( phase ) {
jacint@102
   283
	    level.set(w,++newlevel);
jacint@102
   284
	    stack[newlevel].push(w);
jacint@102
   285
	    b=newlevel;
jacint@102
   286
	  } else {
jacint@102
   287
	    //unlacing
jacint@102
   288
	    NodeIt right_n=right.get(w);
jacint@102
   289
	    NodeIt left_n=left.get(w);
jacint@102
   290
jacint@102
   291
	    if ( right_n != 0 ) {
jacint@102
   292
	      if ( left_n != 0 ) {
jacint@102
   293
		right.set(left_n, right_n);
jacint@102
   294
		left.set(right_n, left_n);
jacint@102
   295
	      } else {
jacint@102
   296
		level_list[lev]=right_n;
jacint@102
   297
		left.set(right_n, 0);
jacint@102
   298
	      } 
jacint@102
   299
	    } else {
jacint@102
   300
	      if ( left_n != 0 ) {
jacint@102
   301
		right.set(left_n, 0);
jacint@102
   302
	      } else { 
jacint@102
   303
		level_list[lev]=0;
jacint@102
   304
	      } 
jacint@102
   305
	    }
jacint@102
   306
		
jacint@102
   307
jacint@102
   308
	    if ( level_list[lev]==0 ) {
jacint@102
   309
jacint@102
   310
	      for (int i=lev; i!=k ; ) {
jacint@102
   311
		NodeIt v=level_list[++i];
jacint@102
   312
		while ( v != 0 ) {
jacint@102
   313
		  level.set(v,n);
jacint@102
   314
		  v=right.get(v);
jacint@102
   315
		}
jacint@102
   316
		level_list[i]=0;
jacint@102
   317
	      }	     
jacint@102
   318
jacint@102
   319
	      level.set(w,n);
jacint@102
   320
jacint@102
   321
	      b=--lev;
jacint@102
   322
	      k=b;
jacint@102
   323
jacint@102
   324
	    } else {
jacint@102
   325
jacint@102
   326
	      if ( newlevel == n ) {
jacint@102
   327
		level.set(w,n);
jacint@102
   328
	      } else {
jacint@102
   329
		
jacint@102
   330
		level.set(w,++newlevel);
jacint@102
   331
		stack[newlevel].push(w);
jacint@102
   332
		b=newlevel;
jacint@102
   333
		if ( k < newlevel ) ++k;
jacint@102
   334
		NodeIt first=level_list[newlevel];
jacint@102
   335
		if ( first != 0 ) left.set(first,w);
jacint@102
   336
		right.set(w,first);
jacint@102
   337
		left.set(w,0);
jacint@102
   338
		level_list[newlevel]=w;
jacint@102
   339
	      }
jacint@102
   340
	    }
jacint@102
   341
	  } //phase 0
jacint@102
   342
	} // if ( exc > 0 )
jacint@102
   343
 
jacint@102
   344
	
jacint@102
   345
	} // if stack[b] is nonempty
jacint@102
   346
	
jacint@102
   347
      } // while(true)
jacint@102
   348
jacint@102
   349
jacint@102
   350
      value = excess.get(t);
jacint@102
   351
      /*Max flow value.*/
jacint@102
   352
jacint@102
   353
jacint@102
   354
    } //void run()
jacint@102
   355
jacint@102
   356
jacint@102
   357
jacint@102
   358
jacint@102
   359
jacint@102
   360
    /*
jacint@102
   361
      Returns the maximum value of a flow.
jacint@102
   362
     */
jacint@102
   363
jacint@102
   364
    T maxflow() {
jacint@102
   365
      return value;
jacint@102
   366
    }
jacint@102
   367
jacint@102
   368
jacint@102
   369
jacint@102
   370
    /*
jacint@102
   371
      For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). 
jacint@102
   372
    */
jacint@102
   373
jacint@102
   374
    T flowonedge(EdgeIt e) {
jacint@102
   375
      return flow.get(e);
jacint@102
   376
    }
jacint@102
   377
jacint@102
   378
jacint@102
   379
jacint@102
   380
    FlowMap allflow() {
jacint@102
   381
      return flow;
jacint@102
   382
    }
jacint@102
   383
jacint@102
   384
jacint@102
   385
    
jacint@102
   386
    void allflow(FlowMap& _flow ) {
jacint@102
   387
      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
jacint@102
   388
	_flow.set(v,flow.get(v));
jacint@102
   389
    }
jacint@102
   390
jacint@102
   391
jacint@102
   392
jacint@102
   393
    /*
jacint@102
   394
      Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@102
   395
    */
jacint@102
   396
    
jacint@102
   397
    template<typename CutMap>
jacint@102
   398
    void mincut(CutMap& M) {
jacint@102
   399
    
jacint@102
   400
      std::queue<NodeIt> queue;
jacint@102
   401
      
jacint@102
   402
      M.set(s,true);      
jacint@102
   403
      queue.push(s);
jacint@102
   404
jacint@102
   405
      while (!queue.empty()) {
jacint@102
   406
        NodeIt w=queue.front();
jacint@102
   407
	queue.pop();
jacint@102
   408
jacint@102
   409
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   410
	  NodeIt v=G.head(e);
jacint@102
   411
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@102
   412
	    queue.push(v);
jacint@102
   413
	    M.set(v, true);
jacint@102
   414
	  }
jacint@102
   415
	} 
jacint@102
   416
jacint@102
   417
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   418
	  NodeIt v=G.tail(e);
jacint@102
   419
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@102
   420
	    queue.push(v);
jacint@102
   421
	    M.set(v, true);
jacint@102
   422
	  }
jacint@102
   423
	} 
jacint@102
   424
jacint@102
   425
      }
jacint@102
   426
jacint@102
   427
    }
jacint@102
   428
jacint@102
   429
jacint@102
   430
jacint@102
   431
    /*
jacint@102
   432
      Returns the maximum min cut, by a reverse bfs 
jacint@102
   433
      from t in the residual graph.
jacint@102
   434
    */
jacint@102
   435
    
jacint@102
   436
    template<typename CutMap>
jacint@102
   437
    void max_mincut(CutMap& M) {
jacint@102
   438
    
jacint@102
   439
      std::queue<NodeIt> queue;
jacint@102
   440
      
jacint@102
   441
      M.set(t,true);        
jacint@102
   442
      queue.push(t);
jacint@102
   443
jacint@102
   444
      while (!queue.empty()) {
jacint@102
   445
        NodeIt w=queue.front();
jacint@102
   446
	queue.pop();
jacint@102
   447
jacint@102
   448
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   449
	  NodeIt v=G.tail(e);
jacint@102
   450
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@102
   451
	    queue.push(v);
jacint@102
   452
	    M.set(v, true);
jacint@102
   453
	  }
jacint@102
   454
	}
jacint@102
   455
jacint@102
   456
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@102
   457
	  NodeIt v=G.head(e);
jacint@102
   458
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@102
   459
	    queue.push(v);
jacint@102
   460
	    M.set(v, true);
jacint@102
   461
	  }
jacint@102
   462
	}
jacint@102
   463
      }
jacint@102
   464
jacint@102
   465
      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
jacint@102
   466
	M.set(v, !M.get(v));
jacint@102
   467
      }
jacint@102
   468
jacint@102
   469
    }
jacint@102
   470
jacint@102
   471
jacint@102
   472
jacint@102
   473
    template<typename CutMap>
jacint@102
   474
    void min_mincut(CutMap& M) {
jacint@102
   475
      mincut(M);
jacint@102
   476
    }
jacint@102
   477
jacint@102
   478
jacint@102
   479
jacint@102
   480
  };
alpar@105
   481
}//namespace hugo
jacint@102
   482
#endif 
jacint@102
   483
jacint@102
   484
jacint@102
   485
jacint@102
   486