jacint@388: // -*- C++ -*-
jacint@388: //The same as preflow.h, using ResGraphWrapper
jacint@388: #ifndef HUGO_PREFLOW_RES_H
jacint@388: #define HUGO_PREFLOW_RES_H
jacint@388: 
jacint@388: #define H0 20
jacint@388: #define H1 1
jacint@388: 
jacint@388: #include <vector>
jacint@388: #include <queue>
jacint@388: #include <graph_wrapper.h>
jacint@388: 
jacint@388: #include<iostream>
jacint@388: 
jacint@388: namespace hugo {
jacint@388: 
jacint@388:   template <typename Graph, typename T, 
marci@392: 	    typename CapMap=typename Graph::template EdgeMap<T>, 
marci@392:             typename FlowMap=typename Graph::template EdgeMap<T> >
jacint@388:   class PreflowRes {
jacint@388:     
jacint@388:     typedef typename Graph::Node Node;
jacint@388:     typedef typename Graph::Edge Edge;
jacint@388:     typedef typename Graph::NodeIt NodeIt;
jacint@388:     typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@388:     typedef typename Graph::InEdgeIt InEdgeIt;
jacint@388:     
jacint@388:     const Graph& G;
jacint@388:     Node s;
jacint@388:     Node t;
jacint@388:     const CapMap& capacity;  
jacint@388:     FlowMap& flow;
jacint@388:     T value;
jacint@388:     bool constzero;
jacint@388: 
jacint@388:     typedef ResGraphWrapper<const Graph, T, CapMap, FlowMap> ResGW;
jacint@388:     typedef typename ResGW::OutEdgeIt ResOutEdgeIt;
jacint@388:     typedef typename ResGW::InEdgeIt ResInEdgeIt;
jacint@388:     typedef typename ResGW::Edge ResEdge;
jacint@388:  
jacint@388:   public:
jacint@388:     PreflowRes(Graph& _G, Node _s, Node _t, CapMap& _capacity, 
jacint@388: 	    FlowMap& _flow, bool _constzero ) :
jacint@388:       G(_G), s(_s), t(_t), capacity(_capacity), flow(_flow), constzero(_constzero) {}
jacint@388:     
jacint@388:     
jacint@388:     void run() {
jacint@388: 
jacint@388:       ResGW res_graph(G, capacity, flow);
jacint@388: 
jacint@388:       value=0;                //for the subsequent runs
jacint@388: 
jacint@388:       bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
jacint@388:       int n=G.nodeNum(); 
jacint@388:       int heur0=(int)(H0*n);  //time while running 'bound decrease' 
jacint@388:       int heur1=(int)(H1*n);  //time while running 'highest label'
jacint@388:       int heur=heur1;         //starting time interval (#of relabels)
jacint@388:       bool what_heur=1;       
jacint@388:       /*
jacint@388: 	what_heur is 0 in case 'bound decrease' 
jacint@388: 	and 1 in case 'highest label'
jacint@388:       */
jacint@388:       bool end=false;     
jacint@388:       /*
jacint@388: 	Needed for 'bound decrease', 'true'
jacint@388: 	means no active nodes are above bound b.
jacint@388:       */
jacint@388:       int relabel=0;
jacint@388:       int k=n-2;  //bound on the highest level under n containing a node
jacint@388:       int b=k;    //bound on the highest level under n of an active node
jacint@388:       
marci@392:       typename Graph::template NodeMap<int> level(G,n);      
marci@392:       typename Graph::template NodeMap<T> excess(G); 
jacint@388: 
jacint@388:       std::vector<Node> active(n-1,INVALID);
marci@392:       typename Graph::template NodeMap<Node> next(G,INVALID);
jacint@388:       //Stack of the active nodes in level i < n.
jacint@388:       //We use it in both phases.
jacint@388: 
marci@392:       typename Graph::template NodeMap<Node> left(G,INVALID);
marci@392:       typename Graph::template NodeMap<Node> right(G,INVALID);
jacint@388:       std::vector<Node> level_list(n,INVALID);
jacint@388:       /*
jacint@388: 	List of the nodes in level i<n.
jacint@388:       */
jacint@388: 
jacint@388: 
jacint@388:       /*
jacint@388: 	Reverse_bfs from t in the residual graph, 
jacint@388: 	to find the starting level.
jacint@388:       */
jacint@388:       level.set(t,0);
jacint@388:       std::queue<Node> bfs_queue;
jacint@388:       bfs_queue.push(t);
jacint@388:       
jacint@388:       while (!bfs_queue.empty()) {
jacint@388: 	
jacint@388: 	Node v=bfs_queue.front();	
jacint@388: 	bfs_queue.pop();
jacint@388: 	int l=level[v]+1;
jacint@388: 	
jacint@388: 	ResInEdgeIt e;
jacint@388: 	for(res_graph.first(e,v); res_graph.valid(e); 
jacint@388: 	    res_graph.next(e)) {
jacint@388: 	  Node w=res_graph.tail(e);
jacint@388: 	  if ( level[w] == n && w != s ) {
jacint@388: 	    bfs_queue.push(w);
jacint@388: 	    Node first=level_list[l];
jacint@388: 	    if ( G.valid(first) ) left.set(first,w);
jacint@388: 	    right.set(w,first);
jacint@388: 	    level_list[l]=w;
jacint@388: 	    level.set(w, l);
jacint@388: 	  }
jacint@388: 	}
jacint@388:       }
jacint@388:       
jacint@388: 	
jacint@388:       if ( !constzero ) {
jacint@388: 	/*
jacint@388: 	  Counting the excess
jacint@388: 	*/
jacint@388: 	NodeIt v;
jacint@388: 	for(G.first(v); G.valid(v); G.next(v)) {
jacint@388: 	  T exc=0;
jacint@388: 
jacint@388: 	  InEdgeIt e;
jacint@388: 	  for(G.first(e,v); G.valid(e); G.next(e)) exc+=flow[e];
jacint@388: 	  OutEdgeIt f;
jacint@444: 	  for(G.first(f,v); G.valid(f); G.next(f)) exc-=flow[f];
jacint@388: 
jacint@388: 	  excess.set(v,exc);	  
jacint@388: 
jacint@388: 	  //putting the active nodes into the stack
jacint@388: 	  int lev=level[v];
jacint@388: 	  if ( exc > 0 && lev < n ) {
jacint@388: 	    next.set(v,active[lev]);
jacint@388: 	    active[lev]=v;
jacint@388: 	  }
jacint@388: 	}
jacint@388:       }
jacint@388:      
jacint@388: 
jacint@388: 
jacint@388:       //the starting flow
jacint@388:       ResOutEdgeIt e;
jacint@388:       for(res_graph.first(e,s); res_graph.valid(e); 
jacint@388: 	  res_graph.next(e)) {
jacint@388: 	  Node w=res_graph.head(e);
jacint@388: 	  if ( level[w] < n ) {	  
jacint@388: 	    if ( excess[w] == 0 && w!=t ) {
jacint@388: 	      next.set(w,active[level[w]]);
jacint@388: 	      active[level[w]]=w;
jacint@388: 	    }
jacint@388: 	    T rem=res_graph.resCap(e);
jacint@388: 	    excess.set(w, excess[w]+rem);
jacint@388: 	    res_graph.augment(e, rem ); 
jacint@388: 	  }
jacint@388:       }
jacint@388: 	
jacint@388: 
jacint@388:       /* 
jacint@388: 	 End of preprocessing 
jacint@388:       */
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388:       /*
jacint@388: 	Push/relabel on the highest level active nodes.
jacint@388:       */	
jacint@388:       while ( true ) {
jacint@388: 	
jacint@388: 	if ( b == 0 ) {
jacint@388: 	  if ( phase ) break;
jacint@388: 	  
jacint@388: 	  if ( !what_heur && !end && k > 0 ) {
jacint@388: 	    b=k;
jacint@388: 	    end=true;
jacint@388: 	  } else {
jacint@388: 	    phase=1;
jacint@388: 	    level.set(s,0);
jacint@388: 	    std::queue<Node> bfs_queue;
jacint@388: 	    bfs_queue.push(s);
jacint@388: 	    
jacint@388: 	    while (!bfs_queue.empty()) {
jacint@388: 	      
jacint@388: 	      Node v=bfs_queue.front();	
jacint@388: 	      bfs_queue.pop();
jacint@388: 	      int l=level[v]+1;
jacint@388: 	      
jacint@388: 	      ResInEdgeIt e;
jacint@388: 	      for(res_graph.first(e,v); 
jacint@388: 		  res_graph.valid(e); res_graph.next(e)) {
jacint@388: 		Node u=res_graph.tail(e);
jacint@388: 		if ( level[u] >= n ) { 
jacint@388: 		  bfs_queue.push(u);
jacint@388: 		  level.set(u, l);
jacint@388: 		  if ( excess[u] > 0 ) {
jacint@388: 		    next.set(u,active[l]);
jacint@388: 		    active[l]=u;
jacint@388: 		  }
jacint@388: 		}
jacint@388: 	      }
jacint@388: 	    
jacint@388: 	    }
jacint@388: 	    b=n-2;
jacint@388: 	  }
jacint@388: 	    
jacint@388: 	}
jacint@388: 	  
jacint@388: 	  
jacint@388: 	if ( !G.valid(active[b]) ) --b; 
jacint@388: 	else {
jacint@388: 	  end=false;  
jacint@388: 
jacint@388: 	  Node w=active[b];
jacint@388: 	  active[b]=next[w];
jacint@388: 	  int lev=level[w];
jacint@388: 	  T exc=excess[w];
jacint@388: 	  int newlevel=n;       //bound on the next level of w
jacint@388: 	  
jacint@388: 	  ResOutEdgeIt e;
jacint@388: 	  for(res_graph.first(e,w); res_graph.valid(e); res_graph.next(e)) {
jacint@388: 	    
jacint@388: 	    Node v=res_graph.head(e);            
jacint@388: 	    if( lev > level[v] ) {      
jacint@388: 	      /*Push is allowed now*/
jacint@388: 	      
jacint@388: 	      if ( excess[v]==0 && v!=t && v!=s ) {
jacint@388: 		int lev_v=level[v];
jacint@388: 		next.set(v,active[lev_v]);
jacint@388: 		active[lev_v]=v;
jacint@388: 	      }
jacint@388: 	      
jacint@388: 	      T remcap=res_graph.resCap(e);
jacint@388: 	      
jacint@388: 	      if ( remcap >= exc ) {       
jacint@388: 		/*A nonsaturating push.*/
jacint@388: 		res_graph.augment(e, exc);
jacint@388: 		excess.set(v, excess[v]+exc);
jacint@388: 		exc=0;
jacint@388: 		break; 
jacint@388: 		
jacint@388: 	      } else { 
jacint@388: 		/*A saturating push.*/
jacint@388: 		
jacint@388: 		res_graph.augment(e, remcap);
jacint@388: 		excess.set(v, excess[v]+remcap);
jacint@388: 		exc-=remcap;
jacint@388: 	      }
jacint@388: 	    } else if ( newlevel > level[v] ){
jacint@388: 	      newlevel = level[v];
jacint@388: 	    }	    
jacint@388: 	    
jacint@388: 	  }
jacint@388: 
jacint@388: 	excess.set(w, exc);
jacint@388: 	 
jacint@388: 	/*
jacint@388: 	  Relabel
jacint@388: 	*/
jacint@388: 	
jacint@388: 
jacint@388: 	if ( exc > 0 ) {
jacint@388: 	  //now 'lev' is the old level of w
jacint@388: 	
jacint@388: 	  if ( phase ) {
jacint@388: 	    level.set(w,++newlevel);
jacint@388: 	    next.set(w,active[newlevel]);
jacint@388: 	    active[newlevel]=w;
jacint@388: 	    b=newlevel;
jacint@388: 	  } else {
jacint@388: 	    //unlacing starts
jacint@388: 	    Node right_n=right[w];
jacint@388: 	    Node left_n=left[w];
jacint@388: 
jacint@388: 	    if ( G.valid(right_n) ) {
jacint@388: 	      if ( G.valid(left_n) ) {
jacint@388: 		right.set(left_n, right_n);
jacint@388: 		left.set(right_n, left_n);
jacint@388: 	      } else {
jacint@388: 		level_list[lev]=right_n;   
jacint@388: 		left.set(right_n, INVALID);
jacint@388: 	      } 
jacint@388: 	    } else {
jacint@388: 	      if ( G.valid(left_n) ) {
jacint@388: 		right.set(left_n, INVALID);
jacint@388: 	      } else { 
jacint@388: 		level_list[lev]=INVALID;   
jacint@388: 	      } 
jacint@388: 	    } 
jacint@388: 	    //unlacing ends
jacint@388: 		
jacint@388: 	    if ( !G.valid(level_list[lev]) ) {
jacint@388: 	      
jacint@388: 	       //gapping starts
jacint@388: 	      for (int i=lev; i!=k ; ) {
jacint@388: 		Node v=level_list[++i];
jacint@388: 		while ( G.valid(v) ) {
jacint@388: 		  level.set(v,n);
jacint@388: 		  v=right[v];
jacint@388: 		}
jacint@388: 		level_list[i]=INVALID;
jacint@388: 		if ( !what_heur ) active[i]=INVALID;
jacint@388: 	      }	     
jacint@388: 
jacint@388: 	      level.set(w,n);
jacint@388: 	      b=lev-1;
jacint@388: 	      k=b;
jacint@388: 	      //gapping ends
jacint@388: 	    
jacint@388: 	    } else {
jacint@388: 	      
jacint@388: 	      if ( newlevel == n ) level.set(w,n); 
jacint@388: 	      else {
jacint@388: 		level.set(w,++newlevel);
jacint@388: 		next.set(w,active[newlevel]);
jacint@388: 		active[newlevel]=w;
jacint@388: 		if ( what_heur ) b=newlevel;
jacint@388: 		if ( k < newlevel ) ++k;      //now k=newlevel
jacint@388: 		Node first=level_list[newlevel];
jacint@388: 		if ( G.valid(first) ) left.set(first,w);
jacint@388: 		right.set(w,first);
jacint@388: 		left.set(w,INVALID);
jacint@388: 		level_list[newlevel]=w;
jacint@388: 	      }
jacint@388: 	    }
jacint@388: 
jacint@388: 
jacint@388: 	    ++relabel; 
jacint@388: 	    if ( relabel >= heur ) {
jacint@388: 	      relabel=0;
jacint@388: 	      if ( what_heur ) {
jacint@388: 		what_heur=0;
jacint@388: 		heur=heur0;
jacint@388: 		end=false;
jacint@388: 	      } else {
jacint@388: 		what_heur=1;
jacint@388: 		heur=heur1;
jacint@388: 		b=k; 
jacint@388: 	      }
jacint@388: 	    }
jacint@388: 	  } //phase 0
jacint@388: 	  
jacint@388: 	  
jacint@388: 	} // if ( exc > 0 )
jacint@388: 	  
jacint@388: 	
jacint@388: 	}  // if stack[b] is nonempty
jacint@388: 	
jacint@388:       } // while(true)
jacint@388: 
jacint@388: 
jacint@388:       value = excess[t];
jacint@388:       /*Max flow value.*/
jacint@388:      
jacint@388:     } //void run()
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388:     /*
jacint@388:       Returns the maximum value of a flow.
jacint@388:      */
jacint@388: 
jacint@388:     T flowValue() {
jacint@388:       return value;
jacint@388:     }
jacint@388: 
jacint@388: 
jacint@388:     FlowMap Flow() {
jacint@388:       return flow;
jacint@388:       }
jacint@388: 
jacint@388: 
jacint@388:     
jacint@388:     void Flow(FlowMap& _flow ) {
jacint@388:       NodeIt v;
jacint@388:       for(G.first(v) ; G.valid(v); G.next(v))
jacint@388: 	_flow.set(v,flow[v]);
jacint@388:     }
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388:     /*
jacint@388:       Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@388:     */
jacint@388:    
jacint@388:     template<typename _CutMap>
jacint@388:     void minMinCut(_CutMap& M) {
jacint@388:     
jacint@388:       std::queue<Node> queue;
jacint@388:       
jacint@388:       M.set(s,true);      
jacint@388:       queue.push(s);
jacint@388: 
jacint@388:       while (!queue.empty()) {
jacint@388:         Node w=queue.front();
jacint@388: 	queue.pop();
jacint@388: 
jacint@388: 	OutEdgeIt e;
jacint@388: 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@388: 	  Node v=G.head(e);
jacint@388: 	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@388: 	    queue.push(v);
jacint@388: 	    M.set(v, true);
jacint@388: 	  }
jacint@388: 	} 
jacint@388: 
jacint@388: 	InEdgeIt f;
jacint@388: 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@388: 	  Node v=G.tail(f);
jacint@388: 	  if (!M[v] && flow[f] > 0 ) {
jacint@388: 	    queue.push(v);
jacint@388: 	    M.set(v, true);
jacint@388: 	  }
jacint@388: 	} 
jacint@388:       }
jacint@388:     }
jacint@388: 
jacint@388: 
jacint@388:   
jacint@388:     /*
jacint@388:       Returns the maximum min cut, by a reverse bfs 
jacint@388:       from t in the residual graph.
jacint@388:     */
jacint@388:     
jacint@388:     template<typename _CutMap>
jacint@388:     void maxMinCut(_CutMap& M) {
jacint@388:     
jacint@388:       std::queue<Node> queue;
jacint@388:       
jacint@388:       M.set(t,true);        
jacint@388:       queue.push(t);
jacint@388: 
jacint@388:       while (!queue.empty()) {
jacint@388:         Node w=queue.front();
jacint@388: 	queue.pop();
jacint@388: 
jacint@388: 
jacint@388: 	InEdgeIt e;
jacint@388: 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@388: 	  Node v=G.tail(e);
jacint@388: 	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@388: 	    queue.push(v);
jacint@388: 	    M.set(v, true);
jacint@388: 	  }
jacint@388: 	}
jacint@388: 	
jacint@388: 	OutEdgeIt f;
jacint@388: 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@388: 	  Node v=G.head(f);
jacint@388: 	  if (!M[v] && flow[f] > 0 ) {
jacint@388: 	    queue.push(v);
jacint@388: 	    M.set(v, true);
jacint@388: 	  }
jacint@388: 	}
jacint@388:       }
jacint@388: 
jacint@388:       NodeIt v;
jacint@388:       for(G.first(v) ; G.valid(v); G.next(v)) {
jacint@388: 	M.set(v, !M[v]);
jacint@388:       }
jacint@388: 
jacint@388:     }
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388:     template<typename CutMap>
jacint@388:     void minCut(CutMap& M) {
jacint@388:       minMinCut(M);
jacint@388:     }
jacint@388: 
jacint@388:     
jacint@444:     
jacint@444:     void resetTarget (Node _t) {t=_t;}
jacint@444:     void resetSource (Node _s) {s=_s;}
jacint@388:    
jacint@444:     void resetCap (CapMap _cap) {capacity=_cap;}
jacint@388: 
jacint@444:     void resetFlow (FlowMap _flow, bool _constzero) {
jacint@388:       flow=_flow;
jacint@388:       constzero=_constzero;
jacint@388:     }
jacint@388: 
jacint@388: 
jacint@388:   };
jacint@388: 
jacint@388: } //namespace hugo
jacint@388: 
marci@390: #endif //HUGO_PREFLOW_RES_H
jacint@388: 
jacint@388: 
jacint@388: 
jacint@388: