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