1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hugo/preflow.h Mon Sep 13 13:57:13 2004 +0000
1.3 @@ -0,0 +1,796 @@
1.4 +// -*- C++ -*-
1.5 +#ifndef HUGO_PREFLOW_H
1.6 +#define HUGO_PREFLOW_H
1.7 +
1.8 +#include <vector>
1.9 +#include <queue>
1.10 +
1.11 +#include <hugo/invalid.h>
1.12 +#include <hugo/maps.h>
1.13 +
1.14 +/// \file
1.15 +/// \ingroup flowalgs
1.16 +
1.17 +namespace hugo {
1.18 +
1.19 + /// \addtogroup flowalgs
1.20 + /// @{
1.21 +
1.22 + ///Preflow algorithms class.
1.23 +
1.24 + ///This class provides an implementation of the \e preflow \e
1.25 + ///algorithm producing a flow of maximum value in a directed
1.26 + ///graph. The preflow algorithms are the fastest max flow algorithms
1.27 + ///up-to-date. The \e source node, the \e target node, the \e
1.28 + ///capacity of the edges and the \e starting \e flow value of the
1.29 + ///edges should be passed to the algorithm through the
1.30 + ///constructor. It is possible to change these quantities using the
1.31 + ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
1.32 + ///setFlow.
1.33 + ///
1.34 + ///After running \c phase1 or \c preflow, the actual flow
1.35 + ///value can be obtained by calling \ref flowValue(). The minimum
1.36 + ///value cut can be written into a \c node map of \c bools by
1.37 + ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
1.38 + ///the inclusionwise minimum and maximum of the minimum value cuts,
1.39 + ///resp.)
1.40 + ///
1.41 + ///\param Graph The directed graph type the algorithm runs on.
1.42 + ///\param Num The number type of the capacities and the flow values.
1.43 + ///\param CapMap The capacity map type.
1.44 + ///\param FlowMap The flow map type.
1.45 + ///
1.46 + ///\author Jacint Szabo
1.47 + template <typename Graph, typename Num,
1.48 + typename CapMap=typename Graph::template EdgeMap<Num>,
1.49 + typename FlowMap=typename Graph::template EdgeMap<Num> >
1.50 + class Preflow {
1.51 + protected:
1.52 + typedef typename Graph::Node Node;
1.53 + typedef typename Graph::NodeIt NodeIt;
1.54 + typedef typename Graph::EdgeIt EdgeIt;
1.55 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.56 + typedef typename Graph::InEdgeIt InEdgeIt;
1.57 +
1.58 + typedef typename Graph::template NodeMap<Node> NNMap;
1.59 + typedef typename std::vector<Node> VecNode;
1.60 +
1.61 + const Graph* g;
1.62 + Node s;
1.63 + Node t;
1.64 + const CapMap* capacity;
1.65 + FlowMap* flow;
1.66 + int n; //the number of nodes of G
1.67 +
1.68 + typename Graph::template NodeMap<int> level;
1.69 + typename Graph::template NodeMap<Num> excess;
1.70 +
1.71 + // constants used for heuristics
1.72 + static const int H0=20;
1.73 + static const int H1=1;
1.74 +
1.75 + public:
1.76 +
1.77 + ///Indicates the property of the starting flow map.
1.78 +
1.79 + ///Indicates the property of the starting flow map. The meanings are as follows:
1.80 + ///- \c ZERO_FLOW: constant zero flow
1.81 + ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
1.82 + ///the sum of the out-flows in every node except the \e source and
1.83 + ///the \e target.
1.84 + ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
1.85 + ///least the sum of the out-flows in every node except the \e source.
1.86 + ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
1.87 + ///set to the constant zero flow in the beginning of the algorithm in this case.
1.88 + ///
1.89 + enum FlowEnum{
1.90 + NO_FLOW,
1.91 + ZERO_FLOW,
1.92 + GEN_FLOW,
1.93 + PRE_FLOW
1.94 + };
1.95 +
1.96 + ///Indicates the state of the preflow algorithm.
1.97 +
1.98 + ///Indicates the state of the preflow algorithm. The meanings are as follows:
1.99 + ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
1.100 + ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
1.101 + ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
1.102 + ///
1.103 + enum StatusEnum {
1.104 + AFTER_NOTHING,
1.105 + AFTER_PREFLOW_PHASE_1,
1.106 + AFTER_PREFLOW_PHASE_2
1.107 + };
1.108 +
1.109 + protected:
1.110 + FlowEnum flow_prop;
1.111 + StatusEnum status; // Do not needle this flag only if necessary.
1.112 +
1.113 + public:
1.114 + ///The constructor of the class.
1.115 +
1.116 + ///The constructor of the class.
1.117 + ///\param _G The directed graph the algorithm runs on.
1.118 + ///\param _s The source node.
1.119 + ///\param _t The target node.
1.120 + ///\param _capacity The capacity of the edges.
1.121 + ///\param _flow The flow of the edges.
1.122 + ///Except the graph, all of these parameters can be reset by
1.123 + ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
1.124 + ///setFlow, resp.
1.125 + Preflow(const Graph& _G, Node _s, Node _t,
1.126 + const CapMap& _capacity, FlowMap& _flow) :
1.127 + g(&_G), s(_s), t(_t), capacity(&_capacity),
1.128 + flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
1.129 + flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
1.130 +
1.131 +
1.132 +
1.133 + ///Runs the preflow algorithm.
1.134 +
1.135 + ///Runs the preflow algorithm.
1.136 + void run() {
1.137 + phase1(flow_prop);
1.138 + phase2();
1.139 + }
1.140 +
1.141 + ///Runs the preflow algorithm.
1.142 +
1.143 + ///Runs the preflow algorithm.
1.144 + ///\pre The starting flow map must be
1.145 + /// - a constant zero flow if \c fp is \c ZERO_FLOW,
1.146 + /// - an arbitrary flow if \c fp is \c GEN_FLOW,
1.147 + /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
1.148 + /// - any map if \c fp is NO_FLOW.
1.149 + ///If the starting flow map is a flow or a preflow then
1.150 + ///the algorithm terminates faster.
1.151 + void run(FlowEnum fp) {
1.152 + flow_prop=fp;
1.153 + run();
1.154 + }
1.155 +
1.156 + ///Runs the first phase of the preflow algorithm.
1.157 +
1.158 + ///The preflow algorithm consists of two phases, this method runs the
1.159 + ///first phase. After the first phase the maximum flow value and a
1.160 + ///minimum value cut can already be computed, though a maximum flow
1.161 + ///is not yet obtained. So after calling this method \ref flowValue
1.162 + ///and \ref minCut gives proper results.
1.163 + ///\warning: \ref minMinCut and \ref maxMinCut do not
1.164 + ///give minimum value cuts unless calling \ref phase2.
1.165 + ///\pre The starting flow must be
1.166 + /// - a constant zero flow if \c fp is \c ZERO_FLOW,
1.167 + /// - an arbitary flow if \c fp is \c GEN_FLOW,
1.168 + /// - an arbitary preflow if \c fp is \c PRE_FLOW,
1.169 + /// - any map if \c fp is NO_FLOW.
1.170 + void phase1(FlowEnum fp)
1.171 + {
1.172 + flow_prop=fp;
1.173 + phase1();
1.174 + }
1.175 +
1.176 +
1.177 + ///Runs the first phase of the preflow algorithm.
1.178 +
1.179 + ///The preflow algorithm consists of two phases, this method runs the
1.180 + ///first phase. After the first phase the maximum flow value and a
1.181 + ///minimum value cut can already be computed, though a maximum flow
1.182 + ///is not yet obtained. So after calling this method \ref flowValue
1.183 + ///and \ref actMinCut gives proper results.
1.184 + ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
1.185 + ///give minimum value cuts unless calling \ref phase2.
1.186 + void phase1()
1.187 + {
1.188 + int heur0=(int)(H0*n); //time while running 'bound decrease'
1.189 + int heur1=(int)(H1*n); //time while running 'highest label'
1.190 + int heur=heur1; //starting time interval (#of relabels)
1.191 + int numrelabel=0;
1.192 +
1.193 + bool what_heur=1;
1.194 + //It is 0 in case 'bound decrease' and 1 in case 'highest label'
1.195 +
1.196 + bool end=false;
1.197 + //Needed for 'bound decrease', true means no active
1.198 + //nodes are above bound b.
1.199 +
1.200 + int k=n-2; //bound on the highest level under n containing a node
1.201 + int b=k; //bound on the highest level under n of an active node
1.202 +
1.203 + VecNode first(n, INVALID);
1.204 + NNMap next(*g, INVALID);
1.205 +
1.206 + NNMap left(*g, INVALID);
1.207 + NNMap right(*g, INVALID);
1.208 + VecNode level_list(n,INVALID);
1.209 + //List of the nodes in level i<n, set to n.
1.210 +
1.211 + preflowPreproc(first, next, level_list, left, right);
1.212 +
1.213 + //Push/relabel on the highest level active nodes.
1.214 + while ( true ) {
1.215 + if ( b == 0 ) {
1.216 + if ( !what_heur && !end && k > 0 ) {
1.217 + b=k;
1.218 + end=true;
1.219 + } else break;
1.220 + }
1.221 +
1.222 + if ( first[b]==INVALID ) --b;
1.223 + else {
1.224 + end=false;
1.225 + Node w=first[b];
1.226 + first[b]=next[w];
1.227 + int newlevel=push(w, next, first);
1.228 + if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
1.229 + left, right, b, k, what_heur);
1.230 +
1.231 + ++numrelabel;
1.232 + if ( numrelabel >= heur ) {
1.233 + numrelabel=0;
1.234 + if ( what_heur ) {
1.235 + what_heur=0;
1.236 + heur=heur0;
1.237 + end=false;
1.238 + } else {
1.239 + what_heur=1;
1.240 + heur=heur1;
1.241 + b=k;
1.242 + }
1.243 + }
1.244 + }
1.245 + }
1.246 + flow_prop=PRE_FLOW;
1.247 + status=AFTER_PREFLOW_PHASE_1;
1.248 + }
1.249 + // Heuristics:
1.250 + // 2 phase
1.251 + // gap
1.252 + // list 'level_list' on the nodes on level i implemented by hand
1.253 + // stack 'active' on the active nodes on level i
1.254 + // runs heuristic 'highest label' for H1*n relabels
1.255 + // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
1.256 + // Parameters H0 and H1 are initialized to 20 and 1.
1.257 +
1.258 +
1.259 + ///Runs the second phase of the preflow algorithm.
1.260 +
1.261 + ///The preflow algorithm consists of two phases, this method runs
1.262 + ///the second phase. After calling \ref phase1 and then
1.263 + ///\ref phase2 the methods \ref flowValue, \ref minCut,
1.264 + ///\ref minMinCut and \ref maxMinCut give proper results.
1.265 + ///\pre \ref phase1 must be called before.
1.266 + void phase2()
1.267 + {
1.268 +
1.269 + int k=n-2; //bound on the highest level under n containing a node
1.270 + int b=k; //bound on the highest level under n of an active node
1.271 +
1.272 +
1.273 + VecNode first(n, INVALID);
1.274 + NNMap next(*g, INVALID);
1.275 + level.set(s,0);
1.276 + std::queue<Node> bfs_queue;
1.277 + bfs_queue.push(s);
1.278 +
1.279 + while ( !bfs_queue.empty() ) {
1.280 +
1.281 + Node v=bfs_queue.front();
1.282 + bfs_queue.pop();
1.283 + int l=level[v]+1;
1.284 +
1.285 + for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
1.286 + if ( (*capacity)[e] <= (*flow)[e] ) continue;
1.287 + Node u=g->tail(e);
1.288 + if ( level[u] >= n ) {
1.289 + bfs_queue.push(u);
1.290 + level.set(u, l);
1.291 + if ( excess[u] > 0 ) {
1.292 + next.set(u,first[l]);
1.293 + first[l]=u;
1.294 + }
1.295 + }
1.296 + }
1.297 +
1.298 + for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
1.299 + if ( 0 >= (*flow)[e] ) continue;
1.300 + Node u=g->head(e);
1.301 + if ( level[u] >= n ) {
1.302 + bfs_queue.push(u);
1.303 + level.set(u, l);
1.304 + if ( excess[u] > 0 ) {
1.305 + next.set(u,first[l]);
1.306 + first[l]=u;
1.307 + }
1.308 + }
1.309 + }
1.310 + }
1.311 + b=n-2;
1.312 +
1.313 + while ( true ) {
1.314 +
1.315 + if ( b == 0 ) break;
1.316 + if ( first[b]==INVALID ) --b;
1.317 + else {
1.318 + Node w=first[b];
1.319 + first[b]=next[w];
1.320 + int newlevel=push(w,next, first);
1.321 +
1.322 + //relabel
1.323 + if ( excess[w] > 0 ) {
1.324 + level.set(w,++newlevel);
1.325 + next.set(w,first[newlevel]);
1.326 + first[newlevel]=w;
1.327 + b=newlevel;
1.328 + }
1.329 + }
1.330 + } // while(true)
1.331 + flow_prop=GEN_FLOW;
1.332 + status=AFTER_PREFLOW_PHASE_2;
1.333 + }
1.334 +
1.335 + /// Returns the value of the maximum flow.
1.336 +
1.337 + /// Returns the value of the maximum flow by returning the excess
1.338 + /// of the target node \ref t. This value equals to the value of
1.339 + /// the maximum flow already after running \ref phase1.
1.340 + Num flowValue() const {
1.341 + return excess[t];
1.342 + }
1.343 +
1.344 +
1.345 + ///Returns a minimum value cut.
1.346 +
1.347 + ///Sets \c M to the characteristic vector of a minimum value
1.348 + ///cut. This method can be called both after running \ref
1.349 + ///phase1 and \ref phase2. It is much faster after
1.350 + ///\ref phase1. \pre M should be a node map of bools. \pre
1.351 + ///If \ref mincut is called after \ref phase2 then M should
1.352 + ///be initialized to false.
1.353 + template<typename _CutMap>
1.354 + void minCut(_CutMap& M) const {
1.355 + switch ( status ) {
1.356 + case AFTER_PREFLOW_PHASE_1:
1.357 + for(NodeIt v(*g); v!=INVALID; ++v) {
1.358 + if (level[v] < n) {
1.359 + M.set(v, false);
1.360 + } else {
1.361 + M.set(v, true);
1.362 + }
1.363 + }
1.364 + break;
1.365 + case AFTER_PREFLOW_PHASE_2:
1.366 + minMinCut(M);
1.367 + break;
1.368 + case AFTER_NOTHING:
1.369 + break;
1.370 + }
1.371 + }
1.372 +
1.373 + ///Returns the inclusionwise minimum of the minimum value cuts.
1.374 +
1.375 + ///Sets \c M to the characteristic vector of the minimum value cut
1.376 + ///which is inclusionwise minimum. It is computed by processing a
1.377 + ///bfs from the source node \c s in the residual graph. \pre M
1.378 + ///should be a node map of bools initialized to false. \pre \ref
1.379 + ///phase2 should already be run.
1.380 + template<typename _CutMap>
1.381 + void minMinCut(_CutMap& M) const {
1.382 +
1.383 + std::queue<Node> queue;
1.384 + M.set(s,true);
1.385 + queue.push(s);
1.386 +
1.387 + while (!queue.empty()) {
1.388 + Node w=queue.front();
1.389 + queue.pop();
1.390 +
1.391 + for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.392 + Node v=g->head(e);
1.393 + if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
1.394 + queue.push(v);
1.395 + M.set(v, true);
1.396 + }
1.397 + }
1.398 +
1.399 + for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.400 + Node v=g->tail(e);
1.401 + if (!M[v] && (*flow)[e] > 0 ) {
1.402 + queue.push(v);
1.403 + M.set(v, true);
1.404 + }
1.405 + }
1.406 + }
1.407 + }
1.408 +
1.409 + ///Returns the inclusionwise maximum of the minimum value cuts.
1.410 +
1.411 + ///Sets \c M to the characteristic vector of the minimum value cut
1.412 + ///which is inclusionwise maximum. It is computed by processing a
1.413 + ///backward bfs from the target node \c t in the residual graph.
1.414 + ///\pre \ref phase2() or preflow() should already be run.
1.415 + template<typename _CutMap>
1.416 + void maxMinCut(_CutMap& M) const {
1.417 +
1.418 + for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
1.419 +
1.420 + std::queue<Node> queue;
1.421 +
1.422 + M.set(t,false);
1.423 + queue.push(t);
1.424 +
1.425 + while (!queue.empty()) {
1.426 + Node w=queue.front();
1.427 + queue.pop();
1.428 +
1.429 + for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.430 + Node v=g->tail(e);
1.431 + if (M[v] && (*flow)[e] < (*capacity)[e] ) {
1.432 + queue.push(v);
1.433 + M.set(v, false);
1.434 + }
1.435 + }
1.436 +
1.437 + for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.438 + Node v=g->head(e);
1.439 + if (M[v] && (*flow)[e] > 0 ) {
1.440 + queue.push(v);
1.441 + M.set(v, false);
1.442 + }
1.443 + }
1.444 + }
1.445 + }
1.446 +
1.447 + ///Sets the source node to \c _s.
1.448 +
1.449 + ///Sets the source node to \c _s.
1.450 + ///
1.451 + void setSource(Node _s) {
1.452 + s=_s;
1.453 + if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
1.454 + status=AFTER_NOTHING;
1.455 + }
1.456 +
1.457 + ///Sets the target node to \c _t.
1.458 +
1.459 + ///Sets the target node to \c _t.
1.460 + ///
1.461 + void setTarget(Node _t) {
1.462 + t=_t;
1.463 + if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
1.464 + status=AFTER_NOTHING;
1.465 + }
1.466 +
1.467 + /// Sets the edge map of the capacities to _cap.
1.468 +
1.469 + /// Sets the edge map of the capacities to _cap.
1.470 + ///
1.471 + void setCap(const CapMap& _cap) {
1.472 + capacity=&_cap;
1.473 + status=AFTER_NOTHING;
1.474 + }
1.475 +
1.476 + /// Sets the edge map of the flows to _flow.
1.477 +
1.478 + /// Sets the edge map of the flows to _flow.
1.479 + ///
1.480 + void setFlow(FlowMap& _flow) {
1.481 + flow=&_flow;
1.482 + flow_prop=NO_FLOW;
1.483 + status=AFTER_NOTHING;
1.484 + }
1.485 +
1.486 +
1.487 + private:
1.488 +
1.489 + int push(Node w, NNMap& next, VecNode& first) {
1.490 +
1.491 + int lev=level[w];
1.492 + Num exc=excess[w];
1.493 + int newlevel=n; //bound on the next level of w
1.494 +
1.495 + for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.496 + if ( (*flow)[e] >= (*capacity)[e] ) continue;
1.497 + Node v=g->head(e);
1.498 +
1.499 + if( lev > level[v] ) { //Push is allowed now
1.500 +
1.501 + if ( excess[v]<=0 && v!=t && v!=s ) {
1.502 + next.set(v,first[level[v]]);
1.503 + first[level[v]]=v;
1.504 + }
1.505 +
1.506 + Num cap=(*capacity)[e];
1.507 + Num flo=(*flow)[e];
1.508 + Num remcap=cap-flo;
1.509 +
1.510 + if ( remcap >= exc ) { //A nonsaturating push.
1.511 +
1.512 + flow->set(e, flo+exc);
1.513 + excess.set(v, excess[v]+exc);
1.514 + exc=0;
1.515 + break;
1.516 +
1.517 + } else { //A saturating push.
1.518 + flow->set(e, cap);
1.519 + excess.set(v, excess[v]+remcap);
1.520 + exc-=remcap;
1.521 + }
1.522 + } else if ( newlevel > level[v] ) newlevel = level[v];
1.523 + } //for out edges wv
1.524 +
1.525 + if ( exc > 0 ) {
1.526 + for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
1.527 +
1.528 + if( (*flow)[e] <= 0 ) continue;
1.529 + Node v=g->tail(e);
1.530 +
1.531 + if( lev > level[v] ) { //Push is allowed now
1.532 +
1.533 + if ( excess[v]<=0 && v!=t && v!=s ) {
1.534 + next.set(v,first[level[v]]);
1.535 + first[level[v]]=v;
1.536 + }
1.537 +
1.538 + Num flo=(*flow)[e];
1.539 +
1.540 + if ( flo >= exc ) { //A nonsaturating push.
1.541 +
1.542 + flow->set(e, flo-exc);
1.543 + excess.set(v, excess[v]+exc);
1.544 + exc=0;
1.545 + break;
1.546 + } else { //A saturating push.
1.547 +
1.548 + excess.set(v, excess[v]+flo);
1.549 + exc-=flo;
1.550 + flow->set(e,0);
1.551 + }
1.552 + } else if ( newlevel > level[v] ) newlevel = level[v];
1.553 + } //for in edges vw
1.554 +
1.555 + } // if w still has excess after the out edge for cycle
1.556 +
1.557 + excess.set(w, exc);
1.558 +
1.559 + return newlevel;
1.560 + }
1.561 +
1.562 +
1.563 +
1.564 + void preflowPreproc(VecNode& first, NNMap& next,
1.565 + VecNode& level_list, NNMap& left, NNMap& right)
1.566 + {
1.567 + for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
1.568 + std::queue<Node> bfs_queue;
1.569 +
1.570 + if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
1.571 + //Reverse_bfs from t in the residual graph,
1.572 + //to find the starting level.
1.573 + level.set(t,0);
1.574 + bfs_queue.push(t);
1.575 +
1.576 + while ( !bfs_queue.empty() ) {
1.577 +
1.578 + Node v=bfs_queue.front();
1.579 + bfs_queue.pop();
1.580 + int l=level[v]+1;
1.581 +
1.582 + for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
1.583 + if ( (*capacity)[e] <= (*flow)[e] ) continue;
1.584 + Node w=g->tail(e);
1.585 + if ( level[w] == n && w != s ) {
1.586 + bfs_queue.push(w);
1.587 + Node z=level_list[l];
1.588 + if ( z!=INVALID ) left.set(z,w);
1.589 + right.set(w,z);
1.590 + level_list[l]=w;
1.591 + level.set(w, l);
1.592 + }
1.593 + }
1.594 +
1.595 + for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
1.596 + if ( 0 >= (*flow)[e] ) continue;
1.597 + Node w=g->head(e);
1.598 + if ( level[w] == n && w != s ) {
1.599 + bfs_queue.push(w);
1.600 + Node z=level_list[l];
1.601 + if ( z!=INVALID ) 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 + } //while
1.608 + } //if
1.609 +
1.610 +
1.611 + switch (flow_prop) {
1.612 + case NO_FLOW:
1.613 + for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
1.614 + case ZERO_FLOW:
1.615 + for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
1.616 +
1.617 + //Reverse_bfs from t, to find the starting level.
1.618 + level.set(t,0);
1.619 + bfs_queue.push(t);
1.620 +
1.621 + while ( !bfs_queue.empty() ) {
1.622 +
1.623 + Node v=bfs_queue.front();
1.624 + bfs_queue.pop();
1.625 + int l=level[v]+1;
1.626 +
1.627 + for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
1.628 + Node w=g->tail(e);
1.629 + if ( level[w] == n && w != s ) {
1.630 + bfs_queue.push(w);
1.631 + Node z=level_list[l];
1.632 + if ( z!=INVALID ) left.set(z,w);
1.633 + right.set(w,z);
1.634 + level_list[l]=w;
1.635 + level.set(w, l);
1.636 + }
1.637 + }
1.638 + }
1.639 +
1.640 + //the starting flow
1.641 + for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
1.642 + Num c=(*capacity)[e];
1.643 + if ( c <= 0 ) continue;
1.644 + Node w=g->head(e);
1.645 + if ( level[w] < n ) {
1.646 + if ( excess[w] <= 0 && w!=t ) { //putting into the stack
1.647 + next.set(w,first[level[w]]);
1.648 + first[level[w]]=w;
1.649 + }
1.650 + flow->set(e, c);
1.651 + excess.set(w, excess[w]+c);
1.652 + }
1.653 + }
1.654 + break;
1.655 +
1.656 + case GEN_FLOW:
1.657 + for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
1.658 + {
1.659 + Num exc=0;
1.660 + for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
1.661 + for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
1.662 + excess.set(t,exc);
1.663 + }
1.664 +
1.665 + //the starting flow
1.666 + for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
1.667 + Num rem=(*capacity)[e]-(*flow)[e];
1.668 + if ( rem <= 0 ) continue;
1.669 + Node w=g->head(e);
1.670 + if ( level[w] < n ) {
1.671 + if ( excess[w] <= 0 && w!=t ) { //putting into the stack
1.672 + next.set(w,first[level[w]]);
1.673 + first[level[w]]=w;
1.674 + }
1.675 + flow->set(e, (*capacity)[e]);
1.676 + excess.set(w, excess[w]+rem);
1.677 + }
1.678 + }
1.679 +
1.680 + for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
1.681 + if ( (*flow)[e] <= 0 ) continue;
1.682 + Node w=g->tail(e);
1.683 + if ( level[w] < n ) {
1.684 + if ( excess[w] <= 0 && w!=t ) {
1.685 + next.set(w,first[level[w]]);
1.686 + first[level[w]]=w;
1.687 + }
1.688 + excess.set(w, excess[w]+(*flow)[e]);
1.689 + flow->set(e, 0);
1.690 + }
1.691 + }
1.692 + break;
1.693 +
1.694 + case PRE_FLOW:
1.695 + //the starting flow
1.696 + for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
1.697 + Num rem=(*capacity)[e]-(*flow)[e];
1.698 + if ( rem <= 0 ) continue;
1.699 + Node w=g->head(e);
1.700 + if ( level[w] < n ) flow->set(e, (*capacity)[e]);
1.701 + }
1.702 +
1.703 + for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
1.704 + if ( (*flow)[e] <= 0 ) continue;
1.705 + Node w=g->tail(e);
1.706 + if ( level[w] < n ) flow->set(e, 0);
1.707 + }
1.708 +
1.709 + //computing the excess
1.710 + for(NodeIt w(*g); w!=INVALID; ++w) {
1.711 + Num exc=0;
1.712 + for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
1.713 + for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
1.714 + excess.set(w,exc);
1.715 +
1.716 + //putting the active nodes into the stack
1.717 + int lev=level[w];
1.718 + if ( exc > 0 && lev < n && Node(w) != t ) {
1.719 + next.set(w,first[lev]);
1.720 + first[lev]=w;
1.721 + }
1.722 + }
1.723 + break;
1.724 + } //switch
1.725 + } //preflowPreproc
1.726 +
1.727 +
1.728 + void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
1.729 + VecNode& level_list, NNMap& left,
1.730 + NNMap& right, int& b, int& k, bool what_heur )
1.731 + {
1.732 +
1.733 + int lev=level[w];
1.734 +
1.735 + Node right_n=right[w];
1.736 + Node left_n=left[w];
1.737 +
1.738 + //unlacing starts
1.739 + if ( right_n!=INVALID ) {
1.740 + if ( left_n!=INVALID ) {
1.741 + right.set(left_n, right_n);
1.742 + left.set(right_n, left_n);
1.743 + } else {
1.744 + level_list[lev]=right_n;
1.745 + left.set(right_n, INVALID);
1.746 + }
1.747 + } else {
1.748 + if ( left_n!=INVALID ) {
1.749 + right.set(left_n, INVALID);
1.750 + } else {
1.751 + level_list[lev]=INVALID;
1.752 + }
1.753 + }
1.754 + //unlacing ends
1.755 +
1.756 + if ( level_list[lev]==INVALID ) {
1.757 +
1.758 + //gapping starts
1.759 + for (int i=lev; i!=k ; ) {
1.760 + Node v=level_list[++i];
1.761 + while ( v!=INVALID ) {
1.762 + level.set(v,n);
1.763 + v=right[v];
1.764 + }
1.765 + level_list[i]=INVALID;
1.766 + if ( !what_heur ) first[i]=INVALID;
1.767 + }
1.768 +
1.769 + level.set(w,n);
1.770 + b=lev-1;
1.771 + k=b;
1.772 + //gapping ends
1.773 +
1.774 + } else {
1.775 +
1.776 + if ( newlevel == n ) level.set(w,n);
1.777 + else {
1.778 + level.set(w,++newlevel);
1.779 + next.set(w,first[newlevel]);
1.780 + first[newlevel]=w;
1.781 + if ( what_heur ) b=newlevel;
1.782 + if ( k < newlevel ) ++k; //now k=newlevel
1.783 + Node z=level_list[newlevel];
1.784 + if ( z!=INVALID ) left.set(z,w);
1.785 + right.set(w,z);
1.786 + left.set(w,INVALID);
1.787 + level_list[newlevel]=w;
1.788 + }
1.789 + }
1.790 + } //relabel
1.791 +
1.792 + };
1.793 +} //namespace hugo
1.794 +
1.795 +#endif //HUGO_PREFLOW_H
1.796 +
1.797 +
1.798 +
1.799 +