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