src/work/jacint/preflow_hl3.h
changeset 113 cf7b01232d86
parent 105 a3c73e9b9b2e
equal deleted inserted replaced
1:fe69f506644c 2:d279d62c1bff
     1 // -*- C++ -*-
     1 // -*- C++ -*-
     2 /*
     2 /*
     3 preflow_hl3.h
     3 preflow_hl3.h
     4 by jacint. 
     4 by jacint. 
     5 Runs the highest label variant of the preflow push algorithm with 
     5 
     6 running time O(n^2\sqrt(m)), with the felszippantos 'empty level' 
     6 Heuristics: 
     7 and with the two-phase heuristic: if there is no active node of
     7 
     8 level at most n, then we go into phase 1, do a bfs
     8  suck gap : if there is a gap, then we put all 
     9 from s, and flow the excess back to s.
     9    nodes reachable from the relabelled node to level n
    10 
    10  2 phase
    11 In phase 1 we shift everything downwards by n.
    11  highest label
    12 
    12 
    13 'A' is a parameter for the empty_level heuristic
    13 The constructor runs the algorithm.
    14 
    14 
    15 Member functions:
    15 Members:
    16 
    16 
    17 void run() : runs the algorithm
    17 T maxFlow() : returns the value of a maximum flow
    18 
    18 
    19  The following functions should be used after run() was already run.
    19 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
    20 
    20 
    21 T maxflow() : returns the value of a maximum flow
    21 FlowMap Flow() : returns the fixed maximum flow x
    22 
    22 
    23 T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
    23 void minCut(CutMap& M) : sets M to the characteristic vector of a 
    24 
       
    25 FlowMap allflow() : returns the fixed maximum flow x
       
    26 
       
    27 void mincut(CutMap& M) : sets M to the characteristic vector of a 
       
    28      minimum cut. M should be a map of bools initialized to false.
    24      minimum cut. M should be a map of bools initialized to false.
    29 
    25 
    30 void min_mincut(CutMap& M) : sets M to the characteristic vector of the 
    26 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
    31      minimum min cut. M should be a map of bools initialized to false.
    27      minimum min cut. M should be a map of bools initialized to false.
    32 
    28 
    33 void max_mincut(CutMap& M) : sets M to the characteristic vector of the 
    29 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
    34      maximum min cut. M should be a map of bools initialized to false.
    30      maximum min cut. M should be a map of bools initialized to false.
    35 
    31 
    36 */
    32 */
    37 
    33 
    38 #ifndef PREFLOW_HL3_H
    34 #ifndef PREFLOW_HL3_H
    39 #define PREFLOW_HL3_H
    35 #define PREFLOW_HL3_H
    40 
       
    41 #define A 1
       
    42 
    36 
    43 #include <vector>
    37 #include <vector>
    44 #include <stack>
    38 #include <stack>
    45 #include <queue>
    39 #include <queue>
    46 
    40 
       
    41 #include <time_measure.h> //for test
       
    42 
    47 namespace hugo {
    43 namespace hugo {
    48 
    44 
    49   template <typename Graph, typename T, 
    45   template <typename Graph, typename T, 
    50     typename FlowMap=typename Graph::EdgeMap<T>, typename CapMap=typename Graph::EdgeMap<T>, 
    46     typename FlowMap=typename Graph::EdgeMap<T>, 
    51     typename IntMap=typename Graph::NodeMap<int>, typename TMap=typename Graph::NodeMap<T> >
    47     typename CapMap=typename Graph::EdgeMap<T> >
    52   class preflow_hl3 {
    48   class preflow_hl3 {
    53     
    49     
    54     typedef typename Graph::NodeIt NodeIt;
    50     typedef typename Graph::NodeIt NodeIt;
    55     typedef typename Graph::EdgeIt EdgeIt;
    51     typedef typename Graph::EdgeIt EdgeIt;
    56     typedef typename Graph::EachNodeIt EachNodeIt;
    52     typedef typename Graph::EachNodeIt EachNodeIt;
    64     CapMap& capacity;  
    60     CapMap& capacity;  
    65     T value;
    61     T value;
    66     
    62     
    67   public:
    63   public:
    68 
    64 
       
    65     double time; //for test
       
    66 
    69     preflow_hl3(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) :
    67     preflow_hl3(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) :
    70       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { }
    68       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) {
    71 
    69       
    72 
       
    73     void run() {
       
    74  
       
    75       bool phase=0;
    70       bool phase=0;
    76       int n=G.nodeNum(); 
    71       int n=G.nodeNum(); 
    77       int b=n-2; 
    72       int b=n-2; 
    78       /*
    73       /*
    79 	b is a bound on the highest level of the stack. 
    74 	b is a bound on the highest level of the stack. 
    80 	In the beginning it is at most n-2.
    75 	In the beginning it is at most n-2.
    81       */
    76       */
    82 
    77 
    83       IntMap level(G,n);      
    78       typename Graph::NodeMap<int> level(G,n);      
    84       TMap excess(G); 
    79       typename Graph::NodeMap<T> excess(G); 
    85       
    80       
    86       std::vector<int> numb(n);    
    81       std::vector<int> numb(n);    
    87       /*
    82       /*
    88 	The number of nodes on level i < n. It is
    83 	The number of nodes on level i < n. It is
    89 	initialized to n+1, because of the reverse_bfs-part.
    84 	initialized to n+1, because of the reverse_bfs-part.
   146       while ( true ) {
   141       while ( true ) {
   147 
   142 
   148 	if ( b == 0 ) {
   143 	if ( b == 0 ) {
   149 	  if ( phase ) break; 
   144 	  if ( phase ) break; 
   150 	  phase=1;
   145 	  phase=1;
   151 
   146 	  time=currTime();
       
   147 	  
   152 	  level.set(s,0);
   148 	  level.set(s,0);
   153 
   149 
   154 	  std::queue<NodeIt> bfs_queue;
   150 	  std::queue<NodeIt> bfs_queue;
   155 	  bfs_queue.push(s);
   151 	  bfs_queue.push(s);
   156 	  
   152 	  
   185 	}
   181 	}
   186 
   182 
   187 	if ( stack[b].empty() ) --b;
   183 	if ( stack[b].empty() ) --b;
   188 	else {
   184 	else {
   189 	  
   185 	  
   190 	  NodeIt w=stack[b].top();        //w is a highest label active node.
   186 	  NodeIt w=stack[b].top(); 
   191 	  stack[b].pop();           
   187 	  stack[b].pop();           
   192 	  int lev=level.get(w);
   188 	  int lev=level.get(w);
   193 	  int exc=excess.get(w);
   189 	  T exc=excess.get(w);
   194 	  int newlevel=n;                 //In newlevel we bound the next level of w.
   190 	  int newlevel=n;          //bound on the next level of w.
   195 	  
   191 	  
   196 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
   192 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
   197 	    
   193 	    
   198 	    if ( flow.get(e) == capacity.get(e) ) continue; 
   194 	    if ( flow.get(e) == capacity.get(e) ) continue; 
   199 	    NodeIt v=G.head(e);            
   195 	    NodeIt v=G.head(e);            
   204 	      
   200 	      
   205 	      if ( excess.get(v)==0 && v !=t && v!=s ) 
   201 	      if ( excess.get(v)==0 && v !=t && v!=s ) 
   206 		stack[level.get(v)].push(v); 
   202 		stack[level.get(v)].push(v); 
   207 	      /*v becomes active.*/
   203 	      /*v becomes active.*/
   208 	      
   204 	      
   209 	      int cap=capacity.get(e);
   205 	      T cap=capacity.get(e);
   210 	      int flo=flow.get(e);
   206 	      T flo=flow.get(e);
   211 	      int remcap=cap-flo;
   207 	      T remcap=cap-flo;
   212 	      
   208 	      
   213 	      if ( remcap >= exc ) {       
   209 	      if ( remcap >= exc ) {       
   214 		/*A nonsaturating push.*/
   210 		/*A nonsaturating push.*/
   215 		
   211 		
   216 		flow.set(e, flo+exc);
   212 		flow.set(e, flo+exc);
   242 	      
   238 	      
   243 	      if ( excess.get(v)==0 && v !=t && v!=s ) 
   239 	      if ( excess.get(v)==0 && v !=t && v!=s ) 
   244 		stack[level.get(v)].push(v); 
   240 		stack[level.get(v)].push(v); 
   245 	      /*v becomes active.*/
   241 	      /*v becomes active.*/
   246 	      
   242 	      
   247 	      int flo=flow.get(e);
   243 	      T flo=flow.get(e);
   248 	      
   244 	      
   249 	      if ( flo >= exc ) { 
   245 	      if ( flo >= exc ) { 
   250 		/*A nonsaturating push.*/
   246 		/*A nonsaturating push.*/
   251 		
   247 		
   252 		flow.set(e, flo-exc);
   248 		flow.set(e, flo-exc);
   347 
   343 
   348     /*
   344     /*
   349       Returns the maximum value of a flow.
   345       Returns the maximum value of a flow.
   350      */
   346      */
   351 
   347 
   352     T maxflow() {
   348     T maxFlow() {
   353       return value;
   349       return value;
   354     }
   350     }
   355 
   351 
   356 
   352 
   357 
   353 
   358     /*
   354     /*
   359       For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). 
   355       For the maximum flow x found by the algorithm, 
       
   356       it returns the flow value on edge e, i.e. x(e). 
   360     */
   357     */
   361 
   358 
   362     T flowonedge(EdgeIt e) {
   359     T flowOnEdge(EdgeIt e) {
   363       return flow.get(e);
   360       return flow.get(e);
   364     }
   361     }
   365 
   362 
   366 
   363 
   367 
   364 
   368     /*
   365     /*
   369       Returns the maximum flow x found by the algorithm.
   366       Returns the maximum flow x found by the algorithm.
   370     */
   367     */
   371 
   368 
   372     FlowMap allflow() {
   369     FlowMap Flow() {
   373       return flow;
   370       return flow;
   374     }
   371     }
   375 
   372 
   376 
   373 
   377 
   374 
   379     /*
   376     /*
   380       Returns the minimum min cut, by a bfs from s in the residual graph.
   377       Returns the minimum min cut, by a bfs from s in the residual graph.
   381     */
   378     */
   382     
   379     
   383     template<typename CutMap>
   380     template<typename CutMap>
   384     void mincut(CutMap& M) {
   381     void minCut(CutMap& M) {
   385     
   382     
   386       std::queue<NodeIt> queue;
   383       std::queue<NodeIt> queue;
   387       
   384       
   388       M.set(s,true);      
   385       M.set(s,true);      
   389       queue.push(s);
   386       queue.push(s);
   418       Returns the maximum min cut, by a reverse bfs 
   415       Returns the maximum min cut, by a reverse bfs 
   419       from t in the residual graph.
   416       from t in the residual graph.
   420     */
   417     */
   421     
   418     
   422     template<typename CutMap>
   419     template<typename CutMap>
   423     void max_mincut(CutMap& M) {
   420     void maxMinCut(CutMap& M) {
   424     
   421     
   425       std::queue<NodeIt> queue;
   422       std::queue<NodeIt> queue;
   426       
   423       
   427       M.set(t,true);        
   424       M.set(t,true);        
   428       queue.push(t);
   425       queue.push(t);
   455     }
   452     }
   456 
   453 
   457 
   454 
   458 
   455 
   459     template<typename CutMap>
   456     template<typename CutMap>
   460     void min_mincut(CutMap& M) {
   457     void minMinCut(CutMap& M) {
   461       mincut(M);
   458       minCut(M);
   462     }
   459     }
   463 
   460 
   464 
   461 
   465 
   462 
   466   };
   463   };
   467 }//namespace hugo
   464 }//namespace
   468 #endif 
   465 #endif 
   469 
   466 
   470 
   467 
   471 
   468 
   472 
   469