src/work/jacint/preflow.h
changeset 109 fc5982b39e10
child 113 cf7b01232d86
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/jacint/preflow.h	Sat Feb 21 21:01:22 2004 +0000
     1.3 @@ -0,0 +1,532 @@
     1.4 +// -*- C++ -*-
     1.5 +/*
     1.6 +preflow.h
     1.7 +by jacint. 
     1.8 +Heuristics: 
     1.9 + 2 phase
    1.10 + gap
    1.11 + list 'level_list' on the nodes on level i implemented by hand
    1.12 + stack 'active' on the active nodes on level i implemented by hand
    1.13 + runs heuristic 'highest label' for H1*n relabels
    1.14 + runs heuristic 'dound decrease' for H0*n relabels, starts with 'highest label'
    1.15 + 
    1.16 +Parameters H0 and H1 are initialized to 20 and 10.
    1.17 +
    1.18 +The best preflow I could ever write.
    1.19 +
    1.20 +The constructor runs the algorithm.
    1.21 +
    1.22 +Members:
    1.23 +
    1.24 +T maxFlow() : returns the value of a maximum flow
    1.25 +
    1.26 +T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
    1.27 +
    1.28 +FlowMap Flow() : returns the fixed maximum flow x
    1.29 +
    1.30 +void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
    1.31 +     minimum min cut. M should be a map of bools initialized to false.
    1.32 +
    1.33 +void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
    1.34 +     maximum min cut. M should be a map of bools initialized to false.
    1.35 +
    1.36 +void minCut(CutMap& M) : sets M to the characteristic vector of 
    1.37 +     a min cut. M should be a map of bools initialized to false.
    1.38 +
    1.39 +*/
    1.40 +
    1.41 +#ifndef PREFLOW_H
    1.42 +#define PREFLOW_H
    1.43 +
    1.44 +#define H0 20
    1.45 +#define H1 1
    1.46 +
    1.47 +#include <vector>
    1.48 +#include <queue>
    1.49 +
    1.50 +namespace hugo {
    1.51 +
    1.52 +  template <typename Graph, typename T, 
    1.53 +    typename FlowMap=typename Graph::EdgeMap<T>,
    1.54 +    typename CapMap=typename Graph::EdgeMap<T> >
    1.55 +  class preflow {
    1.56 +    
    1.57 +    typedef typename Graph::NodeIt NodeIt;
    1.58 +    typedef typename Graph::EdgeIt EdgeIt;
    1.59 +    typedef typename Graph::EachNodeIt EachNodeIt;
    1.60 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.61 +    typedef typename Graph::InEdgeIt InEdgeIt;
    1.62 +    
    1.63 +    Graph& G;
    1.64 +    NodeIt s;
    1.65 +    NodeIt t;
    1.66 +    FlowMap flow;
    1.67 +    CapMap& capacity;  
    1.68 +    T value;
    1.69 +
    1.70 +  public:
    1.71 +    
    1.72 +    preflow(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
    1.73 +      G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity)
    1.74 +    {
    1.75 +
    1.76 +      bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
    1.77 +      int n=G.nodeNum(); 
    1.78 +      int heur0=(int)(H0*n);  //time while running 'bound decrease' 
    1.79 +      int heur1=(int)(H1*n);  //time while running 'highest label'
    1.80 +      int heur=heur1;         //starting time interval (#of relabels)
    1.81 +      bool what_heur=1;       
    1.82 +      /*
    1.83 +	what_heur is 0 in case 'bound decrease' 
    1.84 +	and 1 in case 'highest label'
    1.85 +      */
    1.86 +      bool end=false;     
    1.87 +      /*
    1.88 +	Needed for 'bound decrease', 'true'
    1.89 +	means no active nodes are above bound b.
    1.90 +      */
    1.91 +      int relabel=0;
    1.92 +      int k=n-2;  //bound on the highest level under n containing a node
    1.93 +      int b=k;    //bound on the highest level under n of an active node
    1.94 +      
    1.95 +      typename Graph::NodeMap<int> level(G,n);      
    1.96 +      typename Graph::NodeMap<T> excess(G); 
    1.97 +
    1.98 +      std::vector<NodeIt> active(n);
    1.99 +      typename Graph::NodeMap<NodeIt> next(G);
   1.100 +      //Stack of the active nodes in level i < n.
   1.101 +      //We use it in both phases.
   1.102 +
   1.103 +      typename Graph::NodeMap<NodeIt> left(G);
   1.104 +      typename Graph::NodeMap<NodeIt> right(G);
   1.105 +      std::vector<NodeIt> level_list(n);
   1.106 +      /*
   1.107 +	List of the nodes in level i<n.
   1.108 +      */
   1.109 +
   1.110 +      /*Reverse_bfs from t, to find the starting level.*/
   1.111 +      level.set(t,0);
   1.112 +      std::queue<NodeIt> bfs_queue;
   1.113 +      bfs_queue.push(t);
   1.114 +
   1.115 +      while (!bfs_queue.empty()) {
   1.116 +
   1.117 +	NodeIt v=bfs_queue.front();	
   1.118 +	bfs_queue.pop();
   1.119 +	int l=level.get(v)+1;
   1.120 +
   1.121 +	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
   1.122 +	  NodeIt w=G.tail(e);
   1.123 +	  if ( level.get(w) == n && w != s ) {
   1.124 +	    bfs_queue.push(w);
   1.125 +	    NodeIt first=level_list[l];
   1.126 +	    if ( first != 0 ) left.set(first,w);
   1.127 +	    right.set(w,first);
   1.128 +	    level_list[l]=w;
   1.129 +	    level.set(w, l);
   1.130 +	  }
   1.131 +	}
   1.132 +      }
   1.133 +      
   1.134 +      level.set(s,n);
   1.135 +      
   1.136 +
   1.137 +      /* Starting flow. It is everywhere 0 at the moment. */     
   1.138 +      for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
   1.139 +	{
   1.140 +	  T c=capacity.get(e);
   1.141 +	  if ( c == 0 ) continue;
   1.142 +	  NodeIt w=G.head(e);
   1.143 +	  if ( level.get(w) < n ) {	  
   1.144 +	    if ( excess.get(w) == 0 && w!=t ) {
   1.145 +	      next.set(w,active[level.get(w)]);
   1.146 +	      active[level.get(w)]=w;
   1.147 +	    }
   1.148 +	    flow.set(e, c); 
   1.149 +	    excess.set(w, excess.get(w)+c);
   1.150 +	  }
   1.151 +	}
   1.152 +
   1.153 +      /* 
   1.154 +	 End of preprocessing 
   1.155 +      */
   1.156 +
   1.157 +
   1.158 +
   1.159 +      /*
   1.160 +	Push/relabel on the highest level active nodes.
   1.161 +      */	
   1.162 +      while ( true ) {
   1.163 +	
   1.164 +	if ( b == 0 ) {
   1.165 +	  if ( phase ) break;
   1.166 +	  
   1.167 +	  if ( !what_heur && !end && k > 0 ) {
   1.168 +	    b=k;
   1.169 +	    end=true;
   1.170 +	  } else {
   1.171 +	    phase=1;
   1.172 +
   1.173 +	    level.set(s,0);
   1.174 +	    std::queue<NodeIt> bfs_queue;
   1.175 +	    bfs_queue.push(s);
   1.176 +	    
   1.177 +	    while (!bfs_queue.empty()) {
   1.178 +	      
   1.179 +	      NodeIt v=bfs_queue.front();	
   1.180 +	      bfs_queue.pop();
   1.181 +	      int l=level.get(v)+1;
   1.182 +	      
   1.183 +	      for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
   1.184 +		if ( capacity.get(e) == flow.get(e) ) continue;
   1.185 +		NodeIt u=G.tail(e);
   1.186 +		if ( level.get(u) >= n ) { 
   1.187 +		  bfs_queue.push(u);
   1.188 +		  level.set(u, l);
   1.189 +		  if ( excess.get(u) > 0 ) {
   1.190 +		    next.set(u,active[l]);
   1.191 +		    active[l]=u;
   1.192 +		  }
   1.193 +		}
   1.194 +	      }
   1.195 +	    
   1.196 +	      for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
   1.197 +		if ( 0 == flow.get(e) ) continue;
   1.198 +		NodeIt u=G.head(e);
   1.199 +		if ( level.get(u) >= n ) { 
   1.200 +		  bfs_queue.push(u);
   1.201 +		  level.set(u, l);
   1.202 +		  if ( excess.get(u) > 0 ) {
   1.203 +		    next.set(u,active[l]);
   1.204 +		    active[l]=u;
   1.205 +		  }
   1.206 +		}
   1.207 +	      }
   1.208 +	    }
   1.209 +	    b=n-2;
   1.210 +	    }
   1.211 +	    
   1.212 +	}
   1.213 +	  
   1.214 +	  
   1.215 +	if ( active[b] == 0 ) --b; 
   1.216 +	else {
   1.217 +	  end=false;  
   1.218 +
   1.219 +	  NodeIt w=active[b];
   1.220 +	  active[b]=next.get(w);
   1.221 +	  int lev=level.get(w);
   1.222 +	  T exc=excess.get(w);
   1.223 +	  int newlevel=n;       //bound on the next level of w
   1.224 +	  
   1.225 +	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
   1.226 +	    
   1.227 +	    if ( flow.get(e) == capacity.get(e) ) continue; 
   1.228 +	    NodeIt v=G.head(e);            
   1.229 +	    //e=wv	    
   1.230 +	    
   1.231 +	    if( lev > level.get(v) ) {      
   1.232 +	      /*Push is allowed now*/
   1.233 +	      
   1.234 +	      if ( excess.get(v)==0 && v!=t && v!=s ) {
   1.235 +		int lev_v=level.get(v);
   1.236 +		next.set(v,active[lev_v]);
   1.237 +		active[lev_v]=v;
   1.238 +	      }
   1.239 +	      
   1.240 +	      T cap=capacity.get(e);
   1.241 +	      T flo=flow.get(e);
   1.242 +	      T remcap=cap-flo;
   1.243 +	      
   1.244 +	      if ( remcap >= exc ) {       
   1.245 +		/*A nonsaturating push.*/
   1.246 +		
   1.247 +		flow.set(e, flo+exc);
   1.248 +		excess.set(v, excess.get(v)+exc);
   1.249 +		exc=0;
   1.250 +		break; 
   1.251 +		
   1.252 +	      } else { 
   1.253 +		/*A saturating push.*/
   1.254 +		
   1.255 +		flow.set(e, cap);
   1.256 +		excess.set(v, excess.get(v)+remcap);
   1.257 +		exc-=remcap;
   1.258 +	      }
   1.259 +	    } else if ( newlevel > level.get(v) ){
   1.260 +	      newlevel = level.get(v);
   1.261 +	    }	    
   1.262 +	    
   1.263 +	  } //for out edges wv 
   1.264 +	
   1.265 +	
   1.266 +	if ( exc > 0 ) {	
   1.267 +	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
   1.268 +	    
   1.269 +	    if( flow.get(e) == 0 ) continue; 
   1.270 +	    NodeIt v=G.tail(e);  
   1.271 +	    //e=vw
   1.272 +	    
   1.273 +	    if( lev > level.get(v) ) {  
   1.274 +	      /*Push is allowed now*/
   1.275 +	      
   1.276 +	      if ( excess.get(v)==0 && v!=t && v!=s ) {
   1.277 +		int lev_v=level.get(v);
   1.278 +		next.set(v,active[lev_v]);
   1.279 +		active[lev_v]=v;
   1.280 +	      }
   1.281 +	      
   1.282 +	      T flo=flow.get(e);
   1.283 +	      
   1.284 +	      if ( flo >= exc ) { 
   1.285 +		/*A nonsaturating push.*/
   1.286 +		
   1.287 +		flow.set(e, flo-exc);
   1.288 +		excess.set(v, excess.get(v)+exc);
   1.289 +		exc=0;
   1.290 +		break; 
   1.291 +	      } else {                                               
   1.292 +		/*A saturating push.*/
   1.293 +		
   1.294 +		excess.set(v, excess.get(v)+flo);
   1.295 +		exc-=flo;
   1.296 +		flow.set(e,0);
   1.297 +	      }  
   1.298 +	    } else if ( newlevel > level.get(v) ) {
   1.299 +	      newlevel = level.get(v);
   1.300 +	    }	    
   1.301 +	  } //for in edges vw
   1.302 +	  
   1.303 +	} // if w still has excess after the out edge for cycle
   1.304 +	
   1.305 +	excess.set(w, exc);
   1.306 +	 
   1.307 +	/*
   1.308 +	  Relabel
   1.309 +	*/
   1.310 +	
   1.311 +
   1.312 +	if ( exc > 0 ) {
   1.313 +	  //now 'lev' is the old level of w
   1.314 +	
   1.315 +	  if ( phase ) {
   1.316 +	    level.set(w,++newlevel);
   1.317 +	    next.set(w,active[newlevel]);
   1.318 +	    active[newlevel]=w;
   1.319 +	    b=newlevel;
   1.320 +	  } else {
   1.321 +	    //unlacing starts
   1.322 +	    NodeIt right_n=right.get(w);
   1.323 +	    NodeIt left_n=left.get(w);
   1.324 +
   1.325 +	    if ( right_n != 0 ) {
   1.326 +	      if ( left_n != 0 ) {
   1.327 +		right.set(left_n, right_n);
   1.328 +		left.set(right_n, left_n);
   1.329 +	      } else {
   1.330 +		level_list[lev]=right_n;   
   1.331 +		left.set(right_n, 0);
   1.332 +	      } 
   1.333 +	    } else {
   1.334 +	      if ( left_n != 0 ) {
   1.335 +		right.set(left_n, 0);
   1.336 +	      } else { 
   1.337 +		level_list[lev]=0;   
   1.338 +
   1.339 +	      } 
   1.340 +	    } 
   1.341 +	    //unlacing ends
   1.342 +		
   1.343 +	    //gapping starts
   1.344 +	    if ( level_list[lev]==0 ) {
   1.345 +	      
   1.346 +	      for (int i=lev; i!=k ; ) {
   1.347 +		NodeIt v=level_list[++i];
   1.348 +		while ( v != 0 ) {
   1.349 +		  level.set(v,n);
   1.350 +		  v=right.get(v);
   1.351 +		}
   1.352 +		level_list[i]=0;
   1.353 +		if ( !what_heur ) active[i]=0;
   1.354 +	      }	     
   1.355 +
   1.356 +	      level.set(w,n);
   1.357 +	      b=lev-1;
   1.358 +	      k=b;
   1.359 +	      //gapping ends
   1.360 +	    } else {
   1.361 +	      
   1.362 +	      if ( newlevel == n ) level.set(w,n); 
   1.363 +	      else {
   1.364 +		level.set(w,++newlevel);
   1.365 +		next.set(w,active[newlevel]);
   1.366 +		active[newlevel]=w;
   1.367 +		if ( what_heur ) b=newlevel;
   1.368 +		if ( k < newlevel ) ++k;
   1.369 +		NodeIt first=level_list[newlevel];
   1.370 +		if ( first != 0 ) left.set(first,w);
   1.371 +		right.set(w,first);
   1.372 +		left.set(w,0);
   1.373 +		level_list[newlevel]=w;
   1.374 +	      }
   1.375 +	    }
   1.376 +
   1.377 +
   1.378 +	    ++relabel; 
   1.379 +	    if ( relabel >= heur ) {
   1.380 +	      relabel=0;
   1.381 +	      if ( what_heur ) {
   1.382 +		what_heur=0;
   1.383 +		heur=heur0;
   1.384 +		end=false;
   1.385 +	      } else {
   1.386 +		what_heur=1;
   1.387 +		heur=heur1;
   1.388 +		b=k; 
   1.389 +	      }
   1.390 +	    }
   1.391 +	  } //phase 0
   1.392 +	  
   1.393 +	  
   1.394 +	} // if ( exc > 0 )
   1.395 +	  
   1.396 +	
   1.397 +	}  // if stack[b] is nonempty
   1.398 +	
   1.399 +      } // while(true)
   1.400 +
   1.401 +
   1.402 +      value = excess.get(t);
   1.403 +      /*Max flow value.*/
   1.404 +     
   1.405 +    } //void run()
   1.406 +
   1.407 +
   1.408 +
   1.409 +
   1.410 +
   1.411 +    /*
   1.412 +      Returns the maximum value of a flow.
   1.413 +     */
   1.414 +
   1.415 +    T maxFlow() {
   1.416 +      return value;
   1.417 +    }
   1.418 +
   1.419 +
   1.420 +
   1.421 +    /*
   1.422 +      For the maximum flow x found by the algorithm, 
   1.423 +      it returns the flow value on edge e, i.e. x(e). 
   1.424 +    */
   1.425 +   
   1.426 +    T flowOnEdge(EdgeIt e) {
   1.427 +      return flow.get(e);
   1.428 +    }
   1.429 +
   1.430 +
   1.431 +
   1.432 +    FlowMap Flow() {
   1.433 +      return flow;
   1.434 +      }
   1.435 +
   1.436 +
   1.437 +    
   1.438 +    void Flow(FlowMap& _flow ) {
   1.439 +      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
   1.440 +	_flow.set(v,flow.get(v));
   1.441 +	}
   1.442 +
   1.443 +
   1.444 +
   1.445 +    /*
   1.446 +      Returns the minimum min cut, by a bfs from s in the residual graph.
   1.447 +    */
   1.448 +   
   1.449 +    template<typename _CutMap>
   1.450 +    void minMinCut(_CutMap& M) {
   1.451 +    
   1.452 +      std::queue<NodeIt> queue;
   1.453 +      
   1.454 +      M.set(s,true);      
   1.455 +      queue.push(s);
   1.456 +
   1.457 +      while (!queue.empty()) {
   1.458 +        NodeIt w=queue.front();
   1.459 +	queue.pop();
   1.460 +
   1.461 +	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
   1.462 +	  NodeIt v=G.head(e);
   1.463 +	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
   1.464 +	    queue.push(v);
   1.465 +	    M.set(v, true);
   1.466 +	  }
   1.467 +	} 
   1.468 +
   1.469 +	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
   1.470 +	  NodeIt v=G.tail(e);
   1.471 +	  if (!M.get(v) && flow.get(e) > 0 ) {
   1.472 +	    queue.push(v);
   1.473 +	    M.set(v, true);
   1.474 +	  }
   1.475 +	} 
   1.476 +      }
   1.477 +    }
   1.478 +
   1.479 +
   1.480 +  
   1.481 +    /*
   1.482 +      Returns the maximum min cut, by a reverse bfs 
   1.483 +      from t in the residual graph.
   1.484 +    */
   1.485 +    
   1.486 +    template<typename _CutMap>
   1.487 +    void maxMinCut(_CutMap& M) {
   1.488 +    
   1.489 +      std::queue<NodeIt> queue;
   1.490 +      
   1.491 +      M.set(t,true);        
   1.492 +      queue.push(t);
   1.493 +
   1.494 +      while (!queue.empty()) {
   1.495 +        NodeIt w=queue.front();
   1.496 +	queue.pop();
   1.497 +
   1.498 +	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
   1.499 +	  NodeIt v=G.tail(e);
   1.500 +	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
   1.501 +	    queue.push(v);
   1.502 +	    M.set(v, true);
   1.503 +	  }
   1.504 +	}
   1.505 +
   1.506 +	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
   1.507 +	  NodeIt v=G.head(e);
   1.508 +	  if (!M.get(v) && flow.get(e) > 0 ) {
   1.509 +	    queue.push(v);
   1.510 +	    M.set(v, true);
   1.511 +	  }
   1.512 +	}
   1.513 +      }
   1.514 +
   1.515 +      for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
   1.516 +	M.set(v, !M.get(v));
   1.517 +      }
   1.518 +
   1.519 +    }
   1.520 +
   1.521 +
   1.522 +
   1.523 +    template<typename CutMap>
   1.524 +    void minCut(CutMap& M) {
   1.525 +      minMinCut(M);
   1.526 +    }
   1.527 +
   1.528 +
   1.529 +  };
   1.530 +}//namespace marci
   1.531 +#endif 
   1.532 +
   1.533 +
   1.534 +
   1.535 +