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