src/work/jacint/preflow_hl2.h
author klao
Thu, 26 Feb 2004 16:07:40 +0000
changeset 132 1ac27e476e25
parent 105 a3c73e9b9b2e
permissions -rw-r--r--
makefile: tobb include konyvtar, jobb depend keszites
flow_test.cc: torolve, mert a depend-et megakasztja, igy gondolom nem is fordul
jacint@98
     1
// -*- C++ -*-
jacint@98
     2
/*
jacint@98
     3
preflow_hl2.h
jacint@98
     4
by jacint. 
jacint@98
     5
Runs the highest label variant of the preflow push algorithm with 
jacint@109
     6
running time O(n^2\sqrt(m)). 
jacint@98
     7
jacint@109
     8
Heuristics:
jacint@98
     9
jacint@109
    10
  gap: we iterate through the nodes for finding the nodes above 
jacint@109
    11
       the gap and under level n. So it is quite slow.
jacint@109
    12
  numb: we maintain the number of nodes in level i.
jacint@109
    13
  highest label
jacint@109
    14
  
jacint@109
    15
'A' is a parameter for the gap, we only upgrade the nodes to level n,
jacint@109
    16
  if the gap is under A*n.
jacint@98
    17
jacint@109
    18
The constructor runs the algorithm.
jacint@98
    19
jacint@109
    20
Members:
jacint@98
    21
jacint@109
    22
T maxFlow() : returns the value of a maximum flow
jacint@98
    23
jacint@109
    24
T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
jacint@98
    25
jacint@109
    26
FlowMap Flow() : returns the fixed maximum flow x
jacint@98
    27
jacint@109
    28
void minCut(CutMap& M) : sets M to the characteristic vector of a 
jacint@98
    29
     minimum cut. M should be a map of bools initialized to false.
jacint@98
    30
jacint@109
    31
void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@98
    32
     minimum min cut. M should be a map of bools initialized to false.
jacint@98
    33
jacint@109
    34
void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@98
    35
     maximum min cut. M should be a map of bools initialized to false.
jacint@98
    36
jacint@98
    37
*/
jacint@98
    38
jacint@98
    39
#ifndef PREFLOW_HL2_H
jacint@98
    40
#define PREFLOW_HL2_H
jacint@98
    41
jacint@109
    42
#define A .9
jacint@98
    43
jacint@98
    44
#include <vector>
jacint@98
    45
#include <stack>
jacint@98
    46
#include <queue>
jacint@98
    47
alpar@105
    48
namespace hugo {
jacint@98
    49
jacint@98
    50
  template <typename Graph, typename T, 
jacint@109
    51
    typename FlowMap=typename Graph::EdgeMap<T>, 
jacint@109
    52
    typename CapMap=typename Graph::EdgeMap<T> >
jacint@98
    53
  class preflow_hl2 {
jacint@98
    54
    
jacint@98
    55
    typedef typename Graph::NodeIt NodeIt;
jacint@98
    56
    typedef typename Graph::EdgeIt EdgeIt;
jacint@98
    57
    typedef typename Graph::EachNodeIt EachNodeIt;
jacint@98
    58
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@98
    59
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@98
    60
    
jacint@98
    61
    Graph& G;
jacint@98
    62
    NodeIt s;
jacint@98
    63
    NodeIt t;
jacint@98
    64
    FlowMap flow;
jacint@98
    65
    CapMap& capacity;  
jacint@98
    66
    T value;
jacint@98
    67
    
jacint@98
    68
  public:
jacint@98
    69
jacint@98
    70
    preflow_hl2(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) :
jacint@109
    71
      G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { 
jacint@98
    72
jacint@98
    73
      int n=G.nodeNum(); 
jacint@98
    74
      int b=n-2; 
jacint@98
    75
      /*
jacint@98
    76
	b is a bound on the highest level of an active node. 
jacint@98
    77
      */
jacint@98
    78
jacint@109
    79
      typename Graph::NodeMap<int> level(G,n);      
jacint@109
    80
      typename Graph::NodeMap<T> excess(G); 
jacint@109
    81
jacint@101
    82
      std::vector<int> numb(n);    
jacint@98
    83
      /*
jacint@98
    84
	The number of nodes on level i < n. It is
jacint@98
    85
	initialized to n+1, because of the reverse_bfs-part.
jacint@98
    86
      */
jacint@98
    87
jacint@98
    88
      std::vector<std::stack<NodeIt> > stack(2*n-1);    
jacint@98
    89
      //Stack of the active nodes in level i.
jacint@98
    90
jacint@98
    91
jacint@98
    92
      /*Reverse_bfs from t, to find the starting level.*/
jacint@98
    93
      level.set(t,0);
jacint@98
    94
      std::queue<NodeIt> bfs_queue;
jacint@98
    95
      bfs_queue.push(t);
jacint@98
    96
jacint@98
    97
      while (!bfs_queue.empty()) {
jacint@98
    98
jacint@98
    99
	NodeIt v=bfs_queue.front();	
jacint@98
   100
	bfs_queue.pop();
jacint@98
   101
	int l=level.get(v)+1;
jacint@98
   102
jacint@98
   103
	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@98
   104
	  NodeIt w=G.tail(e);
jacint@98
   105
	  if ( level.get(w) == n ) {
jacint@98
   106
	    bfs_queue.push(w);
jacint@98
   107
	    ++numb[l];
jacint@98
   108
	    level.set(w, l);
jacint@98
   109
	  }
jacint@98
   110
	}
jacint@98
   111
      }
jacint@109
   112
	
jacint@98
   113
      level.set(s,n);
jacint@98
   114
jacint@98
   115
jacint@98
   116
jacint@98
   117
      /* Starting flow. It is everywhere 0 at the moment. */     
jacint@98
   118
      for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
jacint@98
   119
	{
jacint@101
   120
	  T c=capacity.get(e);
jacint@101
   121
	  if ( c == 0 ) continue;
jacint@98
   122
	  NodeIt w=G.head(e);
jacint@98
   123
	  if ( w!=s ) {	  
jacint@98
   124
	    if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w); 
jacint@101
   125
	    flow.set(e, c); 
jacint@101
   126
	    excess.set(w, excess.get(w)+c);
jacint@101
   127
	  }
jacint@98
   128
	}
jacint@109
   129
      
jacint@98
   130
      /* 
jacint@98
   131
	 End of preprocessing 
jacint@98
   132
      */
jacint@98
   133
jacint@98
   134
jacint@98
   135
      /*
jacint@98
   136
	Push/relabel on the highest level active nodes.
jacint@109
   137
      */
jacint@98
   138
      /*While there exists an active node.*/
jacint@98
   139
      while (b) { 
jacint@109
   140
	if ( stack[b].empty() ) { 
jacint@98
   141
	  --b;
jacint@109
   142
	  continue;
jacint@109
   143
	} 
jacint@109
   144
	
jacint@109
   145
	NodeIt w=stack[b].top();   
jacint@109
   146
	stack[b].pop();           
jacint@109
   147
	int lev=level.get(w);
jacint@109
   148
	T exc=excess.get(w);
jacint@109
   149
	int newlevel=2*n;      //In newlevel we bound the next level of w.
jacint@109
   150
	
jacint@98
   151
	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
jacint@98
   152
	    
jacint@98
   153
	    if ( flow.get(e) == capacity.get(e) ) continue; 
jacint@98
   154
	    NodeIt v=G.head(e);            
jacint@98
   155
	    //e=wv	    
jacint@98
   156
	    
jacint@98
   157
	    if( lev > level.get(v) ) {      
jacint@98
   158
	      /*Push is allowed now*/
jacint@98
   159
	      
jacint@98
   160
	      if ( excess.get(v)==0 && v != s && v !=t ) 
jacint@98
   161
		stack[level.get(v)].push(v); 
jacint@98
   162
	      /*v becomes active.*/
jacint@98
   163
	      
jacint@109
   164
	      T cap=capacity.get(e);
jacint@109
   165
	      T flo=flow.get(e);
jacint@109
   166
	      T remcap=cap-flo;
jacint@98
   167
	      
jacint@98
   168
	      if ( remcap >= exc ) {       
jacint@98
   169
		/*A nonsaturating push.*/
jacint@98
   170
		
jacint@98
   171
		flow.set(e, flo+exc);
jacint@98
   172
		excess.set(v, excess.get(v)+exc);
jacint@98
   173
		exc=0;
jacint@98
   174
		break; 
jacint@98
   175
		
jacint@98
   176
	      } else { 
jacint@98
   177
		/*A saturating push.*/
jacint@98
   178
		
jacint@98
   179
		flow.set(e, cap );
jacint@98
   180
		excess.set(v, excess.get(v)+remcap);
jacint@98
   181
		exc-=remcap;
jacint@98
   182
	      }
jacint@98
   183
	    } else if ( newlevel > level.get(v) ) newlevel = level.get(v);
jacint@98
   184
	    
jacint@98
   185
	  } //for out edges wv 
jacint@98
   186
	
jacint@98
   187
	
jacint@98
   188
	if ( exc > 0 ) {	
jacint@98
   189
	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
jacint@98
   190
	    
jacint@98
   191
	    if( flow.get(e) == 0 ) continue; 
jacint@98
   192
	    NodeIt v=G.tail(e);  
jacint@98
   193
	    //e=vw
jacint@98
   194
	    
jacint@98
   195
	    if( lev > level.get(v) ) {  
jacint@98
   196
	      /*Push is allowed now*/
jacint@98
   197
	      
jacint@98
   198
	      if ( excess.get(v)==0 && v != s && v !=t) 
jacint@98
   199
		stack[level.get(v)].push(v); 
jacint@98
   200
	      /*v becomes active.*/
jacint@98
   201
	      
jacint@109
   202
	      T flo=flow.get(e);
jacint@98
   203
	      
jacint@98
   204
	      if ( flo >= exc ) { 
jacint@98
   205
		/*A nonsaturating push.*/
jacint@98
   206
		
jacint@98
   207
		flow.set(e, flo-exc);
jacint@98
   208
		excess.set(v, excess.get(v)+exc);
jacint@98
   209
		exc=0;
jacint@98
   210
		break; 
jacint@98
   211
	      } else {                                               
jacint@98
   212
		/*A saturating push.*/
jacint@98
   213
		
jacint@98
   214
		excess.set(v, excess.get(v)+flo);
jacint@98
   215
		exc-=flo;
jacint@98
   216
		flow.set(e,0);
jacint@98
   217
	      }  
jacint@98
   218
	    } else if ( newlevel > level.get(v) ) newlevel = level.get(v);
jacint@98
   219
	    
jacint@98
   220
	  } //for in edges vw
jacint@98
   221
	  
jacint@98
   222
	} // if w still has excess after the out edge for cycle
jacint@109
   223
	
jacint@109
   224
	excess.set(w, exc);
jacint@109
   225
	
jacint@109
   226
jacint@109
   227
	
jacint@109
   228
jacint@109
   229
	/*
jacint@109
   230
	  Relabel
jacint@109
   231
	*/
jacint@109
   232
	
jacint@109
   233
	if ( exc > 0 ) {
jacint@109
   234
	  //now 'lev' is the old level of w
jacint@109
   235
	  level.set(w,++newlevel);
jacint@98
   236
	  
jacint@109
   237
	  if ( lev < n ) {
jacint@109
   238
	    --numb[lev];
jacint@109
   239
	    
jacint@109
   240
	    if ( !numb[lev] && lev < A*n ) {  //If the level of w gets empty. 
jacint@109
   241
	      
jacint@109
   242
	      for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
jacint@109
   243
		if (level.get(v) > lev && level.get(v) < n ) level.set(v,n);  
jacint@109
   244
	      }
jacint@109
   245
	      for (int i=lev+1 ; i!=n ; ++i) numb[i]=0; 
jacint@109
   246
	      if ( newlevel < n ) newlevel=n; 
jacint@109
   247
	    } else { 
jacint@109
   248
	      if ( newlevel < n ) ++numb[newlevel]; 
jacint@109
   249
	    }
jacint@109
   250
	  } 
jacint@98
   251
	  
jacint@109
   252
	  stack[newlevel].push(w);
jacint@109
   253
	  b=newlevel;
jacint@109
   254
	  
jacint@109
   255
	}
jacint@109
   256
	
jacint@98
   257
      } // while(b)
jacint@109
   258
      
jacint@109
   259
      
jacint@98
   260
      value = excess.get(t);
jacint@98
   261
      /*Max flow value.*/
jacint@98
   262
jacint@98
   263
jacint@98
   264
    } //void run()
jacint@98
   265
jacint@98
   266
jacint@98
   267
jacint@98
   268
jacint@98
   269
jacint@98
   270
    /*
jacint@98
   271
      Returns the maximum value of a flow.
jacint@98
   272
     */
jacint@98
   273
jacint@109
   274
    T maxFlow() {
jacint@98
   275
      return value;
jacint@98
   276
    }
jacint@98
   277
jacint@98
   278
jacint@98
   279
jacint@98
   280
    /*
jacint@109
   281
      For the maximum flow x found by the algorithm, 
jacint@109
   282
      it returns the flow value on edge e, i.e. x(e). 
jacint@98
   283
    */
jacint@98
   284
jacint@109
   285
    T flowOnEdge(const EdgeIt e) {
jacint@98
   286
      return flow.get(e);
jacint@98
   287
    }
jacint@98
   288
jacint@98
   289
jacint@98
   290
jacint@98
   291
    /*
jacint@98
   292
      Returns the maximum flow x found by the algorithm.
jacint@98
   293
    */
jacint@98
   294
jacint@109
   295
    FlowMap Flow() {
jacint@98
   296
      return flow;
jacint@98
   297
    }
jacint@98
   298
jacint@98
   299
jacint@98
   300
jacint@98
   301
jacint@98
   302
    /*
jacint@98
   303
      Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@98
   304
    */
jacint@98
   305
    
jacint@98
   306
    template<typename CutMap>
jacint@109
   307
    void minCut(CutMap& M) {
jacint@98
   308
    
jacint@98
   309
      std::queue<NodeIt> queue;
jacint@98
   310
      
jacint@98
   311
      M.set(s,true);      
jacint@98
   312
      queue.push(s);
jacint@98
   313
jacint@98
   314
      while (!queue.empty()) {
jacint@98
   315
        NodeIt w=queue.front();
jacint@98
   316
	queue.pop();
jacint@109
   317
	
jacint@98
   318
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@98
   319
	  NodeIt v=G.head(e);
jacint@98
   320
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@98
   321
	    queue.push(v);
jacint@98
   322
	    M.set(v, true);
jacint@98
   323
	  }
jacint@98
   324
	} 
jacint@98
   325
jacint@98
   326
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@98
   327
	  NodeIt v=G.tail(e);
jacint@98
   328
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@98
   329
	    queue.push(v);
jacint@98
   330
	    M.set(v, true);
jacint@98
   331
	  }
jacint@109
   332
	}
jacint@98
   333
jacint@98
   334
      }
jacint@98
   335
    }
jacint@98
   336
jacint@98
   337
jacint@98
   338
jacint@98
   339
    /*
jacint@98
   340
      Returns the maximum min cut, by a reverse bfs 
jacint@98
   341
      from t in the residual graph.
jacint@98
   342
    */
jacint@98
   343
    
jacint@98
   344
    template<typename CutMap>
jacint@109
   345
    void maxMinCut(CutMap& M) {
jacint@98
   346
    
jacint@98
   347
      std::queue<NodeIt> queue;
jacint@98
   348
      
jacint@98
   349
      M.set(t,true);        
jacint@98
   350
      queue.push(t);
jacint@98
   351
jacint@98
   352
      while (!queue.empty()) {
jacint@98
   353
        NodeIt w=queue.front();
jacint@98
   354
	queue.pop();
jacint@98
   355
jacint@98
   356
	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@98
   357
	  NodeIt v=G.tail(e);
jacint@98
   358
	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@98
   359
	    queue.push(v);
jacint@98
   360
	    M.set(v, true);
jacint@98
   361
	  }
jacint@98
   362
	}
jacint@98
   363
jacint@98
   364
	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@98
   365
	  NodeIt v=G.head(e);
jacint@98
   366
	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@98
   367
	    queue.push(v);
jacint@98
   368
	    M.set(v, true);
jacint@98
   369
	  }
jacint@98
   370
	}
jacint@98
   371
      }
jacint@98
   372
jacint@98
   373
      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
jacint@98
   374
	M.set(v, !M.get(v));
jacint@98
   375
      }
jacint@98
   376
jacint@98
   377
    }
jacint@98
   378
jacint@98
   379
jacint@98
   380
jacint@98
   381
    template<typename CutMap>
jacint@109
   382
    void minMinCut(CutMap& M) {
jacint@109
   383
      minCut(M);
jacint@98
   384
    }
jacint@98
   385
jacint@98
   386
jacint@98
   387
jacint@98
   388
  };
jacint@109
   389
}//namespace marci
jacint@98
   390
#endif 
jacint@98
   391
jacint@98
   392
jacint@98
   393
jacint@98
   394