src/work/marci/augmenting_flow.h
changeset 762 511200bdb71f
child 775 e46a1f0623a0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/augmenting_flow.h	Tue Aug 17 11:20:16 2004 +0000
     1.3 @@ -0,0 +1,1477 @@
     1.4 +// -*- C++ -*-
     1.5 +#ifndef HUGO_AUGMENTING_FLOW_H
     1.6 +#define HUGO_AUGMENTING_FLOW_H
     1.7 +
     1.8 +#include <vector>
     1.9 +#include <queue>
    1.10 +#include <stack>
    1.11 +#include <iostream>
    1.12 +
    1.13 +#include <hugo/graph_wrapper.h>
    1.14 +#include <bfs_dfs.h>
    1.15 +#include <hugo/invalid.h>
    1.16 +#include <hugo/maps.h>
    1.17 +#include <for_each_macros.h>
    1.18 +
    1.19 +/// \file
    1.20 +/// \brief Maximum flow algorithms.
    1.21 +/// \ingroup galgs
    1.22 +
    1.23 +namespace hugo {
    1.24 +
    1.25 +  /// \addtogroup galgs
    1.26 +  /// @{                                                                                                                                        
    1.27 +  ///Maximum flow algorithms class.
    1.28 +
    1.29 +  ///This class provides various algorithms for finding a flow of
    1.30 +  ///maximum value in a directed graph. The \e source node, the \e
    1.31 +  ///target node, the \e capacity of the edges and the \e starting \e
    1.32 +  ///flow value of the edges should be passed to the algorithm through the
    1.33 +  ///constructor. It is possible to change these quantities using the
    1.34 +  ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
    1.35 +  ///\ref resetFlow. Before any subsequent runs of any algorithm of
    1.36 +  ///the class \ref resetFlow should be called. 
    1.37 +
    1.38 +  ///After running an algorithm of the class, the actual flow value 
    1.39 +  ///can be obtained by calling \ref flowValue(). The minimum
    1.40 +  ///value cut can be written into a \c node map of \c bools by
    1.41 +  ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
    1.42 +  ///the inclusionwise minimum and maximum of the minimum value
    1.43 +  ///cuts, resp.)                                                                                                                               
    1.44 +  ///\param Graph The directed graph type the algorithm runs on.
    1.45 +  ///\param Num The number type of the capacities and the flow values.
    1.46 +  ///\param CapMap The capacity map type.
    1.47 +  ///\param FlowMap The flow map type.                                                                                                           
    1.48 +  ///\author Marton Makai, Jacint Szabo 
    1.49 +//   template <typename Graph, typename Num,
    1.50 +// 	    typename CapMap=typename Graph::template EdgeMap<Num>,
    1.51 +//             typename FlowMap=typename Graph::template EdgeMap<Num> >
    1.52 +//   class MaxFlow {
    1.53 +//   protected:
    1.54 +//     typedef typename Graph::Node Node;
    1.55 +//     typedef typename Graph::NodeIt NodeIt;
    1.56 +//     typedef typename Graph::EdgeIt EdgeIt;
    1.57 +//     typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.58 +//     typedef typename Graph::InEdgeIt InEdgeIt;
    1.59 +
    1.60 +//     typedef typename std::vector<std::stack<Node> > VecStack;
    1.61 +//     typedef typename Graph::template NodeMap<Node> NNMap;
    1.62 +//     typedef typename std::vector<Node> VecNode;
    1.63 +
    1.64 +//     const Graph* g;
    1.65 +//     Node s;
    1.66 +//     Node t;
    1.67 +//     const CapMap* capacity;
    1.68 +//     FlowMap* flow;
    1.69 +//     int n;      //the number of nodes of G
    1.70 +//     typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
    1.71 +//     //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
    1.72 +//     typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
    1.73 +//     typedef typename ResGW::Edge ResGWEdge;
    1.74 +//     //typedef typename ResGW::template NodeMap<bool> ReachedMap;
    1.75 +//     typedef typename Graph::template NodeMap<int> ReachedMap;
    1.76 +
    1.77 +
    1.78 +//     //level works as a bool map in augmenting path algorithms and is
    1.79 +//     //used by bfs for storing reached information.  In preflow, it
    1.80 +//     //shows the levels of nodes.     
    1.81 +//     ReachedMap level;
    1.82 +
    1.83 +//     //excess is needed only in preflow
    1.84 +//     typename Graph::template NodeMap<Num> excess;
    1.85 +
    1.86 +//     //fixme    
    1.87 +// //   protected:
    1.88 +//     //     MaxFlow() { }
    1.89 +//     //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
    1.90 +//     // 	     FlowMap& _flow)
    1.91 +//     //       {
    1.92 +//     // 	g=&_G;
    1.93 +//     // 	s=_s;
    1.94 +//     // 	t=_t;
    1.95 +//     // 	capacity=&_capacity;
    1.96 +//     // 	flow=&_flow;
    1.97 +//     // 	n=_G.nodeNum;
    1.98 +//     // 	level.set (_G); //kellene vmi ilyesmi fv
    1.99 +//     // 	excess(_G,0); //itt is
   1.100 +//     //       }
   1.101 +
   1.102 +//     // constants used for heuristics
   1.103 +//     static const int H0=20;
   1.104 +//     static const int H1=1;
   1.105 +
   1.106 +//   public:
   1.107 +
   1.108 +//     ///Indicates the property of the starting flow.
   1.109 +
   1.110 +//     ///Indicates the property of the starting flow. The meanings are as follows:
   1.111 +//     ///- \c ZERO_FLOW: constant zero flow
   1.112 +//     ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
   1.113 +//     ///the sum of the out-flows in every node except the \e source and
   1.114 +//     ///the \e target.
   1.115 +//     ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   1.116 +//     ///least the sum of the out-flows in every node except the \e source.
   1.117 +//     ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
   1.118 +//     ///set to the constant zero flow in the beginning of the algorithm in this case.
   1.119 +//     enum FlowEnum{
   1.120 +//       ZERO_FLOW,
   1.121 +//       GEN_FLOW,
   1.122 +//       PRE_FLOW,
   1.123 +//       NO_FLOW
   1.124 +//     };
   1.125 +
   1.126 +//     enum StatusEnum {
   1.127 +//       AFTER_NOTHING,
   1.128 +//       AFTER_AUGMENTING,
   1.129 +//       AFTER_FAST_AUGMENTING, 
   1.130 +//       AFTER_PRE_FLOW_PHASE_1,      
   1.131 +//       AFTER_PRE_FLOW_PHASE_2
   1.132 +//     };
   1.133 +
   1.134 +//     /// Don not needle this flag only if necessary.
   1.135 +//     StatusEnum status;
   1.136 +// //     int number_of_augmentations;
   1.137 +
   1.138 +
   1.139 +// //     template<typename IntMap>
   1.140 +// //     class TrickyReachedMap {
   1.141 +// //     protected:
   1.142 +// //       IntMap* map;
   1.143 +// //       int* number_of_augmentations;
   1.144 +// //     public:
   1.145 +// //       TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
   1.146 +// // 	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
   1.147 +// //       void set(const Node& n, bool b) {
   1.148 +// // 	if (b)
   1.149 +// // 	  map->set(n, *number_of_augmentations);
   1.150 +// // 	else 
   1.151 +// // 	  map->set(n, *number_of_augmentations-1);
   1.152 +// //       }
   1.153 +// //       bool operator[](const Node& n) const { 
   1.154 +// // 	return (*map)[n]==*number_of_augmentations; 
   1.155 +// //       }
   1.156 +// //     };
   1.157 +    
   1.158 +//     MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   1.159 +// 	    FlowMap& _flow) :
   1.160 +//       g(&_G), s(_s), t(_t), capacity(&_capacity),
   1.161 +//       flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   1.162 +//       status(AFTER_NOTHING) { }
   1.163 +
   1.164 +//     ///Runs a maximum flow algorithm.
   1.165 +
   1.166 +//     ///Runs a preflow algorithm, which is the fastest maximum flow
   1.167 +//     ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
   1.168 +//     ///\pre The starting flow must be
   1.169 +//     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.170 +//     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.171 +//     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.172 +//     /// - any map if \c fe is NO_FLOW.
   1.173 +//     void run(FlowEnum fe=ZERO_FLOW) {
   1.174 +//       preflow(fe);
   1.175 +//     }
   1.176 +
   1.177 +                                                                              
   1.178 +//     ///Runs a preflow algorithm.  
   1.179 +
   1.180 +//     ///Runs a preflow algorithm. The preflow algorithms provide the
   1.181 +//     ///fastest way to compute a maximum flow in a directed graph.
   1.182 +//     ///\pre The starting flow must be
   1.183 +//     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.184 +//     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.185 +//     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.186 +//     /// - any map if \c fe is NO_FLOW.
   1.187 +//     void preflow(FlowEnum fe) {
   1.188 +//       preflowPhase1(fe);
   1.189 +//       preflowPhase2();
   1.190 +//     }
   1.191 +//     // Heuristics:
   1.192 +//     //   2 phase
   1.193 +//     //   gap
   1.194 +//     //   list 'level_list' on the nodes on level i implemented by hand
   1.195 +//     //   stack 'active' on the active nodes on level i                                                                                    
   1.196 +//     //   runs heuristic 'highest label' for H1*n relabels
   1.197 +//     //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
   1.198 +//     //   Parameters H0 and H1 are initialized to 20 and 1.
   1.199 +
   1.200 +//     ///Runs the first phase of the preflow algorithm.
   1.201 +
   1.202 +//     ///The preflow algorithm consists of two phases, this method runs the
   1.203 +//     ///first phase. After the first phase the maximum flow value and a
   1.204 +//     ///minimum value cut can already be computed, though a maximum flow
   1.205 +//     ///is net yet obtained. So after calling this method \ref flowValue
   1.206 +//     ///and \ref actMinCut gives proper results.
   1.207 +//     ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
   1.208 +//     ///give minimum value cuts unless calling \ref preflowPhase2.
   1.209 +//     ///\pre The starting flow must be
   1.210 +//     /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.211 +//     /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.212 +//     /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.213 +//     /// - any map if \c fe is NO_FLOW.
   1.214 +//     void preflowPhase1(FlowEnum fe);
   1.215 +
   1.216 +//     ///Runs the second phase of the preflow algorithm.
   1.217 +
   1.218 +//     ///The preflow algorithm consists of two phases, this method runs
   1.219 +//     ///the second phase. After calling \ref preflowPhase1 and then
   1.220 +//     ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
   1.221 +//     ///\ref minMinCut and \ref maxMinCut give proper results.
   1.222 +//     ///\pre \ref preflowPhase1 must be called before.
   1.223 +//     void preflowPhase2();
   1.224 +
   1.225 +//     /// Returns the maximum value of a flow.
   1.226 +
   1.227 +//     /// Returns the maximum value of a flow, by counting the 
   1.228 +//     /// over-flow of the target node \ref t.
   1.229 +//     /// It can be called already after running \ref preflowPhase1.
   1.230 +//     Num flowValue() const {
   1.231 +//       Num a=0;
   1.232 +//       FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
   1.233 +//       FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
   1.234 +//       return a;
   1.235 +//       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
   1.236 +//     }
   1.237 +
   1.238 +//     ///Returns a minimum value cut after calling \ref preflowPhase1.
   1.239 +
   1.240 +//     ///After the first phase of the preflow algorithm the maximum flow
   1.241 +//     ///value and a minimum value cut can already be computed. This
   1.242 +//     ///method can be called after running \ref preflowPhase1 for
   1.243 +//     ///obtaining a minimum value cut.
   1.244 +//     /// \warning Gives proper result only right after calling \ref
   1.245 +//     /// preflowPhase1.
   1.246 +//     /// \todo We have to make some status variable which shows the
   1.247 +//     /// actual state
   1.248 +//     /// of the class. This enables us to determine which methods are valid
   1.249 +//     /// for MinCut computation
   1.250 +//     template<typename _CutMap>
   1.251 +//     void actMinCut(_CutMap& M) const {
   1.252 +//       NodeIt v;
   1.253 +//       switch (status) {
   1.254 +//       case AFTER_PRE_FLOW_PHASE_1:
   1.255 +// 	for(g->first(v); g->valid(v); g->next(v)) {
   1.256 +// 	  if (level[v] < n) {
   1.257 +// 	    M.set(v, false);
   1.258 +// 	  } else {
   1.259 +// 	    M.set(v, true);
   1.260 +// 	  }
   1.261 +// 	}
   1.262 +// 	break;
   1.263 +//       case AFTER_PRE_FLOW_PHASE_2:
   1.264 +//       case AFTER_NOTHING:
   1.265 +//       case AFTER_AUGMENTING:
   1.266 +//       case AFTER_FAST_AUGMENTING:
   1.267 +// 	minMinCut(M);
   1.268 +// 	break;
   1.269 +// //       case AFTER_AUGMENTING:
   1.270 +// // 	for(g->first(v); g->valid(v); g->next(v)) {
   1.271 +// // 	  if (level[v]) {
   1.272 +// // 	    M.set(v, true);
   1.273 +// // 	  } else {
   1.274 +// // 	    M.set(v, false);
   1.275 +// // 	  }
   1.276 +// // 	}
   1.277 +// // 	break;
   1.278 +// //       case AFTER_FAST_AUGMENTING:
   1.279 +// // 	for(g->first(v); g->valid(v); g->next(v)) {
   1.280 +// // 	  if (level[v]==number_of_augmentations) {
   1.281 +// // 	    M.set(v, true);
   1.282 +// // 	  } else {
   1.283 +// // 	    M.set(v, false);
   1.284 +// // 	  }
   1.285 +// // 	}
   1.286 +// // 	break;
   1.287 +//       }
   1.288 +//     }
   1.289 +
   1.290 +//     ///Returns the inclusionwise minimum of the minimum value cuts.
   1.291 +
   1.292 +//     ///Sets \c M to the characteristic vector of the minimum value cut
   1.293 +//     ///which is inclusionwise minimum. It is computed by processing
   1.294 +//     ///a bfs from the source node \c s in the residual graph.
   1.295 +//     ///\pre M should be a node map of bools initialized to false.
   1.296 +//     ///\pre \c flow must be a maximum flow.
   1.297 +//     template<typename _CutMap>
   1.298 +//     void minMinCut(_CutMap& M) const {
   1.299 +//       std::queue<Node> queue;
   1.300 +
   1.301 +//       M.set(s,true);
   1.302 +//       queue.push(s);
   1.303 +
   1.304 +//       while (!queue.empty()) {
   1.305 +//         Node w=queue.front();
   1.306 +// 	queue.pop();
   1.307 +
   1.308 +// 	OutEdgeIt e;
   1.309 +// 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   1.310 +// 	  Node v=g->head(e);
   1.311 +// 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.312 +// 	    queue.push(v);
   1.313 +// 	    M.set(v, true);
   1.314 +// 	  }
   1.315 +// 	}
   1.316 +
   1.317 +// 	InEdgeIt f;
   1.318 +// 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   1.319 +// 	  Node v=g->tail(f);
   1.320 +// 	  if (!M[v] && (*flow)[f] > 0 ) {
   1.321 +// 	    queue.push(v);
   1.322 +// 	    M.set(v, true);
   1.323 +// 	  }
   1.324 +// 	}
   1.325 +//       }
   1.326 +//     }
   1.327 +
   1.328 +//     ///Returns the inclusionwise maximum of the minimum value cuts.
   1.329 +
   1.330 +//     ///Sets \c M to the characteristic vector of the minimum value cut
   1.331 +//     ///which is inclusionwise maximum. It is computed by processing a
   1.332 +//     ///backward bfs from the target node \c t in the residual graph.
   1.333 +//     ///\pre M should be a node map of bools initialized to false.
   1.334 +//     ///\pre \c flow must be a maximum flow. 
   1.335 +//     template<typename _CutMap>
   1.336 +//     void maxMinCut(_CutMap& M) const {
   1.337 +
   1.338 +//       NodeIt v;
   1.339 +//       for(g->first(v) ; g->valid(v); g->next(v)) {
   1.340 +// 	M.set(v, true);
   1.341 +//       }
   1.342 +
   1.343 +//       std::queue<Node> queue;
   1.344 +
   1.345 +//       M.set(t,false);
   1.346 +//       queue.push(t);
   1.347 +
   1.348 +//       while (!queue.empty()) {
   1.349 +//         Node w=queue.front();
   1.350 +// 	queue.pop();
   1.351 +
   1.352 +// 	InEdgeIt e;
   1.353 +// 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   1.354 +// 	  Node v=g->tail(e);
   1.355 +// 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.356 +// 	    queue.push(v);
   1.357 +// 	    M.set(v, false);
   1.358 +// 	  }
   1.359 +// 	}
   1.360 +
   1.361 +// 	OutEdgeIt f;
   1.362 +// 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   1.363 +// 	  Node v=g->head(f);
   1.364 +// 	  if (M[v] && (*flow)[f] > 0 ) {
   1.365 +// 	    queue.push(v);
   1.366 +// 	    M.set(v, false);
   1.367 +// 	  }
   1.368 +// 	}
   1.369 +//       }
   1.370 +//     }
   1.371 +
   1.372 +//     ///Returns a minimum value cut.
   1.373 +
   1.374 +//     ///Sets \c M to the characteristic vector of a minimum value cut.
   1.375 +//     ///\pre M should be a node map of bools initialized to false.
   1.376 +//     ///\pre \c flow must be a maximum flow.    
   1.377 +//     template<typename CutMap>
   1.378 +//     void minCut(CutMap& M) const { minMinCut(M); }
   1.379 +
   1.380 +//     ///Resets the source node to \c _s.
   1.381 +
   1.382 +//     ///Resets the source node to \c _s.
   1.383 +//     /// 
   1.384 +//     void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
   1.385 +
   1.386 +//     ///Resets the target node to \c _t.
   1.387 +
   1.388 +//     ///Resets the target node to \c _t.
   1.389 +//     ///
   1.390 +//     void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
   1.391 +
   1.392 +//     /// Resets the edge map of the capacities to _cap.
   1.393 +
   1.394 +//     /// Resets the edge map of the capacities to _cap.
   1.395 +//     /// 
   1.396 +//     void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
   1.397 +
   1.398 +//     /// Resets the edge map of the flows to _flow.
   1.399 +
   1.400 +//     /// Resets the edge map of the flows to _flow.
   1.401 +//     /// 
   1.402 +//     void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
   1.403 +
   1.404 +
   1.405 +//   private:
   1.406 +
   1.407 +//     int push(Node w, VecStack& active) {
   1.408 +
   1.409 +//       int lev=level[w];
   1.410 +//       Num exc=excess[w];
   1.411 +//       int newlevel=n;       //bound on the next level of w
   1.412 +
   1.413 +//       OutEdgeIt e;
   1.414 +//       for(g->first(e,w); g->valid(e); g->next(e)) {
   1.415 +
   1.416 +// 	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   1.417 +// 	Node v=g->head(e);
   1.418 +
   1.419 +// 	if( lev > level[v] ) { //Push is allowed now
   1.420 +
   1.421 +// 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   1.422 +// 	    int lev_v=level[v];
   1.423 +// 	    active[lev_v].push(v);
   1.424 +// 	  }
   1.425 +
   1.426 +// 	  Num cap=(*capacity)[e];
   1.427 +// 	  Num flo=(*flow)[e];
   1.428 +// 	  Num remcap=cap-flo;
   1.429 +
   1.430 +// 	  if ( remcap >= exc ) { //A nonsaturating push.
   1.431 +
   1.432 +// 	    flow->set(e, flo+exc);
   1.433 +// 	    excess.set(v, excess[v]+exc);
   1.434 +// 	    exc=0;
   1.435 +// 	    break;
   1.436 +
   1.437 +// 	  } else { //A saturating push.
   1.438 +// 	    flow->set(e, cap);
   1.439 +// 	    excess.set(v, excess[v]+remcap);
   1.440 +// 	    exc-=remcap;
   1.441 +// 	  }
   1.442 +// 	} else if ( newlevel > level[v] ) newlevel = level[v];
   1.443 +//       } //for out edges wv
   1.444 +
   1.445 +//       if ( exc > 0 ) {
   1.446 +// 	InEdgeIt e;
   1.447 +// 	for(g->first(e,w); g->valid(e); g->next(e)) {
   1.448 +
   1.449 +// 	  if( (*flow)[e] <= 0 ) continue;
   1.450 +// 	  Node v=g->tail(e);
   1.451 +
   1.452 +// 	  if( lev > level[v] ) { //Push is allowed now
   1.453 +
   1.454 +// 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   1.455 +// 	      int lev_v=level[v];
   1.456 +// 	      active[lev_v].push(v);
   1.457 +// 	    }
   1.458 +
   1.459 +// 	    Num flo=(*flow)[e];
   1.460 +
   1.461 +// 	    if ( flo >= exc ) { //A nonsaturating push.
   1.462 +
   1.463 +// 	      flow->set(e, flo-exc);
   1.464 +// 	      excess.set(v, excess[v]+exc);
   1.465 +// 	      exc=0;
   1.466 +// 	      break;
   1.467 +// 	    } else {  //A saturating push.
   1.468 +
   1.469 +// 	      excess.set(v, excess[v]+flo);
   1.470 +// 	      exc-=flo;
   1.471 +// 	      flow->set(e,0);
   1.472 +// 	    }
   1.473 +// 	  } else if ( newlevel > level[v] ) newlevel = level[v];
   1.474 +// 	} //for in edges vw
   1.475 +
   1.476 +//       } // if w still has excess after the out edge for cycle
   1.477 +
   1.478 +//       excess.set(w, exc);
   1.479 +
   1.480 +//       return newlevel;
   1.481 +//     }
   1.482 +
   1.483 +
   1.484 +//     void preflowPreproc(FlowEnum fe, VecStack& active,
   1.485 +// 			VecNode& level_list, NNMap& left, NNMap& right)
   1.486 +//     {
   1.487 +//       std::queue<Node> bfs_queue;
   1.488 +
   1.489 +//       switch (fe) {
   1.490 +//       case NO_FLOW:   //flow is already set to const zero in this case
   1.491 +//       case ZERO_FLOW:
   1.492 +// 	{
   1.493 +// 	  //Reverse_bfs from t, to find the starting level.
   1.494 +// 	  level.set(t,0);
   1.495 +// 	  bfs_queue.push(t);
   1.496 +
   1.497 +// 	  while (!bfs_queue.empty()) {
   1.498 +
   1.499 +// 	    Node v=bfs_queue.front();
   1.500 +// 	    bfs_queue.pop();
   1.501 +// 	    int l=level[v]+1;
   1.502 +
   1.503 +// 	    InEdgeIt e;
   1.504 +// 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   1.505 +// 	      Node w=g->tail(e);
   1.506 +// 	      if ( level[w] == n && w != s ) {
   1.507 +// 		bfs_queue.push(w);
   1.508 +// 		Node first=level_list[l];
   1.509 +// 		if ( g->valid(first) ) left.set(first,w);
   1.510 +// 		right.set(w,first);
   1.511 +// 		level_list[l]=w;
   1.512 +// 		level.set(w, l);
   1.513 +// 	      }
   1.514 +// 	    }
   1.515 +// 	  }
   1.516 +
   1.517 +// 	  //the starting flow
   1.518 +// 	  OutEdgeIt e;
   1.519 +// 	  for(g->first(e,s); g->valid(e); g->next(e))
   1.520 +// 	    {
   1.521 +// 	      Num c=(*capacity)[e];
   1.522 +// 	      if ( c <= 0 ) continue;
   1.523 +// 	      Node w=g->head(e);
   1.524 +// 	      if ( level[w] < n ) {
   1.525 +// 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.526 +// 		flow->set(e, c);
   1.527 +// 		excess.set(w, excess[w]+c);
   1.528 +// 	      }
   1.529 +// 	    }
   1.530 +// 	  break;
   1.531 +// 	}
   1.532 +
   1.533 +//       case GEN_FLOW:
   1.534 +//       case PRE_FLOW:
   1.535 +// 	{
   1.536 +// 	  //Reverse_bfs from t in the residual graph,
   1.537 +// 	  //to find the starting level.
   1.538 +// 	  level.set(t,0);
   1.539 +// 	  bfs_queue.push(t);
   1.540 +
   1.541 +// 	  while (!bfs_queue.empty()) {
   1.542 +
   1.543 +// 	    Node v=bfs_queue.front();
   1.544 +// 	    bfs_queue.pop();
   1.545 +// 	    int l=level[v]+1;
   1.546 +
   1.547 +// 	    InEdgeIt e;
   1.548 +// 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   1.549 +// 	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.550 +// 	      Node w=g->tail(e);
   1.551 +// 	      if ( level[w] == n && w != s ) {
   1.552 +// 		bfs_queue.push(w);
   1.553 +// 		Node first=level_list[l];
   1.554 +// 		if ( g->valid(first) ) left.set(first,w);
   1.555 +// 		right.set(w,first);
   1.556 +// 		level_list[l]=w;
   1.557 +// 		level.set(w, l);
   1.558 +// 	      }
   1.559 +// 	    }
   1.560 +
   1.561 +// 	    OutEdgeIt f;
   1.562 +// 	    for(g->first(f,v); g->valid(f); g->next(f)) {
   1.563 +// 	      if ( 0 >= (*flow)[f] ) continue;
   1.564 +// 	      Node w=g->head(f);
   1.565 +// 	      if ( level[w] == n && w != s ) {
   1.566 +// 		bfs_queue.push(w);
   1.567 +// 		Node first=level_list[l];
   1.568 +// 		if ( g->valid(first) ) left.set(first,w);
   1.569 +// 		right.set(w,first);
   1.570 +// 		level_list[l]=w;
   1.571 +// 		level.set(w, l);
   1.572 +// 	      }
   1.573 +// 	    }
   1.574 +// 	  }
   1.575 +
   1.576 +
   1.577 +// 	  //the starting flow
   1.578 +// 	  OutEdgeIt e;
   1.579 +// 	  for(g->first(e,s); g->valid(e); g->next(e))
   1.580 +// 	    {
   1.581 +// 	      Num rem=(*capacity)[e]-(*flow)[e];
   1.582 +// 	      if ( rem <= 0 ) continue;
   1.583 +// 	      Node w=g->head(e);
   1.584 +// 	      if ( level[w] < n ) {
   1.585 +// 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.586 +// 		flow->set(e, (*capacity)[e]);
   1.587 +// 		excess.set(w, excess[w]+rem);
   1.588 +// 	      }
   1.589 +// 	    }
   1.590 +
   1.591 +// 	  InEdgeIt f;
   1.592 +// 	  for(g->first(f,s); g->valid(f); g->next(f))
   1.593 +// 	    {
   1.594 +// 	      if ( (*flow)[f] <= 0 ) continue;
   1.595 +// 	      Node w=g->tail(f);
   1.596 +// 	      if ( level[w] < n ) {
   1.597 +// 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.598 +// 		excess.set(w, excess[w]+(*flow)[f]);
   1.599 +// 		flow->set(f, 0);
   1.600 +// 	      }
   1.601 +// 	    }
   1.602 +// 	  break;
   1.603 +// 	} //case PRE_FLOW
   1.604 +//       }
   1.605 +//     } //preflowPreproc
   1.606 +
   1.607 +
   1.608 +
   1.609 +//     void relabel(Node w, int newlevel, VecStack& active,
   1.610 +// 		 VecNode& level_list, NNMap& left,
   1.611 +// 		 NNMap& right, int& b, int& k, bool what_heur )
   1.612 +//     {
   1.613 +
   1.614 +//       //FIXME jacint: ez mitol num
   1.615 +// //      Num lev=level[w];
   1.616 +//       int lev=level[w];
   1.617 +
   1.618 +//       Node right_n=right[w];
   1.619 +//       Node left_n=left[w];
   1.620 +
   1.621 +//       //unlacing starts
   1.622 +//       if ( g->valid(right_n) ) {
   1.623 +// 	if ( g->valid(left_n) ) {
   1.624 +// 	  right.set(left_n, right_n);
   1.625 +// 	  left.set(right_n, left_n);
   1.626 +// 	} else {
   1.627 +// 	  level_list[lev]=right_n;
   1.628 +// 	  left.set(right_n, INVALID);
   1.629 +// 	}
   1.630 +//       } else {
   1.631 +// 	if ( g->valid(left_n) ) {
   1.632 +// 	  right.set(left_n, INVALID);
   1.633 +// 	} else {
   1.634 +// 	  level_list[lev]=INVALID;
   1.635 +// 	}
   1.636 +//       }
   1.637 +//       //unlacing ends
   1.638 +
   1.639 +//       if ( !g->valid(level_list[lev]) ) {
   1.640 +
   1.641 +// 	//gapping starts
   1.642 +// 	for (int i=lev; i!=k ; ) {
   1.643 +// 	  Node v=level_list[++i];
   1.644 +// 	  while ( g->valid(v) ) {
   1.645 +// 	    level.set(v,n);
   1.646 +// 	    v=right[v];
   1.647 +// 	  }
   1.648 +// 	  level_list[i]=INVALID;
   1.649 +// 	  if ( !what_heur ) {
   1.650 +// 	    while ( !active[i].empty() ) {
   1.651 +// 	      active[i].pop();    //FIXME: ezt szebben kene
   1.652 +// 	    }
   1.653 +// 	  }
   1.654 +// 	}
   1.655 +
   1.656 +// 	level.set(w,n);
   1.657 +// 	b=lev-1;
   1.658 +// 	k=b;
   1.659 +// 	//gapping ends
   1.660 +
   1.661 +//       } else {
   1.662 +
   1.663 +// 	if ( newlevel == n ) level.set(w,n);
   1.664 +// 	else {
   1.665 +// 	  level.set(w,++newlevel);
   1.666 +// 	  active[newlevel].push(w);
   1.667 +// 	  if ( what_heur ) b=newlevel;
   1.668 +// 	  if ( k < newlevel ) ++k;      //now k=newlevel
   1.669 +// 	  Node first=level_list[newlevel];
   1.670 +// 	  if ( g->valid(first) ) left.set(first,w);
   1.671 +// 	  right.set(w,first);
   1.672 +// 	  left.set(w,INVALID);
   1.673 +// 	  level_list[newlevel]=w;
   1.674 +// 	}
   1.675 +//       }
   1.676 +
   1.677 +//     } //relabel
   1.678 +
   1.679 +//   };
   1.680 +
   1.681 +
   1.682 +
   1.683 +//   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.684 +//   void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
   1.685 +//   {
   1.686 +
   1.687 +//     int heur0=(int)(H0*n);  //time while running 'bound decrease'
   1.688 +//     int heur1=(int)(H1*n);  //time while running 'highest label'
   1.689 +//     int heur=heur1;         //starting time interval (#of relabels)
   1.690 +//     int numrelabel=0;
   1.691 +
   1.692 +//     bool what_heur=1;
   1.693 +//     //It is 0 in case 'bound decrease' and 1 in case 'highest label'
   1.694 +
   1.695 +//     bool end=false;
   1.696 +//     //Needed for 'bound decrease', true means no active nodes are above bound
   1.697 +//     //b.
   1.698 +
   1.699 +//     int k=n-2;  //bound on the highest level under n containing a node
   1.700 +//     int b=k;    //bound on the highest level under n of an active node
   1.701 +
   1.702 +//     VecStack active(n);
   1.703 +
   1.704 +//     NNMap left(*g, INVALID);
   1.705 +//     NNMap right(*g, INVALID);
   1.706 +//     VecNode level_list(n,INVALID);
   1.707 +//     //List of the nodes in level i<n, set to n.
   1.708 +
   1.709 +//     NodeIt v;
   1.710 +//     for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
   1.711 +//     //setting each node to level n
   1.712 +
   1.713 +//     if ( fe == NO_FLOW ) {
   1.714 +//       EdgeIt e;
   1.715 +//       for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
   1.716 +//     }
   1.717 +
   1.718 +//     switch (fe) { //computing the excess
   1.719 +//     case PRE_FLOW:
   1.720 +//       {
   1.721 +// 	NodeIt v;
   1.722 +// 	for(g->first(v); g->valid(v); g->next(v)) {
   1.723 +// 	  Num exc=0;
   1.724 +
   1.725 +// 	  InEdgeIt e;
   1.726 +// 	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
   1.727 +// 	  OutEdgeIt f;
   1.728 +// 	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
   1.729 +
   1.730 +// 	  excess.set(v,exc);
   1.731 +
   1.732 +// 	  //putting the active nodes into the stack
   1.733 +// 	  int lev=level[v];
   1.734 +// 	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
   1.735 +// 	}
   1.736 +// 	break;
   1.737 +//       }
   1.738 +//     case GEN_FLOW:
   1.739 +//       {
   1.740 +// 	NodeIt v;
   1.741 +// 	for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   1.742 +
   1.743 +// 	Num exc=0;
   1.744 +// 	InEdgeIt e;
   1.745 +// 	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
   1.746 +// 	OutEdgeIt f;
   1.747 +// 	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
   1.748 +// 	excess.set(t,exc);
   1.749 +// 	break;
   1.750 +//       }
   1.751 +//     case ZERO_FLOW:
   1.752 +//     case NO_FLOW:
   1.753 +//       {
   1.754 +// 	NodeIt v;
   1.755 +//         for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   1.756 +// 	break;
   1.757 +//       }
   1.758 +//     }
   1.759 +
   1.760 +//     preflowPreproc(fe, active, level_list, left, right);
   1.761 +//     //End of preprocessing
   1.762 +
   1.763 +
   1.764 +//     //Push/relabel on the highest level active nodes.
   1.765 +//     while ( true ) {
   1.766 +//       if ( b == 0 ) {
   1.767 +// 	if ( !what_heur && !end && k > 0 ) {
   1.768 +// 	  b=k;
   1.769 +// 	  end=true;
   1.770 +// 	} else break;
   1.771 +//       }
   1.772 +
   1.773 +//       if ( active[b].empty() ) --b;
   1.774 +//       else {
   1.775 +// 	end=false;
   1.776 +// 	Node w=active[b].top();
   1.777 +// 	active[b].pop();
   1.778 +// 	int newlevel=push(w,active);
   1.779 +// 	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
   1.780 +// 				     left, right, b, k, what_heur);
   1.781 +
   1.782 +// 	++numrelabel;
   1.783 +// 	if ( numrelabel >= heur ) {
   1.784 +// 	  numrelabel=0;
   1.785 +// 	  if ( what_heur ) {
   1.786 +// 	    what_heur=0;
   1.787 +// 	    heur=heur0;
   1.788 +// 	    end=false;
   1.789 +// 	  } else {
   1.790 +// 	    what_heur=1;
   1.791 +// 	    heur=heur1;
   1.792 +// 	    b=k;
   1.793 +// 	  }
   1.794 +// 	}
   1.795 +//       }
   1.796 +//     }
   1.797 +
   1.798 +//     status=AFTER_PRE_FLOW_PHASE_1;
   1.799 +//   }
   1.800 +
   1.801 +
   1.802 +
   1.803 +//   template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.804 +//   void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
   1.805 +//   {
   1.806 +
   1.807 +//     int k=n-2;  //bound on the highest level under n containing a node
   1.808 +//     int b=k;    //bound on the highest level under n of an active node
   1.809 +
   1.810 +//     VecStack active(n);
   1.811 +//     level.set(s,0);
   1.812 +//     std::queue<Node> bfs_queue;
   1.813 +//     bfs_queue.push(s);
   1.814 +
   1.815 +//     while (!bfs_queue.empty()) {
   1.816 +
   1.817 +//       Node v=bfs_queue.front();
   1.818 +//       bfs_queue.pop();
   1.819 +//       int l=level[v]+1;
   1.820 +
   1.821 +//       InEdgeIt e;
   1.822 +//       for(g->first(e,v); g->valid(e); g->next(e)) {
   1.823 +// 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.824 +// 	Node u=g->tail(e);
   1.825 +// 	if ( level[u] >= n ) {
   1.826 +// 	  bfs_queue.push(u);
   1.827 +// 	  level.set(u, l);
   1.828 +// 	  if ( excess[u] > 0 ) active[l].push(u);
   1.829 +// 	}
   1.830 +//       }
   1.831 +
   1.832 +//       OutEdgeIt f;
   1.833 +//       for(g->first(f,v); g->valid(f); g->next(f)) {
   1.834 +// 	if ( 0 >= (*flow)[f] ) continue;
   1.835 +// 	Node u=g->head(f);
   1.836 +// 	if ( level[u] >= n ) {
   1.837 +// 	  bfs_queue.push(u);
   1.838 +// 	  level.set(u, l);
   1.839 +// 	  if ( excess[u] > 0 ) active[l].push(u);
   1.840 +// 	}
   1.841 +//       }
   1.842 +//     }
   1.843 +//     b=n-2;
   1.844 +
   1.845 +//     while ( true ) {
   1.846 +
   1.847 +//       if ( b == 0 ) break;
   1.848 +
   1.849 +//       if ( active[b].empty() ) --b;
   1.850 +//       else {
   1.851 +// 	Node w=active[b].top();
   1.852 +// 	active[b].pop();
   1.853 +// 	int newlevel=push(w,active);
   1.854 +
   1.855 +// 	//relabel
   1.856 +// 	if ( excess[w] > 0 ) {
   1.857 +// 	  level.set(w,++newlevel);
   1.858 +// 	  active[newlevel].push(w);
   1.859 +// 	  b=newlevel;
   1.860 +// 	}
   1.861 +//       }  // if stack[b] is nonempty
   1.862 +//     } // while(true)
   1.863 +
   1.864 +//     status=AFTER_PRE_FLOW_PHASE_2;
   1.865 +//   }
   1.866 +
   1.867 +
   1.868 +  template <typename Graph, typename Num,
   1.869 +	    typename CapMap=typename Graph::template EdgeMap<Num>,
   1.870 +            typename FlowMap=typename Graph::template EdgeMap<Num> >
   1.871 +  class AugmentingFlow {
   1.872 +  protected:
   1.873 +    typedef typename Graph::Node Node;
   1.874 +    typedef typename Graph::NodeIt NodeIt;
   1.875 +    typedef typename Graph::EdgeIt EdgeIt;
   1.876 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
   1.877 +    typedef typename Graph::InEdgeIt InEdgeIt;
   1.878 +
   1.879 +//    typedef typename std::vector<std::stack<Node> > VecStack;
   1.880 +//    typedef typename Graph::template NodeMap<Node> NNMap;
   1.881 +//    typedef typename std::vector<Node> VecNode;
   1.882 +
   1.883 +    const Graph* g;
   1.884 +    Node s;
   1.885 +    Node t;
   1.886 +    const CapMap* capacity;
   1.887 +    FlowMap* flow;
   1.888 +//    int n;      //the number of nodes of G
   1.889 +    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
   1.890 +    //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
   1.891 +    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
   1.892 +    typedef typename ResGW::Edge ResGWEdge;
   1.893 +    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
   1.894 +    typedef typename Graph::template NodeMap<int> ReachedMap;
   1.895 +
   1.896 +
   1.897 +    //level works as a bool map in augmenting path algorithms and is
   1.898 +    //used by bfs for storing reached information.  In preflow, it
   1.899 +    //shows the levels of nodes.     
   1.900 +    ReachedMap level;
   1.901 +
   1.902 +    //excess is needed only in preflow
   1.903 +//    typename Graph::template NodeMap<Num> excess;
   1.904 +
   1.905 +    //fixme    
   1.906 +//   protected:
   1.907 +    //     MaxFlow() { }
   1.908 +    //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   1.909 +    // 	     FlowMap& _flow)
   1.910 +    //       {
   1.911 +    // 	g=&_G;
   1.912 +    // 	s=_s;
   1.913 +    // 	t=_t;
   1.914 +    // 	capacity=&_capacity;
   1.915 +    // 	flow=&_flow;
   1.916 +    // 	n=_G.nodeNum;
   1.917 +    // 	level.set (_G); //kellene vmi ilyesmi fv
   1.918 +    // 	excess(_G,0); //itt is
   1.919 +    //       }
   1.920 +
   1.921 +    // constants used for heuristics
   1.922 +//    static const int H0=20;
   1.923 +//    static const int H1=1;
   1.924 +
   1.925 +  public:
   1.926 +
   1.927 +    ///Indicates the property of the starting flow.
   1.928 +
   1.929 +    ///Indicates the property of the starting flow. The meanings are as follows:
   1.930 +    ///- \c ZERO_FLOW: constant zero flow
   1.931 +    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
   1.932 +    ///the sum of the out-flows in every node except the \e source and
   1.933 +    ///the \e target.
   1.934 +    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   1.935 +    ///least the sum of the out-flows in every node except the \e source.
   1.936 +    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
   1.937 +    ///set to the constant zero flow in the beginning of the algorithm in this case.
   1.938 +    enum FlowEnum{
   1.939 +      ZERO_FLOW,
   1.940 +      GEN_FLOW,
   1.941 +      PRE_FLOW,
   1.942 +      NO_FLOW
   1.943 +    };
   1.944 +
   1.945 +    enum StatusEnum {
   1.946 +      AFTER_NOTHING,
   1.947 +      AFTER_AUGMENTING,
   1.948 +      AFTER_FAST_AUGMENTING, 
   1.949 +      AFTER_PRE_FLOW_PHASE_1,      
   1.950 +      AFTER_PRE_FLOW_PHASE_2
   1.951 +    };
   1.952 +
   1.953 +    /// Don not needle this flag only if necessary.
   1.954 +    StatusEnum status;
   1.955 +    int number_of_augmentations;
   1.956 +
   1.957 +
   1.958 +    template<typename IntMap>
   1.959 +    class TrickyReachedMap {
   1.960 +    protected:
   1.961 +      IntMap* map;
   1.962 +      int* number_of_augmentations;
   1.963 +    public:
   1.964 +      TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
   1.965 +	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
   1.966 +      void set(const Node& n, bool b) {
   1.967 +	if (b)
   1.968 +	  map->set(n, *number_of_augmentations);
   1.969 +	else 
   1.970 +	  map->set(n, *number_of_augmentations-1);
   1.971 +      }
   1.972 +      bool operator[](const Node& n) const { 
   1.973 +	return (*map)[n]==*number_of_augmentations; 
   1.974 +      }
   1.975 +    };
   1.976 +    
   1.977 +    AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   1.978 +		   FlowMap& _flow) :
   1.979 +      g(&_G), s(_s), t(_t), capacity(&_capacity),
   1.980 +      flow(&_flow), //n(_G.nodeNum()), 
   1.981 +      level(_G), //excess(_G,0), 
   1.982 +      status(AFTER_NOTHING), number_of_augmentations(0) { }
   1.983 +
   1.984 +    /// Starting from a flow, this method searches for an augmenting path
   1.985 +    /// according to the Edmonds-Karp algorithm
   1.986 +    /// and augments the flow on if any.
   1.987 +    /// The return value shows if the augmentation was succesful.
   1.988 +    bool augmentOnShortestPath();
   1.989 +    bool augmentOnShortestPath2();
   1.990 +
   1.991 +    /// Starting from a flow, this method searches for an augmenting blocking
   1.992 +    /// flow according to Dinits' algorithm and augments the flow on if any.
   1.993 +    /// The blocking flow is computed in a physically constructed
   1.994 +    /// residual graph of type \c Mutablegraph.
   1.995 +    /// The return value show sif the augmentation was succesful.
   1.996 +    template<typename MutableGraph> bool augmentOnBlockingFlow();
   1.997 +
   1.998 +    /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
   1.999 +    /// residual graph is not constructed physically.
  1.1000 +    /// The return value shows if the augmentation was succesful.
  1.1001 +    bool augmentOnBlockingFlow2();
  1.1002 +
  1.1003 +    template<typename _CutMap>
  1.1004 +    void actMinCut(_CutMap& M) const {
  1.1005 +      NodeIt v;
  1.1006 +      switch (status) {
  1.1007 +	case AFTER_PRE_FLOW_PHASE_1:
  1.1008 +//	std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
  1.1009 +// 	for(g->first(v); g->valid(v); g->next(v)) {
  1.1010 +// 	  if (level[v] < n) {
  1.1011 +// 	    M.set(v, false);
  1.1012 +// 	  } else {
  1.1013 +// 	    M.set(v, true);
  1.1014 +// 	  }
  1.1015 +// 	}
  1.1016 +	break;
  1.1017 +      case AFTER_PRE_FLOW_PHASE_2:
  1.1018 +//	std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
  1.1019 +	break;
  1.1020 +      case AFTER_NOTHING:
  1.1021 +//	std::cout << "AFTER_NOTHING" << std::endl;
  1.1022 +	minMinCut(M);
  1.1023 +	break;
  1.1024 +      case AFTER_AUGMENTING:
  1.1025 +//	std::cout << "AFTER_AUGMENTING" << std::endl;
  1.1026 +	for(g->first(v); g->valid(v); g->next(v)) {
  1.1027 +	  if (level[v]) {
  1.1028 +	    M.set(v, true);
  1.1029 +	  } else {
  1.1030 +	    M.set(v, false);
  1.1031 +	  }
  1.1032 +	}
  1.1033 +	break;
  1.1034 +      case AFTER_FAST_AUGMENTING:
  1.1035 +//	std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
  1.1036 +	for(g->first(v); g->valid(v); g->next(v)) {
  1.1037 +	  if (level[v]==number_of_augmentations) {
  1.1038 +	    M.set(v, true);
  1.1039 +	  } else {
  1.1040 +	    M.set(v, false);
  1.1041 +	  }
  1.1042 +	}
  1.1043 +	break;
  1.1044 +      }
  1.1045 +    }
  1.1046 +
  1.1047 +    template<typename _CutMap>
  1.1048 +    void minMinCut(_CutMap& M) const {
  1.1049 +      std::queue<Node> queue;
  1.1050 +
  1.1051 +      M.set(s,true);
  1.1052 +      queue.push(s);
  1.1053 +
  1.1054 +      while (!queue.empty()) {
  1.1055 +        Node w=queue.front();
  1.1056 +	queue.pop();
  1.1057 +
  1.1058 +	OutEdgeIt e;
  1.1059 +	for(g->first(e,w) ; g->valid(e); g->next(e)) {
  1.1060 +	  Node v=g->head(e);
  1.1061 +	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
  1.1062 +	    queue.push(v);
  1.1063 +	    M.set(v, true);
  1.1064 +	  }
  1.1065 +	}
  1.1066 +
  1.1067 +	InEdgeIt f;
  1.1068 +	for(g->first(f,w) ; g->valid(f); g->next(f)) {
  1.1069 +	  Node v=g->tail(f);
  1.1070 +	  if (!M[v] && (*flow)[f] > 0 ) {
  1.1071 +	    queue.push(v);
  1.1072 +	    M.set(v, true);
  1.1073 +	  }
  1.1074 +	}
  1.1075 +      }
  1.1076 +    }
  1.1077 +
  1.1078 +    template<typename _CutMap>
  1.1079 +    void minMinCut2(_CutMap& M) const {
  1.1080 +      ResGW res_graph(*g, *capacity, *flow);
  1.1081 +      BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
  1.1082 +      bfs.pushAndSetReached(s);
  1.1083 +      while (!bfs.finished()) ++bfs;
  1.1084 +    }
  1.1085 +
  1.1086 +    Num flowValue() const {
  1.1087 +      Num a=0;
  1.1088 +      FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
  1.1089 +      FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
  1.1090 +      return a;
  1.1091 +      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
  1.1092 +    }
  1.1093 +
  1.1094 +    template<typename MapGraphWrapper>
  1.1095 +    class DistanceMap {
  1.1096 +    protected:
  1.1097 +      const MapGraphWrapper* g;
  1.1098 +      typename MapGraphWrapper::template NodeMap<int> dist;
  1.1099 +    public:
  1.1100 +      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
  1.1101 +      void set(const typename MapGraphWrapper::Node& n, int a) {
  1.1102 +	dist.set(n, a);
  1.1103 +      }
  1.1104 +      int operator[](const typename MapGraphWrapper::Node& n) const { 
  1.1105 +	return dist[n]; 
  1.1106 +      }
  1.1107 +      //       int get(const typename MapGraphWrapper::Node& n) const {
  1.1108 +      // 	return dist[n]; }
  1.1109 +      //       bool get(const typename MapGraphWrapper::Edge& e) const {
  1.1110 +      // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
  1.1111 +      bool operator[](const typename MapGraphWrapper::Edge& e) const {
  1.1112 +	return (dist[g->tail(e)]<dist[g->head(e)]);
  1.1113 +      }
  1.1114 +    };
  1.1115 +
  1.1116 +  };
  1.1117 +
  1.1118 +
  1.1119 +
  1.1120 +  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1121 +  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
  1.1122 +  {
  1.1123 +    ResGW res_graph(*g, *capacity, *flow);
  1.1124 +    bool _augment=false;
  1.1125 +
  1.1126 +    //ReachedMap level(res_graph);
  1.1127 +    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1128 +    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1.1129 +    bfs.pushAndSetReached(s);
  1.1130 +
  1.1131 +    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1.1132 +    pred.set(s, INVALID);
  1.1133 +
  1.1134 +    typename ResGW::template NodeMap<Num> free(res_graph);
  1.1135 +
  1.1136 +    //searching for augmenting path
  1.1137 +    while ( !bfs.finished() ) {
  1.1138 +      ResGWOutEdgeIt e=bfs;
  1.1139 +      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1.1140 +	Node v=res_graph.tail(e);
  1.1141 +	Node w=res_graph.head(e);
  1.1142 +	pred.set(w, e);
  1.1143 +	if (res_graph.valid(pred[v])) {
  1.1144 +	  free.set(w, std::min(free[v], res_graph.resCap(e)));
  1.1145 +	} else {
  1.1146 +	  free.set(w, res_graph.resCap(e));
  1.1147 +	}
  1.1148 +	if (res_graph.head(e)==t) { _augment=true; break; }
  1.1149 +      }
  1.1150 +
  1.1151 +      ++bfs;
  1.1152 +    } //end of searching augmenting path
  1.1153 +
  1.1154 +    if (_augment) {
  1.1155 +      Node n=t;
  1.1156 +      Num augment_value=free[t];
  1.1157 +      while (res_graph.valid(pred[n])) {
  1.1158 +	ResGWEdge e=pred[n];
  1.1159 +	res_graph.augment(e, augment_value);
  1.1160 +	n=res_graph.tail(e);
  1.1161 +      }
  1.1162 +    }
  1.1163 +
  1.1164 +    status=AFTER_AUGMENTING;
  1.1165 +    return _augment;
  1.1166 +  }
  1.1167 +
  1.1168 +  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1169 +  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
  1.1170 +  {
  1.1171 +    ResGW res_graph(*g, *capacity, *flow);
  1.1172 +    bool _augment=false;
  1.1173 +
  1.1174 +    if (status!=AFTER_FAST_AUGMENTING) {
  1.1175 +      FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); 
  1.1176 +      number_of_augmentations=1;
  1.1177 +    } else {
  1.1178 +      ++number_of_augmentations;
  1.1179 +    }
  1.1180 +    TrickyReachedMap<ReachedMap> 
  1.1181 +      tricky_reached_map(level, number_of_augmentations);
  1.1182 +    //ReachedMap level(res_graph);
  1.1183 +//    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1184 +    BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > 
  1.1185 +      bfs(res_graph, tricky_reached_map);
  1.1186 +    bfs.pushAndSetReached(s);
  1.1187 +
  1.1188 +    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1.1189 +    pred.set(s, INVALID);
  1.1190 +
  1.1191 +    typename ResGW::template NodeMap<Num> free(res_graph);
  1.1192 +
  1.1193 +    //searching for augmenting path
  1.1194 +    while ( !bfs.finished() ) {
  1.1195 +      ResGWOutEdgeIt e=bfs;
  1.1196 +      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1.1197 +	Node v=res_graph.tail(e);
  1.1198 +	Node w=res_graph.head(e);
  1.1199 +	pred.set(w, e);
  1.1200 +	if (res_graph.valid(pred[v])) {
  1.1201 +	  free.set(w, std::min(free[v], res_graph.resCap(e)));
  1.1202 +	} else {
  1.1203 +	  free.set(w, res_graph.resCap(e));
  1.1204 +	}
  1.1205 +	if (res_graph.head(e)==t) { _augment=true; break; }
  1.1206 +      }
  1.1207 +
  1.1208 +      ++bfs;
  1.1209 +    } //end of searching augmenting path
  1.1210 +
  1.1211 +    if (_augment) {
  1.1212 +      Node n=t;
  1.1213 +      Num augment_value=free[t];
  1.1214 +      while (res_graph.valid(pred[n])) {
  1.1215 +	ResGWEdge e=pred[n];
  1.1216 +	res_graph.augment(e, augment_value);
  1.1217 +	n=res_graph.tail(e);
  1.1218 +      }
  1.1219 +    }
  1.1220 +
  1.1221 +    status=AFTER_FAST_AUGMENTING;
  1.1222 +    return _augment;
  1.1223 +  }
  1.1224 +
  1.1225 +
  1.1226 +  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1227 +  template<typename MutableGraph>
  1.1228 +  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
  1.1229 +  {
  1.1230 +    typedef MutableGraph MG;
  1.1231 +    bool _augment=false;
  1.1232 +
  1.1233 +    ResGW res_graph(*g, *capacity, *flow);
  1.1234 +
  1.1235 +    //bfs for distances on the residual graph
  1.1236 +    //ReachedMap level(res_graph);
  1.1237 +    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1238 +    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1.1239 +    bfs.pushAndSetReached(s);
  1.1240 +    typename ResGW::template NodeMap<int>
  1.1241 +      dist(res_graph); //filled up with 0's
  1.1242 +
  1.1243 +    //F will contain the physical copy of the residual graph
  1.1244 +    //with the set of edges which are on shortest paths
  1.1245 +    MG F;
  1.1246 +    typename ResGW::template NodeMap<typename MG::Node>
  1.1247 +      res_graph_to_F(res_graph);
  1.1248 +    {
  1.1249 +      typename ResGW::NodeIt n;
  1.1250 +      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
  1.1251 +	res_graph_to_F.set(n, F.addNode());
  1.1252 +      }
  1.1253 +    }
  1.1254 +
  1.1255 +    typename MG::Node sF=res_graph_to_F[s];
  1.1256 +    typename MG::Node tF=res_graph_to_F[t];
  1.1257 +    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
  1.1258 +    typename MG::template EdgeMap<Num> residual_capacity(F);
  1.1259 +
  1.1260 +    while ( !bfs.finished() ) {
  1.1261 +      ResGWOutEdgeIt e=bfs;
  1.1262 +      if (res_graph.valid(e)) {
  1.1263 +	if (bfs.isBNodeNewlyReached()) {
  1.1264 +	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1.1265 +	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1.1266 +					res_graph_to_F[res_graph.head(e)]);
  1.1267 +	  original_edge.update();
  1.1268 +	  original_edge.set(f, e);
  1.1269 +	  residual_capacity.update();
  1.1270 +	  residual_capacity.set(f, res_graph.resCap(e));
  1.1271 +	} else {
  1.1272 +	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
  1.1273 +	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1.1274 +					  res_graph_to_F[res_graph.head(e)]);
  1.1275 +	    original_edge.update();
  1.1276 +	    original_edge.set(f, e);
  1.1277 +	    residual_capacity.update();
  1.1278 +	    residual_capacity.set(f, res_graph.resCap(e));
  1.1279 +	  }
  1.1280 +	}
  1.1281 +      }
  1.1282 +      ++bfs;
  1.1283 +    } //computing distances from s in the residual graph
  1.1284 +
  1.1285 +    bool __augment=true;
  1.1286 +
  1.1287 +    while (__augment) {
  1.1288 +      __augment=false;
  1.1289 +      //computing blocking flow with dfs
  1.1290 +      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
  1.1291 +      typename MG::template NodeMap<typename MG::Edge> pred(F);
  1.1292 +      pred.set(sF, INVALID);
  1.1293 +      //invalid iterators for sources
  1.1294 +
  1.1295 +      typename MG::template NodeMap<Num> free(F);
  1.1296 +
  1.1297 +      dfs.pushAndSetReached(sF);
  1.1298 +      while (!dfs.finished()) {
  1.1299 +	++dfs;
  1.1300 +	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
  1.1301 +	  if (dfs.isBNodeNewlyReached()) {
  1.1302 +	    typename MG::Node v=F.aNode(dfs);
  1.1303 +	    typename MG::Node w=F.bNode(dfs);
  1.1304 +	    pred.set(w, dfs);
  1.1305 +	    if (F.valid(pred[v])) {
  1.1306 +	      free.set(w, std::min(free[v], residual_capacity[dfs]));
  1.1307 +	    } else {
  1.1308 +	      free.set(w, residual_capacity[dfs]);
  1.1309 +	    }
  1.1310 +	    if (w==tF) {
  1.1311 +	      __augment=true;
  1.1312 +	      _augment=true;
  1.1313 +	      break;
  1.1314 +	    }
  1.1315 +
  1.1316 +	  } else {
  1.1317 +	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
  1.1318 +	  }
  1.1319 +	}
  1.1320 +      }
  1.1321 +
  1.1322 +      if (__augment) {
  1.1323 +	typename MG::Node n=tF;
  1.1324 +	Num augment_value=free[tF];
  1.1325 +	while (F.valid(pred[n])) {
  1.1326 +	  typename MG::Edge e=pred[n];
  1.1327 +	  res_graph.augment(original_edge[e], augment_value);
  1.1328 +	  n=F.tail(e);
  1.1329 +	  if (residual_capacity[e]==augment_value)
  1.1330 +	    F.erase(e);
  1.1331 +	  else
  1.1332 +	    residual_capacity.set(e, residual_capacity[e]-augment_value);
  1.1333 +	}
  1.1334 +      }
  1.1335 +
  1.1336 +    }
  1.1337 +
  1.1338 +    status=AFTER_AUGMENTING;
  1.1339 +    return _augment;
  1.1340 +  }
  1.1341 +
  1.1342 +
  1.1343 +
  1.1344 +
  1.1345 +  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1346 +  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
  1.1347 +  {
  1.1348 +    bool _augment=false;
  1.1349 +
  1.1350 +    ResGW res_graph(*g, *capacity, *flow);
  1.1351 +
  1.1352 +    //ReachedMap level(res_graph);
  1.1353 +    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1354 +    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1.1355 +
  1.1356 +    bfs.pushAndSetReached(s);
  1.1357 +    DistanceMap<ResGW> dist(res_graph);
  1.1358 +    while ( !bfs.finished() ) {
  1.1359 +      ResGWOutEdgeIt e=bfs;
  1.1360 +      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1.1361 +	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1.1362 +      }
  1.1363 +      ++bfs;
  1.1364 +    } //computing distances from s in the residual graph
  1.1365 +
  1.1366 +      //Subgraph containing the edges on some shortest paths
  1.1367 +    ConstMap<typename ResGW::Node, bool> true_map(true);
  1.1368 +    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
  1.1369 +      DistanceMap<ResGW> > FilterResGW;
  1.1370 +    FilterResGW filter_res_graph(res_graph, true_map, dist);
  1.1371 +
  1.1372 +    //Subgraph, which is able to delete edges which are already
  1.1373 +    //met by the dfs
  1.1374 +    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
  1.1375 +      first_out_edges(filter_res_graph);
  1.1376 +    typename FilterResGW::NodeIt v;
  1.1377 +    for(filter_res_graph.first(v); filter_res_graph.valid(v);
  1.1378 +	filter_res_graph.next(v))
  1.1379 +      {
  1.1380 + 	typename FilterResGW::OutEdgeIt e;
  1.1381 + 	filter_res_graph.first(e, v);
  1.1382 + 	first_out_edges.set(v, e);
  1.1383 +      }
  1.1384 +    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
  1.1385 +      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
  1.1386 +    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
  1.1387 +
  1.1388 +    bool __augment=true;
  1.1389 +
  1.1390 +    while (__augment) {
  1.1391 +
  1.1392 +      __augment=false;
  1.1393 +      //computing blocking flow with dfs
  1.1394 +      DfsIterator< ErasingResGW,
  1.1395 +	typename ErasingResGW::template NodeMap<bool> >
  1.1396 +	dfs(erasing_res_graph);
  1.1397 +      typename ErasingResGW::
  1.1398 +	template NodeMap<typename ErasingResGW::OutEdgeIt>
  1.1399 +	pred(erasing_res_graph);
  1.1400 +      pred.set(s, INVALID);
  1.1401 +      //invalid iterators for sources
  1.1402 +
  1.1403 +      typename ErasingResGW::template NodeMap<Num>
  1.1404 +	free1(erasing_res_graph);
  1.1405 +
  1.1406 +      dfs.pushAndSetReached
  1.1407 +	///\bug hugo 0.2
  1.1408 +	(typename ErasingResGW::Node
  1.1409 +	 (typename FilterResGW::Node
  1.1410 +	  (typename ResGW::Node(s)
  1.1411 +	   )
  1.1412 +	  )
  1.1413 +	 );
  1.1414 +      while (!dfs.finished()) {
  1.1415 +	++dfs;
  1.1416 +	if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
  1.1417 + 	  {
  1.1418 +  	    if (dfs.isBNodeNewlyReached()) {
  1.1419 +
  1.1420 + 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
  1.1421 + 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
  1.1422 +
  1.1423 + 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
  1.1424 + 	      if (erasing_res_graph.valid(pred[v])) {
  1.1425 + 		free1.set
  1.1426 +		  (w, std::min(free1[v], res_graph.resCap
  1.1427 +			       (typename ErasingResGW::OutEdgeIt(dfs))));
  1.1428 + 	      } else {
  1.1429 + 		free1.set
  1.1430 +		  (w, res_graph.resCap
  1.1431 +		   (typename ErasingResGW::OutEdgeIt(dfs)));
  1.1432 + 	      }
  1.1433 +
  1.1434 + 	      if (w==t) {
  1.1435 + 		__augment=true;
  1.1436 + 		_augment=true;
  1.1437 + 		break;
  1.1438 + 	      }
  1.1439 + 	    } else {
  1.1440 + 	      erasing_res_graph.erase(dfs);
  1.1441 +	    }
  1.1442 +	  }
  1.1443 +      }
  1.1444 +
  1.1445 +      if (__augment) {
  1.1446 +	typename ErasingResGW::Node
  1.1447 +	  n=typename FilterResGW::Node(typename ResGW::Node(t));
  1.1448 +	// 	  typename ResGW::NodeMap<Num> a(res_graph);
  1.1449 +	// 	  typename ResGW::Node b;
  1.1450 +	// 	  Num j=a[b];
  1.1451 +	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
  1.1452 +	// 	  typename FilterResGW::Node b1;
  1.1453 +	// 	  Num j1=a1[b1];
  1.1454 +	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
  1.1455 +	// 	  typename ErasingResGW::Node b2;
  1.1456 +	// 	  Num j2=a2[b2];
  1.1457 +	Num augment_value=free1[n];
  1.1458 +	while (erasing_res_graph.valid(pred[n])) {
  1.1459 +	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1.1460 +	  res_graph.augment(e, augment_value);
  1.1461 +	  n=erasing_res_graph.tail(e);
  1.1462 +	  if (res_graph.resCap(e)==0)
  1.1463 +	    erasing_res_graph.erase(e);
  1.1464 +	}
  1.1465 +      }
  1.1466 +
  1.1467 +    } //while (__augment)
  1.1468 +
  1.1469 +    status=AFTER_AUGMENTING;
  1.1470 +    return _augment;
  1.1471 +  }
  1.1472 +
  1.1473 +
  1.1474 +} //namespace hugo
  1.1475 +
  1.1476 +#endif //HUGO_AUGMENTING_FLOW_H
  1.1477 +
  1.1478 +
  1.1479 +
  1.1480 +