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