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