jacint@72: // -*- C++ -*-
jacint@72: /*
jacint@72: preflow_push_hl.hh
jacint@72: by jacint. 
jacint@72: Runs the highest label variant of the preflow push algorithm with 
jacint@72: running time O(n^2\sqrt(m)). 
jacint@72: 
jacint@72: Member functions:
jacint@72: 
jacint@72: void run() : runs the algorithm
jacint@72: 
jacint@72:  The following functions should be used after run() was already run.
jacint@72: 
jacint@72: T maxflow() : returns the value of a maximum flow
jacint@72: 
jacint@72: T flowonEdge(Edge_iterator e) : for a fixed maximum flow x it returns x(e) 
jacint@72: 
jacint@72: EdgeMap<graph_type, T> allflow() : returns the fixed maximum flow x
jacint@72: 
jacint@72: NodeMap<graph_type, bool> mincut() : returns a 
jacint@72:      characteristic vector of a minimum cut. (An empty level 
jacint@72:      in the algorithm gives a minimum cut.)
jacint@72: */
jacint@72: 
jacint@72: #ifndef PREFLOW_PUSH_HL_H
jacint@72: #define PREFLOW_PUSH_HL_H
jacint@72: 
jacint@72: #include <algorithm>
jacint@72: #include <vector>
jacint@72: #include <stack>
jacint@72: 
jacint@72: #include <reverse_bfs.hh>
jacint@72: 
jacint@72: namespace marci {
jacint@72: 
jacint@72:   template <typename Graph, typename T, typename FlowMap, typename CapacityMap>
jacint@72:   class preflow_push_hl {
jacint@72:     
jacint@72:     typedef typename Graph::NodeIt NodeIt;
jacint@72:     typedef typename Graph::EdgeIt EdgeIt;
jacint@72:     typedef typename Graph::EachNodeIt EachNodeIt;
jacint@72:     typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@72:     typedef typename Graph::InEdgeIt InEdgeIt;
jacint@72:     typedef typename Graph::EachEdgeIt EachEdgeIt;
jacint@72:     
jacint@72: 
jacint@72:     Graph& G;
jacint@72:     NodeIt s;
jacint@72:     NodeIt t;
jacint@72:     Graph::EdgeMap<T> flow;
jacint@72:     Graph::EdgeMap<T> capacity; 
jacint@72:     T value;
jacint@72:     Graph::NodeMap<bool> mincutvector;
jacint@72: 
jacint@72:    
jacint@72:   public:
jacint@72: 
jacint@72:     preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t, 
jacint@72: 		    Graph::EdgeMap<T>& _capacity) :
jacint@72:       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), mincutvector(_G, true) { }
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     /*
jacint@72:       The run() function runs the highest label preflow-push, 
jacint@72:       running time: O(n^2\sqrt(m))
jacint@72:     */
jacint@72:     void run() {
jacint@72:  
jacint@72:       Graph::NodeMap<int> level(G);         //level of Node
jacint@72:       Graph::NodeMap<T> excess(G);          //excess of Node
jacint@72:             
jacint@72:       int n=G.nodeNum();                        //number of Nodes 
jacint@72:       int b=n; 
jacint@72:       /*b is a bound on the highest level of an active Node. In the beginning it is at most n-2.*/
jacint@72: 
jacint@72:       std::vector<std::stack<NodeIt> > stack(2*n-1);    //Stack of the active Nodes in level i.
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:       /*Reverse_bfs from t, to find the starting level.*/
jacint@72: 
jacint@72:       reverse_bfs<list_graph> bfs(G, t);
jacint@72:       bfs.run();
jacint@72:       for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) {
jacint@72: 	level.set(v, bfs.dist(v)); 
jacint@72: 	//std::cout << "the level of " << v << " is " << bfs.dist(v);
jacint@72:       }
jacint@72: 
jacint@72:       /*The level of s is fixed to n*/ 
jacint@72:       level.set(s,n);
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:       /* Starting flow. It is everywhere 0 at the moment. */
jacint@72:      
jacint@72:       for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) 
jacint@72: 	{
jacint@72: 	  NodeIt w=G.head(i);
jacint@72: 	  flow.set(i, capacity.get(i)); 
jacint@72: 	  stack[bfs.dist(w)].push(w); 
jacint@72: 	  excess.set(w, capacity.get(i));
jacint@72: 	}
jacint@72: 
jacint@72: 
jacint@72:       /* 
jacint@72: 	 End of preprocessing 
jacint@72:       */
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:       /*
jacint@72: 	Push/relabel on the highest level active Nodes.
jacint@72:       */
jacint@72: 	
jacint@72:       /*While there exists active Node.*/
jacint@72:       while (b) { 
jacint@72: 
jacint@72: 	/*We decrease the bound if there is no active Node of level b.*/
jacint@72: 	if (stack[b].empty()) {
jacint@72: 	  --b;
jacint@72: 	} else {
jacint@72: 
jacint@72: 	  NodeIt w=stack[b].top();    //w is the highest label active Node.
jacint@72: 	  stack[b].pop();                    //We delete w from the stack.
jacint@72: 	
jacint@72: 	  int newlevel=2*n-2;                   //In newlevel we maintain the next level of w.
jacint@72: 	
jacint@72: 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
jacint@72: 	    NodeIt v=G.head(e);
jacint@72: 	    /*e is the Edge wv.*/
jacint@72: 
jacint@72: 	    if (flow.get(e)<capacity.get(e)) {              
jacint@72: 	      /*e is an Edge of the residual graph */
jacint@72: 
jacint@72: 	      if(level.get(w)==level.get(v)+1) {      
jacint@72: 		/*Push is allowed now*/
jacint@72: 
jacint@72: 		if (capacity.get(e)-flow.get(e) > excess.get(w)) {       
jacint@72: 		  /*A nonsaturating push.*/
jacint@72: 		  
jacint@72: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@72: 		  /*v becomes active.*/
jacint@72: 		  
jacint@72: 		  flow.set(e, flow.get(e)+excess.get(w));
jacint@72: 		  excess.set(v, excess.get(v)+excess.get(w));
jacint@72: 		  excess.set(w,0);
jacint@72: 		  //std::cout << w << " " << v <<" elore elen nonsat pump "  << std::endl;
jacint@72: 		  break; 
jacint@72: 		} else { 
jacint@72: 		  /*A saturating push.*/
jacint@72: 
jacint@72: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@72: 		  /*v becomes active.*/
jacint@72: 
jacint@72: 		  excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
jacint@72: 		  excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
jacint@72: 		  flow.set(e, capacity.get(e));
jacint@72: 		  //std::cout << w<<" " <<v<<" elore elen sat pump "   << std::endl;
jacint@72: 		  if (excess.get(w)==0) break;
jacint@72: 		  /*If w is not active any more, then we go on to the next Node.*/
jacint@72: 		  
jacint@72: 		} // if (capacity.get(e)-flow.get(e) > excess.get(w))
jacint@72: 	      } // if(level.get(w)==level.get(v)+1)
jacint@72: 	    
jacint@72: 	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@72: 	    
jacint@72: 	    } //if (flow.get(e)<capacity.get(e))
jacint@72: 	 
jacint@72: 	  } //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e) 
jacint@72: 	  
jacint@72: 
jacint@72: 
jacint@72: 	  for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
jacint@72: 	    NodeIt v=G.tail(e);
jacint@72: 	    /*e is the Edge vw.*/
jacint@72: 
jacint@72: 	    if (excess.get(w)==0) break;
jacint@72: 	    /*It may happen, that w became inactive in the first for cycle.*/		
jacint@72: 	    if(flow.get(e)>0) {             
jacint@72: 	      /*e is an Edge of the residual graph */
jacint@72: 
jacint@72: 	      if(level.get(w)==level.get(v)+1) {  
jacint@72: 		/*Push is allowed now*/
jacint@72: 		
jacint@72: 		if (flow.get(e) > excess.get(w)) { 
jacint@72: 		  /*A nonsaturating push.*/
jacint@72: 		  
jacint@72: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@72: 		  /*v becomes active.*/
jacint@72: 
jacint@72: 		  flow.set(e, flow.get(e)-excess.get(w));
jacint@72: 		  excess.set(v, excess.get(v)+excess.get(w));
jacint@72: 		  excess.set(w,0);
jacint@72: 		  //std::cout << v << " " << w << " vissza elen nonsat pump "     << std::endl;
jacint@72: 		  break; 
jacint@72: 		} else {                                               
jacint@72: 		  /*A saturating push.*/
jacint@72: 		  
jacint@72: 		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
jacint@72: 		  /*v becomes active.*/
jacint@72: 		  
jacint@72: 		  excess.set(v, excess.get(v)+flow.get(e));
jacint@72: 		  excess.set(w, excess.get(w)-flow.get(e));
jacint@72: 		  flow.set(e,0);
jacint@72: 		  //std::cout << v <<" " << w << " vissza elen sat pump "     << std::endl;
jacint@72: 		  if (excess.get(w)==0) { break;}
jacint@72: 		} //if (flow.get(e) > excess.get(v)) 
jacint@72: 	      } //if(level.get(w)==level.get(v)+1)
jacint@72: 	      
jacint@72: 	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
jacint@72: 	      
jacint@72: 
jacint@72: 	    } //if (flow.get(e)>0)
jacint@72: 
jacint@72: 	  } //for
jacint@72: 
jacint@72: 
jacint@72: 	  if (excess.get(w)>0) {
jacint@72: 	    level.set(w,++newlevel);
jacint@72: 	    stack[newlevel].push(w);
jacint@72: 	    b=newlevel;
jacint@72: 	    //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl; 
jacint@72: 	  }
jacint@72: 
jacint@72: 
jacint@72: 	} //else
jacint@72:        
jacint@72:       } //while(b)
jacint@72: 
jacint@72:       value = excess.get(t);
jacint@72:       /*Max flow value.*/
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     } //void run()
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     /*
jacint@72:       Returns the maximum value of a flow.
jacint@72:      */
jacint@72: 
jacint@72:     T maxflow() {
jacint@72:       return value;
jacint@72:     }
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     /*
jacint@72:       For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). 
jacint@72:     */
jacint@72: 
jacint@72:     T flowonEdge(EdgeIt e) {
jacint@72:       return flow.get(e);
jacint@72:     }
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     /*
jacint@72:       Returns the maximum flow x found by the algorithm.
jacint@72:     */
jacint@72: 
jacint@72:     EdgeMap<graph_type, T> allflow() {
jacint@72:       return flow;
jacint@72:     }
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72:     /*
jacint@72:       Returns a minimum cut by using a reverse bfs from t in the residual graph.
jacint@72:     */
jacint@72:     
jacint@72:     NodeMap<graph_type, bool> mincut() {
jacint@72:     
jacint@72:       std::queue<NodeIt> queue;
jacint@72:       
jacint@72:       mincutvector.set(t,false);      
jacint@72:       queue.push(t);
jacint@72: 
jacint@72:       while (!queue.empty()) {
jacint@72:         NodeIt w=queue.front();
jacint@72: 	queue.pop();
jacint@72: 
jacint@72: 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@72: 	  NodeIt v=G.tail(e);
jacint@72: 	  if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@72: 	    queue.push(v);
jacint@72: 	    mincutvector.set(v, false);
jacint@72: 	  }
jacint@72: 	} // for
jacint@72: 
jacint@72: 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@72: 	  NodeIt v=G.head(e);
jacint@72: 	  if (mincutvector.get(v) && flow.get(e) > 0 ) {
jacint@72: 	    queue.push(v);
jacint@72: 	    mincutvector.set(v, false);
jacint@72: 	  }
jacint@72: 	} // for
jacint@72: 
jacint@72:       }
jacint@72: 
jacint@72:       return mincutvector;
jacint@72:     
jacint@72:     }
jacint@72: 
jacint@72: 
jacint@72:   };
jacint@72: }//namespace marci
jacint@72: #endif 
jacint@72: 
jacint@72: 
jacint@72: 
jacint@72: