/*
preflow_push_hl.hh
by jacint. 
Runs the highest label variant of the preflow push algorithm with 
running time O(n^2\sqrt(m)). 

Member functions:

void run() : runs the algorithm

 The following functions should be used after run() was already run.

T maxflow() : returns the value of a maximum flow

T flowonedge(edge_iterator e) : for a fixed maximum flow x it returns x(e) 

edge_property_vector<graph_type, T> allflow() : returns the fixed maximum flow x

node_property_vector<graph_type, bool> mincut() : returns a 
     characteristic vector of a minimum cut. (An empty level 
     in the algorithm gives a minimum cut.)
*/

#ifndef PREFLOW_PUSH_HL_HH
#define PREFLOW_PUSH_HL_HH

#include <algorithm>
#include <vector>
#include <stack>

#include <marci_graph_traits.hh>
#include <marci_property_vector.hh>
#include <reverse_bfs.hh>

namespace marci {

  template <typename graph_type, typename T>
  class preflow_push_hl {
    
    typedef typename graph_traits<graph_type>::node_iterator node_iterator;
    typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
    typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
    typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
    typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
    typedef typename graph_traits<graph_type>::each_edge_iterator each_edge_iterator;
    

    graph_type& G;
    node_iterator s;
    node_iterator t;
    edge_property_vector<graph_type, T> flow;
    edge_property_vector<graph_type, T>& capacity; 
    T value;
    node_property_vector<graph_type, bool> mincutvector;

   
  public:

    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) { }




    /*
      The run() function runs the highest label preflow-push, 
      running time: O(n^2\sqrt(m))
    */
    void run() {
 
      node_property_vector<graph_type, int> level(G);         //level of node
      node_property_vector<graph_type, T> excess(G);          //excess of node
            
      int n=number_of(G.first_node());                        //number of nodes 
      int b=n; 
      /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/

      std::vector<std::stack<node_iterator> > stack(2*n-1);    //Stack of the active nodes in level i.




      /*Reverse_bfs from t, to find the starting level.*/

      reverse_bfs<list_graph> bfs(G, t);
      bfs.run();
      for(each_node_iterator v=G.first_node(); v.is_valid(); ++v) {
	level.put(v, bfs.dist(v)); 
	//std::cout << "the level of " << v << " is " << bfs.dist(v);
      }

      /*The level of s is fixed to n*/ 
      level.put(s,n);





      /* Starting flow. It is everywhere 0 at the moment. */
     
      for(out_edge_iterator i=G.first_out_edge(s); i.is_valid(); ++i) 
	{
	  node_iterator w=G.head(i);
	  flow.put(i, capacity.get(i)); 
	  stack[bfs.dist(w)].push(w); 
	  excess.put(w, capacity.get(i));
	}


      /* 
	 End of preprocessing 
      */



      /*
	Push/relabel on the highest level active nodes.
      */
	
      /*While there exists active node.*/
      while (b) { 

	/*We decrease the bound if there is no active node of level b.*/
	if (stack[b].empty()) {
	  --b;
	} else {

	  node_iterator w=stack[b].top();    //w is the highest label active node.
	  stack[b].pop();                    //We delete w from the stack.
	
	  int newlevel=2*n-2;                   //In newlevel we maintain the next level of w.
	
	  for(out_edge_iterator e=G.first_out_edge(w); e.is_valid(); ++e) {
	    node_iterator v=G.head(e);
	    /*e is the edge wv.*/

	    if (flow.get(e)<capacity.get(e)) {              
	      /*e is an edge of the residual graph */

	      if(level.get(w)==level.get(v)+1) {      
		/*Push is allowed now*/

		if (capacity.get(e)-flow.get(e) > excess.get(w)) {       
		  /*A nonsaturating push.*/
		  
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
		  /*v becomes active.*/
		  
		  flow.put(e, flow.get(e)+excess.get(w));
		  excess.put(v, excess.get(v)+excess.get(w));
		  excess.put(w,0);
		  //std::cout << w << " " << v <<" elore elen nonsat pump "  << std::endl;
		  break; 
		} else { 
		  /*A saturating push.*/

		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
		  /*v becomes active.*/

		  excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e));
		  excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e));
		  flow.put(e, capacity.get(e));
		  //std::cout << w<<" " <<v<<" elore elen sat pump "   << std::endl;
		  if (excess.get(w)==0) break;
		  /*If w is not active any more, then we go on to the next node.*/
		  
		} // if (capacity.get(e)-flow.get(e) > excess.get(w))
	      } // if(level.get(w)==level.get(v)+1)
	    
	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
	    
	    } //if (flow.get(e)<capacity.get(e))
	 
	  } //for(out_edge_iterator e=G.first_out_edge(w); e.is_valid(); ++e) 
	  


	  for(in_edge_iterator e=G.first_in_edge(w); e.is_valid(); ++e) {
	    node_iterator v=G.tail(e);
	    /*e is the edge vw.*/

	    if (excess.get(w)==0) break;
	    /*It may happen, that w became inactive in the first for cycle.*/		
	    if(flow.get(e)>0) {             
	      /*e is an edge of the residual graph */

	      if(level.get(w)==level.get(v)+1) {  
		/*Push is allowed now*/
		
		if (flow.get(e) > excess.get(w)) { 
		  /*A nonsaturating push.*/
		  
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
		  /*v becomes active.*/

		  flow.put(e, flow.get(e)-excess.get(w));
		  excess.put(v, excess.get(v)+excess.get(w));
		  excess.put(w,0);
		  //std::cout << v << " " << w << " vissza elen nonsat pump "     << std::endl;
		  break; 
		} else {                                               
		  /*A saturating push.*/
		  
		  if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); 
		  /*v becomes active.*/
		  
		  excess.put(v, excess.get(v)+flow.get(e));
		  excess.put(w, excess.get(w)-flow.get(e));
		  flow.put(e,0);
		  //std::cout << v <<" " << w << " vissza elen sat pump "     << std::endl;
		  if (excess.get(w)==0) { break;}
		} //if (flow.get(e) > excess.get(v)) 
	      } //if(level.get(w)==level.get(v)+1)
	      
	      else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
	      

	    } //if (flow.get(e)>0)

	  } //for


	  if (excess.get(w)>0) {
	    level.put(w,++newlevel);
	    stack[newlevel].push(w);
	    b=newlevel;
	    //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl; 
	  }


	} //else
       
      } //while(b)

      value = excess.get(t);
      /*Max flow value.*/




    } //void run()





    /*
      Returns the maximum value of a flow.
     */

    T maxflow() {
      return value;
    }



    /*
      For the maximum flow x found by the algorithm, it returns the flow value on edge e, i.e. x(e). 
    */

    T flowonedge(edge_iterator e) {
      return flow.get(e);
    }



    /*
      Returns the maximum flow x found by the algorithm.
    */

    edge_property_vector<graph_type, T> allflow() {
      return flow;
    }



    /*
      Returns a minimum cut by using a reverse bfs from t in the residual graph.
    */
    
    node_property_vector<graph_type, bool> mincut() {
    
      std::queue<node_iterator> queue;
      
      mincutvector.put(t,false);      
      queue.push(t);

      while (!queue.empty()) {
        node_iterator w=queue.front();
	queue.pop();

	for(in_edge_iterator e=G.first_in_edge(w) ; e.is_valid(); ++e) {
	  node_iterator v=G.tail(e);
	  if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
	    queue.push(v);
	    mincutvector.put(v, false);
	  }
	} // for

	for(out_edge_iterator e=G.first_out_edge(w) ; e.is_valid(); ++e) {
	  node_iterator v=G.head(e);
	  if (mincutvector.get(v) && flow.get(e) > 0 ) {
	    queue.push(v);
	    mincutvector.put(v, false);
	  }
	} // for

      }

      return mincutvector;
    
    }


  };
}//namespace marci
#endif 




