jacint@109: // -*- C++ -*-
jacint@109: /*
jacint@109: preflow_hl0.h
jacint@109: by jacint. 
jacint@109: Heuristics: 
jacint@109:  2 phase
jacint@109:  gap
jacint@109:  list 'level_list' on the nodes on level i implemented by hand
jacint@109:  stack 'active' on the active nodes on level i implemented by hand
jacint@109:  bound decrease
jacint@109:  
jacint@109: The bound decrease heuristic behaves unexpectedly well.
jacint@109: 
jacint@109: The constructor runs the algorithm.
jacint@109: 
jacint@109: Members:
jacint@109: 
jacint@109: T maxFlow() : returns the value of a maximum flow
jacint@109: 
jacint@109: T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
jacint@109: 
jacint@109: FlowMap Flow() : returns the fixed maximum flow x
jacint@109: 
jacint@109: void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@109:      minimum min cut. M should be a map of bools initialized to false.
jacint@109: 
jacint@109: void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@109:      maximum min cut. M should be a map of bools initialized to false.
jacint@109: 
jacint@109: 
jacint@109: void minCut(CutMap& M) : sets M to the characteristic vector of 
jacint@109:      a min cut. M should be a map of bools initialized to false.
jacint@109: 
jacint@109: */
jacint@109: 
jacint@109: #ifndef PREFLOW_HL0_H
jacint@109: #define PREFLOW_HL0_H
jacint@109: 
jacint@109: #include <vector>
jacint@109: #include <queue>
jacint@109: 
jacint@109: #include <time_measure.h> //for test
jacint@109: 
jacint@109: namespace hugo {
jacint@109: 
jacint@109:   template <typename Graph, typename T, 
jacint@109:     typename FlowMap=typename Graph::EdgeMap<T>,
jacint@109:     typename CapMap=typename Graph::EdgeMap<T> >
jacint@109:   class preflow_hl0 {
jacint@109:     
jacint@109:     typedef typename Graph::NodeIt NodeIt;
jacint@109:     typedef typename Graph::EdgeIt EdgeIt;
jacint@109:     typedef typename Graph::EachNodeIt EachNodeIt;
jacint@109:     typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@109:     typedef typename Graph::InEdgeIt InEdgeIt;
jacint@109:     
jacint@109:     Graph& G;
jacint@109:     NodeIt s;
jacint@109:     NodeIt t;
jacint@109:     FlowMap flow;
jacint@109:     CapMap& capacity;
jacint@109:     T value;
jacint@109: 
jacint@109:   public:
jacint@109:     double time;    
jacint@109:     
jacint@109:     preflow_hl0(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
jacint@109:       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) {
jacint@109: 
jacint@109:       bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
jacint@109:       int n=G.nodeNum(); 
jacint@109:       bool end=false;     
jacint@109:       /*
jacint@109: 	'true' means no active nodes are above bound b.
jacint@109:       */
jacint@109:       int k=n-2;  //bound on the highest level under n containing a node
jacint@109:       int b=k;    //bound on the highest level under n of an active node
jacint@109:       /*
jacint@109: 	b is a bound on the highest level of the stack. 
jacint@109: 	k is a bound on the highest nonempty level i < n.
jacint@109:       */
jacint@109: 
jacint@109:       typename Graph::NodeMap<int> level(G,n);      
jacint@109:       typename Graph::NodeMap<T> excess(G); 
jacint@109: 
jacint@109:       std::vector<NodeIt> active(n);
jacint@109:       typename Graph::NodeMap<NodeIt> next(G);
jacint@109:       //Stack of the active nodes in level i < n.
jacint@109:       //We use it in both phases.
jacint@109: 
jacint@109:       typename Graph::NodeMap<NodeIt> left(G);
jacint@109:       typename Graph::NodeMap<NodeIt> right(G);
jacint@109:       std::vector<NodeIt> level_list(n);
jacint@109:       /*
jacint@109: 	List of the nodes in level i<n.
jacint@109:       */
jacint@109: 
jacint@109:       /*Reverse_bfs from t, to find the starting level.*/
jacint@109:       level.set(t,0);
jacint@109:       std::queue<NodeIt> bfs_queue;
jacint@109:       bfs_queue.push(t);
jacint@109: 
jacint@109:       while (!bfs_queue.empty()) {
jacint@109: 
jacint@109: 	NodeIt v=bfs_queue.front();	
jacint@109: 	bfs_queue.pop();
jacint@109: 	int l=level.get(v)+1;
jacint@109: 
jacint@109: 	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@109: 	  NodeIt w=G.tail(e);
jacint@109: 	  if ( level.get(w) == n && w != s ) {
jacint@109: 	    bfs_queue.push(w);
jacint@109: 	    NodeIt first=level_list[l];
jacint@109: 	    if ( first != 0 ) left.set(first,w);
jacint@109: 	    right.set(w,first);
jacint@109: 	    level_list[l]=w;
jacint@109: 	    level.set(w, l);
jacint@109: 	  }
jacint@109: 	}
jacint@109:       }
jacint@109:       
jacint@109:       level.set(s,n);
jacint@109:       
jacint@109: 
jacint@109:       /* Starting flow. It is everywhere 0 at the moment. */     
jacint@109:       for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
jacint@109: 	{
jacint@109: 	  T c=capacity.get(e);
jacint@109: 	  if ( c == 0 ) continue;
jacint@109: 	  NodeIt w=G.head(e);
jacint@109: 	  if ( level.get(w) < n ) {	  
jacint@109: 	    if ( excess.get(w) == 0 && w!=t ) {
jacint@109: 	      next.set(w,active[level.get(w)]);
jacint@109: 	      active[level.get(w)]=w;
jacint@109: 	    }
jacint@109: 	    flow.set(e, c); 
jacint@109: 	    excess.set(w, excess.get(w)+c);
jacint@109: 	  }
jacint@109: 	}
jacint@109: 
jacint@109:       /* 
jacint@109: 	 End of preprocessing 
jacint@109:       */
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:       /*
jacint@109: 	Push/relabel on the highest level active nodes.
jacint@109:       */	
jacint@109:       while ( true ) {
jacint@109: 	
jacint@109: 	if ( b == 0 ) {
jacint@109: 	  if ( phase ) break;
jacint@109: 	  
jacint@109: 	  if ( !end && k > 0 ) {
jacint@109: 	    b=k;
jacint@109: 	    end=true;
jacint@109: 	  } else {
jacint@109: 	    phase=1;
jacint@109: 	    time=currTime();
jacint@109: 	    level.set(s,0);
jacint@109: 	    std::queue<NodeIt> bfs_queue;
jacint@109: 	    bfs_queue.push(s);
jacint@109: 	    
jacint@109: 	    while (!bfs_queue.empty()) {
jacint@109: 	      
jacint@109: 	      NodeIt v=bfs_queue.front();	
jacint@109: 	      bfs_queue.pop();
jacint@109: 	      int l=level.get(v)+1;
jacint@109: 	      
jacint@109: 	      for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
jacint@109: 		if ( capacity.get(e) == flow.get(e) ) continue;
jacint@109: 		NodeIt u=G.tail(e);
jacint@109: 		if ( level.get(u) >= n ) { 
jacint@109: 		  bfs_queue.push(u);
jacint@109: 		  level.set(u, l);
jacint@109: 		  if ( excess.get(u) > 0 ) {
jacint@109: 		    next.set(u,active[l]);
jacint@109: 		    active[l]=u;
jacint@109: 		  }
jacint@109: 		}
jacint@109: 	      }
jacint@109: 	    
jacint@109: 	      for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
jacint@109: 		if ( 0 == flow.get(e) ) continue;
jacint@109: 		NodeIt u=G.head(e);
jacint@109: 		if ( level.get(u) >= n ) { 
jacint@109: 		  bfs_queue.push(u);
jacint@109: 		  level.set(u, l);
jacint@109: 		  if ( excess.get(u) > 0 ) {
jacint@109: 		    next.set(u,active[l]);
jacint@109: 		    active[l]=u;
jacint@109: 		  }
jacint@109: 		}
jacint@109: 	      }
jacint@109: 	    }
jacint@109: 	    b=n-2;
jacint@109: 	    }
jacint@109: 	    
jacint@109: 	}
jacint@109: 	  
jacint@109: 	  
jacint@109: 	if ( active[b] == 0 ) --b; 
jacint@109: 	else {
jacint@109: 	  end=false;  
jacint@109: 
jacint@109: 	  NodeIt w=active[b];
jacint@109: 	  active[b]=next.get(w);
jacint@109: 	  int lev=level.get(w);
jacint@109: 	  T exc=excess.get(w);
jacint@109: 	  int newlevel=n;       //bound on the next level of w
jacint@109: 	  
jacint@109: 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
jacint@109: 	    
jacint@109: 	    if ( flow.get(e) == capacity.get(e) ) continue; 
jacint@109: 	    NodeIt v=G.head(e);            
jacint@109: 	    //e=wv	    
jacint@109: 	    
jacint@109: 	    if( lev > level.get(v) ) {      
jacint@109: 	      /*Push is allowed now*/
jacint@109: 	      
jacint@109: 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
jacint@109: 		int lev_v=level.get(v);
jacint@109: 		next.set(v,active[lev_v]);
jacint@109: 		active[lev_v]=v;
jacint@109: 	      }
jacint@109: 	      
jacint@109: 	      T cap=capacity.get(e);
jacint@109: 	      T flo=flow.get(e);
jacint@109: 	      T remcap=cap-flo;
jacint@109: 	      
jacint@109: 	      if ( remcap >= exc ) {       
jacint@109: 		/*A nonsaturating push.*/
jacint@109: 		
jacint@109: 		flow.set(e, flo+exc);
jacint@109: 		excess.set(v, excess.get(v)+exc);
jacint@109: 		exc=0;
jacint@109: 		break; 
jacint@109: 		
jacint@109: 	      } else { 
jacint@109: 		/*A saturating push.*/
jacint@109: 		
jacint@109: 		flow.set(e, cap);
jacint@109: 		excess.set(v, excess.get(v)+remcap);
jacint@109: 		exc-=remcap;
jacint@109: 	      }
jacint@109: 	    } else if ( newlevel > level.get(v) ){
jacint@109: 	      newlevel = level.get(v);
jacint@109: 	    }	    
jacint@109: 	    
jacint@109: 	  } //for out edges wv 
jacint@109: 	
jacint@109: 	
jacint@109: 	if ( exc > 0 ) {	
jacint@109: 	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
jacint@109: 	    
jacint@109: 	    if( flow.get(e) == 0 ) continue; 
jacint@109: 	    NodeIt v=G.tail(e);  
jacint@109: 	    //e=vw
jacint@109: 	    
jacint@109: 	    if( lev > level.get(v) ) {  
jacint@109: 	      /*Push is allowed now*/
jacint@109: 	      
jacint@109: 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
jacint@109: 		int lev_v=level.get(v);
jacint@109: 		next.set(v,active[lev_v]);
jacint@109: 		active[lev_v]=v;
jacint@109: 	      }
jacint@109: 	      
jacint@109: 	      T flo=flow.get(e);
jacint@109: 	      
jacint@109: 	      if ( flo >= exc ) { 
jacint@109: 		/*A nonsaturating push.*/
jacint@109: 		
jacint@109: 		flow.set(e, flo-exc);
jacint@109: 		excess.set(v, excess.get(v)+exc);
jacint@109: 		exc=0;
jacint@109: 		break; 
jacint@109: 	      } else {                                               
jacint@109: 		/*A saturating push.*/
jacint@109: 		
jacint@109: 		excess.set(v, excess.get(v)+flo);
jacint@109: 		exc-=flo;
jacint@109: 		flow.set(e,0);
jacint@109: 	      }  
jacint@109: 	    } else if ( newlevel > level.get(v) ) {
jacint@109: 	      newlevel = level.get(v);
jacint@109: 	    }	    
jacint@109: 	  } //for in edges vw
jacint@109: 	  
jacint@109: 	} // if w still has excess after the out edge for cycle
jacint@109: 	
jacint@109: 	excess.set(w, exc);
jacint@109: 	 
jacint@109: 	/*
jacint@109: 	  Relabel
jacint@109: 	*/
jacint@109: 	
jacint@109: 
jacint@109: 	if ( exc > 0 ) {
jacint@109: 	  //now 'lev' is the old level of w
jacint@109: 	
jacint@109: 	  if ( phase ) {
jacint@109: 	    level.set(w,++newlevel);
jacint@109: 	    next.set(w,active[newlevel]);
jacint@109: 	    active[newlevel]=w;
jacint@109: 	    b=newlevel;
jacint@109: 	  } else {
jacint@109: 	    //unlacing starts
jacint@109: 	    NodeIt right_n=right.get(w);
jacint@109: 	    NodeIt left_n=left.get(w);
jacint@109: 
jacint@109: 	    if ( right_n != 0 ) {
jacint@109: 	      if ( left_n != 0 ) {
jacint@109: 		right.set(left_n, right_n);
jacint@109: 		left.set(right_n, left_n);
jacint@109: 	      } else {
jacint@109: 		level_list[lev]=right_n;   
jacint@109: 		left.set(right_n, 0);
jacint@109: 	      } 
jacint@109: 	    } else {
jacint@109: 	      if ( left_n != 0 ) {
jacint@109: 		right.set(left_n, 0);
jacint@109: 	      } else { 
jacint@109: 		level_list[lev]=0;   
jacint@109: 
jacint@109: 	      } 
jacint@109: 	    } 
jacint@109: 	    //unlacing ends
jacint@109: 		
jacint@109: 	    //gapping starts
jacint@109: 	    if ( level_list[lev]==0 ) {
jacint@109: 	      
jacint@109: 	      for (int i=lev; i!=k ; ) {
jacint@109: 		NodeIt v=level_list[++i];
jacint@109: 		while ( v != 0 ) {
jacint@109: 		  level.set(v,n);
jacint@109: 		  v=right.get(v);
jacint@109: 		}
jacint@109: 		level_list[i]=0;
jacint@109: 		active[i]=0;
jacint@109: 	      }	     
jacint@109: 
jacint@109: 	      level.set(w,n);
jacint@109: 	      b=lev-1;
jacint@109: 	      k=b;
jacint@109: 	      //gapping ends
jacint@109: 	    } else {
jacint@109: 	      
jacint@109: 	      if ( newlevel == n ) level.set(w,n); 
jacint@109: 	      else {
jacint@109: 		level.set(w,++newlevel);
jacint@109: 		next.set(w,active[newlevel]);
jacint@109: 		active[newlevel]=w;
jacint@109: 		if ( k < newlevel ) ++k;
jacint@109: 		NodeIt first=level_list[newlevel];
jacint@109: 		if ( first != 0 ) left.set(first,w);
jacint@109: 		right.set(w,first);
jacint@109: 		left.set(w,0);
jacint@109: 		level_list[newlevel]=w;
jacint@109: 	      }
jacint@109: 	    }
jacint@109: 
jacint@109: 
jacint@109: 	  } //phase 0
jacint@109: 	  
jacint@109: 	  
jacint@109: 	} // if ( exc > 0 )
jacint@109: 	  
jacint@109: 	
jacint@109: 	}  // if stack[b] is nonempty
jacint@109: 	
jacint@109:       } // while(true)
jacint@109: 
jacint@109: 
jacint@109:       value = excess.get(t);
jacint@109:       /*Max flow value.*/
jacint@109:      
jacint@109:     } //void run()
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:     /*
jacint@109:       Returns the maximum value of a flow.
jacint@109:      */
jacint@109: 
jacint@109:     T maxFlow() {
jacint@109:       return value;
jacint@109:     }
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:     /*
jacint@109:       For the maximum flow x found by the algorithm, 
jacint@109:       it returns the flow value on edge e, i.e. x(e). 
jacint@109:     */
jacint@109:    
jacint@109:     T flowOnEdge(EdgeIt e) {
jacint@109:       return flow.get(e);
jacint@109:     }
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:     FlowMap Flow() {
jacint@109:       return flow;
jacint@109:       }
jacint@109: 
jacint@109: 
jacint@109:     
jacint@109:     void Flow(FlowMap& _flow ) {
jacint@109:       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
jacint@109: 	_flow.set(v,flow.get(v));
jacint@109: 	}
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:     /*
jacint@109:       Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@109:     */
jacint@109:    
jacint@109:     template<typename _CutMap>
jacint@109:     void minMinCut(_CutMap& M) {
jacint@109:     
jacint@109:       std::queue<NodeIt> queue;
jacint@109:       
jacint@109:       M.set(s,true);      
jacint@109:       queue.push(s);
jacint@109: 
jacint@109:       while (!queue.empty()) {
jacint@109:         NodeIt w=queue.front();
jacint@109: 	queue.pop();
jacint@109: 
jacint@109: 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@109: 	  NodeIt v=G.head(e);
jacint@109: 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	} 
jacint@109: 
jacint@109: 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@109: 	  NodeIt v=G.tail(e);
jacint@109: 	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	} 
jacint@109:       }
jacint@109:     }
jacint@109: 
jacint@109: 
jacint@109:   
jacint@109:     /*
jacint@109:       Returns the maximum min cut, by a reverse bfs 
jacint@109:       from t in the residual graph.
jacint@109:     */
jacint@109:     
jacint@109:     template<typename _CutMap>
jacint@109:     void maxMinCut(_CutMap& M) {
jacint@109:     
jacint@109:       std::queue<NodeIt> queue;
jacint@109:       
jacint@109:       M.set(t,true);        
jacint@109:       queue.push(t);
jacint@109: 
jacint@109:       while (!queue.empty()) {
jacint@109:         NodeIt w=queue.front();
jacint@109: 	queue.pop();
jacint@109: 
jacint@109: 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
jacint@109: 	  NodeIt v=G.tail(e);
jacint@109: 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	}
jacint@109: 
jacint@109: 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
jacint@109: 	  NodeIt v=G.head(e);
jacint@109: 	  if (!M.get(v) && flow.get(e) > 0 ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	}
jacint@109:       }
jacint@109: 
jacint@109:       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
jacint@109: 	M.set(v, !M.get(v));
jacint@109:       }
jacint@109: 
jacint@109:     }
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109:     template<typename _CutMap>
jacint@109:     void minCut(_CutMap& M) {
jacint@109:       minMinCut(M);
jacint@109:     }
jacint@109: 
jacint@109:   };
jacint@109: }//namespace marci
jacint@109: #endif 
jacint@109: 
jacint@109: 
jacint@109: 
jacint@109: