src/work/jacint/preflow.h
author marci
Thu, 29 Apr 2004 09:08:14 +0000
changeset 465 d72e56f1730d
parent 451 6b36be4cffa4
child 466 cd40ecf4d2a9
permissions -rw-r--r--
mods implied by preflow mods
     1 // -*- C++ -*-
     2 
     3 /*
     4 Heuristics: 
     5  2 phase
     6  gap
     7  list 'level_list' on the nodes on level i implemented by hand
     8  stack 'active' on the active nodes on level i
     9  runs heuristic 'highest label' for H1*n relabels
    10  runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
    11  
    12 Parameters H0 and H1 are initialized to 20 and 1.
    13 
    14 Constructors:
    15 
    16 Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if 
    17      FlowMap is not constant zero, and should be true if it is
    18 
    19 Members:
    20 
    21 void run()
    22 
    23 T flowValue() : returns the value of a maximum flow
    24 
    25 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
    26      minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
    27 
    28 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
    29      maximum min cut. M should be a map of bools initialized to false.
    30 
    31 void minCut(CutMap& M) : sets M to the characteristic vector of 
    32      a min cut. M should be a map of bools initialized to false.
    33 
    34 */
    35 
    36 #ifndef HUGO_PREFLOW_H
    37 #define HUGO_PREFLOW_H
    38 
    39 #define H0 20
    40 #define H1 1
    41 
    42 #include <vector>
    43 #include <queue>
    44 #include <stack>
    45 
    46 namespace hugo {
    47 
    48   template <typename Graph, typename T, 
    49 	    typename CapMap=typename Graph::template EdgeMap<T>, 
    50             typename FlowMap=typename Graph::template EdgeMap<T> >
    51   class Preflow {
    52     
    53     typedef typename Graph::Node Node;
    54     typedef typename Graph::NodeIt NodeIt;
    55     typedef typename Graph::OutEdgeIt OutEdgeIt;
    56     typedef typename Graph::InEdgeIt InEdgeIt;
    57 
    58     typedef typename std::vector<std::stack<Node> > VecStack;
    59     typedef typename Graph::template NodeMap<Node> NNMap;
    60     typedef typename std::vector<Node> VecNode;
    61 
    62     const Graph& G;
    63     Node s;
    64     Node t;
    65     const CapMap* capacity;  
    66     FlowMap* flow;
    67     int n;      //the number of nodes of G
    68     typename Graph::template NodeMap<int> level;      
    69     typename Graph::template NodeMap<T> excess; 
    70 
    71 
    72   public:
    73  
    74     enum flowEnum{
    75       ZERO_FLOW=0,
    76       GEN_FLOW=1,
    77       PREFLOW=2
    78     };
    79 
    80     Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, 
    81 	    FlowMap& _flow) :
    82       G(_G), s(_s), t(_t), capacity(&_capacity), 
    83       flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
    84 
    85     void run() {
    86       preflow( ZERO_FLOW );
    87     }
    88     
    89     void preflow( flowEnum fe ) {
    90       preflowPhase0(fe);
    91       preflowPhase1();
    92     }
    93 
    94     void preflowPhase0( flowEnum fe ) {
    95       
    96       int heur0=(int)(H0*n);  //time while running 'bound decrease' 
    97       int heur1=(int)(H1*n);  //time while running 'highest label'
    98       int heur=heur1;         //starting time interval (#of relabels)
    99       int numrelabel=0;
   100      
   101       bool what_heur=1;       
   102       //It is 0 in case 'bound decrease' and 1 in case 'highest label'
   103 
   104       bool end=false;     
   105       //Needed for 'bound decrease', true means no active nodes are above bound b.
   106 
   107       int k=n-2;  //bound on the highest level under n containing a node
   108       int b=k;    //bound on the highest level under n of an active node
   109       
   110       VecStack active(n);
   111       
   112       NNMap left(G,INVALID);
   113       NNMap right(G,INVALID);
   114       VecNode level_list(n,INVALID);
   115       //List of the nodes in level i<n, set to n.
   116 
   117       NodeIt v;
   118       for(G.first(v); G.valid(v); G.next(v)) level.set(v,n);
   119       //setting each node to level n
   120       
   121       switch ( fe ) {
   122       case PREFLOW:
   123 	{
   124 	  //counting the excess
   125 	  NodeIt v;
   126 	  for(G.first(v); G.valid(v); G.next(v)) {
   127 	    T exc=0;
   128 	  
   129 	    InEdgeIt e;
   130 	    for(G.first(e,v); G.valid(e); G.next(e)) exc+=(*flow)[e];
   131 	    OutEdgeIt f;
   132 	    for(G.first(f,v); G.valid(f); G.next(f)) exc-=(*flow)[f];
   133 	    
   134 	    excess.set(v,exc);	  
   135 	    
   136 	    //putting the active nodes into the stack
   137 	    int lev=level[v];
   138 	    if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
   139 	  }
   140 	  break;
   141 	}
   142       case GEN_FLOW:
   143 	{
   144 	  //Counting the excess of t
   145 	  T exc=0;
   146 	  
   147 	  InEdgeIt e;
   148 	  for(G.first(e,t); G.valid(e); G.next(e)) exc+=(*flow)[e];
   149 	  OutEdgeIt f;
   150 	  for(G.first(f,t); G.valid(f); G.next(f)) exc-=(*flow)[f];
   151 	  
   152 	  excess.set(t,exc);	
   153 	  
   154 	  break;
   155 	}
   156 //       default:
   157 // 	break;
   158 // 	ZERO_FLOW ize kell
   159 
   160       }
   161       
   162       preflowPreproc( fe, active, level_list, left, right );
   163       //End of preprocessing 
   164       
   165       
   166       //Push/relabel on the highest level active nodes.
   167       while ( true ) {
   168 	if ( b == 0 ) {
   169 	  if ( !what_heur && !end && k > 0 ) {
   170 	    b=k;
   171 	    end=true;
   172 	  } else break;
   173 	}
   174 	
   175 	if ( active[b].empty() ) --b; 
   176 	else {
   177 	  end=false;  
   178 	  Node w=active[b].top();
   179 	  active[b].pop();
   180 	  int newlevel=push(w,active);
   181 	  if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list, 
   182 				       left, right, b, k, what_heur);
   183 	  
   184 	  ++numrelabel; 
   185 	  if ( numrelabel >= heur ) {
   186 	    numrelabel=0;
   187 	    if ( what_heur ) {
   188 	      what_heur=0;
   189 	      heur=heur0;
   190 	      end=false;
   191 	    } else {
   192 	      what_heur=1;
   193 	      heur=heur1;
   194 	      b=k; 
   195 	    }
   196 	  }
   197 	} 
   198       } 
   199     }
   200 
   201 
   202     void preflowPhase1() {
   203       
   204       int k=n-2;  //bound on the highest level under n containing a node
   205       int b=k;    //bound on the highest level under n of an active node
   206       
   207       VecStack active(n);
   208       level.set(s,0);
   209       std::queue<Node> bfs_queue;
   210       bfs_queue.push(s);
   211 	    
   212       while (!bfs_queue.empty()) {
   213 	
   214 	Node v=bfs_queue.front();	
   215 	bfs_queue.pop();
   216 	int l=level[v]+1;
   217 	      
   218 	InEdgeIt e;
   219 	for(G.first(e,v); G.valid(e); G.next(e)) {
   220 	  if ( (*capacity)[e] == (*flow)[e] ) continue;
   221 	  Node u=G.tail(e);
   222 	  if ( level[u] >= n ) { 
   223 	    bfs_queue.push(u);
   224 	    level.set(u, l);
   225 	    if ( excess[u] > 0 ) active[l].push(u);
   226 	  }
   227 	}
   228 	
   229 	OutEdgeIt f;
   230 	for(G.first(f,v); G.valid(f); G.next(f)) {
   231 	  if ( 0 == (*flow)[f] ) continue;
   232 	  Node u=G.head(f);
   233 	  if ( level[u] >= n ) { 
   234 	    bfs_queue.push(u);
   235 	    level.set(u, l);
   236 	    if ( excess[u] > 0 ) active[l].push(u);
   237 	  }
   238 	}
   239       }
   240       b=n-2;
   241 
   242       while ( true ) {
   243 	
   244 	if ( b == 0 ) break;
   245 
   246 	if ( active[b].empty() ) --b; 
   247 	else {
   248 	  Node w=active[b].top();
   249 	  active[b].pop();
   250 	  int newlevel=push(w,active);	  
   251 
   252 	  //relabel
   253 	  if ( excess[w] > 0 ) {
   254 	    level.set(w,++newlevel);
   255 	    active[newlevel].push(w);
   256 	    b=newlevel;
   257 	  }
   258 	}  // if stack[b] is nonempty
   259       } // while(true)
   260     }
   261 
   262 
   263     //Returns the maximum value of a flow.
   264     T flowValue() {
   265       return excess[t];
   266     }
   267 
   268     //should be used only between preflowPhase0 and preflowPhase1
   269     template<typename _CutMap>
   270     void actMinCut(_CutMap& M) {
   271       NodeIt v;
   272       for(G.first(v); G.valid(v); G.next(v)) 
   273 	if ( level[v] < n ) M.set(v,false);
   274 	else M.set(v,true);
   275     }
   276 
   277 
   278 
   279     /*
   280       Returns the minimum min cut, by a bfs from s in the residual graph.
   281     */
   282     template<typename _CutMap>
   283     void minMinCut(_CutMap& M) {
   284     
   285       std::queue<Node> queue;
   286       
   287       M.set(s,true);      
   288       queue.push(s);
   289 
   290       while (!queue.empty()) {
   291         Node w=queue.front();
   292 	queue.pop();
   293 
   294 	OutEdgeIt e;
   295 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
   296 	  Node v=G.head(e);
   297 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   298 	    queue.push(v);
   299 	    M.set(v, true);
   300 	  }
   301 	} 
   302 
   303 	InEdgeIt f;
   304 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
   305 	  Node v=G.tail(f);
   306 	  if (!M[v] && (*flow)[f] > 0 ) {
   307 	    queue.push(v);
   308 	    M.set(v, true);
   309 	  }
   310 	} 
   311       }
   312     }
   313 
   314 
   315   
   316     /*
   317       Returns the maximum min cut, by a reverse bfs 
   318       from t in the residual graph.
   319     */
   320     
   321     template<typename _CutMap>
   322     void maxMinCut(_CutMap& M) {
   323 
   324       NodeIt v;
   325       for(G.first(v) ; G.valid(v); G.next(v)) {
   326 	M.set(v, true);
   327       }
   328 
   329       std::queue<Node> queue;
   330       
   331       M.set(t,false);        
   332       queue.push(t);
   333 
   334       while (!queue.empty()) {
   335         Node w=queue.front();
   336 	queue.pop();
   337 
   338 
   339 	InEdgeIt e;
   340 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
   341 	  Node v=G.tail(e);
   342 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   343 	    queue.push(v);
   344 	    M.set(v, false);
   345 	  }
   346 	}
   347 	
   348 	OutEdgeIt f;
   349 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
   350 	  Node v=G.head(f);
   351 	  if (M[v] && (*flow)[f] > 0 ) {
   352 	    queue.push(v);
   353 	    M.set(v, false);
   354 	  }
   355 	}
   356       }
   357     }
   358 
   359 
   360     template<typename CutMap>
   361     void minCut(CutMap& M) {
   362       minMinCut(M);
   363     }
   364 
   365     
   366     void resetTarget (const Node _t) {t=_t;}
   367 
   368     void resetSource (const Node _s) {s=_s;}
   369    
   370     void resetCap (const CapMap& _cap) {
   371       capacity=&_cap;
   372     }
   373     
   374     void resetFlow (FlowMap& _flow) {
   375       flow=&_flow;
   376     }
   377 
   378 
   379   private:
   380 
   381     int push(const Node w, VecStack& active) {
   382       
   383       int lev=level[w];
   384       T exc=excess[w];
   385       int newlevel=n;       //bound on the next level of w
   386 	  
   387       OutEdgeIt e;
   388       for(G.first(e,w); G.valid(e); G.next(e)) {
   389 	    
   390 	if ( (*flow)[e] == (*capacity)[e] ) continue; 
   391 	Node v=G.head(e);            
   392 	    
   393 	if( lev > level[v] ) { //Push is allowed now
   394 	  
   395 	  if ( excess[v]==0 && v!=t && v!=s ) {
   396 	    int lev_v=level[v];
   397 	    active[lev_v].push(v);
   398 	  }
   399 	  
   400 	  T cap=(*capacity)[e];
   401 	  T flo=(*flow)[e];
   402 	  T remcap=cap-flo;
   403 	  
   404 	  if ( remcap >= exc ) { //A nonsaturating push.
   405 	    
   406 	    flow->set(e, flo+exc);
   407 	    excess.set(v, excess[v]+exc);
   408 	    exc=0;
   409 	    break; 
   410 	    
   411 	  } else { //A saturating push.
   412 	    flow->set(e, cap);
   413 	    excess.set(v, excess[v]+remcap);
   414 	    exc-=remcap;
   415 	  }
   416 	} else if ( newlevel > level[v] ) newlevel = level[v];
   417       } //for out edges wv 
   418       
   419       if ( exc > 0 ) {	
   420 	InEdgeIt e;
   421 	for(G.first(e,w); G.valid(e); G.next(e)) {
   422 	  
   423 	  if( (*flow)[e] == 0 ) continue; 
   424 	  Node v=G.tail(e); 
   425 	  
   426 	  if( lev > level[v] ) { //Push is allowed now
   427 	    
   428 	    if ( excess[v]==0 && v!=t && v!=s ) {
   429 	      int lev_v=level[v];
   430 	      active[lev_v].push(v);
   431 	    }
   432 	    
   433 	    T flo=(*flow)[e];
   434 	    
   435 	    if ( flo >= exc ) { //A nonsaturating push.
   436 	      
   437 	      flow->set(e, flo-exc);
   438 	      excess.set(v, excess[v]+exc);
   439 	      exc=0;
   440 	      break; 
   441 	    } else {  //A saturating push.
   442 	      
   443 	      excess.set(v, excess[v]+flo);
   444 	      exc-=flo;
   445 	      flow->set(e,0);
   446 	    }  
   447 	  } else if ( newlevel > level[v] ) newlevel = level[v];
   448 	} //for in edges vw
   449 	
   450       } // if w still has excess after the out edge for cycle
   451       
   452       excess.set(w, exc);
   453       
   454       return newlevel;
   455      }
   456 
   457 
   458     void preflowPreproc ( flowEnum fe, VecStack& active, 
   459 			  VecNode& level_list, NNMap& left, NNMap& right ) {
   460 
   461       std::queue<Node> bfs_queue;
   462       
   463       switch ( fe ) {
   464       case ZERO_FLOW: 
   465 	{
   466 	  //Reverse_bfs from t, to find the starting level.
   467 	  level.set(t,0);
   468 	  bfs_queue.push(t);
   469 	
   470 	  while (!bfs_queue.empty()) {
   471 	    
   472 	    Node v=bfs_queue.front();	
   473 	    bfs_queue.pop();
   474 	    int l=level[v]+1;
   475 	    
   476 	    InEdgeIt e;
   477 	    for(G.first(e,v); G.valid(e); G.next(e)) {
   478 	      Node w=G.tail(e);
   479 	      if ( level[w] == n && w != s ) {
   480 		bfs_queue.push(w);
   481 		Node first=level_list[l];
   482 		if ( G.valid(first) ) left.set(first,w);
   483 		right.set(w,first);
   484 		level_list[l]=w;
   485 		level.set(w, l);
   486 	      }
   487 	    }
   488 	  }
   489 	  
   490 	  //the starting flow
   491 	  OutEdgeIt e;
   492 	  for(G.first(e,s); G.valid(e); G.next(e)) 
   493 	    {
   494 	      T c=(*capacity)[e];
   495 	      if ( c == 0 ) continue;
   496 	      Node w=G.head(e);
   497 	      if ( level[w] < n ) {	  
   498 		if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
   499 		flow->set(e, c); 
   500 		excess.set(w, excess[w]+c);
   501 	      }
   502 	    }
   503 	  break;
   504 	}
   505 	
   506       case GEN_FLOW:
   507       case PREFLOW:
   508 	{
   509 	  //Reverse_bfs from t in the residual graph, 
   510 	  //to find the starting level.
   511 	  level.set(t,0);
   512 	  bfs_queue.push(t);
   513 	  
   514 	  while (!bfs_queue.empty()) {
   515 	    
   516 	    Node v=bfs_queue.front();	
   517 	    bfs_queue.pop();
   518 	    int l=level[v]+1;
   519 	    
   520 	    InEdgeIt e;
   521 	    for(G.first(e,v); G.valid(e); G.next(e)) {
   522 	      if ( (*capacity)[e] == (*flow)[e] ) continue;
   523 	      Node w=G.tail(e);
   524 	      if ( level[w] == n && w != s ) {
   525 		bfs_queue.push(w);
   526 		Node first=level_list[l];
   527 		if ( G.valid(first) ) left.set(first,w);
   528 		right.set(w,first);
   529 		level_list[l]=w;
   530 		level.set(w, l);
   531 	      }
   532 	    }
   533 	    
   534 	    OutEdgeIt f;
   535 	    for(G.first(f,v); G.valid(f); G.next(f)) {
   536 	      if ( 0 == (*flow)[f] ) continue;
   537 	      Node w=G.head(f);
   538 	      if ( level[w] == n && w != s ) {
   539 		bfs_queue.push(w);
   540 		Node first=level_list[l];
   541 		if ( G.valid(first) ) left.set(first,w);
   542 		right.set(w,first);
   543 		level_list[l]=w;
   544 		level.set(w, l);
   545 	      }
   546 	    }
   547 	  }
   548 	  
   549 	  
   550 	  //the starting flow
   551 	  OutEdgeIt e;
   552 	  for(G.first(e,s); G.valid(e); G.next(e)) 
   553 	    {
   554 	      T rem=(*capacity)[e]-(*flow)[e];
   555 	      if ( rem == 0 ) continue;
   556 	      Node w=G.head(e);
   557 	      if ( level[w] < n ) {	  
   558 		if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
   559 		flow->set(e, (*capacity)[e]); 
   560 		excess.set(w, excess[w]+rem);
   561 	      }
   562 	    }
   563 	  
   564 	  InEdgeIt f;
   565 	  for(G.first(f,s); G.valid(f); G.next(f)) 
   566 	    {
   567 	      if ( (*flow)[f] == 0 ) continue;
   568 	      Node w=G.tail(f);
   569 	      if ( level[w] < n ) {	  
   570 		if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
   571 		excess.set(w, excess[w]+(*flow)[f]);
   572 		flow->set(f, 0); 
   573 	      }
   574 	    }  
   575 	  break;
   576 	} //case PREFLOW
   577       }
   578     } //preflowPreproc
   579 
   580 
   581 
   582     void relabel( const Node w, int newlevel, VecStack& active,  
   583 		  VecNode& level_list, NNMap& left, 
   584 		  NNMap& right, int& b, int& k, const bool what_heur ) {
   585 
   586       T lev=level[w];	
   587       
   588       Node right_n=right[w];
   589       Node left_n=left[w];
   590       
   591       //unlacing starts
   592       if ( G.valid(right_n) ) {
   593 	if ( G.valid(left_n) ) {
   594 	  right.set(left_n, right_n);
   595 	  left.set(right_n, left_n);
   596 	} else {
   597 	  level_list[lev]=right_n;   
   598 	  left.set(right_n, INVALID);
   599 	} 
   600       } else {
   601 	if ( G.valid(left_n) ) {
   602 	  right.set(left_n, INVALID);
   603 	} else { 
   604 	  level_list[lev]=INVALID;   
   605 	} 
   606       } 
   607       //unlacing ends
   608 		
   609       if ( !G.valid(level_list[lev]) ) {
   610 	      
   611 	//gapping starts
   612 	for (int i=lev; i!=k ; ) {
   613 	  Node v=level_list[++i];
   614 	  while ( G.valid(v) ) {
   615 	    level.set(v,n);
   616 	    v=right[v];
   617 	  }
   618 	  level_list[i]=INVALID;
   619 	  if ( !what_heur ) {
   620 	    while ( !active[i].empty() ) {
   621 	      active[i].pop();    //FIXME: ezt szebben kene
   622 	    }
   623 	  }	     
   624 	}
   625 	
   626 	level.set(w,n);
   627 	b=lev-1;
   628 	k=b;
   629 	//gapping ends
   630 	
   631       } else {
   632 	
   633 	if ( newlevel == n ) level.set(w,n); 
   634 	else {
   635 	  level.set(w,++newlevel);
   636 	  active[newlevel].push(w);
   637 	  if ( what_heur ) b=newlevel;
   638 	  if ( k < newlevel ) ++k;      //now k=newlevel
   639 	  Node first=level_list[newlevel];
   640 	  if ( G.valid(first) ) left.set(first,w);
   641 	  right.set(w,first);
   642 	  left.set(w,INVALID);
   643 	  level_list[newlevel]=w;
   644 	}
   645       }
   646       
   647     } //relabel
   648     
   649 
   650   };
   651 
   652 } //namespace hugo
   653 
   654 #endif //HUGO_PREFLOW_H
   655 
   656 
   657 
   658