6 For testing the parameters H0, H1 of preflow.h
 
     8 The constructor runs the algorithm.
 
    12 T maxFlow() : returns the value of a maximum flow
 
    14 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
 
    16 FlowMap Flow() : returns the fixed maximum flow x
 
    18 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
 
    19      minimum min cut. M should be a map of bools initialized to false.
 
    21 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
 
    22      maximum min cut. M should be a map of bools initialized to false.
 
    24 void minCut(CutMap& M) : fast function, sets M to the characteristic 
 
    25      vector of a minimum cut. 
 
    27 Different member from the other preflow_hl-s (here we have a member 
 
    30 CutMap minCut() : fast function, giving the characteristic 
 
    31      vector of a minimum cut.
 
    36 #ifndef PREFLOW_PARAM_H
 
    37 #define PREFLOW_PARAM_H
 
    42 #include <time_measure.h> //for test
 
    46   template <typename Graph, typename T, 
 
    47     typename FlowMap=typename Graph::EdgeMap<T>, 
 
    48     typename CapMap=typename Graph::EdgeMap<T> >
 
    51     typedef typename Graph::NodeIt NodeIt;
 
    52     typedef typename Graph::EdgeIt EdgeIt;
 
    53     typedef typename Graph::EachNodeIt EachNodeIt;
 
    54     typedef typename Graph::OutEdgeIt OutEdgeIt;
 
    55     typedef typename Graph::InEdgeIt InEdgeIt;
 
    69     preflow_param(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity, 
 
    71       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), H0(_H0), H1(_H1) {
 
    73       bool phase=0;       //phase 0 is the 1st phase, phase 1 is the 2nd
 
    75       int heur0=(int)(H0*n);   //time while running 'bound decrease' 
 
    76       int heur1=(int)(H1*n);   //time while running 'highest label'
 
    77       int heur=heur1;          //starting time interval (#of relabels)
 
    80 	what_heur is 0 in case 'bound decrease' 
 
    81 	and 1 in case 'highest label'
 
    83       bool end=false;   //for the 0 case
 
    85 	Needed for 'bound decrease', 'true'
 
    86 	means no active nodes are above bound b.
 
    92 	b is a bound on the highest level of the stack. 
 
    93 	k is a bound on the highest nonempty level i < n.
 
    96       typename Graph::NodeMap<int> level(G,n);      
 
    97       typename Graph::NodeMap<T> excess(G); 
 
    99       std::vector<NodeIt> active(n);
 
   100       typename Graph::NodeMap<NodeIt> next(G);
 
   101       //Stack of the active nodes in level i < n.
 
   102       //We use it in both phases.
 
   104       typename Graph::NodeMap<NodeIt> left(G);
 
   105       typename Graph::NodeMap<NodeIt> right(G);
 
   106       std::vector<NodeIt> level_list(n);
 
   108 	Needed for the list of the nodes in level i.
 
   111       /*Reverse_bfs from t, to find the starting level.*/
 
   113       std::queue<NodeIt> bfs_queue;
 
   116       while (!bfs_queue.empty()) {
 
   118 	NodeIt v=bfs_queue.front();	
 
   120 	int l=level.get(v)+1;
 
   122 	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
 
   124 	  if ( level.get(w) == n && w != s ) {
 
   126 	    NodeIt first=level_list[l];
 
   127 	    if ( first != 0 ) left.set(first,w);
 
   138       /* Starting flow. It is everywhere 0 at the moment. */     
 
   139       for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
 
   142 	  if ( c == 0 ) continue;
 
   144 	  if ( level.get(w) < n ) {	  
 
   145 	    if ( excess.get(w) == 0 && w!=t ) {
 
   146 	      next.set(w,active[level.get(w)]);
 
   147 	      active[level.get(w)]=w;
 
   150 	    excess.set(w, excess.get(w)+c);
 
   161 	Push/relabel on the highest level active nodes.
 
   168 	  if ( !what_heur && !end && k > 0 ) {
 
   176 	    std::queue<NodeIt> bfs_queue;
 
   179 	    while (!bfs_queue.empty()) {
 
   181 	      NodeIt v=bfs_queue.front();	
 
   183 	      int l=level.get(v)+1;
 
   185 	      for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
 
   186 		if ( capacity.get(e) == flow.get(e) ) continue;
 
   188 		if ( level.get(u) >= n ) { 
 
   191 		  if ( excess.get(u) > 0 ) {
 
   192 		    next.set(u,active[l]);
 
   199 	      for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
 
   200 		if ( 0 == flow.get(e) ) continue;
 
   202 		if ( level.get(u) >= n ) { 
 
   205 		  if ( excess.get(u) > 0 ) {
 
   206 		    next.set(u,active[l]);
 
   218 	if ( active[b] == 0 ) --b; 
 
   220 	  end=false;                      //needed only for phase 0, case hl2
 
   222 	  NodeIt w=active[b];        //w is a highest label active node.
 
   223 	  active[b]=next.get(w);
 
   224 	  int lev=level.get(w);
 
   226 	  int newlevel=n;          //In newlevel we bound the next level of w.
 
   228 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
 
   230 	    if ( flow.get(e) == capacity.get(e) ) continue; 
 
   234 	    if( lev > level.get(v) ) {      
 
   235 	      /*Push is allowed now*/
 
   237 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
 
   238 		int lev_v=level.get(v);
 
   239 		next.set(v,active[lev_v]);
 
   242 	      /*v becomes active.*/
 
   244 	      T cap=capacity.get(e);
 
   248 	      if ( remcap >= exc ) {       
 
   249 		/*A nonsaturating push.*/
 
   251 		flow.set(e, flo+exc);
 
   252 		excess.set(v, excess.get(v)+exc);
 
   257 		/*A saturating push.*/
 
   260 		excess.set(v, excess.get(v)+remcap);
 
   263 	    } else if ( newlevel > level.get(v) ){
 
   264 	      newlevel = level.get(v);
 
   271 	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
 
   273 	    if( flow.get(e) == 0 ) continue; 
 
   277 	    if( lev > level.get(v) ) {  
 
   278 	      /*Push is allowed now*/
 
   280 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
 
   281 		int lev_v=level.get(v);
 
   282 		next.set(v,active[lev_v]);
 
   284 		/*v becomes active.*/
 
   290 		/*A nonsaturating push.*/
 
   292 		flow.set(e, flo-exc);
 
   293 		excess.set(v, excess.get(v)+exc);
 
   297 		/*A saturating push.*/
 
   299 		excess.set(v, excess.get(v)+flo);
 
   303 	    } else if ( newlevel > level.get(v) ) {
 
   304 	      newlevel = level.get(v);
 
   308 	} // if w still has excess after the out edge for cycle
 
   318 	  //now 'lev' is the old level of w
 
   321 	    level.set(w,++newlevel);
 
   322 	    next.set(w,active[newlevel]);
 
   327 	    NodeIt right_n=right.get(w);
 
   328 	    NodeIt left_n=left.get(w);
 
   330 	    if ( right_n != 0 ) {
 
   332 		right.set(left_n, right_n);
 
   333 		left.set(right_n, left_n);
 
   335 		level_list[lev]=right_n;   
 
   336 		left.set(right_n, 0);
 
   340 		right.set(left_n, 0);
 
   347 	    if ( level_list[lev]==0 ) {
 
   349 	      for (int i=lev; i!=k ; ) {
 
   350 		NodeIt v=level_list[++i];
 
   356 		if ( !what_heur ) active[i]=0;
 
   365 	      if ( newlevel == n ) level.set(w,n); 
 
   367 		level.set(w,++newlevel);
 
   368 		next.set(w,active[newlevel]);
 
   370 		if ( what_heur ) b=newlevel;
 
   371 		if ( k < newlevel ) ++k;
 
   372 		NodeIt first=level_list[newlevel];
 
   373 		if ( first != 0 ) left.set(first,w);
 
   376 		level_list[newlevel]=w;
 
   382 	    if ( relabel >= heur ) {
 
   400 	}  // if stack[b] is nonempty
 
   405       value = excess.get(t);
 
   415       Returns the maximum value of a flow.
 
   425       For the maximum flow x found by the algorithm, 
 
   426       it returns the flow value on edge e, i.e. x(e). 
 
   429     T flowOnEdge(EdgeIt e) {
 
   441     void Flow(FlowMap& _flow ) {
 
   442       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
 
   443 	_flow.set(v,flow.get(v));
 
   449       Returns the minimum min cut, by a bfs from s in the residual graph.
 
   452     template<typename CutMap>
 
   453     void minCut(CutMap& M) {
 
   455       std::queue<NodeIt> queue;
 
   460       while (!queue.empty()) {
 
   461         NodeIt w=queue.front();
 
   464 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
 
   466 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
 
   472 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
 
   474 	  if (!M.get(v) && flow.get(e) > 0 ) {
 
   487       Returns the maximum min cut, by a reverse bfs 
 
   488       from t in the residual graph.
 
   491     template<typename CutMap>
 
   492     void maxMinCut(CutMap& M) {
 
   494       std::queue<NodeIt> queue;
 
   499       while (!queue.empty()) {
 
   500         NodeIt w=queue.front();
 
   503 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
 
   505 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
 
   511 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
 
   513 	  if (!M.get(v) && flow.get(e) > 0 ) {
 
   520       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
 
   528     template<typename CutMap>
 
   529     void minMinCut(CutMap& M) {