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