src/work/preflow_push_hl.hh
author jacint
Fri, 23 Jan 2004 22:26:13 +0000
changeset 33 f505c414feb9
parent 20 bf088f14b87a
permissions -rw-r--r--
Primitive Dijkstra with stl priority queue. flow_test.cc is for testing flows and Dijkstra.
jacint@20
     1
/*
jacint@20
     2
preflow_push_hl.hh
jacint@20
     3
by jacint. 
jacint@20
     4
Runs the highest label variant of the preflow push algorithm with 
jacint@20
     5
running time O(n^2\sqrt(m)). 
jacint@20
     6
jacint@20
     7
Member functions:
jacint@20
     8
jacint@20
     9
void run() : runs the algorithm
jacint@20
    10
jacint@20
    11
 The following functions should be used after run() was already run.
jacint@20
    12
jacint@20
    13
T maxflow() : returns the value of a maximum flow
jacint@20
    14
jacint@20
    15
T flowonedge(edge_iterator e) : for a fixed maximum flow x it returns x(e) 
jacint@20
    16
jacint@20
    17
edge_property_vector<graph_type, T> allflow() : returns the fixed maximum flow x
jacint@20
    18
jacint@20
    19
node_property_vector<graph_type, bool> mincut() : returns a 
jacint@20
    20
     characteristic vector of a minimum cut. (An empty level 
jacint@20
    21
     in the algorithm gives a minimum cut.)
jacint@20
    22
*/
jacint@20
    23
jacint@20
    24
#ifndef PREFLOW_PUSH_HL_HH
jacint@20
    25
#define PREFLOW_PUSH_HL_HH
jacint@20
    26
jacint@20
    27
#include <algorithm>
jacint@20
    28
#include <vector>
jacint@20
    29
#include <stack>
jacint@20
    30
jacint@20
    31
#include <marci_graph_traits.hh>
jacint@20
    32
#include <marci_property_vector.hh>
jacint@20
    33
#include <reverse_bfs.hh>
jacint@20
    34
jacint@20
    35
namespace marci {
jacint@20
    36
jacint@20
    37
  template <typename graph_type, typename T>
jacint@20
    38
  class preflow_push_hl {
jacint@20
    39
    
jacint@20
    40
    typedef typename graph_traits<graph_type>::node_iterator node_iterator;
jacint@20
    41
    typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
jacint@20
    42
    typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
jacint@20
    43
    typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
jacint@20
    44
    typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
jacint@20
    45
    typedef typename graph_traits<graph_type>::each_edge_iterator each_edge_iterator;
jacint@20
    46
    
jacint@20
    47
jacint@20
    48
    graph_type& G;
jacint@20
    49
    node_iterator s;
jacint@20
    50
    node_iterator t;
jacint@20
    51
    edge_property_vector<graph_type, T> flow;
jacint@20
    52
    edge_property_vector<graph_type, T>& capacity; 
jacint@20
    53
    T value;
jacint@20
    54
    node_property_vector<graph_type, bool> mincutvector;
jacint@20
    55
jacint@20
    56
   
jacint@20
    57
  public:
jacint@20
    58
jacint@20
    59
    preflow_push_hl(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), mincutvector(_G, true) { }
jacint@20
    60
jacint@20
    61
jacint@20
    62
jacint@20
    63
jacint@20
    64
    /*
jacint@20
    65
      The run() function runs the highest label preflow-push, 
jacint@20
    66
      running time: O(n^2\sqrt(m))
jacint@20
    67
    */
jacint@20
    68
    void run() {
jacint@20
    69
 
jacint@20
    70
      node_property_vector<graph_type, int> level(G);         //level of node
jacint@20
    71
      node_property_vector<graph_type, T> excess(G);          //excess of node
jacint@20
    72
            
jacint@20
    73
      int n=number_of(G.first_node());                        //number of nodes 
jacint@20
    74
      int b=n; 
jacint@20
    75
      /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/
jacint@20
    76
jacint@20
    77
      std::vector<std::stack<node_iterator> > stack(2*n-1);    //Stack of the active nodes in level i.
jacint@20
    78
jacint@20
    79
jacint@20
    80
jacint@20
    81
jacint@20
    82
      /*Reverse_bfs from t, to find the starting level.*/
jacint@20
    83
jacint@20
    84
      reverse_bfs<list_graph> bfs(G, t);
jacint@20
    85
      bfs.run();
jacint@30
    86
      for(each_node_iterator v=G.first_node(); v.valid(); ++v) {
jacint@20
    87
	level.put(v, bfs.dist(v)); 
jacint@20
    88
	//std::cout << "the level of " << v << " is " << bfs.dist(v);
jacint@20
    89
      }
jacint@20
    90
jacint@20
    91
      /*The level of s is fixed to n*/ 
jacint@20
    92
      level.put(s,n);
jacint@20
    93
jacint@20
    94
jacint@20
    95
jacint@20
    96
jacint@20
    97
jacint@20
    98
      /* Starting flow. It is everywhere 0 at the moment. */
jacint@20
    99
     
jacint@30
   100
      for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i) 
jacint@20
   101
	{
jacint@20
   102
	  node_iterator w=G.head(i);
jacint@20
   103
	  flow.put(i, capacity.get(i)); 
jacint@20
   104
	  stack[bfs.dist(w)].push(w); 
jacint@20
   105
	  excess.put(w, capacity.get(i));
jacint@20
   106
	}
jacint@20
   107
jacint@20
   108
jacint@20
   109
      /* 
jacint@20
   110
	 End of preprocessing 
jacint@20
   111
      */
jacint@20
   112
jacint@20
   113
jacint@20
   114
jacint@20
   115
      /*
jacint@20
   116
	Push/relabel on the highest level active nodes.
jacint@20
   117
      */
jacint@20
   118
	
jacint@20
   119
      /*While there exists active node.*/
jacint@20
   120
      while (b) { 
jacint@20
   121
jacint@20
   122
	/*We decrease the bound if there is no active node of level b.*/
jacint@20
   123
	if (stack[b].empty()) {
jacint@20
   124
	  --b;
jacint@20
   125
	} else {
jacint@20
   126
jacint@20
   127
	  node_iterator w=stack[b].top();    //w is the highest label active node.
jacint@20
   128
	  stack[b].pop();                    //We delete w from the stack.
jacint@20
   129
	
jacint@20
   130
	  int newlevel=2*n-2;                   //In newlevel we maintain the next level of w.
jacint@20
   131
	
jacint@30
   132
	  for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) {
jacint@20
   133
	    node_iterator v=G.head(e);
jacint@20
   134
	    /*e is the edge wv.*/
jacint@20
   135
jacint@20
   136
	    if (flow.get(e)<capacity.get(e)) {              
jacint@20
   137
	      /*e is an edge of the residual graph */
jacint@20
   138
jacint@20
   139
	      if(level.get(w)==level.get(v)+1) {      
jacint@20
   140
		/*Push is allowed now*/
jacint@20
   141
jacint@20
   142
		if (capacity.get(e)-flow.get(e) > excess.get(w)) {       
jacint@20
   143
		  /*A nonsaturating push.*/
jacint@20
   144
		  
jacint@20
   145
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@20
   146
		  /*v becomes active.*/
jacint@20
   147
		  
jacint@20
   148
		  flow.put(e, flow.get(e)+excess.get(w));
jacint@20
   149
		  excess.put(v, excess.get(v)+excess.get(w));
jacint@20
   150
		  excess.put(w,0);
jacint@20
   151
		  //std::cout << w << " " << v <<" elore elen nonsat pump "  << std::endl;
jacint@20
   152
		  break; 
jacint@20
   153
		} else { 
jacint@20
   154
		  /*A saturating push.*/
jacint@20
   155
jacint@20
   156
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@20
   157
		  /*v becomes active.*/
jacint@20
   158
jacint@20
   159
		  excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e));
jacint@20
   160
		  excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e));
jacint@20
   161
		  flow.put(e, capacity.get(e));
jacint@20
   162
		  //std::cout << w<<" " <<v<<" elore elen sat pump "   << std::endl;
jacint@20
   163
		  if (excess.get(w)==0) break;
jacint@20
   164
		  /*If w is not active any more, then we go on to the next node.*/
jacint@20
   165
		  
jacint@20
   166
		} // if (capacity.get(e)-flow.get(e) > excess.get(w))
jacint@20
   167
	      } // if(level.get(w)==level.get(v)+1)
jacint@20
   168
	    
jacint@20
   169
	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@20
   170
	    
jacint@20
   171
	    } //if (flow.get(e)<capacity.get(e))
jacint@20
   172
	 
jacint@30
   173
	  } //for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) 
jacint@20
   174
	  
jacint@20
   175
jacint@20
   176
jacint@30
   177
	  for(in_edge_iterator e=G.first_in_edge(w); e.valid(); ++e) {
jacint@20
   178
	    node_iterator v=G.tail(e);
jacint@20
   179
	    /*e is the edge vw.*/
jacint@20
   180
jacint@20
   181
	    if (excess.get(w)==0) break;
jacint@20
   182
	    /*It may happen, that w became inactive in the first for cycle.*/		
jacint@20
   183
	    if(flow.get(e)>0) {             
jacint@20
   184
	      /*e is an edge of the residual graph */
jacint@20
   185
jacint@20
   186
	      if(level.get(w)==level.get(v)+1) {  
jacint@20
   187
		/*Push is allowed now*/
jacint@20
   188
		
jacint@20
   189
		if (flow.get(e) > excess.get(w)) { 
jacint@20
   190
		  /*A nonsaturating push.*/
jacint@20
   191
		  
jacint@20
   192
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@20
   193
		  /*v becomes active.*/
jacint@20
   194
jacint@20
   195
		  flow.put(e, flow.get(e)-excess.get(w));
jacint@20
   196
		  excess.put(v, excess.get(v)+excess.get(w));
jacint@20
   197
		  excess.put(w,0);
jacint@20
   198
		  //std::cout << v << " " << w << " vissza elen nonsat pump "     << std::endl;
jacint@20
   199
		  break; 
jacint@20
   200
		} else {                                               
jacint@20
   201
		  /*A saturating push.*/
jacint@20
   202
		  
jacint@20
   203
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@20
   204
		  /*v becomes active.*/
jacint@20
   205
		  
jacint@20
   206
		  excess.put(v, excess.get(v)+flow.get(e));
jacint@20
   207
		  excess.put(w, excess.get(w)-flow.get(e));
jacint@20
   208
		  flow.put(e,0);
jacint@20
   209
		  //std::cout << v <<" " << w << " vissza elen sat pump "     << std::endl;
jacint@20
   210
		  if (excess.get(w)==0) { break;}
jacint@20
   211
		} //if (flow.get(e) > excess.get(v)) 
jacint@20
   212
	      } //if(level.get(w)==level.get(v)+1)
jacint@20
   213
	      
jacint@20
   214
	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@20
   215
	      
jacint@20
   216
jacint@20
   217
	    } //if (flow.get(e)>0)
jacint@20
   218
jacint@20
   219
	  } //for
jacint@20
   220
jacint@20
   221
jacint@20
   222
	  if (excess.get(w)>0) {
jacint@20
   223
	    level.put(w,++newlevel);
jacint@20
   224
	    stack[newlevel].push(w);
jacint@20
   225
	    b=newlevel;
jacint@20
   226
	    //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl; 
jacint@20
   227
	  }
jacint@20
   228
jacint@20
   229
jacint@20
   230
	} //else
jacint@20
   231
       
jacint@20
   232
      } //while(b)
jacint@20
   233
jacint@20
   234
      value = excess.get(t);
jacint@20
   235
      /*Max flow value.*/
jacint@20
   236
jacint@20
   237
jacint@20
   238
jacint@20
   239
jacint@20
   240
    } //void run()
jacint@20
   241
jacint@20
   242
jacint@20
   243
jacint@20
   244
jacint@20
   245
jacint@20
   246
    /*
jacint@20
   247
      Returns the maximum value of a flow.
jacint@20
   248
     */
jacint@20
   249
jacint@20
   250
    T maxflow() {
jacint@20
   251
      return value;
jacint@20
   252
    }
jacint@20
   253
jacint@20
   254
jacint@20
   255
jacint@20
   256
    /*
jacint@20
   257
      For the maximum flow x found by the algorithm, it returns the flow value on edge e, i.e. x(e). 
jacint@20
   258
    */
jacint@20
   259
jacint@20
   260
    T flowonedge(edge_iterator e) {
jacint@20
   261
      return flow.get(e);
jacint@20
   262
    }
jacint@20
   263
jacint@20
   264
jacint@20
   265
jacint@20
   266
    /*
jacint@20
   267
      Returns the maximum flow x found by the algorithm.
jacint@20
   268
    */
jacint@20
   269
jacint@20
   270
    edge_property_vector<graph_type, T> allflow() {
jacint@20
   271
      return flow;
jacint@20
   272
    }
jacint@20
   273
jacint@20
   274
jacint@20
   275
jacint@20
   276
    /*
jacint@20
   277
      Returns a minimum cut by using a reverse bfs from t in the residual graph.
jacint@20
   278
    */
jacint@20
   279
    
jacint@20
   280
    node_property_vector<graph_type, bool> mincut() {
jacint@20
   281
    
jacint@20
   282
      std::queue<node_iterator> queue;
jacint@20
   283
      
jacint@20
   284
      mincutvector.put(t,false);      
jacint@20
   285
      queue.push(t);
jacint@20
   286
jacint@20
   287
      while (!queue.empty()) {
jacint@20
   288
        node_iterator w=queue.front();
jacint@20
   289
	queue.pop();
jacint@20
   290
jacint@30
   291
	for(in_edge_iterator e=G.first_in_edge(w) ; e.valid(); ++e) {
jacint@20
   292
	  node_iterator v=G.tail(e);
jacint@20
   293
	  if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@20
   294
	    queue.push(v);
jacint@20
   295
	    mincutvector.put(v, false);
jacint@20
   296
	  }
jacint@20
   297
	} // for
jacint@20
   298
jacint@30
   299
	for(out_edge_iterator e=G.first_out_edge(w) ; e.valid(); ++e) {
jacint@20
   300
	  node_iterator v=G.head(e);
jacint@20
   301
	  if (mincutvector.get(v) && flow.get(e) > 0 ) {
jacint@20
   302
	    queue.push(v);
jacint@20
   303
	    mincutvector.put(v, false);
jacint@20
   304
	  }
jacint@20
   305
	} // for
jacint@20
   306
jacint@20
   307
      }
jacint@20
   308
jacint@20
   309
      return mincutvector;
jacint@20
   310
    
jacint@20
   311
    }
jacint@20
   312
jacint@20
   313
jacint@20
   314
  };
jacint@20
   315
}//namespace marci
jacint@20
   316
#endif 
jacint@20
   317
jacint@20
   318
jacint@20
   319
jacint@20
   320