1.1 --- a/src/work/jacint/max_flow.h Tue May 11 19:38:00 2004 +0000
1.2 +++ b/src/work/jacint/max_flow.h Tue May 11 19:50:21 2004 +0000
1.3 @@ -1,19 +1,19 @@
1.4 // -*- C++ -*-
1.5
1.6 /*
1.7 - Heuristics:
1.8 + Heuristics:
1.9 2 phase
1.10 gap
1.11 list 'level_list' on the nodes on level i implemented by hand
1.12 stack 'active' on the active nodes on level i
1.13 runs heuristic 'highest label' for H1*n relabels
1.14 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
1.15 -
1.16 +
1.17 Parameters H0 and H1 are initialized to 20 and 1.
1.18
1.19 Constructors:
1.20
1.21 - Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
1.22 + Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
1.23 FlowMap is not constant zero, and should be true if it is
1.24
1.25 Members:
1.26 @@ -22,13 +22,13 @@
1.27
1.28 Num flowValue() : returns the value of a maximum flow
1.29
1.30 - void minMinCut(CutMap& M) : sets M to the characteristic vector of the
1.31 + void minMinCut(CutMap& M) : sets M to the characteristic vector of the
1.32 minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
1.33
1.34 - void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
1.35 + void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
1.36 maximum min cut. M should be a map of bools initialized to false.
1.37
1.38 - void minCut(CutMap& M) : sets M to the characteristic vector of
1.39 + void minCut(CutMap& M) : sets M to the characteristic vector of
1.40 a min cut. M should be a map of bools initialized to false.
1.41
1.42 */
1.43 @@ -36,9 +36,6 @@
1.44 #ifndef HUGO_MAX_FLOW_H
1.45 #define HUGO_MAX_FLOW_H
1.46
1.47 -#define H0 20
1.48 -#define H1 1
1.49 -
1.50 #include <vector>
1.51 #include <queue>
1.52 #include <stack>
1.53 @@ -50,18 +47,20 @@
1.54 #include <for_each_macros.h>
1.55
1.56 /// \file
1.57 -/// \brief Dimacs file format reader.
1.58 +/// \brief Maximum flows.
1.59 +/// \ingroup galgs
1.60
1.61 namespace hugo {
1.62
1.63
1.64 // ///\author Marton Makai, Jacint Szabo
1.65 /// A class for computing max flows and related quantities.
1.66 - template <typename Graph, typename Num,
1.67 - typename CapMap=typename Graph::template EdgeMap<Num>,
1.68 + /// \ingroup galgs
1.69 + template <typename Graph, typename Num,
1.70 + typename CapMap=typename Graph::template EdgeMap<Num>,
1.71 typename FlowMap=typename Graph::template EdgeMap<Num> >
1.72 class MaxFlow {
1.73 -
1.74 + protected:
1.75 typedef typename Graph::Node Node;
1.76 typedef typename Graph::NodeIt NodeIt;
1.77 typedef typename Graph::OutEdgeIt OutEdgeIt;
1.78 @@ -74,7 +73,7 @@
1.79 const Graph* g;
1.80 Node s;
1.81 Node t;
1.82 - const CapMap* capacity;
1.83 + const CapMap* capacity;
1.84 FlowMap* flow;
1.85 int n; //the number of nodes of G
1.86 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
1.87 @@ -83,98 +82,107 @@
1.88 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
1.89 typedef typename Graph::template NodeMap<int> ReachedMap;
1.90 ReachedMap level;
1.91 - //level works as a bool map in augmenting path algorithms
1.92 + //level works as a bool map in augmenting path algorithms
1.93 //and is used by bfs for storing reached information.
1.94 //In preflow, it shows levels of nodes.
1.95 - //typename Graph::template NodeMap<int> level;
1.96 - typename Graph::template NodeMap<Num> excess;
1.97 + //typename Graph::template NodeMap<int> level;
1.98 + typename Graph::template NodeMap<Num> excess;
1.99 // protected:
1.100 // MaxFlow() { }
1.101 - // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
1.102 - // FlowMap& _flow)
1.103 + // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
1.104 + // FlowMap& _flow)
1.105 // {
1.106 - // g=&_G;
1.107 - // s=_s;
1.108 - // t=_t;
1.109 + // g=&_G;
1.110 + // s=_s;
1.111 + // t=_t;
1.112 // capacity=&_capacity;
1.113 // flow=&_flow;
1.114 // n=_G.nodeNum;
1.115 - // level.set (_G); //kellene vmi ilyesmi fv
1.116 + // level.set (_G); //kellene vmi ilyesmi fv
1.117 // excess(_G,0); //itt is
1.118 // }
1.119
1.120 + // constants used for heuristics
1.121 + static const int H0=20;
1.122 + static const int H1=1;
1.123 +
1.124 public:
1.125 -
1.126 +
1.127 ///\todo Document this.
1.128 ///\todo Maybe, it should be PRE_FLOW instead.
1.129 + ///- \c NO_FLOW means nothing,
1.130 ///- \c ZERO_FLOW means something,
1.131 ///- \c GEN_FLOW means something else,
1.132 - ///- \c PREFLOW is something different.
1.133 + ///- \c PRE_FLOW is something different.
1.134 enum flowEnum{
1.135 - ZERO_FLOW=0,
1.136 - GEN_FLOW=1,
1.137 - PREFLOW=2
1.138 + ZERO_FLOW,
1.139 + GEN_FLOW,
1.140 + PRE_FLOW,
1.141 + NO_FLOW
1.142 };
1.143
1.144 - MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
1.145 + MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
1.146 FlowMap& _flow) :
1.147 - g(&_G), s(_s), t(_t), capacity(&_capacity),
1.148 + g(&_G), s(_s), t(_t), capacity(&_capacity),
1.149 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
1.150
1.151 /// A max flow algorithm is run.
1.152 - ///\pre the flow have to be 0 at the beginning.
1.153 - void run() {
1.154 - preflow(ZERO_FLOW);
1.155 + /// \pre The flow have to satisfy the requirements
1.156 + /// stated in fe.
1.157 + void run(flowEnum fe=ZERO_FLOW) {
1.158 + preflow(fe);
1.159 }
1.160 -
1.161 - /// A preflow algorithm is run.
1.162 - ///\pre The initial edge-map have to be a
1.163 +
1.164 + /// A preflow algorithm is run.
1.165 + /// \pre The initial edge-map have to be a
1.166 /// zero flow if \c fe is \c ZERO_FLOW,
1.167 - /// a flow if \c fe is \c GEN_FLOW,
1.168 - /// and a pre-flow it is \c PREFLOW.
1.169 + /// a flow if \c fe is \c GEN_FLOW,
1.170 + /// a pre-flow if fe is \c PRE_FLOW and
1.171 + /// anything if fe is NO_FLOW.
1.172 void preflow(flowEnum fe) {
1.173 preflowPhase0(fe);
1.174 preflowPhase1();
1.175 }
1.176
1.177 - /// Run the first phase of preflow, starting from a 0 flow, from a flow,
1.178 - /// or from a preflow, according to \c fe.
1.179 - void preflowPhase0( flowEnum fe );
1.180 + /// Run the first phase of preflow, starting from a 0 flow, from a flow,
1.181 + /// or from a preflow, of from undefined value according to \c fe.
1.182 + void preflowPhase0(flowEnum fe);
1.183
1.184 /// Second phase of preflow.
1.185 void preflowPhase1();
1.186
1.187 - /// Starting from a flow, this method searches for an augmenting path
1.188 - /// according to the Edmonds-Karp algorithm
1.189 - /// and augments the flow on if any.
1.190 + /// Starting from a flow, this method searches for an augmenting path
1.191 + /// according to the Edmonds-Karp algorithm
1.192 + /// and augments the flow on if any.
1.193 /// The return value shows if the augmentation was succesful.
1.194 bool augmentOnShortestPath();
1.195
1.196 - /// Starting from a flow, this method searches for an augmenting blockin
1.197 - /// flow according to Dinits' algorithm and augments the flow on if any.
1.198 - /// The blocking flow is computed in a physically constructed
1.199 + /// Starting from a flow, this method searches for an augmenting blocking
1.200 + /// flow according to Dinits' algorithm and augments the flow on if any.
1.201 + /// The blocking flow is computed in a physically constructed
1.202 /// residual graph of type \c Mutablegraph.
1.203 /// The return value show sif the augmentation was succesful.
1.204 template<typename MutableGraph> bool augmentOnBlockingFlow();
1.205
1.206 - /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
1.207 + /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
1.208 /// residual graph is not constructed physically.
1.209 /// The return value shows if the augmentation was succesful.
1.210 bool augmentOnBlockingFlow2();
1.211
1.212 /// Returns the actual flow value.
1.213 - /// More precisely, it returns the negative excess of s, thus
1.214 + /// More precisely, it returns the negative excess of s, thus
1.215 /// this works also for preflows.
1.216 - Num flowValue() {
1.217 + Num flowValue() {
1.218 Num a=0;
1.219 - FOR_EACH_INC_LOC(OutEdgeIt, e, *g, s) a+=(*flow)[e];
1.220 - FOR_EACH_INC_LOC(InEdgeIt, e, *g, s) a-=(*flow)[e];
1.221 + FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
1.222 + FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
1.223 return a;
1.224 }
1.225
1.226 /// Should be used between preflowPhase0 and preflowPhase1.
1.227 - ///\todo We have to make some status variable which shows the actual state
1.228 - /// of the class. This enables us to determine which methods are valid
1.229 + /// \todo We have to make some status variable which shows the
1.230 + /// actual state
1.231 + /// of the class. This enables us to determine which methods are valid
1.232 /// for MinCut computation
1.233 template<typename _CutMap>
1.234 void actMinCut(_CutMap& M) {
1.235 @@ -188,15 +196,15 @@
1.236 }
1.237 }
1.238
1.239 - /// The unique inclusionwise minimum cut is computed by
1.240 + /// The unique inclusionwise minimum cut is computed by
1.241 /// processing a bfs from s in the residual graph.
1.242 - ///\pre flow have to be a max flow otherwise it will the whole node-set.
1.243 + /// \pre flow have to be a max flow otherwise it will the whole node-set.
1.244 template<typename _CutMap>
1.245 void minMinCut(_CutMap& M) {
1.246 -
1.247 +
1.248 std::queue<Node> queue;
1.249 -
1.250 - M.set(s,true);
1.251 +
1.252 + M.set(s,true);
1.253 queue.push(s);
1.254
1.255 while (!queue.empty()) {
1.256 @@ -210,7 +218,7 @@
1.257 queue.push(v);
1.258 M.set(v, true);
1.259 }
1.260 - }
1.261 + }
1.262
1.263 InEdgeIt f;
1.264 for(g->first(f,w) ; g->valid(f); g->next(f)) {
1.265 @@ -219,14 +227,14 @@
1.266 queue.push(v);
1.267 M.set(v, true);
1.268 }
1.269 - }
1.270 + }
1.271 }
1.272 }
1.273
1.274
1.275 - /// The unique inclusionwise maximum cut is computed by
1.276 + /// The unique inclusionwise maximum cut is computed by
1.277 /// processing a reverse bfs from t in the residual graph.
1.278 - ///\pre flow have to be a max flow otherwise it will be empty.
1.279 + /// \pre flow have to be a max flow otherwise it will be empty.
1.280 template<typename _CutMap>
1.281 void maxMinCut(_CutMap& M) {
1.282
1.283 @@ -236,15 +244,14 @@
1.284 }
1.285
1.286 std::queue<Node> queue;
1.287 -
1.288 - M.set(t,false);
1.289 +
1.290 + M.set(t,false);
1.291 queue.push(t);
1.292
1.293 while (!queue.empty()) {
1.294 Node w=queue.front();
1.295 queue.pop();
1.296
1.297 -
1.298 InEdgeIt e;
1.299 for(g->first(e,w) ; g->valid(e); g->next(e)) {
1.300 Node v=g->tail(e);
1.301 @@ -253,7 +260,7 @@
1.302 M.set(v, false);
1.303 }
1.304 }
1.305 -
1.306 +
1.307 OutEdgeIt f;
1.308 for(g->first(f,w) ; g->valid(f); g->next(f)) {
1.309 Node v=g->head(f);
1.310 @@ -274,112 +281,111 @@
1.311 void resetSource(Node _s) { s=_s; }
1.312 ///
1.313 void resetTarget(Node _t) { t=_t; }
1.314 -
1.315 +
1.316 /// capacity-map is changed.
1.317 void resetCap(const CapMap& _cap) { capacity=&_cap; }
1.318 -
1.319 - /// flow-map is changed.
1.320 +
1.321 + /// flow-map is changed.
1.322 void resetFlow(FlowMap& _flow) { flow=&_flow; }
1.323
1.324
1.325 private:
1.326
1.327 int push(Node w, VecStack& active) {
1.328 -
1.329 +
1.330 int lev=level[w];
1.331 Num exc=excess[w];
1.332 int newlevel=n; //bound on the next level of w
1.333 -
1.334 +
1.335 OutEdgeIt e;
1.336 for(g->first(e,w); g->valid(e); g->next(e)) {
1.337 -
1.338 - if ( (*flow)[e] >= (*capacity)[e] ) continue;
1.339 - Node v=g->head(e);
1.340 -
1.341 +
1.342 + if ( (*flow)[e] >= (*capacity)[e] ) continue;
1.343 + Node v=g->head(e);
1.344 +
1.345 if( lev > level[v] ) { //Push is allowed now
1.346 -
1.347 +
1.348 if ( excess[v]<=0 && v!=t && v!=s ) {
1.349 int lev_v=level[v];
1.350 active[lev_v].push(v);
1.351 }
1.352 -
1.353 +
1.354 Num cap=(*capacity)[e];
1.355 Num flo=(*flow)[e];
1.356 Num remcap=cap-flo;
1.357 -
1.358 +
1.359 if ( remcap >= exc ) { //A nonsaturating push.
1.360 -
1.361 +
1.362 flow->set(e, flo+exc);
1.363 excess.set(v, excess[v]+exc);
1.364 exc=0;
1.365 - break;
1.366 -
1.367 + break;
1.368 +
1.369 } else { //A saturating push.
1.370 flow->set(e, cap);
1.371 excess.set(v, excess[v]+remcap);
1.372 exc-=remcap;
1.373 }
1.374 } else if ( newlevel > level[v] ) newlevel = level[v];
1.375 - } //for out edges wv
1.376 -
1.377 - if ( exc > 0 ) {
1.378 + } //for out edges wv
1.379 +
1.380 + if ( exc > 0 ) {
1.381 InEdgeIt e;
1.382 for(g->first(e,w); g->valid(e); g->next(e)) {
1.383 -
1.384 - if( (*flow)[e] <= 0 ) continue;
1.385 - Node v=g->tail(e);
1.386 -
1.387 +
1.388 + if( (*flow)[e] <= 0 ) continue;
1.389 + Node v=g->tail(e);
1.390 +
1.391 if( lev > level[v] ) { //Push is allowed now
1.392 -
1.393 +
1.394 if ( excess[v]<=0 && v!=t && v!=s ) {
1.395 int lev_v=level[v];
1.396 active[lev_v].push(v);
1.397 }
1.398 -
1.399 +
1.400 Num flo=(*flow)[e];
1.401 -
1.402 +
1.403 if ( flo >= exc ) { //A nonsaturating push.
1.404 -
1.405 +
1.406 flow->set(e, flo-exc);
1.407 excess.set(v, excess[v]+exc);
1.408 exc=0;
1.409 - break;
1.410 + break;
1.411 } else { //A saturating push.
1.412 -
1.413 +
1.414 excess.set(v, excess[v]+flo);
1.415 exc-=flo;
1.416 flow->set(e,0);
1.417 - }
1.418 + }
1.419 } else if ( newlevel > level[v] ) newlevel = level[v];
1.420 } //for in edges vw
1.421 -
1.422 +
1.423 } // if w still has excess after the out edge for cycle
1.424 -
1.425 +
1.426 excess.set(w, exc);
1.427 -
1.428 +
1.429 return newlevel;
1.430 }
1.431
1.432
1.433 - void preflowPreproc ( flowEnum fe, VecStack& active,
1.434 - VecNode& level_list, NNMap& left, NNMap& right )
1.435 + void preflowPreproc(flowEnum fe, VecStack& active,
1.436 + VecNode& level_list, NNMap& left, NNMap& right)
1.437 {
1.438 + std::queue<Node> bfs_queue;
1.439
1.440 - std::queue<Node> bfs_queue;
1.441 -
1.442 - switch ( fe ) {
1.443 - case ZERO_FLOW:
1.444 + switch (fe) {
1.445 + case ZERO_FLOW:
1.446 {
1.447 //Reverse_bfs from t, to find the starting level.
1.448 level.set(t,0);
1.449 bfs_queue.push(t);
1.450 -
1.451 +
1.452 while (!bfs_queue.empty()) {
1.453 -
1.454 - Node v=bfs_queue.front();
1.455 +
1.456 + Node v=bfs_queue.front();
1.457 bfs_queue.pop();
1.458 int l=level[v]+1;
1.459 -
1.460 +
1.461 InEdgeIt e;
1.462 for(g->first(e,v); g->valid(e); g->next(e)) {
1.463 Node w=g->tail(e);
1.464 @@ -393,37 +399,37 @@
1.465 }
1.466 }
1.467 }
1.468 -
1.469 +
1.470 //the starting flow
1.471 OutEdgeIt e;
1.472 - for(g->first(e,s); g->valid(e); g->next(e))
1.473 + for(g->first(e,s); g->valid(e); g->next(e))
1.474 {
1.475 Num c=(*capacity)[e];
1.476 if ( c <= 0 ) continue;
1.477 Node w=g->head(e);
1.478 - if ( level[w] < n ) {
1.479 + if ( level[w] < n ) {
1.480 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
1.481 - flow->set(e, c);
1.482 + flow->set(e, c);
1.483 excess.set(w, excess[w]+c);
1.484 }
1.485 }
1.486 break;
1.487 }
1.488 -
1.489 +
1.490 case GEN_FLOW:
1.491 - case PREFLOW:
1.492 + case PRE_FLOW:
1.493 {
1.494 - //Reverse_bfs from t in the residual graph,
1.495 + //Reverse_bfs from t in the residual graph,
1.496 //to find the starting level.
1.497 level.set(t,0);
1.498 bfs_queue.push(t);
1.499 -
1.500 +
1.501 while (!bfs_queue.empty()) {
1.502 -
1.503 - Node v=bfs_queue.front();
1.504 +
1.505 + Node v=bfs_queue.front();
1.506 bfs_queue.pop();
1.507 int l=level[v]+1;
1.508 -
1.509 +
1.510 InEdgeIt e;
1.511 for(g->first(e,v); g->valid(e); g->next(e)) {
1.512 if ( (*capacity)[e] <= (*flow)[e] ) continue;
1.513 @@ -437,7 +443,7 @@
1.514 level.set(w, l);
1.515 }
1.516 }
1.517 -
1.518 +
1.519 OutEdgeIt f;
1.520 for(g->first(f,v); g->valid(f); g->next(f)) {
1.521 if ( 0 >= (*flow)[f] ) continue;
1.522 @@ -452,70 +458,70 @@
1.523 }
1.524 }
1.525 }
1.526 -
1.527 -
1.528 +
1.529 +
1.530 //the starting flow
1.531 OutEdgeIt e;
1.532 - for(g->first(e,s); g->valid(e); g->next(e))
1.533 + for(g->first(e,s); g->valid(e); g->next(e))
1.534 {
1.535 Num rem=(*capacity)[e]-(*flow)[e];
1.536 if ( rem <= 0 ) continue;
1.537 Node w=g->head(e);
1.538 - if ( level[w] < n ) {
1.539 + if ( level[w] < n ) {
1.540 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
1.541 - flow->set(e, (*capacity)[e]);
1.542 + flow->set(e, (*capacity)[e]);
1.543 excess.set(w, excess[w]+rem);
1.544 }
1.545 }
1.546 -
1.547 +
1.548 InEdgeIt f;
1.549 - for(g->first(f,s); g->valid(f); g->next(f))
1.550 + for(g->first(f,s); g->valid(f); g->next(f))
1.551 {
1.552 if ( (*flow)[f] <= 0 ) continue;
1.553 Node w=g->tail(f);
1.554 - if ( level[w] < n ) {
1.555 + if ( level[w] < n ) {
1.556 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
1.557 excess.set(w, excess[w]+(*flow)[f]);
1.558 - flow->set(f, 0);
1.559 + flow->set(f, 0);
1.560 }
1.561 - }
1.562 + }
1.563 break;
1.564 - } //case PREFLOW
1.565 + } //case PRE_FLOW
1.566 }
1.567 } //preflowPreproc
1.568
1.569
1.570
1.571 - void relabel(Node w, int newlevel, VecStack& active,
1.572 - VecNode& level_list, NNMap& left,
1.573 - NNMap& right, int& b, int& k, bool what_heur )
1.574 + void relabel(Node w, int newlevel, VecStack& active,
1.575 + VecNode& level_list, NNMap& left,
1.576 + NNMap& right, int& b, int& k, bool what_heur )
1.577 {
1.578
1.579 - Num lev=level[w];
1.580 -
1.581 + Num lev=level[w];
1.582 +
1.583 Node right_n=right[w];
1.584 Node left_n=left[w];
1.585 -
1.586 +
1.587 //unlacing starts
1.588 if ( g->valid(right_n) ) {
1.589 if ( g->valid(left_n) ) {
1.590 right.set(left_n, right_n);
1.591 left.set(right_n, left_n);
1.592 } else {
1.593 - level_list[lev]=right_n;
1.594 + level_list[lev]=right_n;
1.595 left.set(right_n, INVALID);
1.596 - }
1.597 + }
1.598 } else {
1.599 if ( g->valid(left_n) ) {
1.600 right.set(left_n, INVALID);
1.601 - } else {
1.602 - level_list[lev]=INVALID;
1.603 - }
1.604 - }
1.605 + } else {
1.606 + level_list[lev]=INVALID;
1.607 + }
1.608 + }
1.609 //unlacing ends
1.610 -
1.611 +
1.612 if ( !g->valid(level_list[lev]) ) {
1.613 -
1.614 +
1.615 //gapping starts
1.616 for (int i=lev; i!=k ; ) {
1.617 Node v=level_list[++i];
1.618 @@ -528,17 +534,17 @@
1.619 while ( !active[i].empty() ) {
1.620 active[i].pop(); //FIXME: ezt szebben kene
1.621 }
1.622 - }
1.623 + }
1.624 }
1.625 -
1.626 +
1.627 level.set(w,n);
1.628 b=lev-1;
1.629 k=b;
1.630 //gapping ends
1.631 -
1.632 +
1.633 } else {
1.634 -
1.635 - if ( newlevel == n ) level.set(w,n);
1.636 +
1.637 + if ( newlevel == n ) level.set(w,n);
1.638 else {
1.639 level.set(w,++newlevel);
1.640 active[newlevel].push(w);
1.641 @@ -551,54 +557,55 @@
1.642 level_list[newlevel]=w;
1.643 }
1.644 }
1.645 -
1.646 +
1.647 } //relabel
1.648
1.649
1.650 - template<typename MapGraphWrapper>
1.651 + template<typename MapGraphWrapper>
1.652 class DistanceMap {
1.653 protected:
1.654 const MapGraphWrapper* g;
1.655 - typename MapGraphWrapper::template NodeMap<int> dist;
1.656 + typename MapGraphWrapper::template NodeMap<int> dist;
1.657 public:
1.658 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
1.659 - void set(const typename MapGraphWrapper::Node& n, int a) {
1.660 - dist.set(n, a);
1.661 + void set(const typename MapGraphWrapper::Node& n, int a) {
1.662 + dist.set(n, a);
1.663 }
1.664 - int operator[](const typename MapGraphWrapper::Node& n)
1.665 + int operator[](const typename MapGraphWrapper::Node& n)
1.666 { return dist[n]; }
1.667 - // int get(const typename MapGraphWrapper::Node& n) const {
1.668 + // int get(const typename MapGraphWrapper::Node& n) const {
1.669 // return dist[n]; }
1.670 - // bool get(const typename MapGraphWrapper::Edge& e) const {
1.671 + // bool get(const typename MapGraphWrapper::Edge& e) const {
1.672 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
1.673 - bool operator[](const typename MapGraphWrapper::Edge& e) const {
1.674 - return (dist[g->tail(e)]<dist[g->head(e)]);
1.675 + bool operator[](const typename MapGraphWrapper::Edge& e) const {
1.676 + return (dist[g->tail(e)]<dist[g->head(e)]);
1.677 }
1.678 };
1.679 -
1.680 +
1.681 };
1.682
1.683
1.684 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1.685 - void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe )
1.686 + void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe )
1.687 {
1.688 -
1.689 - int heur0=(int)(H0*n); //time while running 'bound decrease'
1.690 +
1.691 + int heur0=(int)(H0*n); //time while running 'bound decrease'
1.692 int heur1=(int)(H1*n); //time while running 'highest label'
1.693 int heur=heur1; //starting time interval (#of relabels)
1.694 int numrelabel=0;
1.695 -
1.696 - bool what_heur=1;
1.697 +
1.698 + bool what_heur=1;
1.699 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
1.700
1.701 - bool end=false;
1.702 - //Needed for 'bound decrease', true means no active nodes are above bound b.
1.703 + bool end=false;
1.704 + //Needed for 'bound decrease', true means no active nodes are above bound
1.705 + //b.
1.706
1.707 int k=n-2; //bound on the highest level under n containing a node
1.708 int b=k; //bound on the highest level under n of an active node
1.709 -
1.710 +
1.711 VecStack active(n);
1.712 -
1.713 +
1.714 NNMap left(*g, INVALID);
1.715 NNMap right(*g, INVALID);
1.716 VecNode level_list(n,INVALID);
1.717 @@ -607,22 +614,22 @@
1.718 NodeIt v;
1.719 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
1.720 //setting each node to level n
1.721 -
1.722 - switch ( fe ) {
1.723 - case PREFLOW:
1.724 +
1.725 + switch (fe) {
1.726 + case PRE_FLOW:
1.727 {
1.728 - //counting the excess
1.729 + //computing the excess
1.730 NodeIt v;
1.731 for(g->first(v); g->valid(v); g->next(v)) {
1.732 Num exc=0;
1.733 -
1.734 +
1.735 InEdgeIt e;
1.736 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
1.737 OutEdgeIt f;
1.738 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
1.739 -
1.740 - excess.set(v,exc);
1.741 -
1.742 +
1.743 + excess.set(v,exc);
1.744 +
1.745 //putting the active nodes into the stack
1.746 int lev=level[v];
1.747 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
1.748 @@ -631,26 +638,25 @@
1.749 }
1.750 case GEN_FLOW:
1.751 {
1.752 - //Counting the excess of t
1.753 + //computing the excess of t
1.754 Num exc=0;
1.755 -
1.756 +
1.757 InEdgeIt e;
1.758 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
1.759 OutEdgeIt f;
1.760 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
1.761 -
1.762 - excess.set(t,exc);
1.763 -
1.764 +
1.765 + excess.set(t,exc);
1.766 +
1.767 break;
1.768 }
1.769 - default:
1.770 - break;
1.771 + default:;
1.772 }
1.773 -
1.774 - preflowPreproc( fe, active, level_list, left, right );
1.775 - //End of preprocessing
1.776 -
1.777 -
1.778 +
1.779 + preflowPreproc(fe, active, level_list, left, right);
1.780 + //End of preprocessing
1.781 +
1.782 +
1.783 //Push/relabel on the highest level active nodes.
1.784 while ( true ) {
1.785 if ( b == 0 ) {
1.786 @@ -659,17 +665,17 @@
1.787 end=true;
1.788 } else break;
1.789 }
1.790 -
1.791 - if ( active[b].empty() ) --b;
1.792 +
1.793 + if ( active[b].empty() ) --b;
1.794 else {
1.795 - end=false;
1.796 + end=false;
1.797 Node w=active[b].top();
1.798 active[b].pop();
1.799 int newlevel=push(w,active);
1.800 - if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
1.801 + if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
1.802 left, right, b, k, what_heur);
1.803 -
1.804 - ++numrelabel;
1.805 +
1.806 + ++numrelabel;
1.807 if ( numrelabel >= heur ) {
1.808 numrelabel=0;
1.809 if ( what_heur ) {
1.810 @@ -679,49 +685,49 @@
1.811 } else {
1.812 what_heur=1;
1.813 heur=heur1;
1.814 - b=k;
1.815 + b=k;
1.816 }
1.817 }
1.818 - }
1.819 - }
1.820 + }
1.821 + }
1.822 }
1.823
1.824
1.825
1.826 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1.827 - void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1()
1.828 + void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1()
1.829 {
1.830 -
1.831 +
1.832 int k=n-2; //bound on the highest level under n containing a node
1.833 int b=k; //bound on the highest level under n of an active node
1.834 -
1.835 +
1.836 VecStack active(n);
1.837 level.set(s,0);
1.838 std::queue<Node> bfs_queue;
1.839 bfs_queue.push(s);
1.840 -
1.841 +
1.842 while (!bfs_queue.empty()) {
1.843 -
1.844 - Node v=bfs_queue.front();
1.845 +
1.846 + Node v=bfs_queue.front();
1.847 bfs_queue.pop();
1.848 int l=level[v]+1;
1.849 -
1.850 +
1.851 InEdgeIt e;
1.852 for(g->first(e,v); g->valid(e); g->next(e)) {
1.853 if ( (*capacity)[e] <= (*flow)[e] ) continue;
1.854 Node u=g->tail(e);
1.855 - if ( level[u] >= n ) {
1.856 + if ( level[u] >= n ) {
1.857 bfs_queue.push(u);
1.858 level.set(u, l);
1.859 if ( excess[u] > 0 ) active[l].push(u);
1.860 }
1.861 }
1.862 -
1.863 +
1.864 OutEdgeIt f;
1.865 for(g->first(f,v); g->valid(f); g->next(f)) {
1.866 if ( 0 >= (*flow)[f] ) continue;
1.867 Node u=g->head(f);
1.868 - if ( level[u] >= n ) {
1.869 + if ( level[u] >= n ) {
1.870 bfs_queue.push(u);
1.871 level.set(u, l);
1.872 if ( excess[u] > 0 ) active[l].push(u);
1.873 @@ -731,14 +737,14 @@
1.874 b=n-2;
1.875
1.876 while ( true ) {
1.877 -
1.878 +
1.879 if ( b == 0 ) break;
1.880
1.881 - if ( active[b].empty() ) --b;
1.882 + if ( active[b].empty() ) --b;
1.883 else {
1.884 Node w=active[b].top();
1.885 active[b].pop();
1.886 - int newlevel=push(w,active);
1.887 + int newlevel=push(w,active);
1.888
1.889 //relabel
1.890 if ( excess[w] > 0 ) {
1.891 @@ -753,23 +759,23 @@
1.892
1.893
1.894 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1.895 - bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
1.896 + bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
1.897 {
1.898 ResGW res_graph(*g, *capacity, *flow);
1.899 bool _augment=false;
1.900 -
1.901 +
1.902 //ReachedMap level(res_graph);
1.903 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1.904 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1.905 bfs.pushAndSetReached(s);
1.906 -
1.907 - typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1.908 +
1.909 + typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1.910 pred.set(s, INVALID);
1.911 -
1.912 +
1.913 typename ResGW::template NodeMap<Num> free(res_graph);
1.914 -
1.915 +
1.916 //searching for augmenting path
1.917 - while ( !bfs.finished() ) {
1.918 + while ( !bfs.finished() ) {
1.919 ResGWOutEdgeIt e=bfs;
1.920 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1.921 Node v=res_graph.tail(e);
1.922 @@ -778,20 +784,20 @@
1.923 if (res_graph.valid(pred[v])) {
1.924 free.set(w, std::min(free[v], res_graph.resCap(e)));
1.925 } else {
1.926 - free.set(w, res_graph.resCap(e));
1.927 + free.set(w, res_graph.resCap(e));
1.928 }
1.929 if (res_graph.head(e)==t) { _augment=true; break; }
1.930 }
1.931 -
1.932 +
1.933 ++bfs;
1.934 } //end of searching augmenting path
1.935
1.936 if (_augment) {
1.937 Node n=t;
1.938 Num augment_value=free[t];
1.939 - while (res_graph.valid(pred[n])) {
1.940 + while (res_graph.valid(pred[n])) {
1.941 ResGWEdge e=pred[n];
1.942 - res_graph.augment(e, augment_value);
1.943 + res_graph.augment(e, augment_value);
1.944 n=res_graph.tail(e);
1.945 }
1.946 }
1.947 @@ -805,12 +811,10 @@
1.948
1.949
1.950
1.951 -
1.952 -
1.953 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1.954 - template<typename MutableGraph>
1.955 - bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1.956 - {
1.957 + template<typename MutableGraph>
1.958 + bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1.959 + {
1.960 typedef MutableGraph MG;
1.961 bool _augment=false;
1.962
1.963 @@ -821,13 +825,13 @@
1.964 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1.965 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1.966 bfs.pushAndSetReached(s);
1.967 - typename ResGW::template NodeMap<int>
1.968 + typename ResGW::template NodeMap<int>
1.969 dist(res_graph); //filled up with 0's
1.970
1.971 //F will contain the physical copy of the residual graph
1.972 //with the set of edges which are on shortest paths
1.973 MG F;
1.974 - typename ResGW::template NodeMap<typename MG::Node>
1.975 + typename ResGW::template NodeMap<typename MG::Node>
1.976 res_graph_to_F(res_graph);
1.977 {
1.978 typename ResGW::NodeIt n;
1.979 @@ -841,19 +845,21 @@
1.980 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1.981 typename MG::template EdgeMap<Num> residual_capacity(F);
1.982
1.983 - while ( !bfs.finished() ) {
1.984 + while ( !bfs.finished() ) {
1.985 ResGWOutEdgeIt e=bfs;
1.986 if (res_graph.valid(e)) {
1.987 if (bfs.isBNodeNewlyReached()) {
1.988 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1.989 - typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
1.990 + typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1.991 + res_graph_to_F[res_graph.head(e)]);
1.992 original_edge.update();
1.993 original_edge.set(f, e);
1.994 residual_capacity.update();
1.995 residual_capacity.set(f, res_graph.resCap(e));
1.996 } else {
1.997 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1.998 - typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
1.999 + typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1.1000 + res_graph_to_F[res_graph.head(e)]);
1.1001 original_edge.update();
1.1002 original_edge.set(f, e);
1.1003 residual_capacity.update();
1.1004 @@ -876,7 +882,7 @@
1.1005
1.1006 typename MG::template NodeMap<Num> free(F);
1.1007
1.1008 - dfs.pushAndSetReached(sF);
1.1009 + dfs.pushAndSetReached(sF);
1.1010 while (!dfs.finished()) {
1.1011 ++dfs;
1.1012 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1.1013 @@ -887,58 +893,56 @@
1.1014 if (F.valid(pred[v])) {
1.1015 free.set(w, std::min(free[v], residual_capacity[dfs]));
1.1016 } else {
1.1017 - free.set(w, residual_capacity[dfs]);
1.1018 + free.set(w, residual_capacity[dfs]);
1.1019 }
1.1020 - if (w==tF) {
1.1021 - __augment=true;
1.1022 + if (w==tF) {
1.1023 + __augment=true;
1.1024 _augment=true;
1.1025 - break;
1.1026 + break;
1.1027 }
1.1028 -
1.1029 +
1.1030 } else {
1.1031 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1.1032 }
1.1033 - }
1.1034 + }
1.1035 }
1.1036
1.1037 if (__augment) {
1.1038 typename MG::Node n=tF;
1.1039 Num augment_value=free[tF];
1.1040 - while (F.valid(pred[n])) {
1.1041 + while (F.valid(pred[n])) {
1.1042 typename MG::Edge e=pred[n];
1.1043 - res_graph.augment(original_edge[e], augment_value);
1.1044 + res_graph.augment(original_edge[e], augment_value);
1.1045 n=F.tail(e);
1.1046 - if (residual_capacity[e]==augment_value)
1.1047 - F.erase(e);
1.1048 - else
1.1049 + if (residual_capacity[e]==augment_value)
1.1050 + F.erase(e);
1.1051 + else
1.1052 residual_capacity.set(e, residual_capacity[e]-augment_value);
1.1053 }
1.1054 }
1.1055 -
1.1056 +
1.1057 }
1.1058 -
1.1059 +
1.1060 return _augment;
1.1061 }
1.1062
1.1063
1.1064
1.1065
1.1066 -
1.1067 -
1.1068 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1.1069 - bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1.1070 + bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1.1071 {
1.1072 bool _augment=false;
1.1073
1.1074 ResGW res_graph(*g, *capacity, *flow);
1.1075 -
1.1076 +
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
1.1081 bfs.pushAndSetReached(s);
1.1082 DistanceMap<ResGW> dist(res_graph);
1.1083 - while ( !bfs.finished() ) {
1.1084 + while ( !bfs.finished() ) {
1.1085 ResGWOutEdgeIt e=bfs;
1.1086 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1.1087 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1.1088 @@ -948,17 +952,17 @@
1.1089
1.1090 //Subgraph containing the edges on some shortest paths
1.1091 ConstMap<typename ResGW::Node, bool> true_map(true);
1.1092 - typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1.1093 + typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1.1094 DistanceMap<ResGW> > FilterResGW;
1.1095 FilterResGW filter_res_graph(res_graph, true_map, dist);
1.1096
1.1097 - //Subgraph, which is able to delete edges which are already
1.1098 + //Subgraph, which is able to delete edges which are already
1.1099 //met by the dfs
1.1100 - typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1.1101 + typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1.1102 first_out_edges(filter_res_graph);
1.1103 typename FilterResGW::NodeIt v;
1.1104 - for(filter_res_graph.first(v); filter_res_graph.valid(v);
1.1105 - filter_res_graph.next(v))
1.1106 + for(filter_res_graph.first(v); filter_res_graph.valid(v);
1.1107 + filter_res_graph.next(v))
1.1108 {
1.1109 typename FilterResGW::OutEdgeIt e;
1.1110 filter_res_graph.first(e, v);
1.1111 @@ -974,57 +978,60 @@
1.1112
1.1113 __augment=false;
1.1114 //computing blocking flow with dfs
1.1115 - DfsIterator< ErasingResGW,
1.1116 - typename ErasingResGW::template NodeMap<bool> >
1.1117 + DfsIterator< ErasingResGW,
1.1118 + typename ErasingResGW::template NodeMap<bool> >
1.1119 dfs(erasing_res_graph);
1.1120 typename ErasingResGW::
1.1121 - template NodeMap<typename ErasingResGW::OutEdgeIt>
1.1122 - pred(erasing_res_graph);
1.1123 + template NodeMap<typename ErasingResGW::OutEdgeIt>
1.1124 + pred(erasing_res_graph);
1.1125 pred.set(s, INVALID);
1.1126 //invalid iterators for sources
1.1127
1.1128 - typename ErasingResGW::template NodeMap<Num>
1.1129 + typename ErasingResGW::template NodeMap<Num>
1.1130 free1(erasing_res_graph);
1.1131
1.1132 - dfs.pushAndSetReached(
1.1133 - typename ErasingResGW::Node(
1.1134 - typename FilterResGW::Node(
1.1135 - typename ResGW::Node(s)
1.1136 - )
1.1137 - )
1.1138 - );
1.1139 + dfs.pushAndSetReached
1.1140 + ///\bug hugo 0.2
1.1141 + (typename ErasingResGW::Node
1.1142 + (typename FilterResGW::Node
1.1143 + (typename ResGW::Node(s)
1.1144 + )
1.1145 + )
1.1146 + );
1.1147 while (!dfs.finished()) {
1.1148 ++dfs;
1.1149 - if (erasing_res_graph.valid(
1.1150 - typename ErasingResGW::OutEdgeIt(dfs)))
1.1151 - {
1.1152 + if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1.1153 + {
1.1154 if (dfs.isBNodeNewlyReached()) {
1.1155 -
1.1156 +
1.1157 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1.1158 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1.1159
1.1160 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1.1161 if (erasing_res_graph.valid(pred[v])) {
1.1162 - free1.set(w, std::min(free1[v], res_graph.resCap(
1.1163 - typename ErasingResGW::OutEdgeIt(dfs))));
1.1164 + free1.set
1.1165 + (w, std::min(free1[v], res_graph.resCap
1.1166 + (typename ErasingResGW::OutEdgeIt(dfs))));
1.1167 } else {
1.1168 - free1.set(w, res_graph.resCap(
1.1169 - typename ErasingResGW::OutEdgeIt(dfs)));
1.1170 + free1.set
1.1171 + (w, res_graph.resCap
1.1172 + (typename ErasingResGW::OutEdgeIt(dfs)));
1.1173 }
1.1174 -
1.1175 - if (w==t) {
1.1176 - __augment=true;
1.1177 +
1.1178 + if (w==t) {
1.1179 + __augment=true;
1.1180 _augment=true;
1.1181 - break;
1.1182 + break;
1.1183 }
1.1184 } else {
1.1185 erasing_res_graph.erase(dfs);
1.1186 }
1.1187 }
1.1188 - }
1.1189 + }
1.1190
1.1191 if (__augment) {
1.1192 - typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
1.1193 + typename ErasingResGW::Node
1.1194 + n=typename FilterResGW::Node(typename ResGW::Node(t));
1.1195 // typename ResGW::NodeMap<Num> a(res_graph);
1.1196 // typename ResGW::Node b;
1.1197 // Num j=a[b];
1.1198 @@ -1035,7 +1042,7 @@
1.1199 // typename ErasingResGW::Node b2;
1.1200 // Num j2=a2[b2];
1.1201 Num augment_value=free1[n];
1.1202 - while (erasing_res_graph.valid(pred[n])) {
1.1203 + while (erasing_res_graph.valid(pred[n])) {
1.1204 typename ErasingResGW::OutEdgeIt e=pred[n];
1.1205 res_graph.augment(e, augment_value);
1.1206 n=erasing_res_graph.tail(e);
1.1207 @@ -1043,15 +1050,13 @@
1.1208 erasing_res_graph.erase(e);
1.1209 }
1.1210 }
1.1211 -
1.1212 - } //while (__augment)
1.1213 -
1.1214 +
1.1215 + } //while (__augment)
1.1216 +
1.1217 return _augment;
1.1218 }
1.1219
1.1220
1.1221 -
1.1222 -
1.1223 } //namespace hugo
1.1224
1.1225 #endif //HUGO_MAX_FLOW_H