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