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