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