jacint@47: /*
jacint@47: preflow_push_hl.hh
jacint@47: by jacint. 
jacint@47: Runs the highest label variant of the preflow push algorithm with 
jacint@47: running time O(n^2\sqrt(m)). 
jacint@47: 
jacint@47: Member functions:
jacint@47: 
jacint@47: void run() : runs the algorithm
jacint@47: 
jacint@47:  The following functions should be used after run() was already run.
jacint@47: 
jacint@47: T maxflow() : returns the value of a maximum flow
jacint@47: 
jacint@47: T flowonedge(edge_iterator e) : for a fixed maximum flow x it returns x(e) 
jacint@47: 
jacint@47: edge_property_vector<graph_type, T> allflow() : returns the fixed maximum flow x
jacint@47: 
jacint@47: node_property_vector<graph_type, bool> mincut() : returns a 
jacint@47:      characteristic vector of a minimum cut. (An empty level 
jacint@47:      in the algorithm gives a minimum cut.)
jacint@47: */
jacint@47: 
jacint@47: #ifndef PREFLOW_PUSH_HL_HH
jacint@47: #define PREFLOW_PUSH_HL_HH
jacint@47: 
jacint@47: #include <algorithm>
jacint@47: #include <vector>
jacint@47: #include <stack>
jacint@47: 
jacint@47: #include <marci_graph_traits.hh>
jacint@47: #include <marci_property_vector.hh>
jacint@47: #include <reverse_bfs.hh>
jacint@47: 
jacint@47: namespace marci {
jacint@47: 
jacint@47:   template <typename graph_type, typename T>
jacint@47:   class preflow_push_hl {
jacint@47:     
jacint@47:     typedef typename graph_traits<graph_type>::node_iterator node_iterator;
jacint@47:     typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
jacint@47:     typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
jacint@47:     typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
jacint@47:     typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
jacint@47:     typedef typename graph_traits<graph_type>::each_edge_iterator each_edge_iterator;
jacint@47:     
jacint@47: 
jacint@47:     graph_type& G;
jacint@47:     node_iterator s;
jacint@47:     node_iterator t;
jacint@47:     edge_property_vector<graph_type, T> flow;
jacint@47:     edge_property_vector<graph_type, T>& capacity; 
jacint@47:     T value;
jacint@47:     node_property_vector<graph_type, bool> mincutvector;
jacint@47: 
jacint@47:    
jacint@47:   public:
jacint@47: 
jacint@47:     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: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     /*
jacint@47:       The run() function runs the highest label preflow-push, 
jacint@47:       running time: O(n^2\sqrt(m))
jacint@47:     */
jacint@47:     void run() {
jacint@47:  
jacint@47:       node_property_vector<graph_type, int> level(G);         //level of node
jacint@47:       node_property_vector<graph_type, T> excess(G);          //excess of node
jacint@47:             
jacint@47:       int n=number_of(G.first_node());                        //number of nodes 
jacint@47:       int b=n; 
jacint@47:       /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/
jacint@47: 
jacint@47:       std::vector<std::stack<node_iterator> > stack(2*n-1);    //Stack of the active nodes in level i.
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:       /*Reverse_bfs from t, to find the starting level.*/
jacint@47: 
jacint@47:       reverse_bfs<list_graph> bfs(G, t);
jacint@47:       bfs.run();
jacint@47:       for(each_node_iterator v=G.first_node(); v.valid(); ++v) {
jacint@47: 	level.put(v, bfs.dist(v)); 
jacint@47: 	//std::cout << "the level of " << v << " is " << bfs.dist(v);
jacint@47:       }
jacint@47: 
jacint@47:       /*The level of s is fixed to n*/ 
jacint@47:       level.put(s,n);
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:       /* Starting flow. It is everywhere 0 at the moment. */
jacint@47:      
jacint@47:       for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i) 
jacint@47: 	{
jacint@47: 	  node_iterator w=G.head(i);
jacint@47: 	  flow.put(i, capacity.get(i)); 
jacint@47: 	  stack[bfs.dist(w)].push(w); 
jacint@47: 	  excess.put(w, capacity.get(i));
jacint@47: 	}
jacint@47: 
jacint@47: 
jacint@47:       /* 
jacint@47: 	 End of preprocessing 
jacint@47:       */
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:       /*
jacint@47: 	Push/relabel on the highest level active nodes.
jacint@47:       */
jacint@47: 	
jacint@47:       /*While there exists active node.*/
jacint@47:       while (b) { 
jacint@47: 
jacint@47: 	/*We decrease the bound if there is no active node of level b.*/
jacint@47: 	if (stack[b].empty()) {
jacint@47: 	  --b;
jacint@47: 	} else {
jacint@47: 
jacint@47: 	  node_iterator w=stack[b].top();    //w is the highest label active node.
jacint@47: 	  stack[b].pop();                    //We delete w from the stack.
jacint@47: 	
jacint@47: 	  int newlevel=2*n-2;                   //In newlevel we maintain the next level of w.
jacint@47: 	
jacint@47: 	  for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) {
jacint@47: 	    node_iterator v=G.head(e);
jacint@47: 	    /*e is the edge wv.*/
jacint@47: 
jacint@47: 	    if (flow.get(e)<capacity.get(e)) {              
jacint@47: 	      /*e is an edge of the residual graph */
jacint@47: 
jacint@47: 	      if(level.get(w)==level.get(v)+1) {      
jacint@47: 		/*Push is allowed now*/
jacint@47: 
jacint@47: 		if (capacity.get(e)-flow.get(e) > excess.get(w)) {       
jacint@47: 		  /*A nonsaturating push.*/
jacint@47: 		  
jacint@47: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@47: 		  /*v becomes active.*/
jacint@47: 		  
jacint@47: 		  flow.put(e, flow.get(e)+excess.get(w));
jacint@47: 		  excess.put(v, excess.get(v)+excess.get(w));
jacint@47: 		  excess.put(w,0);
jacint@47: 		  //std::cout << w << " " << v <<" elore elen nonsat pump "  << std::endl;
jacint@47: 		  break; 
jacint@47: 		} else { 
jacint@47: 		  /*A saturating push.*/
jacint@47: 
jacint@47: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@47: 		  /*v becomes active.*/
jacint@47: 
jacint@47: 		  excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e));
jacint@47: 		  excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e));
jacint@47: 		  flow.put(e, capacity.get(e));
jacint@47: 		  //std::cout << w<<" " <<v<<" elore elen sat pump "   << std::endl;
jacint@47: 		  if (excess.get(w)==0) break;
jacint@47: 		  /*If w is not active any more, then we go on to the next node.*/
jacint@47: 		  
jacint@47: 		} // if (capacity.get(e)-flow.get(e) > excess.get(w))
jacint@47: 	      } // if(level.get(w)==level.get(v)+1)
jacint@47: 	    
jacint@47: 	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@47: 	    
jacint@47: 	    } //if (flow.get(e)<capacity.get(e))
jacint@47: 	 
jacint@47: 	  } //for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) 
jacint@47: 	  
jacint@47: 
jacint@47: 
jacint@47: 	  for(in_edge_iterator e=G.first_in_edge(w); e.valid(); ++e) {
jacint@47: 	    node_iterator v=G.tail(e);
jacint@47: 	    /*e is the edge vw.*/
jacint@47: 
jacint@47: 	    if (excess.get(w)==0) break;
jacint@47: 	    /*It may happen, that w became inactive in the first for cycle.*/		
jacint@47: 	    if(flow.get(e)>0) {             
jacint@47: 	      /*e is an edge of the residual graph */
jacint@47: 
jacint@47: 	      if(level.get(w)==level.get(v)+1) {  
jacint@47: 		/*Push is allowed now*/
jacint@47: 		
jacint@47: 		if (flow.get(e) > excess.get(w)) { 
jacint@47: 		  /*A nonsaturating push.*/
jacint@47: 		  
jacint@47: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@47: 		  /*v becomes active.*/
jacint@47: 
jacint@47: 		  flow.put(e, flow.get(e)-excess.get(w));
jacint@47: 		  excess.put(v, excess.get(v)+excess.get(w));
jacint@47: 		  excess.put(w,0);
jacint@47: 		  //std::cout << v << " " << w << " vissza elen nonsat pump "     << std::endl;
jacint@47: 		  break; 
jacint@47: 		} else {                                               
jacint@47: 		  /*A saturating push.*/
jacint@47: 		  
jacint@47: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@47: 		  /*v becomes active.*/
jacint@47: 		  
jacint@47: 		  excess.put(v, excess.get(v)+flow.get(e));
jacint@47: 		  excess.put(w, excess.get(w)-flow.get(e));
jacint@47: 		  flow.put(e,0);
jacint@47: 		  //std::cout << v <<" " << w << " vissza elen sat pump "     << std::endl;
jacint@47: 		  if (excess.get(w)==0) { break;}
jacint@47: 		} //if (flow.get(e) > excess.get(v)) 
jacint@47: 	      } //if(level.get(w)==level.get(v)+1)
jacint@47: 	      
jacint@47: 	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@47: 	      
jacint@47: 
jacint@47: 	    } //if (flow.get(e)>0)
jacint@47: 
jacint@47: 	  } //for
jacint@47: 
jacint@47: 
jacint@47: 	  if (excess.get(w)>0) {
jacint@47: 	    level.put(w,++newlevel);
jacint@47: 	    stack[newlevel].push(w);
jacint@47: 	    b=newlevel;
jacint@47: 	    //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl; 
jacint@47: 	  }
jacint@47: 
jacint@47: 
jacint@47: 	} //else
jacint@47:        
jacint@47:       } //while(b)
jacint@47: 
jacint@47:       value = excess.get(t);
jacint@47:       /*Max flow value.*/
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     } //void run()
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     /*
jacint@47:       Returns the maximum value of a flow.
jacint@47:      */
jacint@47: 
jacint@47:     T maxflow() {
jacint@47:       return value;
jacint@47:     }
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     /*
jacint@47:       For the maximum flow x found by the algorithm, it returns the flow value on edge e, i.e. x(e). 
jacint@47:     */
jacint@47: 
jacint@47:     T flowonedge(edge_iterator e) {
jacint@47:       return flow.get(e);
jacint@47:     }
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     /*
jacint@47:       Returns the maximum flow x found by the algorithm.
jacint@47:     */
jacint@47: 
jacint@47:     edge_property_vector<graph_type, T> allflow() {
jacint@47:       return flow;
jacint@47:     }
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47:     /*
jacint@47:       Returns a minimum cut by using a reverse bfs from t in the residual graph.
jacint@47:     */
jacint@47:     
jacint@47:     node_property_vector<graph_type, bool> mincut() {
jacint@47:     
jacint@47:       std::queue<node_iterator> queue;
jacint@47:       
jacint@47:       mincutvector.put(t,false);      
jacint@47:       queue.push(t);
jacint@47: 
jacint@47:       while (!queue.empty()) {
jacint@47:         node_iterator w=queue.front();
jacint@47: 	queue.pop();
jacint@47: 
jacint@47: 	for(in_edge_iterator e=G.first_in_edge(w) ; e.valid(); ++e) {
jacint@47: 	  node_iterator v=G.tail(e);
jacint@47: 	  if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@47: 	    queue.push(v);
jacint@47: 	    mincutvector.put(v, false);
jacint@47: 	  }
jacint@47: 	} // for
jacint@47: 
jacint@47: 	for(out_edge_iterator e=G.first_out_edge(w) ; e.valid(); ++e) {
jacint@47: 	  node_iterator v=G.head(e);
jacint@47: 	  if (mincutvector.get(v) && flow.get(e) > 0 ) {
jacint@47: 	    queue.push(v);
jacint@47: 	    mincutvector.put(v, false);
jacint@47: 	  }
jacint@47: 	} // for
jacint@47: 
jacint@47:       }
jacint@47: 
jacint@47:       return mincutvector;
jacint@47:     
jacint@47:     }
jacint@47: 
jacint@47: 
jacint@47:   };
jacint@47: }//namespace marci
jacint@47: #endif 
jacint@47: 
jacint@47: 
jacint@47: 
jacint@47: