src/work/marci/augmenting_flow.h
author deba
Wed, 08 Sep 2004 12:06:45 +0000 (2004-09-08)
changeset 822 88226d9fe821
parent 775 e46a1f0623a0
child 854 baf0b6e40211
permissions -rw-r--r--
The MapFactories have been removed from the code because
if we use macros then they increases only the complexity.

The pair iterators of the maps are separeted from the maps.

Some macros and comments has been changed.
     1 // -*- C++ -*-
     2 #ifndef HUGO_AUGMENTING_FLOW_H
     3 #define HUGO_AUGMENTING_FLOW_H
     4 
     5 #include <vector>
     6 #include <queue>
     7 #include <stack>
     8 #include <iostream>
     9 
    10 #include <hugo/graph_wrapper.h>
    11 #include <bfs_dfs.h>
    12 #include <hugo/invalid.h>
    13 #include <hugo/maps.h>
    14 //#include <for_each_macros.h>
    15 
    16 /// \file
    17 /// \brief Maximum flow algorithms.
    18 /// \ingroup galgs
    19 
    20 namespace hugo {
    21 
    22   /// \addtogroup galgs
    23   /// @{                                                                                                                                        
    24   ///Maximum flow algorithms class.
    25 
    26   ///This class provides various algorithms for finding a flow of
    27   ///maximum value in a directed graph. The \e source node, the \e
    28   ///target node, the \e capacity of the edges and the \e starting \e
    29   ///flow value of the edges should be passed to the algorithm through the
    30   ///constructor. It is possible to change these quantities using the
    31   ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
    32   ///\ref resetFlow. Before any subsequent runs of any algorithm of
    33   ///the class \ref resetFlow should be called. 
    34 
    35   ///After running an algorithm of the class, the actual flow value 
    36   ///can be obtained by calling \ref flowValue(). The minimum
    37   ///value cut can be written into a \c node map of \c bools by
    38   ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
    39   ///the inclusionwise minimum and maximum of the minimum value
    40   ///cuts, resp.)                                                                                                                               
    41   ///\param Graph The directed graph type the algorithm runs on.
    42   ///\param Num The number type of the capacities and the flow values.
    43   ///\param CapMap The capacity map type.
    44   ///\param FlowMap The flow map type.                                                                                                           
    45   ///\author Marton Makai, Jacint Szabo 
    46 //   template <typename Graph, typename Num,
    47 // 	    typename CapMap=typename Graph::template EdgeMap<Num>,
    48 //             typename FlowMap=typename Graph::template EdgeMap<Num> >
    49 //   class MaxFlow {
    50 //   protected:
    51 //     typedef typename Graph::Node Node;
    52 //     typedef typename Graph::NodeIt NodeIt;
    53 //     typedef typename Graph::EdgeIt EdgeIt;
    54 //     typedef typename Graph::OutEdgeIt OutEdgeIt;
    55 //     typedef typename Graph::InEdgeIt InEdgeIt;
    56 
    57 //     typedef typename std::vector<std::stack<Node> > VecStack;
    58 //     typedef typename Graph::template NodeMap<Node> NNMap;
    59 //     typedef typename std::vector<Node> VecNode;
    60 
    61 //     const Graph* g;
    62 //     Node s;
    63 //     Node t;
    64 //     const CapMap* capacity;
    65 //     FlowMap* flow;
    66 //     int n;      //the number of nodes of G
    67 //     typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
    68 //     //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
    69 //     typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
    70 //     typedef typename ResGW::Edge ResGWEdge;
    71 //     //typedef typename ResGW::template NodeMap<bool> ReachedMap;
    72 //     typedef typename Graph::template NodeMap<int> ReachedMap;
    73 
    74 
    75 //     //level works as a bool map in augmenting path algorithms and is
    76 //     //used by bfs for storing reached information.  In preflow, it
    77 //     //shows the levels of nodes.     
    78 //     ReachedMap level;
    79 
    80 //     //excess is needed only in preflow
    81 //     typename Graph::template NodeMap<Num> excess;
    82 
    83 //     //fixme    
    84 // //   protected:
    85 //     //     MaxFlow() { }
    86 //     //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
    87 //     // 	     FlowMap& _flow)
    88 //     //       {
    89 //     // 	g=&_G;
    90 //     // 	s=_s;
    91 //     // 	t=_t;
    92 //     // 	capacity=&_capacity;
    93 //     // 	flow=&_flow;
    94 //     // 	n=_G.nodeNum;
    95 //     // 	level.set (_G); //kellene vmi ilyesmi fv
    96 //     // 	excess(_G,0); //itt is
    97 //     //       }
    98 
    99 //     // constants used for heuristics
   100 //     static const int H0=20;
   101 //     static const int H1=1;
   102 
   103 //   public:
   104 
   105 //     ///Indicates the property of the starting flow.
   106 
   107 //     ///Indicates the property of the starting flow. The meanings are as follows:
   108 //     ///- \c ZERO_FLOW: constant zero flow
   109 //     ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
   110 //     ///the sum of the out-flows in every node except the \e source and
   111 //     ///the \e target.
   112 //     ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   113 //     ///least the sum of the out-flows in every node except the \e source.
   114 //     ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
   115 //     ///set to the constant zero flow in the beginning of the algorithm in this case.
   116 //     enum FlowEnum{
   117 //       ZERO_FLOW,
   118 //       GEN_FLOW,
   119 //       PRE_FLOW,
   120 //       NO_FLOW
   121 //     };
   122 
   123 //     enum StatusEnum {
   124 //       AFTER_NOTHING,
   125 //       AFTER_AUGMENTING,
   126 //       AFTER_FAST_AUGMENTING, 
   127 //       AFTER_PRE_FLOW_PHASE_1,      
   128 //       AFTER_PRE_FLOW_PHASE_2
   129 //     };
   130 
   131 //     /// Don not needle this flag only if necessary.
   132 //     StatusEnum status;
   133 // //     int number_of_augmentations;
   134 
   135 
   136 // //     template<typename IntMap>
   137 // //     class TrickyReachedMap {
   138 // //     protected:
   139 // //       IntMap* map;
   140 // //       int* number_of_augmentations;
   141 // //     public:
   142 // //       TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
   143 // // 	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
   144 // //       void set(const Node& n, bool b) {
   145 // // 	if (b)
   146 // // 	  map->set(n, *number_of_augmentations);
   147 // // 	else 
   148 // // 	  map->set(n, *number_of_augmentations-1);
   149 // //       }
   150 // //       bool operator[](const Node& n) const { 
   151 // // 	return (*map)[n]==*number_of_augmentations; 
   152 // //       }
   153 // //     };
   154     
   155 //     MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   156 // 	    FlowMap& _flow) :
   157 //       g(&_G), s(_s), t(_t), capacity(&_capacity),
   158 //       flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   159 //       status(AFTER_NOTHING) { }
   160 
   161 //     ///Runs a maximum flow algorithm.
   162 
   163 //     ///Runs a preflow algorithm, which is the fastest maximum flow
   164 //     ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
   165 //     ///\pre The starting flow must be
   166 //     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   167 //     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   168 //     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   169 //     /// - any map if \c fe is NO_FLOW.
   170 //     void run(FlowEnum fe=ZERO_FLOW) {
   171 //       preflow(fe);
   172 //     }
   173 
   174                                                                               
   175 //     ///Runs a preflow algorithm.  
   176 
   177 //     ///Runs a preflow algorithm. The preflow algorithms provide the
   178 //     ///fastest way to compute a maximum flow in a directed graph.
   179 //     ///\pre The starting flow must be
   180 //     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   181 //     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   182 //     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   183 //     /// - any map if \c fe is NO_FLOW.
   184 //     void preflow(FlowEnum fe) {
   185 //       preflowPhase1(fe);
   186 //       preflowPhase2();
   187 //     }
   188 //     // Heuristics:
   189 //     //   2 phase
   190 //     //   gap
   191 //     //   list 'level_list' on the nodes on level i implemented by hand
   192 //     //   stack 'active' on the active nodes on level i                                                                                    
   193 //     //   runs heuristic 'highest label' for H1*n relabels
   194 //     //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
   195 //     //   Parameters H0 and H1 are initialized to 20 and 1.
   196 
   197 //     ///Runs the first phase of the preflow algorithm.
   198 
   199 //     ///The preflow algorithm consists of two phases, this method runs the
   200 //     ///first phase. After the first phase the maximum flow value and a
   201 //     ///minimum value cut can already be computed, though a maximum flow
   202 //     ///is net yet obtained. So after calling this method \ref flowValue
   203 //     ///and \ref actMinCut gives proper results.
   204 //     ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
   205 //     ///give minimum value cuts unless calling \ref preflowPhase2.
   206 //     ///\pre The starting flow must be
   207 //     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   208 //     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   209 //     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   210 //     /// - any map if \c fe is NO_FLOW.
   211 //     void preflowPhase1(FlowEnum fe);
   212 
   213 //     ///Runs the second phase of the preflow algorithm.
   214 
   215 //     ///The preflow algorithm consists of two phases, this method runs
   216 //     ///the second phase. After calling \ref preflowPhase1 and then
   217 //     ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
   218 //     ///\ref minMinCut and \ref maxMinCut give proper results.
   219 //     ///\pre \ref preflowPhase1 must be called before.
   220 //     void preflowPhase2();
   221 
   222 //     /// Returns the maximum value of a flow.
   223 
   224 //     /// Returns the maximum value of a flow, by counting the 
   225 //     /// over-flow of the target node \ref t.
   226 //     /// It can be called already after running \ref preflowPhase1.
   227 //     Num flowValue() const {
   228 //       Num a=0;
   229 //       FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
   230 //       FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
   231 //       return a;
   232 //       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
   233 //     }
   234 
   235 //     ///Returns a minimum value cut after calling \ref preflowPhase1.
   236 
   237 //     ///After the first phase of the preflow algorithm the maximum flow
   238 //     ///value and a minimum value cut can already be computed. This
   239 //     ///method can be called after running \ref preflowPhase1 for
   240 //     ///obtaining a minimum value cut.
   241 //     /// \warning Gives proper result only right after calling \ref
   242 //     /// preflowPhase1.
   243 //     /// \todo We have to make some status variable which shows the
   244 //     /// actual state
   245 //     /// of the class. This enables us to determine which methods are valid
   246 //     /// for MinCut computation
   247 //     template<typename _CutMap>
   248 //     void actMinCut(_CutMap& M) const {
   249 //       NodeIt v;
   250 //       switch (status) {
   251 //       case AFTER_PRE_FLOW_PHASE_1:
   252 // 	for(g->first(v); g->valid(v); g->next(v)) {
   253 // 	  if (level[v] < n) {
   254 // 	    M.set(v, false);
   255 // 	  } else {
   256 // 	    M.set(v, true);
   257 // 	  }
   258 // 	}
   259 // 	break;
   260 //       case AFTER_PRE_FLOW_PHASE_2:
   261 //       case AFTER_NOTHING:
   262 //       case AFTER_AUGMENTING:
   263 //       case AFTER_FAST_AUGMENTING:
   264 // 	minMinCut(M);
   265 // 	break;
   266 // //       case AFTER_AUGMENTING:
   267 // // 	for(g->first(v); g->valid(v); g->next(v)) {
   268 // // 	  if (level[v]) {
   269 // // 	    M.set(v, true);
   270 // // 	  } else {
   271 // // 	    M.set(v, false);
   272 // // 	  }
   273 // // 	}
   274 // // 	break;
   275 // //       case AFTER_FAST_AUGMENTING:
   276 // // 	for(g->first(v); g->valid(v); g->next(v)) {
   277 // // 	  if (level[v]==number_of_augmentations) {
   278 // // 	    M.set(v, true);
   279 // // 	  } else {
   280 // // 	    M.set(v, false);
   281 // // 	  }
   282 // // 	}
   283 // // 	break;
   284 //       }
   285 //     }
   286 
   287 //     ///Returns the inclusionwise minimum of the minimum value cuts.
   288 
   289 //     ///Sets \c M to the characteristic vector of the minimum value cut
   290 //     ///which is inclusionwise minimum. It is computed by processing
   291 //     ///a bfs from the source node \c s in the residual graph.
   292 //     ///\pre M should be a node map of bools initialized to false.
   293 //     ///\pre \c flow must be a maximum flow.
   294 //     template<typename _CutMap>
   295 //     void minMinCut(_CutMap& M) const {
   296 //       std::queue<Node> queue;
   297 
   298 //       M.set(s,true);
   299 //       queue.push(s);
   300 
   301 //       while (!queue.empty()) {
   302 //         Node w=queue.front();
   303 // 	queue.pop();
   304 
   305 // 	OutEdgeIt e;
   306 // 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   307 // 	  Node v=g->head(e);
   308 // 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   309 // 	    queue.push(v);
   310 // 	    M.set(v, true);
   311 // 	  }
   312 // 	}
   313 
   314 // 	InEdgeIt f;
   315 // 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   316 // 	  Node v=g->tail(f);
   317 // 	  if (!M[v] && (*flow)[f] > 0 ) {
   318 // 	    queue.push(v);
   319 // 	    M.set(v, true);
   320 // 	  }
   321 // 	}
   322 //       }
   323 //     }
   324 
   325 //     ///Returns the inclusionwise maximum of the minimum value cuts.
   326 
   327 //     ///Sets \c M to the characteristic vector of the minimum value cut
   328 //     ///which is inclusionwise maximum. It is computed by processing a
   329 //     ///backward bfs from the target node \c t in the residual graph.
   330 //     ///\pre M should be a node map of bools initialized to false.
   331 //     ///\pre \c flow must be a maximum flow. 
   332 //     template<typename _CutMap>
   333 //     void maxMinCut(_CutMap& M) const {
   334 
   335 //       NodeIt v;
   336 //       for(g->first(v) ; g->valid(v); g->next(v)) {
   337 // 	M.set(v, true);
   338 //       }
   339 
   340 //       std::queue<Node> queue;
   341 
   342 //       M.set(t,false);
   343 //       queue.push(t);
   344 
   345 //       while (!queue.empty()) {
   346 //         Node w=queue.front();
   347 // 	queue.pop();
   348 
   349 // 	InEdgeIt e;
   350 // 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   351 // 	  Node v=g->tail(e);
   352 // 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   353 // 	    queue.push(v);
   354 // 	    M.set(v, false);
   355 // 	  }
   356 // 	}
   357 
   358 // 	OutEdgeIt f;
   359 // 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   360 // 	  Node v=g->head(f);
   361 // 	  if (M[v] && (*flow)[f] > 0 ) {
   362 // 	    queue.push(v);
   363 // 	    M.set(v, false);
   364 // 	  }
   365 // 	}
   366 //       }
   367 //     }
   368 
   369 //     ///Returns a minimum value cut.
   370 
   371 //     ///Sets \c M to the characteristic vector of a minimum value cut.
   372 //     ///\pre M should be a node map of bools initialized to false.
   373 //     ///\pre \c flow must be a maximum flow.    
   374 //     template<typename CutMap>
   375 //     void minCut(CutMap& M) const { minMinCut(M); }
   376 
   377 //     ///Resets the source node to \c _s.
   378 
   379 //     ///Resets the source node to \c _s.
   380 //     /// 
   381 //     void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
   382 
   383 //     ///Resets the target node to \c _t.
   384 
   385 //     ///Resets the target node to \c _t.
   386 //     ///
   387 //     void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
   388 
   389 //     /// Resets the edge map of the capacities to _cap.
   390 
   391 //     /// Resets the edge map of the capacities to _cap.
   392 //     /// 
   393 //     void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
   394 
   395 //     /// Resets the edge map of the flows to _flow.
   396 
   397 //     /// Resets the edge map of the flows to _flow.
   398 //     /// 
   399 //     void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
   400 
   401 
   402 //   private:
   403 
   404 //     int push(Node w, VecStack& active) {
   405 
   406 //       int lev=level[w];
   407 //       Num exc=excess[w];
   408 //       int newlevel=n;       //bound on the next level of w
   409 
   410 //       OutEdgeIt e;
   411 //       for(g->first(e,w); g->valid(e); g->next(e)) {
   412 
   413 // 	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   414 // 	Node v=g->head(e);
   415 
   416 // 	if( lev > level[v] ) { //Push is allowed now
   417 
   418 // 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   419 // 	    int lev_v=level[v];
   420 // 	    active[lev_v].push(v);
   421 // 	  }
   422 
   423 // 	  Num cap=(*capacity)[e];
   424 // 	  Num flo=(*flow)[e];
   425 // 	  Num remcap=cap-flo;
   426 
   427 // 	  if ( remcap >= exc ) { //A nonsaturating push.
   428 
   429 // 	    flow->set(e, flo+exc);
   430 // 	    excess.set(v, excess[v]+exc);
   431 // 	    exc=0;
   432 // 	    break;
   433 
   434 // 	  } else { //A saturating push.
   435 // 	    flow->set(e, cap);
   436 // 	    excess.set(v, excess[v]+remcap);
   437 // 	    exc-=remcap;
   438 // 	  }
   439 // 	} else if ( newlevel > level[v] ) newlevel = level[v];
   440 //       } //for out edges wv
   441 
   442 //       if ( exc > 0 ) {
   443 // 	InEdgeIt e;
   444 // 	for(g->first(e,w); g->valid(e); g->next(e)) {
   445 
   446 // 	  if( (*flow)[e] <= 0 ) continue;
   447 // 	  Node v=g->tail(e);
   448 
   449 // 	  if( lev > level[v] ) { //Push is allowed now
   450 
   451 // 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   452 // 	      int lev_v=level[v];
   453 // 	      active[lev_v].push(v);
   454 // 	    }
   455 
   456 // 	    Num flo=(*flow)[e];
   457 
   458 // 	    if ( flo >= exc ) { //A nonsaturating push.
   459 
   460 // 	      flow->set(e, flo-exc);
   461 // 	      excess.set(v, excess[v]+exc);
   462 // 	      exc=0;
   463 // 	      break;
   464 // 	    } else {  //A saturating push.
   465 
   466 // 	      excess.set(v, excess[v]+flo);
   467 // 	      exc-=flo;
   468 // 	      flow->set(e,0);
   469 // 	    }
   470 // 	  } else if ( newlevel > level[v] ) newlevel = level[v];
   471 // 	} //for in edges vw
   472 
   473 //       } // if w still has excess after the out edge for cycle
   474 
   475 //       excess.set(w, exc);
   476 
   477 //       return newlevel;
   478 //     }
   479 
   480 
   481 //     void preflowPreproc(FlowEnum fe, VecStack& active,
   482 // 			VecNode& level_list, NNMap& left, NNMap& right)
   483 //     {
   484 //       std::queue<Node> bfs_queue;
   485 
   486 //       switch (fe) {
   487 //       case NO_FLOW:   //flow is already set to const zero in this case
   488 //       case ZERO_FLOW:
   489 // 	{
   490 // 	  //Reverse_bfs from t, to find the starting level.
   491 // 	  level.set(t,0);
   492 // 	  bfs_queue.push(t);
   493 
   494 // 	  while (!bfs_queue.empty()) {
   495 
   496 // 	    Node v=bfs_queue.front();
   497 // 	    bfs_queue.pop();
   498 // 	    int l=level[v]+1;
   499 
   500 // 	    InEdgeIt e;
   501 // 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   502 // 	      Node w=g->tail(e);
   503 // 	      if ( level[w] == n && w != s ) {
   504 // 		bfs_queue.push(w);
   505 // 		Node first=level_list[l];
   506 // 		if ( g->valid(first) ) left.set(first,w);
   507 // 		right.set(w,first);
   508 // 		level_list[l]=w;
   509 // 		level.set(w, l);
   510 // 	      }
   511 // 	    }
   512 // 	  }
   513 
   514 // 	  //the starting flow
   515 // 	  OutEdgeIt e;
   516 // 	  for(g->first(e,s); g->valid(e); g->next(e))
   517 // 	    {
   518 // 	      Num c=(*capacity)[e];
   519 // 	      if ( c <= 0 ) continue;
   520 // 	      Node w=g->head(e);
   521 // 	      if ( level[w] < n ) {
   522 // 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   523 // 		flow->set(e, c);
   524 // 		excess.set(w, excess[w]+c);
   525 // 	      }
   526 // 	    }
   527 // 	  break;
   528 // 	}
   529 
   530 //       case GEN_FLOW:
   531 //       case PRE_FLOW:
   532 // 	{
   533 // 	  //Reverse_bfs from t in the residual graph,
   534 // 	  //to find the starting level.
   535 // 	  level.set(t,0);
   536 // 	  bfs_queue.push(t);
   537 
   538 // 	  while (!bfs_queue.empty()) {
   539 
   540 // 	    Node v=bfs_queue.front();
   541 // 	    bfs_queue.pop();
   542 // 	    int l=level[v]+1;
   543 
   544 // 	    InEdgeIt e;
   545 // 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   546 // 	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
   547 // 	      Node w=g->tail(e);
   548 // 	      if ( level[w] == n && w != s ) {
   549 // 		bfs_queue.push(w);
   550 // 		Node first=level_list[l];
   551 // 		if ( g->valid(first) ) left.set(first,w);
   552 // 		right.set(w,first);
   553 // 		level_list[l]=w;
   554 // 		level.set(w, l);
   555 // 	      }
   556 // 	    }
   557 
   558 // 	    OutEdgeIt f;
   559 // 	    for(g->first(f,v); g->valid(f); g->next(f)) {
   560 // 	      if ( 0 >= (*flow)[f] ) continue;
   561 // 	      Node w=g->head(f);
   562 // 	      if ( level[w] == n && w != s ) {
   563 // 		bfs_queue.push(w);
   564 // 		Node first=level_list[l];
   565 // 		if ( g->valid(first) ) left.set(first,w);
   566 // 		right.set(w,first);
   567 // 		level_list[l]=w;
   568 // 		level.set(w, l);
   569 // 	      }
   570 // 	    }
   571 // 	  }
   572 
   573 
   574 // 	  //the starting flow
   575 // 	  OutEdgeIt e;
   576 // 	  for(g->first(e,s); g->valid(e); g->next(e))
   577 // 	    {
   578 // 	      Num rem=(*capacity)[e]-(*flow)[e];
   579 // 	      if ( rem <= 0 ) continue;
   580 // 	      Node w=g->head(e);
   581 // 	      if ( level[w] < n ) {
   582 // 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   583 // 		flow->set(e, (*capacity)[e]);
   584 // 		excess.set(w, excess[w]+rem);
   585 // 	      }
   586 // 	    }
   587 
   588 // 	  InEdgeIt f;
   589 // 	  for(g->first(f,s); g->valid(f); g->next(f))
   590 // 	    {
   591 // 	      if ( (*flow)[f] <= 0 ) continue;
   592 // 	      Node w=g->tail(f);
   593 // 	      if ( level[w] < n ) {
   594 // 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   595 // 		excess.set(w, excess[w]+(*flow)[f]);
   596 // 		flow->set(f, 0);
   597 // 	      }
   598 // 	    }
   599 // 	  break;
   600 // 	} //case PRE_FLOW
   601 //       }
   602 //     } //preflowPreproc
   603 
   604 
   605 
   606 //     void relabel(Node w, int newlevel, VecStack& active,
   607 // 		 VecNode& level_list, NNMap& left,
   608 // 		 NNMap& right, int& b, int& k, bool what_heur )
   609 //     {
   610 
   611 //       //FIXME jacint: ez mitol num
   612 // //      Num lev=level[w];
   613 //       int lev=level[w];
   614 
   615 //       Node right_n=right[w];
   616 //       Node left_n=left[w];
   617 
   618 //       //unlacing starts
   619 //       if ( g->valid(right_n) ) {
   620 // 	if ( g->valid(left_n) ) {
   621 // 	  right.set(left_n, right_n);
   622 // 	  left.set(right_n, left_n);
   623 // 	} else {
   624 // 	  level_list[lev]=right_n;
   625 // 	  left.set(right_n, INVALID);
   626 // 	}
   627 //       } else {
   628 // 	if ( g->valid(left_n) ) {
   629 // 	  right.set(left_n, INVALID);
   630 // 	} else {
   631 // 	  level_list[lev]=INVALID;
   632 // 	}
   633 //       }
   634 //       //unlacing ends
   635 
   636 //       if ( !g->valid(level_list[lev]) ) {
   637 
   638 // 	//gapping starts
   639 // 	for (int i=lev; i!=k ; ) {
   640 // 	  Node v=level_list[++i];
   641 // 	  while ( g->valid(v) ) {
   642 // 	    level.set(v,n);
   643 // 	    v=right[v];
   644 // 	  }
   645 // 	  level_list[i]=INVALID;
   646 // 	  if ( !what_heur ) {
   647 // 	    while ( !active[i].empty() ) {
   648 // 	      active[i].pop();    //FIXME: ezt szebben kene
   649 // 	    }
   650 // 	  }
   651 // 	}
   652 
   653 // 	level.set(w,n);
   654 // 	b=lev-1;
   655 // 	k=b;
   656 // 	//gapping ends
   657 
   658 //       } else {
   659 
   660 // 	if ( newlevel == n ) level.set(w,n);
   661 // 	else {
   662 // 	  level.set(w,++newlevel);
   663 // 	  active[newlevel].push(w);
   664 // 	  if ( what_heur ) b=newlevel;
   665 // 	  if ( k < newlevel ) ++k;      //now k=newlevel
   666 // 	  Node first=level_list[newlevel];
   667 // 	  if ( g->valid(first) ) left.set(first,w);
   668 // 	  right.set(w,first);
   669 // 	  left.set(w,INVALID);
   670 // 	  level_list[newlevel]=w;
   671 // 	}
   672 //       }
   673 
   674 //     } //relabel
   675 
   676 //   };
   677 
   678 
   679 
   680 //   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   681 //   void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
   682 //   {
   683 
   684 //     int heur0=(int)(H0*n);  //time while running 'bound decrease'
   685 //     int heur1=(int)(H1*n);  //time while running 'highest label'
   686 //     int heur=heur1;         //starting time interval (#of relabels)
   687 //     int numrelabel=0;
   688 
   689 //     bool what_heur=1;
   690 //     //It is 0 in case 'bound decrease' and 1 in case 'highest label'
   691 
   692 //     bool end=false;
   693 //     //Needed for 'bound decrease', true means no active nodes are above bound
   694 //     //b.
   695 
   696 //     int k=n-2;  //bound on the highest level under n containing a node
   697 //     int b=k;    //bound on the highest level under n of an active node
   698 
   699 //     VecStack active(n);
   700 
   701 //     NNMap left(*g, INVALID);
   702 //     NNMap right(*g, INVALID);
   703 //     VecNode level_list(n,INVALID);
   704 //     //List of the nodes in level i<n, set to n.
   705 
   706 //     NodeIt v;
   707 //     for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
   708 //     //setting each node to level n
   709 
   710 //     if ( fe == NO_FLOW ) {
   711 //       EdgeIt e;
   712 //       for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
   713 //     }
   714 
   715 //     switch (fe) { //computing the excess
   716 //     case PRE_FLOW:
   717 //       {
   718 // 	NodeIt v;
   719 // 	for(g->first(v); g->valid(v); g->next(v)) {
   720 // 	  Num exc=0;
   721 
   722 // 	  InEdgeIt e;
   723 // 	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
   724 // 	  OutEdgeIt f;
   725 // 	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
   726 
   727 // 	  excess.set(v,exc);
   728 
   729 // 	  //putting the active nodes into the stack
   730 // 	  int lev=level[v];
   731 // 	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
   732 // 	}
   733 // 	break;
   734 //       }
   735 //     case GEN_FLOW:
   736 //       {
   737 // 	NodeIt v;
   738 // 	for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   739 
   740 // 	Num exc=0;
   741 // 	InEdgeIt e;
   742 // 	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
   743 // 	OutEdgeIt f;
   744 // 	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
   745 // 	excess.set(t,exc);
   746 // 	break;
   747 //       }
   748 //     case ZERO_FLOW:
   749 //     case NO_FLOW:
   750 //       {
   751 // 	NodeIt v;
   752 //         for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   753 // 	break;
   754 //       }
   755 //     }
   756 
   757 //     preflowPreproc(fe, active, level_list, left, right);
   758 //     //End of preprocessing
   759 
   760 
   761 //     //Push/relabel on the highest level active nodes.
   762 //     while ( true ) {
   763 //       if ( b == 0 ) {
   764 // 	if ( !what_heur && !end && k > 0 ) {
   765 // 	  b=k;
   766 // 	  end=true;
   767 // 	} else break;
   768 //       }
   769 
   770 //       if ( active[b].empty() ) --b;
   771 //       else {
   772 // 	end=false;
   773 // 	Node w=active[b].top();
   774 // 	active[b].pop();
   775 // 	int newlevel=push(w,active);
   776 // 	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
   777 // 				     left, right, b, k, what_heur);
   778 
   779 // 	++numrelabel;
   780 // 	if ( numrelabel >= heur ) {
   781 // 	  numrelabel=0;
   782 // 	  if ( what_heur ) {
   783 // 	    what_heur=0;
   784 // 	    heur=heur0;
   785 // 	    end=false;
   786 // 	  } else {
   787 // 	    what_heur=1;
   788 // 	    heur=heur1;
   789 // 	    b=k;
   790 // 	  }
   791 // 	}
   792 //       }
   793 //     }
   794 
   795 //     status=AFTER_PRE_FLOW_PHASE_1;
   796 //   }
   797 
   798 
   799 
   800 //   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   801 //   void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
   802 //   {
   803 
   804 //     int k=n-2;  //bound on the highest level under n containing a node
   805 //     int b=k;    //bound on the highest level under n of an active node
   806 
   807 //     VecStack active(n);
   808 //     level.set(s,0);
   809 //     std::queue<Node> bfs_queue;
   810 //     bfs_queue.push(s);
   811 
   812 //     while (!bfs_queue.empty()) {
   813 
   814 //       Node v=bfs_queue.front();
   815 //       bfs_queue.pop();
   816 //       int l=level[v]+1;
   817 
   818 //       InEdgeIt e;
   819 //       for(g->first(e,v); g->valid(e); g->next(e)) {
   820 // 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   821 // 	Node u=g->tail(e);
   822 // 	if ( level[u] >= n ) {
   823 // 	  bfs_queue.push(u);
   824 // 	  level.set(u, l);
   825 // 	  if ( excess[u] > 0 ) active[l].push(u);
   826 // 	}
   827 //       }
   828 
   829 //       OutEdgeIt f;
   830 //       for(g->first(f,v); g->valid(f); g->next(f)) {
   831 // 	if ( 0 >= (*flow)[f] ) continue;
   832 // 	Node u=g->head(f);
   833 // 	if ( level[u] >= n ) {
   834 // 	  bfs_queue.push(u);
   835 // 	  level.set(u, l);
   836 // 	  if ( excess[u] > 0 ) active[l].push(u);
   837 // 	}
   838 //       }
   839 //     }
   840 //     b=n-2;
   841 
   842 //     while ( true ) {
   843 
   844 //       if ( b == 0 ) break;
   845 
   846 //       if ( active[b].empty() ) --b;
   847 //       else {
   848 // 	Node w=active[b].top();
   849 // 	active[b].pop();
   850 // 	int newlevel=push(w,active);
   851 
   852 // 	//relabel
   853 // 	if ( excess[w] > 0 ) {
   854 // 	  level.set(w,++newlevel);
   855 // 	  active[newlevel].push(w);
   856 // 	  b=newlevel;
   857 // 	}
   858 //       }  // if stack[b] is nonempty
   859 //     } // while(true)
   860 
   861 //     status=AFTER_PRE_FLOW_PHASE_2;
   862 //   }
   863 
   864 
   865   template <typename Graph, typename Num,
   866 	    typename CapMap=typename Graph::template EdgeMap<Num>,
   867             typename FlowMap=typename Graph::template EdgeMap<Num> >
   868   class AugmentingFlow {
   869   protected:
   870     typedef typename Graph::Node Node;
   871     typedef typename Graph::NodeIt NodeIt;
   872     typedef typename Graph::EdgeIt EdgeIt;
   873     typedef typename Graph::OutEdgeIt OutEdgeIt;
   874     typedef typename Graph::InEdgeIt InEdgeIt;
   875 
   876 //    typedef typename std::vector<std::stack<Node> > VecStack;
   877 //    typedef typename Graph::template NodeMap<Node> NNMap;
   878 //    typedef typename std::vector<Node> VecNode;
   879 
   880     const Graph* g;
   881     Node s;
   882     Node t;
   883     const CapMap* capacity;
   884     FlowMap* flow;
   885 //    int n;      //the number of nodes of G
   886     typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
   887     //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
   888     typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
   889     typedef typename ResGW::Edge ResGWEdge;
   890     //typedef typename ResGW::template NodeMap<bool> ReachedMap;
   891     typedef typename Graph::template NodeMap<int> ReachedMap;
   892 
   893 
   894     //level works as a bool map in augmenting path algorithms and is
   895     //used by bfs for storing reached information.  In preflow, it
   896     //shows the levels of nodes.     
   897     ReachedMap level;
   898 
   899     //excess is needed only in preflow
   900 //    typename Graph::template NodeMap<Num> excess;
   901 
   902     //fixme    
   903 //   protected:
   904     //     MaxFlow() { }
   905     //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   906     // 	     FlowMap& _flow)
   907     //       {
   908     // 	g=&_G;
   909     // 	s=_s;
   910     // 	t=_t;
   911     // 	capacity=&_capacity;
   912     // 	flow=&_flow;
   913     // 	n=_G.nodeNum;
   914     // 	level.set (_G); //kellene vmi ilyesmi fv
   915     // 	excess(_G,0); //itt is
   916     //       }
   917 
   918     // constants used for heuristics
   919 //    static const int H0=20;
   920 //    static const int H1=1;
   921 
   922   public:
   923 
   924     ///Indicates the property of the starting flow.
   925 
   926     ///Indicates the property of the starting flow. The meanings are as follows:
   927     ///- \c ZERO_FLOW: constant zero flow
   928     ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
   929     ///the sum of the out-flows in every node except the \e source and
   930     ///the \e target.
   931     ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   932     ///least the sum of the out-flows in every node except the \e source.
   933     ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
   934     ///set to the constant zero flow in the beginning of the algorithm in this case.
   935     enum FlowEnum{
   936       ZERO_FLOW,
   937       GEN_FLOW,
   938       PRE_FLOW,
   939       NO_FLOW
   940     };
   941 
   942     enum StatusEnum {
   943       AFTER_NOTHING,
   944       AFTER_AUGMENTING,
   945       AFTER_FAST_AUGMENTING, 
   946       AFTER_PRE_FLOW_PHASE_1,      
   947       AFTER_PRE_FLOW_PHASE_2
   948     };
   949 
   950     /// Don not needle this flag only if necessary.
   951     StatusEnum status;
   952     int number_of_augmentations;
   953 
   954 
   955     template<typename IntMap>
   956     class TrickyReachedMap {
   957     protected:
   958       IntMap* map;
   959       int* number_of_augmentations;
   960     public:
   961       TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
   962 	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
   963       void set(const Node& n, bool b) {
   964 	if (b)
   965 	  map->set(n, *number_of_augmentations);
   966 	else 
   967 	  map->set(n, *number_of_augmentations-1);
   968       }
   969       bool operator[](const Node& n) const { 
   970 	return (*map)[n]==*number_of_augmentations; 
   971       }
   972     };
   973     
   974     AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   975 		   FlowMap& _flow) :
   976       g(&_G), s(_s), t(_t), capacity(&_capacity),
   977       flow(&_flow), //n(_G.nodeNum()), 
   978       level(_G), //excess(_G,0), 
   979       status(AFTER_NOTHING), number_of_augmentations(0) { }
   980 
   981     /// Starting from a flow, this method searches for an augmenting path
   982     /// according to the Edmonds-Karp algorithm
   983     /// and augments the flow on if any.
   984     /// The return value shows if the augmentation was succesful.
   985     bool augmentOnShortestPath();
   986     bool augmentOnShortestPath2();
   987 
   988     /// Starting from a flow, this method searches for an augmenting blocking
   989     /// flow according to Dinits' algorithm and augments the flow on if any.
   990     /// The blocking flow is computed in a physically constructed
   991     /// residual graph of type \c Mutablegraph.
   992     /// The return value show sif the augmentation was succesful.
   993     template<typename MutableGraph> bool augmentOnBlockingFlow();
   994 
   995     /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
   996     /// residual graph is not constructed physically.
   997     /// The return value shows if the augmentation was succesful.
   998     bool augmentOnBlockingFlow2();
   999 
  1000     template<typename _CutMap>
  1001     void actMinCut(_CutMap& M) const {
  1002       NodeIt v;
  1003       switch (status) {
  1004 	case AFTER_PRE_FLOW_PHASE_1:
  1005 //	std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
  1006 // 	for(g->first(v); g->valid(v); g->next(v)) {
  1007 // 	  if (level[v] < n) {
  1008 // 	    M.set(v, false);
  1009 // 	  } else {
  1010 // 	    M.set(v, true);
  1011 // 	  }
  1012 // 	}
  1013 	break;
  1014       case AFTER_PRE_FLOW_PHASE_2:
  1015 //	std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
  1016 	break;
  1017       case AFTER_NOTHING:
  1018 //	std::cout << "AFTER_NOTHING" << std::endl;
  1019 	minMinCut(M);
  1020 	break;
  1021       case AFTER_AUGMENTING:
  1022 //	std::cout << "AFTER_AUGMENTING" << std::endl;
  1023 	for(g->first(v); v!=INVALID; ++v) {
  1024 	  if (level[v]) {
  1025 	    M.set(v, true);
  1026 	  } else {
  1027 	    M.set(v, false);
  1028 	  }
  1029 	}
  1030 	break;
  1031       case AFTER_FAST_AUGMENTING:
  1032 //	std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
  1033 	for(g->first(v); v!=INVALID; ++v) {
  1034 	  if (level[v]==number_of_augmentations) {
  1035 	    M.set(v, true);
  1036 	  } else {
  1037 	    M.set(v, false);
  1038 	  }
  1039 	}
  1040 	break;
  1041       }
  1042     }
  1043 
  1044     template<typename _CutMap>
  1045     void minMinCut(_CutMap& M) const {
  1046       std::queue<Node> queue;
  1047 
  1048       M.set(s,true);
  1049       queue.push(s);
  1050 
  1051       while (!queue.empty()) {
  1052         Node w=queue.front();
  1053 	queue.pop();
  1054 
  1055 	OutEdgeIt e;
  1056 	for(g->first(e,w) ; e!=INVALID; ++e) {
  1057 	  Node v=g->head(e);
  1058 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
  1059 	    queue.push(v);
  1060 	    M.set(v, true);
  1061 	  }
  1062 	}
  1063 
  1064 	InEdgeIt f;
  1065 	for(g->first(f,w) ; f!=INVALID; ++f) {
  1066 	  Node v=g->tail(f);
  1067 	  if (!M[v] && (*flow)[f] > 0 ) {
  1068 	    queue.push(v);
  1069 	    M.set(v, true);
  1070 	  }
  1071 	}
  1072       }
  1073     }
  1074 
  1075     template<typename _CutMap>
  1076     void minMinCut2(_CutMap& M) const {
  1077       ResGW res_graph(*g, *capacity, *flow);
  1078       BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
  1079       bfs.pushAndSetReached(s);
  1080       while (!bfs.finished()) ++bfs;
  1081     }
  1082 
  1083     Num flowValue() const {
  1084       Num a=0;
  1085       for (InEdgeIt e(*g, t); e!=INVALID; ++e) a+=(*flow)[e];
  1086       for (OutEdgeIt e(*g, t); e!=INVALID; ++e) a-=(*flow)[e];
  1087       return a;
  1088       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
  1089     }
  1090 
  1091     template<typename MapGraphWrapper>
  1092     class DistanceMap {
  1093     protected:
  1094       const MapGraphWrapper* g;
  1095       typename MapGraphWrapper::template NodeMap<int> dist;
  1096     public:
  1097       DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
  1098       void set(const typename MapGraphWrapper::Node& n, int a) {
  1099 	dist.set(n, a);
  1100       }
  1101       int operator[](const typename MapGraphWrapper::Node& n) const { 
  1102 	return dist[n]; 
  1103       }
  1104       //       int get(const typename MapGraphWrapper::Node& n) const {
  1105       // 	return dist[n]; }
  1106       //       bool get(const typename MapGraphWrapper::Edge& e) const {
  1107       // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
  1108       bool operator[](const typename MapGraphWrapper::Edge& e) const {
  1109 	return (dist[g->tail(e)]<dist[g->head(e)]);
  1110       }
  1111     };
  1112 
  1113   };
  1114 
  1115 
  1116 
  1117   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1118   bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
  1119   {
  1120     ResGW res_graph(*g, *capacity, *flow);
  1121     bool _augment=false;
  1122 
  1123     //ReachedMap level(res_graph);
  1124     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1125     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1126     bfs.pushAndSetReached(s);
  1127 
  1128     typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1129     pred.set(s, INVALID);
  1130 
  1131     typename ResGW::template NodeMap<Num> free(res_graph);
  1132 
  1133     //searching for augmenting path
  1134     while ( !bfs.finished() ) {
  1135       ResGWEdge e=bfs;
  1136       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1137 	Node v=res_graph.tail(e);
  1138 	Node w=res_graph.head(e);
  1139 	pred.set(w, e);
  1140 	if (pred[v]!=INVALID) {
  1141 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
  1142 	} else {
  1143 	  free.set(w, res_graph.resCap(e));
  1144 	}
  1145 	if (res_graph.head(e)==t) { _augment=true; break; }
  1146       }
  1147 
  1148       ++bfs;
  1149     } //end of searching augmenting path
  1150 
  1151     if (_augment) {
  1152       Node n=t;
  1153       Num augment_value=free[t];
  1154       while (pred[n]!=INVALID) {
  1155 	ResGWEdge e=pred[n];
  1156 	res_graph.augment(e, augment_value);
  1157 	n=res_graph.tail(e);
  1158       }
  1159     }
  1160 
  1161     status=AFTER_AUGMENTING;
  1162     return _augment;
  1163   }
  1164 
  1165   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1166   bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
  1167   {
  1168     ResGW res_graph(*g, *capacity, *flow);
  1169     bool _augment=false;
  1170 
  1171     if (status!=AFTER_FAST_AUGMENTING) {
  1172       for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0); 
  1173       number_of_augmentations=1;
  1174     } else {
  1175       ++number_of_augmentations;
  1176     }
  1177     TrickyReachedMap<ReachedMap> 
  1178       tricky_reached_map(level, number_of_augmentations);
  1179     //ReachedMap level(res_graph);
  1180 //    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1181     BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > 
  1182       bfs(res_graph, tricky_reached_map);
  1183     bfs.pushAndSetReached(s);
  1184 
  1185     typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1186     pred.set(s, INVALID);
  1187 
  1188     typename ResGW::template NodeMap<Num> free(res_graph);
  1189 
  1190     //searching for augmenting path
  1191     while ( !bfs.finished() ) {
  1192       ResGWEdge e=bfs;
  1193       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1194 	Node v=res_graph.tail(e);
  1195 	Node w=res_graph.head(e);
  1196 	pred.set(w, e);
  1197 	if (pred[v]!=INVALID) {
  1198 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
  1199 	} else {
  1200 	  free.set(w, res_graph.resCap(e));
  1201 	}
  1202 	if (res_graph.head(e)==t) { _augment=true; break; }
  1203       }
  1204 
  1205       ++bfs;
  1206     } //end of searching augmenting path
  1207 
  1208     if (_augment) {
  1209       Node n=t;
  1210       Num augment_value=free[t];
  1211       while (pred[n]!=INVALID) {
  1212 	ResGWEdge e=pred[n];
  1213 	res_graph.augment(e, augment_value);
  1214 	n=res_graph.tail(e);
  1215       }
  1216     }
  1217 
  1218     status=AFTER_FAST_AUGMENTING;
  1219     return _augment;
  1220   }
  1221 
  1222 
  1223   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1224   template<typename MutableGraph>
  1225   bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
  1226   {
  1227     typedef MutableGraph MG;
  1228     bool _augment=false;
  1229 
  1230     ResGW res_graph(*g, *capacity, *flow);
  1231 
  1232     //bfs for distances on the residual graph
  1233     //ReachedMap level(res_graph);
  1234     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1235     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1236     bfs.pushAndSetReached(s);
  1237     typename ResGW::template NodeMap<int>
  1238       dist(res_graph); //filled up with 0's
  1239 
  1240     //F will contain the physical copy of the residual graph
  1241     //with the set of edges which are on shortest paths
  1242     MG F;
  1243     typename ResGW::template NodeMap<typename MG::Node>
  1244       res_graph_to_F(res_graph);
  1245     {
  1246       typename ResGW::NodeIt n;
  1247       for(res_graph.first(n); n!=INVALID; ++n) {
  1248 	res_graph_to_F.set(n, F.addNode());
  1249       }
  1250     }
  1251 
  1252     typename MG::Node sF=res_graph_to_F[s];
  1253     typename MG::Node tF=res_graph_to_F[t];
  1254     typename MG::template EdgeMap<ResGWEdge> original_edge(F);
  1255     typename MG::template EdgeMap<Num> residual_capacity(F);
  1256 
  1257     while ( !bfs.finished() ) {
  1258       ResGWEdge e=bfs;
  1259       if (e!=INVALID) {
  1260 	if (bfs.isBNodeNewlyReached()) {
  1261 	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1262 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1263 					res_graph_to_F[res_graph.head(e)]);
  1264 	  original_edge.update();
  1265 	  original_edge.set(f, e);
  1266 	  residual_capacity.update();
  1267 	  residual_capacity.set(f, res_graph.resCap(e));
  1268 	} else {
  1269 	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
  1270 	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1271 					  res_graph_to_F[res_graph.head(e)]);
  1272 	    original_edge.update();
  1273 	    original_edge.set(f, e);
  1274 	    residual_capacity.update();
  1275 	    residual_capacity.set(f, res_graph.resCap(e));
  1276 	  }
  1277 	}
  1278       }
  1279       ++bfs;
  1280     } //computing distances from s in the residual graph
  1281 
  1282     bool __augment=true;
  1283 
  1284     while (__augment) {
  1285       __augment=false;
  1286       //computing blocking flow with dfs
  1287       DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
  1288       typename MG::template NodeMap<typename MG::Edge> pred(F);
  1289       pred.set(sF, INVALID);
  1290       //invalid iterators for sources
  1291 
  1292       typename MG::template NodeMap<Num> free(F);
  1293 
  1294       dfs.pushAndSetReached(sF);
  1295       while (!dfs.finished()) {
  1296 	++dfs;
  1297 	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
  1298 	  if (dfs.isBNodeNewlyReached()) {
  1299 	    typename MG::Node v=F.tail(dfs);
  1300 	    typename MG::Node w=F.head(dfs);
  1301 	    pred.set(w, dfs);
  1302 	    if (pred[v]!=INVALID) {
  1303 	      free.set(w, std::min(free[v], residual_capacity[dfs]));
  1304 	    } else {
  1305 	      free.set(w, residual_capacity[dfs]);
  1306 	    }
  1307 	    if (w==tF) {
  1308 	      __augment=true;
  1309 	      _augment=true;
  1310 	      break;
  1311 	    }
  1312 
  1313 	  } else {
  1314 	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
  1315 	  }
  1316 	}
  1317       }
  1318 
  1319       if (__augment) {
  1320 	typename MG::Node n=tF;
  1321 	Num augment_value=free[tF];
  1322 	while (pred[n]!=INVALID) {
  1323 	  typename MG::Edge e=pred[n];
  1324 	  res_graph.augment(original_edge[e], augment_value);
  1325 	  n=F.tail(e);
  1326 	  if (residual_capacity[e]==augment_value)
  1327 	    F.erase(e);
  1328 	  else
  1329 	    residual_capacity.set(e, residual_capacity[e]-augment_value);
  1330 	}
  1331       }
  1332 
  1333     }
  1334 
  1335     status=AFTER_AUGMENTING;
  1336     return _augment;
  1337   }
  1338 
  1339 
  1340   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1341   bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
  1342   {
  1343     bool _augment=false;
  1344 
  1345     ResGW res_graph(*g, *capacity, *flow);
  1346 
  1347     //ReachedMap level(res_graph);
  1348     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1349     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1350 
  1351     bfs.pushAndSetReached(s);
  1352     DistanceMap<ResGW> dist(res_graph);
  1353     while ( !bfs.finished() ) {
  1354       ResGWEdge e=bfs;
  1355       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1356 	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1357       }
  1358       ++bfs;
  1359     } //computing distances from s in the residual graph
  1360 
  1361     //Subgraph containing the edges on some shortest paths
  1362     ConstMap<typename ResGW::Node, bool> true_map(true);
  1363     typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
  1364       DistanceMap<ResGW> > FilterResGW;
  1365     FilterResGW filter_res_graph(res_graph, true_map, dist);
  1366 
  1367     //Subgraph, which is able to delete edges which are already
  1368     //met by the dfs
  1369     typename FilterResGW::template NodeMap<typename FilterResGW::Edge>
  1370       first_out_edges(filter_res_graph);
  1371     typename FilterResGW::NodeIt v;
  1372     for(filter_res_graph.first(v); v!=INVALID; ++v)
  1373       {
  1374   	typename FilterResGW::OutEdgeIt e;
  1375   	filter_res_graph.first(e, v);
  1376   	first_out_edges.set(v, e);
  1377       }
  1378     typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
  1379       template NodeMap<typename FilterResGW::Edge> > ErasingResGW;
  1380     ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
  1381 
  1382     bool __augment=true;
  1383 
  1384     while (__augment) {
  1385 
  1386       __augment=false;
  1387       //computing blocking flow with dfs
  1388       DfsIterator< ErasingResGW,
  1389 	typename ErasingResGW::template NodeMap<bool> >
  1390 	dfs(erasing_res_graph);
  1391       typename ErasingResGW::
  1392 	template NodeMap<typename ErasingResGW::Edge> pred(erasing_res_graph);
  1393       pred.set(s, INVALID);
  1394       //invalid iterators for sources
  1395 
  1396       typename ErasingResGW::template NodeMap<Num>
  1397 	free1(erasing_res_graph);
  1398 
  1399       dfs.pushAndSetReached
  1400 	/// \bug hugo 0.2
  1401 	(typename ErasingResGW::Node
  1402 	 (typename FilterResGW::Node
  1403 	  (typename ResGW::Node(s)
  1404 	   )
  1405 	  )
  1406 	 );
  1407 	
  1408       while (!dfs.finished()) {
  1409 	++dfs;
  1410 	if (typename ErasingResGW::Edge(dfs)!=INVALID)
  1411  	  {
  1412   	    if (dfs.isBNodeNewlyReached()) {
  1413 
  1414  	      typename ErasingResGW::Node v=erasing_res_graph.tail(dfs);
  1415  	      typename ErasingResGW::Node w=erasing_res_graph.head(dfs);
  1416 
  1417  	      pred.set(w, typename ErasingResGW::Edge(dfs));
  1418  	      if (pred[v]!=INVALID) {
  1419  		free1.set
  1420 		  (w, std::min(free1[v], res_graph.resCap
  1421 			       (typename ErasingResGW::Edge(dfs))));
  1422  	      } else {
  1423  		free1.set
  1424 		  (w, res_graph.resCap
  1425 		   (typename ErasingResGW::Edge(dfs)));
  1426  	      }
  1427 
  1428  	      if (w==t) {
  1429  		__augment=true;
  1430  		_augment=true;
  1431  		break;
  1432  	      }
  1433  	    } else {
  1434  	      erasing_res_graph.erase(dfs);
  1435 	    }
  1436 	  }
  1437       }
  1438 
  1439       if (__augment) {
  1440 	typename ErasingResGW::Node
  1441 	  n=typename FilterResGW::Node(typename ResGW::Node(t));
  1442 	// 	  typename ResGW::NodeMap<Num> a(res_graph);
  1443 	// 	  typename ResGW::Node b;
  1444 	// 	  Num j=a[b];
  1445 	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
  1446 	// 	  typename FilterResGW::Node b1;
  1447 	// 	  Num j1=a1[b1];
  1448 	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
  1449 	// 	  typename ErasingResGW::Node b2;
  1450 	// 	  Num j2=a2[b2];
  1451 	Num augment_value=free1[n];
  1452 	while (pred[n]!=INVALID) {
  1453 	  typename ErasingResGW::Edge e=pred[n];
  1454 	  res_graph.augment(e, augment_value);
  1455 	  n=erasing_res_graph.tail(e);
  1456 	  if (res_graph.resCap(e)==0)
  1457 	    erasing_res_graph.erase(e);
  1458 	}
  1459       }
  1460 
  1461     } //while (__augment)
  1462 
  1463     status=AFTER_AUGMENTING;
  1464     return _augment;
  1465   }
  1466 
  1467 
  1468 } //namespace hugo
  1469 
  1470 #endif //HUGO_AUGMENTING_FLOW_H
  1471 
  1472 
  1473 
  1474