jacint@109: // -*- C++ -*-
jacint@372: 
jacint@372: //run gyorsan tudna adni a minmincutot a 2 fazis elejen , ne vegyuk be konstruktorba egy cutmapet?
jacint@372: //constzero jo igy?
jacint@372: 
jacint@372: //majd marci megmondja betegyem-e bfs-t meg resgraphot
jacint@372: 
jacint@109: /*
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:  runs heuristic 'highest label' for H1*n relabels
jacint@113:  runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
jacint@109:  
jacint@109: Parameters H0 and H1 are initialized to 20 and 10.
jacint@109: 
jacint@211: Constructors:
jacint@109: 
jacint@372: Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if 
jacint@372:      FlowMap is not constant zero, and should be true if it is
jacint@109: 
jacint@109: Members:
jacint@109: 
jacint@211: void run()
jacint@109: 
jacint@211: T flowValue() : returns the value of a maximum flow
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: 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@372: FIXME reset
jacint@372: 
jacint@109: */
jacint@109: 
jacint@211: #ifndef HUGO_PREFLOW_H
jacint@211: #define HUGO_PREFLOW_H
jacint@109: 
jacint@109: #define H0 20
jacint@109: #define H1 1
jacint@109: 
jacint@109: #include <vector>
jacint@109: #include <queue>
jacint@109: 
jacint@109: namespace hugo {
jacint@109: 
jacint@109:   template <typename Graph, typename T, 
marci@389: 	    typename CapMap=typename Graph::template EdgeMap<T>, 
marci@389:             typename FlowMap=typename Graph::template EdgeMap<T> >
jacint@211:   class Preflow {
jacint@109:     
jacint@211:     typedef typename Graph::Node Node;
jacint@211:     typedef typename Graph::Edge Edge;
jacint@109:     typedef typename Graph::NodeIt NodeIt;
jacint@109:     typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@109:     typedef typename Graph::InEdgeIt InEdgeIt;
jacint@109:     
jacint@211:     const Graph& G;
jacint@211:     Node s;
jacint@211:     Node t;
marci@330:     const CapMap& capacity;  
jacint@211:     FlowMap& flow;
jacint@109:     T value;
jacint@372:     bool constzero;
jacint@109: 
jacint@109:   public:
jacint@372:     Preflow(Graph& _G, Node _s, Node _t, CapMap& _capacity, 
jacint@372: 	    FlowMap& _flow, bool _constzero ) :
jacint@372:       G(_G), s(_s), t(_t), capacity(_capacity), flow(_flow), constzero(_constzero) {}
jacint@372:     
jacint@372:     
jacint@211:     void run() {
jacint@372:       
jacint@372:       value=0;                //for the subsequent runs
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:       int heur0=(int)(H0*n);  //time while running 'bound decrease' 
jacint@109:       int heur1=(int)(H1*n);  //time while running 'highest label'
jacint@109:       int heur=heur1;         //starting time interval (#of relabels)
jacint@109:       bool what_heur=1;       
jacint@109:       /*
jacint@109: 	what_heur is 0 in case 'bound decrease' 
jacint@109: 	and 1 in case 'highest label'
jacint@109:       */
jacint@109:       bool end=false;     
jacint@109:       /*
jacint@109: 	Needed for 'bound decrease', 'true'
jacint@109: 	means no active nodes are above bound b.
jacint@109:       */
jacint@109:       int relabel=0;
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:       
marci@389:       typename Graph::template NodeMap<int> level(G,n);      
marci@389:       typename Graph::template NodeMap<T> excess(G); 
jacint@109: 
jacint@372:       std::vector<Node> active(n-1,INVALID);
marci@389:       typename Graph::template NodeMap<Node> next(G,INVALID);
jacint@109:       //Stack of the active nodes in level i < n.
jacint@109:       //We use it in both phases.
jacint@109: 
marci@389:       typename Graph::template NodeMap<Node> left(G,INVALID);
marci@389:       typename Graph::template NodeMap<Node> right(G,INVALID);
jacint@211:       std::vector<Node> level_list(n,INVALID);
jacint@109:       /*
jacint@109: 	List of the nodes in level i<n.
jacint@109:       */
jacint@109: 
jacint@109: 
jacint@372:       if ( constzero ) {
jacint@372:      
jacint@372: 	/*Reverse_bfs from t, to find the starting level.*/
jacint@372: 	level.set(t,0);
jacint@372: 	std::queue<Node> bfs_queue;
jacint@372: 	bfs_queue.push(t);
jacint@372: 	
jacint@372: 	while (!bfs_queue.empty()) {
jacint@372: 	  
jacint@372: 	  Node v=bfs_queue.front();	
jacint@372: 	  bfs_queue.pop();
jacint@372: 	  int l=level[v]+1;
jacint@372: 	  
jacint@372: 	  InEdgeIt e;
jacint@372: 	  for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@372: 	    Node w=G.tail(e);
jacint@372: 	    if ( level[w] == n && w != s ) {
jacint@372: 	      bfs_queue.push(w);
jacint@372: 	      Node first=level_list[l];
jacint@372: 	      if ( G.valid(first) ) left.set(first,w);
jacint@372: 	      right.set(w,first);
jacint@372: 	      level_list[l]=w;
jacint@372: 	      level.set(w, l);
jacint@372: 	    }
jacint@109: 	  }
jacint@109: 	}
jacint@109: 
jacint@372: 	//the starting flow
jacint@372: 	OutEdgeIt e;
jacint@372: 	for(G.first(e,s); G.valid(e); G.next(e)) 
jacint@109: 	{
jacint@211: 	  T c=capacity[e];
jacint@109: 	  if ( c == 0 ) continue;
jacint@211: 	  Node w=G.head(e);
jacint@211: 	  if ( level[w] < n ) {	  
jacint@211: 	    if ( excess[w] == 0 && w!=t ) {
jacint@211: 	      next.set(w,active[level[w]]);
jacint@211: 	      active[level[w]]=w;
jacint@109: 	    }
jacint@109: 	    flow.set(e, c); 
jacint@211: 	    excess.set(w, excess[w]+c);
jacint@109: 	  }
jacint@109: 	}
jacint@372:       }
jacint@372:       else 
jacint@372:       {
jacint@372: 	
jacint@372: 	/*
jacint@372: 	  Reverse_bfs from t in the residual graph, 
jacint@372: 	  to find the starting level.
jacint@372: 	*/
jacint@372: 	level.set(t,0);
jacint@372: 	std::queue<Node> bfs_queue;
jacint@372: 	bfs_queue.push(t);
jacint@372: 	
jacint@372: 	while (!bfs_queue.empty()) {
jacint@372: 	  
jacint@372: 	  Node v=bfs_queue.front();	
jacint@372: 	  bfs_queue.pop();
jacint@372: 	  int l=level[v]+1;
jacint@372: 	  
jacint@372: 	  InEdgeIt e;
jacint@372: 	  for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@372: 	    if ( capacity[e] == flow[e] ) continue;
jacint@372: 	    Node w=G.tail(e);
jacint@372: 	    if ( level[w] == n && w != s ) {
jacint@372: 	      bfs_queue.push(w);
jacint@372: 	      Node first=level_list[l];
jacint@372: 	      if ( G.valid(first) ) left.set(first,w);
jacint@372: 	      right.set(w,first);
jacint@372: 	      level_list[l]=w;
jacint@372: 	      level.set(w, l);
jacint@372: 	    }
jacint@372: 	  }
jacint@372: 	    
jacint@372: 	  OutEdgeIt f;
jacint@372: 	  for(G.first(f,v); G.valid(f); G.next(f)) {
jacint@372: 	    if ( 0 == flow[f] ) continue;
jacint@372: 	    Node w=G.head(f);
jacint@372: 	    if ( level[w] == n && w != s ) {
jacint@372: 	      bfs_queue.push(w);
jacint@372: 	      Node first=level_list[l];
jacint@372: 	      if ( G.valid(first) ) left.set(first,w);
jacint@372: 	      right.set(w,first);
jacint@372: 	      level_list[l]=w;
jacint@372: 	      level.set(w, l);
jacint@372: 	    }
jacint@372: 	  }
jacint@372: 	}
jacint@372:       
jacint@372: 	
jacint@372: 	/*
jacint@372: 	  Counting the excess
jacint@372: 	*/
jacint@372: 	NodeIt v;
jacint@372: 	for(G.first(v); G.valid(v); G.next(v)) {
jacint@372: 	  T exc=0;
jacint@372: 
jacint@372: 	  InEdgeIt e;
jacint@372: 	  for(G.first(e,v); G.valid(e); G.next(e)) exc+=flow[e];
jacint@372: 	  OutEdgeIt f;
jacint@372: 	  for(G.first(f,v); G.valid(f); G.next(f)) exc-=flow[e];
jacint@372: 
jacint@372: 	  excess.set(v,exc);	  
jacint@372: 
jacint@372: 	  //putting the active nodes into the stack
jacint@372: 	  int lev=level[v];
jacint@372: 	  if ( exc > 0 && lev < n ) {
jacint@372: 	    next.set(v,active[lev]);
jacint@372: 	    active[lev]=v;
jacint@372: 	  }
jacint@372: 	}
jacint@372: 
jacint@372: 
jacint@372: 	//the starting flow
jacint@372: 	OutEdgeIt e;
jacint@372: 	for(G.first(e,s); G.valid(e); G.next(e)) 
jacint@372: 	{
jacint@372: 	  T rem=capacity[e]-flow[e];
jacint@372: 	  if ( rem == 0 ) continue;
jacint@372: 	  Node w=G.head(e);
jacint@372: 	  if ( level[w] < n ) {	  
jacint@372: 	    if ( excess[w] == 0 && w!=t ) {
jacint@372: 	      next.set(w,active[level[w]]);
jacint@372: 	      active[level[w]]=w;
jacint@372: 	    }
jacint@372: 	    flow.set(e, capacity[e]); 
jacint@372: 	    excess.set(w, excess[w]+rem);
jacint@372: 	  }
jacint@372: 	}
jacint@372: 	
jacint@372: 	InEdgeIt f;
jacint@372: 	for(G.first(f,s); G.valid(f); G.next(f)) 
jacint@372: 	{
jacint@372: 	  if ( flow[f] == 0 ) continue;
jacint@372: 	  Node w=G.head(f);
jacint@372: 	  if ( level[w] < n ) {	  
jacint@372: 	    if ( excess[w] == 0 && w!=t ) {
jacint@372: 	      next.set(w,active[level[w]]);
jacint@372: 	      active[level[w]]=w;
jacint@372: 	    }
jacint@372: 	    excess.set(w, excess[w]+flow[f]);
jacint@372: 	    flow.set(f, 0); 
jacint@372: 	  }
jacint@372: 	}
jacint@372:       }
jacint@372: 
jacint@372: 
jacint@372: 
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 ( !what_heur && !end && k > 0 ) {
jacint@109: 	    b=k;
jacint@109: 	    end=true;
jacint@109: 	  } else {
jacint@109: 	    phase=1;
jacint@109: 	    level.set(s,0);
jacint@211: 	    std::queue<Node> bfs_queue;
jacint@109: 	    bfs_queue.push(s);
jacint@109: 	    
jacint@109: 	    while (!bfs_queue.empty()) {
jacint@109: 	      
jacint@211: 	      Node v=bfs_queue.front();	
jacint@109: 	      bfs_queue.pop();
jacint@211: 	      int l=level[v]+1;
jacint@109: 	      
jacint@211: 	      InEdgeIt e;
jacint@211: 	      for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@211: 		if ( capacity[e] == flow[e] ) continue;
jacint@211: 		Node u=G.tail(e);
jacint@211: 		if ( level[u] >= n ) { 
jacint@109: 		  bfs_queue.push(u);
jacint@109: 		  level.set(u, l);
jacint@211: 		  if ( excess[u] > 0 ) {
jacint@109: 		    next.set(u,active[l]);
jacint@109: 		    active[l]=u;
jacint@109: 		  }
jacint@109: 		}
jacint@109: 	      }
jacint@109: 	    
jacint@211: 	      OutEdgeIt f;
jacint@211: 	      for(G.first(f,v); G.valid(f); G.next(f)) {
jacint@211: 		if ( 0 == flow[f] ) continue;
jacint@211: 		Node u=G.head(f);
jacint@211: 		if ( level[u] >= n ) { 
jacint@109: 		  bfs_queue.push(u);
jacint@109: 		  level.set(u, l);
jacint@211: 		  if ( excess[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@211: 	if ( !G.valid(active[b]) ) --b; 
jacint@109: 	else {
jacint@109: 	  end=false;  
jacint@109: 
jacint@211: 	  Node w=active[b];
jacint@211: 	  active[b]=next[w];
jacint@211: 	  int lev=level[w];
jacint@211: 	  T exc=excess[w];
jacint@109: 	  int newlevel=n;       //bound on the next level of w
jacint@109: 	  
jacint@211: 	  OutEdgeIt e;
jacint@211: 	  for(G.first(e,w); G.valid(e); G.next(e)) {
jacint@109: 	    
jacint@211: 	    if ( flow[e] == capacity[e] ) continue; 
jacint@211: 	    Node v=G.head(e);            
jacint@109: 	    //e=wv	    
jacint@109: 	    
jacint@211: 	    if( lev > level[v] ) {      
jacint@109: 	      /*Push is allowed now*/
jacint@109: 	      
jacint@211: 	      if ( excess[v]==0 && v!=t && v!=s ) {
jacint@211: 		int lev_v=level[v];
jacint@109: 		next.set(v,active[lev_v]);
jacint@109: 		active[lev_v]=v;
jacint@109: 	      }
jacint@109: 	      
jacint@211: 	      T cap=capacity[e];
jacint@211: 	      T flo=flow[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@211: 		excess.set(v, excess[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@211: 		excess.set(v, excess[v]+remcap);
jacint@109: 		exc-=remcap;
jacint@109: 	      }
jacint@211: 	    } else if ( newlevel > level[v] ){
jacint@211: 	      newlevel = level[v];
jacint@109: 	    }	    
jacint@109: 	    
jacint@109: 	  } //for out edges wv 
jacint@109: 	
jacint@109: 	
jacint@109: 	if ( exc > 0 ) {	
jacint@211: 	  InEdgeIt e;
jacint@211: 	  for(G.first(e,w); G.valid(e); G.next(e)) {
jacint@109: 	    
jacint@211: 	    if( flow[e] == 0 ) continue; 
jacint@211: 	    Node v=G.tail(e);  
jacint@109: 	    //e=vw
jacint@109: 	    
jacint@211: 	    if( lev > level[v] ) {  
jacint@109: 	      /*Push is allowed now*/
jacint@109: 	      
jacint@211: 	      if ( excess[v]==0 && v!=t && v!=s ) {
jacint@211: 		int lev_v=level[v];
jacint@109: 		next.set(v,active[lev_v]);
jacint@109: 		active[lev_v]=v;
jacint@109: 	      }
jacint@109: 	      
jacint@211: 	      T flo=flow[e];
jacint@109: 	      
jacint@109: 	      if ( flo >= exc ) { 
jacint@109: 		/*A nonsaturating push.*/
jacint@109: 		
jacint@109: 		flow.set(e, flo-exc);
jacint@211: 		excess.set(v, excess[v]+exc);
jacint@109: 		exc=0;
jacint@109: 		break; 
jacint@109: 	      } else {                                               
jacint@109: 		/*A saturating push.*/
jacint@109: 		
jacint@211: 		excess.set(v, excess[v]+flo);
jacint@109: 		exc-=flo;
jacint@109: 		flow.set(e,0);
jacint@109: 	      }  
jacint@211: 	    } else if ( newlevel > level[v] ) {
jacint@211: 	      newlevel = level[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@211: 	    Node right_n=right[w];
jacint@211: 	    Node left_n=left[w];
jacint@109: 
jacint@211: 	    if ( G.valid(right_n) ) {
jacint@211: 	      if ( G.valid(left_n) ) {
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@211: 		left.set(right_n, INVALID);
jacint@109: 	      } 
jacint@109: 	    } else {
jacint@211: 	      if ( G.valid(left_n) ) {
jacint@211: 		right.set(left_n, INVALID);
jacint@109: 	      } else { 
jacint@211: 		level_list[lev]=INVALID;   
jacint@109: 	      } 
jacint@109: 	    } 
jacint@109: 	    //unlacing ends
jacint@109: 		
jacint@211: 	    if ( !G.valid(level_list[lev]) ) {
jacint@109: 	      
jacint@372: 	       //gapping starts
jacint@109: 	      for (int i=lev; i!=k ; ) {
jacint@211: 		Node v=level_list[++i];
jacint@211: 		while ( G.valid(v) ) {
jacint@109: 		  level.set(v,n);
jacint@211: 		  v=right[v];
jacint@109: 		}
jacint@211: 		level_list[i]=INVALID;
jacint@211: 		if ( !what_heur ) active[i]=INVALID;
jacint@109: 	      }	     
jacint@109: 
jacint@109: 	      level.set(w,n);
jacint@109: 	      b=lev-1;
jacint@109: 	      k=b;
jacint@109: 	      //gapping ends
jacint@372: 	    
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 ( what_heur ) b=newlevel;
jacint@372: 		if ( k < newlevel ) ++k;      //now k=newlevel
jacint@211: 		Node first=level_list[newlevel];
jacint@211: 		if ( G.valid(first) ) left.set(first,w);
jacint@109: 		right.set(w,first);
jacint@211: 		left.set(w,INVALID);
jacint@109: 		level_list[newlevel]=w;
jacint@109: 	      }
jacint@109: 	    }
jacint@109: 
jacint@109: 
jacint@109: 	    ++relabel; 
jacint@109: 	    if ( relabel >= heur ) {
jacint@109: 	      relabel=0;
jacint@109: 	      if ( what_heur ) {
jacint@109: 		what_heur=0;
jacint@109: 		heur=heur0;
jacint@109: 		end=false;
jacint@109: 	      } else {
jacint@109: 		what_heur=1;
jacint@109: 		heur=heur1;
jacint@109: 		b=k; 
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@211:       value = excess[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@211:     T flowValue() {
jacint@109:       return value;
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@211:       NodeIt v;
jacint@211:       for(G.first(v) ; G.valid(v); G.next(v))
jacint@211: 	_flow.set(v,flow[v]);
jacint@211:     }
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@211:       std::queue<Node> queue;
jacint@109:       
jacint@109:       M.set(s,true);      
jacint@109:       queue.push(s);
jacint@109: 
jacint@109:       while (!queue.empty()) {
jacint@211:         Node w=queue.front();
jacint@109: 	queue.pop();
jacint@109: 
jacint@211: 	OutEdgeIt e;
jacint@211: 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@211: 	  Node v=G.head(e);
jacint@211: 	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	} 
jacint@109: 
jacint@211: 	InEdgeIt f;
jacint@211: 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@211: 	  Node v=G.tail(f);
jacint@211: 	  if (!M[v] && flow[f] > 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@211:       std::queue<Node> queue;
jacint@109:       
jacint@109:       M.set(t,true);        
jacint@109:       queue.push(t);
jacint@109: 
jacint@109:       while (!queue.empty()) {
jacint@211:         Node w=queue.front();
jacint@109: 	queue.pop();
jacint@109: 
jacint@211: 
jacint@211: 	InEdgeIt e;
jacint@211: 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@211: 	  Node v=G.tail(e);
jacint@211: 	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	}
jacint@211: 	
jacint@211: 	OutEdgeIt f;
jacint@211: 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@211: 	  Node v=G.head(f);
jacint@211: 	  if (!M[v] && flow[f] > 0 ) {
jacint@109: 	    queue.push(v);
jacint@109: 	    M.set(v, true);
jacint@109: 	  }
jacint@109: 	}
jacint@109:       }
jacint@109: 
jacint@211:       NodeIt v;
jacint@211:       for(G.first(v) ; G.valid(v); G.next(v)) {
jacint@211: 	M.set(v, !M[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@372:     
jacint@372:     void reset_target (Node _t) {t=_t;}
jacint@372:     void reset_source (Node _s) {s=_s;}
jacint@372:    
jacint@372:     template<typename _CapMap>   
jacint@372:     void reset_cap (_CapMap _cap) {capacity=_cap;}
jacint@372: 
jacint@372:     template<typename _FlowMap>   
jacint@372:     void reset_cap (_FlowMap _flow, bool _constzero) {
jacint@372:       flow=_flow;
jacint@372:       constzero=_constzero;
jacint@372:     }
jacint@372: 
jacint@372: 
jacint@109: 
jacint@109:   };
jacint@109: 
marci@174: } //namespace hugo
jacint@109: 
marci@174: #endif //PREFLOW_H
jacint@109: 
jacint@109: 
marci@174: 
marci@174: