1 | /* -*- C++ -*- |
---|
2 | * src/hugo/preflow.h - Part of HUGOlib, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef HUGO_PREFLOW_H |
---|
18 | #define HUGO_PREFLOW_H |
---|
19 | |
---|
20 | #include <vector> |
---|
21 | #include <queue> |
---|
22 | |
---|
23 | #include <hugo/invalid.h> |
---|
24 | #include <hugo/maps.h> |
---|
25 | |
---|
26 | /// \file |
---|
27 | /// \ingroup flowalgs |
---|
28 | /// Implementation of the preflow algorithm. |
---|
29 | |
---|
30 | namespace hugo { |
---|
31 | |
---|
32 | /// \addtogroup flowalgs |
---|
33 | /// @{ |
---|
34 | |
---|
35 | ///%Preflow algorithms class. |
---|
36 | |
---|
37 | ///This class provides an implementation of the \e preflow \e |
---|
38 | ///algorithm producing a flow of maximum value in a directed |
---|
39 | ///graph. The preflow algorithms are the fastest max flow algorithms |
---|
40 | ///up to now. The \e source node, the \e target node, the \e |
---|
41 | ///capacity of the edges and the \e starting \e flow value of the |
---|
42 | ///edges should be passed to the algorithm through the |
---|
43 | ///constructor. It is possible to change these quantities using the |
---|
44 | ///functions \ref setSource, \ref setTarget, \ref setCap and \ref |
---|
45 | ///setFlow. |
---|
46 | /// |
---|
47 | ///After running \ref hugo::Preflow::phase1() "phase1()" |
---|
48 | ///or \ref hugo::Preflow::run() "run()", the actual flow |
---|
49 | ///value can be obtained by calling \ref flowValue(). The minimum |
---|
50 | ///value cut can be written into a <tt>bool</tt> node map by |
---|
51 | ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes |
---|
52 | ///the inclusionwise minimum and maximum of the minimum value cuts, |
---|
53 | ///resp.) |
---|
54 | /// |
---|
55 | ///\param Graph The directed graph type the algorithm runs on. |
---|
56 | ///\param Num The number type of the capacities and the flow values. |
---|
57 | ///\param CapMap The capacity map type. |
---|
58 | ///\param FlowMap The flow map type. |
---|
59 | /// |
---|
60 | ///\author Jacint Szabo |
---|
61 | template <typename Graph, typename Num, |
---|
62 | typename CapMap=typename Graph::template EdgeMap<Num>, |
---|
63 | typename FlowMap=typename Graph::template EdgeMap<Num> > |
---|
64 | class Preflow { |
---|
65 | protected: |
---|
66 | typedef typename Graph::Node Node; |
---|
67 | typedef typename Graph::NodeIt NodeIt; |
---|
68 | typedef typename Graph::EdgeIt EdgeIt; |
---|
69 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
70 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
71 | |
---|
72 | typedef typename Graph::template NodeMap<Node> NNMap; |
---|
73 | typedef typename std::vector<Node> VecNode; |
---|
74 | |
---|
75 | const Graph* g; |
---|
76 | Node s; |
---|
77 | Node t; |
---|
78 | const CapMap* capacity; |
---|
79 | FlowMap* flow; |
---|
80 | int n; //the number of nodes of G |
---|
81 | |
---|
82 | typename Graph::template NodeMap<int> level; |
---|
83 | typename Graph::template NodeMap<Num> excess; |
---|
84 | |
---|
85 | // constants used for heuristics |
---|
86 | static const int H0=20; |
---|
87 | static const int H1=1; |
---|
88 | |
---|
89 | public: |
---|
90 | |
---|
91 | ///Indicates the property of the starting flow map. |
---|
92 | |
---|
93 | ///Indicates the property of the starting flow map. The meanings are as follows: |
---|
94 | ///- \c ZERO_FLOW: constant zero flow |
---|
95 | ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to |
---|
96 | ///the sum of the out-flows in every node except the \e source and |
---|
97 | ///the \e target. |
---|
98 | ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at |
---|
99 | ///least the sum of the out-flows in every node except the \e source. |
---|
100 | ///- \c NO_FLOW: indicates an unspecified edge map. \c flow will be |
---|
101 | ///set to the constant zero flow in the beginning of |
---|
102 | ///the algorithm in this case. |
---|
103 | /// |
---|
104 | enum FlowEnum{ |
---|
105 | NO_FLOW, |
---|
106 | ZERO_FLOW, |
---|
107 | GEN_FLOW, |
---|
108 | PRE_FLOW |
---|
109 | }; |
---|
110 | |
---|
111 | ///Indicates the state of the preflow algorithm. |
---|
112 | |
---|
113 | ///Indicates the state of the preflow algorithm. The meanings are as follows: |
---|
114 | ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state. |
---|
115 | ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1 |
---|
116 | ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2() |
---|
117 | /// |
---|
118 | enum StatusEnum { |
---|
119 | AFTER_NOTHING, |
---|
120 | AFTER_PREFLOW_PHASE_1, |
---|
121 | AFTER_PREFLOW_PHASE_2 |
---|
122 | }; |
---|
123 | |
---|
124 | protected: |
---|
125 | FlowEnum flow_prop; |
---|
126 | StatusEnum status; // Do not needle this flag only if necessary. |
---|
127 | |
---|
128 | public: |
---|
129 | ///The constructor of the class. |
---|
130 | |
---|
131 | ///The constructor of the class. |
---|
132 | ///\param _G The directed graph the algorithm runs on. |
---|
133 | ///\param _s The source node. |
---|
134 | ///\param _t The target node. |
---|
135 | ///\param _capacity The capacity of the edges. |
---|
136 | ///\param _flow The flow of the edges. |
---|
137 | ///Except the graph, all of these parameters can be reset by |
---|
138 | ///calling \ref setSource, \ref setTarget, \ref setCap and \ref |
---|
139 | ///setFlow, resp. |
---|
140 | Preflow(const Graph& _G, Node _s, Node _t, |
---|
141 | const CapMap& _capacity, FlowMap& _flow) : |
---|
142 | g(&_G), s(_s), t(_t), capacity(&_capacity), |
---|
143 | flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), |
---|
144 | flow_prop(NO_FLOW), status(AFTER_NOTHING) { } |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | ///Runs the preflow algorithm. |
---|
149 | |
---|
150 | ///Runs the preflow algorithm. |
---|
151 | /// |
---|
152 | void run() { |
---|
153 | phase1(flow_prop); |
---|
154 | phase2(); |
---|
155 | } |
---|
156 | |
---|
157 | ///Runs the preflow algorithm. |
---|
158 | |
---|
159 | ///Runs the preflow algorithm. |
---|
160 | ///\pre The starting flow map must be |
---|
161 | /// - a constant zero flow if \c fp is \c ZERO_FLOW, |
---|
162 | /// - an arbitrary flow if \c fp is \c GEN_FLOW, |
---|
163 | /// - an arbitrary preflow if \c fp is \c PRE_FLOW, |
---|
164 | /// - any map if \c fp is NO_FLOW. |
---|
165 | ///If the starting flow map is a flow or a preflow then |
---|
166 | ///the algorithm terminates faster. |
---|
167 | void run(FlowEnum fp) { |
---|
168 | flow_prop=fp; |
---|
169 | run(); |
---|
170 | } |
---|
171 | |
---|
172 | ///Runs the first phase of the preflow algorithm. |
---|
173 | |
---|
174 | ///The preflow algorithm consists of two phases, this method runs the |
---|
175 | ///first phase. After the first phase the maximum flow value and a |
---|
176 | ///minimum value cut can already be computed, though a maximum flow |
---|
177 | ///is not yet obtained. So after calling this method \ref flowValue |
---|
178 | ///and \ref minCut gives proper results. |
---|
179 | ///\warning \ref minMinCut and \ref maxMinCut do not |
---|
180 | ///give minimum value cuts unless calling \ref phase2. |
---|
181 | ///\pre The starting flow must be |
---|
182 | /// - a constant zero flow if \c fp is \c ZERO_FLOW, |
---|
183 | /// - an arbitary flow if \c fp is \c GEN_FLOW, |
---|
184 | /// - an arbitary preflow if \c fp is \c PRE_FLOW, |
---|
185 | /// - any map if \c fp is NO_FLOW. |
---|
186 | void phase1(FlowEnum fp) |
---|
187 | { |
---|
188 | flow_prop=fp; |
---|
189 | phase1(); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | ///Runs the first phase of the preflow algorithm. |
---|
194 | |
---|
195 | ///The preflow algorithm consists of two phases, this method runs the |
---|
196 | ///first phase. After the first phase the maximum flow value and a |
---|
197 | ///minimum value cut can already be computed, though a maximum flow |
---|
198 | ///is not yet obtained. So after calling this method \ref flowValue |
---|
199 | ///and \ref MinCut() gives proper results. |
---|
200 | ///\warning \ref minCut(), \ref minMinCut() and \ref maxMinCut() do not |
---|
201 | ///give minimum value cuts unless calling \ref phase2(). |
---|
202 | void phase1() |
---|
203 | { |
---|
204 | int heur0=(int)(H0*n); //time while running 'bound decrease' |
---|
205 | int heur1=(int)(H1*n); //time while running 'highest label' |
---|
206 | int heur=heur1; //starting time interval (#of relabels) |
---|
207 | int numrelabel=0; |
---|
208 | |
---|
209 | bool what_heur=1; |
---|
210 | //It is 0 in case 'bound decrease' and 1 in case 'highest label' |
---|
211 | |
---|
212 | bool end=false; |
---|
213 | //Needed for 'bound decrease', true means no active |
---|
214 | //nodes are above bound b. |
---|
215 | |
---|
216 | int k=n-2; //bound on the highest level under n containing a node |
---|
217 | int b=k; //bound on the highest level under n of an active node |
---|
218 | |
---|
219 | VecNode first(n, INVALID); |
---|
220 | NNMap next(*g, INVALID); |
---|
221 | |
---|
222 | NNMap left(*g, INVALID); |
---|
223 | NNMap right(*g, INVALID); |
---|
224 | VecNode level_list(n,INVALID); |
---|
225 | //List of the nodes in level i<n, set to n. |
---|
226 | |
---|
227 | preflowPreproc(first, next, level_list, left, right); |
---|
228 | |
---|
229 | //Push/relabel on the highest level active nodes. |
---|
230 | while ( true ) { |
---|
231 | if ( b == 0 ) { |
---|
232 | if ( !what_heur && !end && k > 0 ) { |
---|
233 | b=k; |
---|
234 | end=true; |
---|
235 | } else break; |
---|
236 | } |
---|
237 | |
---|
238 | if ( first[b]==INVALID ) --b; |
---|
239 | else { |
---|
240 | end=false; |
---|
241 | Node w=first[b]; |
---|
242 | first[b]=next[w]; |
---|
243 | int newlevel=push(w, next, first); |
---|
244 | if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list, |
---|
245 | left, right, b, k, what_heur); |
---|
246 | |
---|
247 | ++numrelabel; |
---|
248 | if ( numrelabel >= heur ) { |
---|
249 | numrelabel=0; |
---|
250 | if ( what_heur ) { |
---|
251 | what_heur=0; |
---|
252 | heur=heur0; |
---|
253 | end=false; |
---|
254 | } else { |
---|
255 | what_heur=1; |
---|
256 | heur=heur1; |
---|
257 | b=k; |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | flow_prop=PRE_FLOW; |
---|
263 | status=AFTER_PREFLOW_PHASE_1; |
---|
264 | } |
---|
265 | // Heuristics: |
---|
266 | // 2 phase |
---|
267 | // gap |
---|
268 | // list 'level_list' on the nodes on level i implemented by hand |
---|
269 | // stack 'active' on the active nodes on level i |
---|
270 | // runs heuristic 'highest label' for H1*n relabels |
---|
271 | // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' |
---|
272 | // Parameters H0 and H1 are initialized to 20 and 1. |
---|
273 | |
---|
274 | |
---|
275 | ///Runs the second phase of the preflow algorithm. |
---|
276 | |
---|
277 | ///The preflow algorithm consists of two phases, this method runs |
---|
278 | ///the second phase. After calling \ref phase1 and then |
---|
279 | ///\ref phase2 the methods \ref flowValue, \ref minCut, |
---|
280 | ///\ref minMinCut and \ref maxMinCut give proper results. |
---|
281 | ///\pre \ref phase1 must be called before. |
---|
282 | void phase2() |
---|
283 | { |
---|
284 | |
---|
285 | int k=n-2; //bound on the highest level under n containing a node |
---|
286 | int b=k; //bound on the highest level under n of an active node |
---|
287 | |
---|
288 | |
---|
289 | VecNode first(n, INVALID); |
---|
290 | NNMap next(*g, INVALID); |
---|
291 | level.set(s,0); |
---|
292 | std::queue<Node> bfs_queue; |
---|
293 | bfs_queue.push(s); |
---|
294 | |
---|
295 | while ( !bfs_queue.empty() ) { |
---|
296 | |
---|
297 | Node v=bfs_queue.front(); |
---|
298 | bfs_queue.pop(); |
---|
299 | int l=level[v]+1; |
---|
300 | |
---|
301 | for(InEdgeIt e(*g,v); e!=INVALID; ++e) { |
---|
302 | if ( (*capacity)[e] <= (*flow)[e] ) continue; |
---|
303 | Node u=g->tail(e); |
---|
304 | if ( level[u] >= n ) { |
---|
305 | bfs_queue.push(u); |
---|
306 | level.set(u, l); |
---|
307 | if ( excess[u] > 0 ) { |
---|
308 | next.set(u,first[l]); |
---|
309 | first[l]=u; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | for(OutEdgeIt e(*g,v); e!=INVALID; ++e) { |
---|
315 | if ( 0 >= (*flow)[e] ) continue; |
---|
316 | Node u=g->head(e); |
---|
317 | if ( level[u] >= n ) { |
---|
318 | bfs_queue.push(u); |
---|
319 | level.set(u, l); |
---|
320 | if ( excess[u] > 0 ) { |
---|
321 | next.set(u,first[l]); |
---|
322 | first[l]=u; |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | b=n-2; |
---|
328 | |
---|
329 | while ( true ) { |
---|
330 | |
---|
331 | if ( b == 0 ) break; |
---|
332 | if ( first[b]==INVALID ) --b; |
---|
333 | else { |
---|
334 | Node w=first[b]; |
---|
335 | first[b]=next[w]; |
---|
336 | int newlevel=push(w,next, first); |
---|
337 | |
---|
338 | //relabel |
---|
339 | if ( excess[w] > 0 ) { |
---|
340 | level.set(w,++newlevel); |
---|
341 | next.set(w,first[newlevel]); |
---|
342 | first[newlevel]=w; |
---|
343 | b=newlevel; |
---|
344 | } |
---|
345 | } |
---|
346 | } // while(true) |
---|
347 | flow_prop=GEN_FLOW; |
---|
348 | status=AFTER_PREFLOW_PHASE_2; |
---|
349 | } |
---|
350 | |
---|
351 | /// Returns the value of the maximum flow. |
---|
352 | |
---|
353 | /// Returns the value of the maximum flow by returning the excess |
---|
354 | /// of the target node \c t. This value equals to the value of |
---|
355 | /// the maximum flow already after running \ref phase1. |
---|
356 | Num flowValue() const { |
---|
357 | return excess[t]; |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | ///Returns a minimum value cut. |
---|
362 | |
---|
363 | ///Sets \c M to the characteristic vector of a minimum value |
---|
364 | ///cut. This method can be called both after running \ref |
---|
365 | ///phase1 and \ref phase2. It is much faster after |
---|
366 | ///\ref phase1. \pre M should be a bool-valued node-map. \pre |
---|
367 | ///If \ref minCut() is called after \ref phase2() then M should |
---|
368 | ///be initialized to false. |
---|
369 | template<typename _CutMap> |
---|
370 | void minCut(_CutMap& M) const { |
---|
371 | switch ( status ) { |
---|
372 | case AFTER_PREFLOW_PHASE_1: |
---|
373 | for(NodeIt v(*g); v!=INVALID; ++v) { |
---|
374 | if (level[v] < n) { |
---|
375 | M.set(v, false); |
---|
376 | } else { |
---|
377 | M.set(v, true); |
---|
378 | } |
---|
379 | } |
---|
380 | break; |
---|
381 | case AFTER_PREFLOW_PHASE_2: |
---|
382 | minMinCut(M); |
---|
383 | break; |
---|
384 | case AFTER_NOTHING: |
---|
385 | break; |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | ///Returns the inclusionwise minimum of the minimum value cuts. |
---|
390 | |
---|
391 | ///Sets \c M to the characteristic vector of the minimum value cut |
---|
392 | ///which is inclusionwise minimum. It is computed by processing a |
---|
393 | ///bfs from the source node \c s in the residual graph. \pre M |
---|
394 | ///should be a node map of bools initialized to false. \pre \ref |
---|
395 | ///phase2 should already be run. |
---|
396 | template<typename _CutMap> |
---|
397 | void minMinCut(_CutMap& M) const { |
---|
398 | |
---|
399 | std::queue<Node> queue; |
---|
400 | M.set(s,true); |
---|
401 | queue.push(s); |
---|
402 | |
---|
403 | while (!queue.empty()) { |
---|
404 | Node w=queue.front(); |
---|
405 | queue.pop(); |
---|
406 | |
---|
407 | for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
408 | Node v=g->head(e); |
---|
409 | if (!M[v] && (*flow)[e] < (*capacity)[e] ) { |
---|
410 | queue.push(v); |
---|
411 | M.set(v, true); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
416 | Node v=g->tail(e); |
---|
417 | if (!M[v] && (*flow)[e] > 0 ) { |
---|
418 | queue.push(v); |
---|
419 | M.set(v, true); |
---|
420 | } |
---|
421 | } |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | ///Returns the inclusionwise maximum of the minimum value cuts. |
---|
426 | |
---|
427 | ///Sets \c M to the characteristic vector of the minimum value cut |
---|
428 | ///which is inclusionwise maximum. It is computed by processing a |
---|
429 | ///backward bfs from the target node \c t in the residual graph. |
---|
430 | ///\pre \ref phase2() or run() should already be run. |
---|
431 | template<typename _CutMap> |
---|
432 | void maxMinCut(_CutMap& M) const { |
---|
433 | |
---|
434 | for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true); |
---|
435 | |
---|
436 | std::queue<Node> queue; |
---|
437 | |
---|
438 | M.set(t,false); |
---|
439 | queue.push(t); |
---|
440 | |
---|
441 | while (!queue.empty()) { |
---|
442 | Node w=queue.front(); |
---|
443 | queue.pop(); |
---|
444 | |
---|
445 | for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
446 | Node v=g->tail(e); |
---|
447 | if (M[v] && (*flow)[e] < (*capacity)[e] ) { |
---|
448 | queue.push(v); |
---|
449 | M.set(v, false); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
454 | Node v=g->head(e); |
---|
455 | if (M[v] && (*flow)[e] > 0 ) { |
---|
456 | queue.push(v); |
---|
457 | M.set(v, false); |
---|
458 | } |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | ///Sets the source node to \c _s. |
---|
464 | |
---|
465 | ///Sets the source node to \c _s. |
---|
466 | /// |
---|
467 | void setSource(Node _s) { |
---|
468 | s=_s; |
---|
469 | if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW; |
---|
470 | status=AFTER_NOTHING; |
---|
471 | } |
---|
472 | |
---|
473 | ///Sets the target node to \c _t. |
---|
474 | |
---|
475 | ///Sets the target node to \c _t. |
---|
476 | /// |
---|
477 | void setTarget(Node _t) { |
---|
478 | t=_t; |
---|
479 | if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW; |
---|
480 | status=AFTER_NOTHING; |
---|
481 | } |
---|
482 | |
---|
483 | /// Sets the edge map of the capacities to _cap. |
---|
484 | |
---|
485 | /// Sets the edge map of the capacities to _cap. |
---|
486 | /// |
---|
487 | void setCap(const CapMap& _cap) { |
---|
488 | capacity=&_cap; |
---|
489 | status=AFTER_NOTHING; |
---|
490 | } |
---|
491 | |
---|
492 | /// Sets the edge map of the flows to _flow. |
---|
493 | |
---|
494 | /// Sets the edge map of the flows to _flow. |
---|
495 | /// |
---|
496 | void setFlow(FlowMap& _flow) { |
---|
497 | flow=&_flow; |
---|
498 | flow_prop=NO_FLOW; |
---|
499 | status=AFTER_NOTHING; |
---|
500 | } |
---|
501 | |
---|
502 | |
---|
503 | private: |
---|
504 | |
---|
505 | int push(Node w, NNMap& next, VecNode& first) { |
---|
506 | |
---|
507 | int lev=level[w]; |
---|
508 | Num exc=excess[w]; |
---|
509 | int newlevel=n; //bound on the next level of w |
---|
510 | |
---|
511 | for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
512 | if ( (*flow)[e] >= (*capacity)[e] ) continue; |
---|
513 | Node v=g->head(e); |
---|
514 | |
---|
515 | if( lev > level[v] ) { //Push is allowed now |
---|
516 | |
---|
517 | if ( excess[v]<=0 && v!=t && v!=s ) { |
---|
518 | next.set(v,first[level[v]]); |
---|
519 | first[level[v]]=v; |
---|
520 | } |
---|
521 | |
---|
522 | Num cap=(*capacity)[e]; |
---|
523 | Num flo=(*flow)[e]; |
---|
524 | Num remcap=cap-flo; |
---|
525 | |
---|
526 | if ( remcap >= exc ) { //A nonsaturating push. |
---|
527 | |
---|
528 | flow->set(e, flo+exc); |
---|
529 | excess.set(v, excess[v]+exc); |
---|
530 | exc=0; |
---|
531 | break; |
---|
532 | |
---|
533 | } else { //A saturating push. |
---|
534 | flow->set(e, cap); |
---|
535 | excess.set(v, excess[v]+remcap); |
---|
536 | exc-=remcap; |
---|
537 | } |
---|
538 | } else if ( newlevel > level[v] ) newlevel = level[v]; |
---|
539 | } //for out edges wv |
---|
540 | |
---|
541 | if ( exc > 0 ) { |
---|
542 | for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { |
---|
543 | |
---|
544 | if( (*flow)[e] <= 0 ) continue; |
---|
545 | Node v=g->tail(e); |
---|
546 | |
---|
547 | if( lev > level[v] ) { //Push is allowed now |
---|
548 | |
---|
549 | if ( excess[v]<=0 && v!=t && v!=s ) { |
---|
550 | next.set(v,first[level[v]]); |
---|
551 | first[level[v]]=v; |
---|
552 | } |
---|
553 | |
---|
554 | Num flo=(*flow)[e]; |
---|
555 | |
---|
556 | if ( flo >= exc ) { //A nonsaturating push. |
---|
557 | |
---|
558 | flow->set(e, flo-exc); |
---|
559 | excess.set(v, excess[v]+exc); |
---|
560 | exc=0; |
---|
561 | break; |
---|
562 | } else { //A saturating push. |
---|
563 | |
---|
564 | excess.set(v, excess[v]+flo); |
---|
565 | exc-=flo; |
---|
566 | flow->set(e,0); |
---|
567 | } |
---|
568 | } else if ( newlevel > level[v] ) newlevel = level[v]; |
---|
569 | } //for in edges vw |
---|
570 | |
---|
571 | } // if w still has excess after the out edge for cycle |
---|
572 | |
---|
573 | excess.set(w, exc); |
---|
574 | |
---|
575 | return newlevel; |
---|
576 | } |
---|
577 | |
---|
578 | |
---|
579 | |
---|
580 | void preflowPreproc(VecNode& first, NNMap& next, |
---|
581 | VecNode& level_list, NNMap& left, NNMap& right) |
---|
582 | { |
---|
583 | for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n); |
---|
584 | std::queue<Node> bfs_queue; |
---|
585 | |
---|
586 | if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) { |
---|
587 | //Reverse_bfs from t in the residual graph, |
---|
588 | //to find the starting level. |
---|
589 | level.set(t,0); |
---|
590 | bfs_queue.push(t); |
---|
591 | |
---|
592 | while ( !bfs_queue.empty() ) { |
---|
593 | |
---|
594 | Node v=bfs_queue.front(); |
---|
595 | bfs_queue.pop(); |
---|
596 | int l=level[v]+1; |
---|
597 | |
---|
598 | for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { |
---|
599 | if ( (*capacity)[e] <= (*flow)[e] ) continue; |
---|
600 | Node w=g->tail(e); |
---|
601 | if ( level[w] == n && w != s ) { |
---|
602 | bfs_queue.push(w); |
---|
603 | Node z=level_list[l]; |
---|
604 | if ( z!=INVALID ) left.set(z,w); |
---|
605 | right.set(w,z); |
---|
606 | level_list[l]=w; |
---|
607 | level.set(w, l); |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) { |
---|
612 | if ( 0 >= (*flow)[e] ) continue; |
---|
613 | Node w=g->head(e); |
---|
614 | if ( level[w] == n && w != s ) { |
---|
615 | bfs_queue.push(w); |
---|
616 | Node z=level_list[l]; |
---|
617 | if ( z!=INVALID ) left.set(z,w); |
---|
618 | right.set(w,z); |
---|
619 | level_list[l]=w; |
---|
620 | level.set(w, l); |
---|
621 | } |
---|
622 | } |
---|
623 | } //while |
---|
624 | } //if |
---|
625 | |
---|
626 | |
---|
627 | switch (flow_prop) { |
---|
628 | case NO_FLOW: |
---|
629 | for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0); |
---|
630 | case ZERO_FLOW: |
---|
631 | for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); |
---|
632 | |
---|
633 | //Reverse_bfs from t, to find the starting level. |
---|
634 | level.set(t,0); |
---|
635 | bfs_queue.push(t); |
---|
636 | |
---|
637 | while ( !bfs_queue.empty() ) { |
---|
638 | |
---|
639 | Node v=bfs_queue.front(); |
---|
640 | bfs_queue.pop(); |
---|
641 | int l=level[v]+1; |
---|
642 | |
---|
643 | for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { |
---|
644 | Node w=g->tail(e); |
---|
645 | if ( level[w] == n && w != s ) { |
---|
646 | bfs_queue.push(w); |
---|
647 | Node z=level_list[l]; |
---|
648 | if ( z!=INVALID ) left.set(z,w); |
---|
649 | right.set(w,z); |
---|
650 | level_list[l]=w; |
---|
651 | level.set(w, l); |
---|
652 | } |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | //the starting flow |
---|
657 | for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) { |
---|
658 | Num c=(*capacity)[e]; |
---|
659 | if ( c <= 0 ) continue; |
---|
660 | Node w=g->head(e); |
---|
661 | if ( level[w] < n ) { |
---|
662 | if ( excess[w] <= 0 && w!=t ) { //putting into the stack |
---|
663 | next.set(w,first[level[w]]); |
---|
664 | first[level[w]]=w; |
---|
665 | } |
---|
666 | flow->set(e, c); |
---|
667 | excess.set(w, excess[w]+c); |
---|
668 | } |
---|
669 | } |
---|
670 | break; |
---|
671 | |
---|
672 | case GEN_FLOW: |
---|
673 | for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); |
---|
674 | { |
---|
675 | Num exc=0; |
---|
676 | for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e]; |
---|
677 | for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e]; |
---|
678 | excess.set(t,exc); |
---|
679 | } |
---|
680 | |
---|
681 | //the starting flow |
---|
682 | for(OutEdgeIt e(*g,s); e!=INVALID; ++e) { |
---|
683 | Num rem=(*capacity)[e]-(*flow)[e]; |
---|
684 | if ( rem <= 0 ) continue; |
---|
685 | Node w=g->head(e); |
---|
686 | if ( level[w] < n ) { |
---|
687 | if ( excess[w] <= 0 && w!=t ) { //putting into the stack |
---|
688 | next.set(w,first[level[w]]); |
---|
689 | first[level[w]]=w; |
---|
690 | } |
---|
691 | flow->set(e, (*capacity)[e]); |
---|
692 | excess.set(w, excess[w]+rem); |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | for(InEdgeIt e(*g,s); e!=INVALID; ++e) { |
---|
697 | if ( (*flow)[e] <= 0 ) continue; |
---|
698 | Node w=g->tail(e); |
---|
699 | if ( level[w] < n ) { |
---|
700 | if ( excess[w] <= 0 && w!=t ) { |
---|
701 | next.set(w,first[level[w]]); |
---|
702 | first[level[w]]=w; |
---|
703 | } |
---|
704 | excess.set(w, excess[w]+(*flow)[e]); |
---|
705 | flow->set(e, 0); |
---|
706 | } |
---|
707 | } |
---|
708 | break; |
---|
709 | |
---|
710 | case PRE_FLOW: |
---|
711 | //the starting flow |
---|
712 | for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) { |
---|
713 | Num rem=(*capacity)[e]-(*flow)[e]; |
---|
714 | if ( rem <= 0 ) continue; |
---|
715 | Node w=g->head(e); |
---|
716 | if ( level[w] < n ) flow->set(e, (*capacity)[e]); |
---|
717 | } |
---|
718 | |
---|
719 | for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) { |
---|
720 | if ( (*flow)[e] <= 0 ) continue; |
---|
721 | Node w=g->tail(e); |
---|
722 | if ( level[w] < n ) flow->set(e, 0); |
---|
723 | } |
---|
724 | |
---|
725 | //computing the excess |
---|
726 | for(NodeIt w(*g); w!=INVALID; ++w) { |
---|
727 | Num exc=0; |
---|
728 | for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e]; |
---|
729 | for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e]; |
---|
730 | excess.set(w,exc); |
---|
731 | |
---|
732 | //putting the active nodes into the stack |
---|
733 | int lev=level[w]; |
---|
734 | if ( exc > 0 && lev < n && Node(w) != t ) { |
---|
735 | next.set(w,first[lev]); |
---|
736 | first[lev]=w; |
---|
737 | } |
---|
738 | } |
---|
739 | break; |
---|
740 | } //switch |
---|
741 | } //preflowPreproc |
---|
742 | |
---|
743 | |
---|
744 | void relabel(Node w, int newlevel, VecNode& first, NNMap& next, |
---|
745 | VecNode& level_list, NNMap& left, |
---|
746 | NNMap& right, int& b, int& k, bool what_heur ) |
---|
747 | { |
---|
748 | |
---|
749 | int lev=level[w]; |
---|
750 | |
---|
751 | Node right_n=right[w]; |
---|
752 | Node left_n=left[w]; |
---|
753 | |
---|
754 | //unlacing starts |
---|
755 | if ( right_n!=INVALID ) { |
---|
756 | if ( left_n!=INVALID ) { |
---|
757 | right.set(left_n, right_n); |
---|
758 | left.set(right_n, left_n); |
---|
759 | } else { |
---|
760 | level_list[lev]=right_n; |
---|
761 | left.set(right_n, INVALID); |
---|
762 | } |
---|
763 | } else { |
---|
764 | if ( left_n!=INVALID ) { |
---|
765 | right.set(left_n, INVALID); |
---|
766 | } else { |
---|
767 | level_list[lev]=INVALID; |
---|
768 | } |
---|
769 | } |
---|
770 | //unlacing ends |
---|
771 | |
---|
772 | if ( level_list[lev]==INVALID ) { |
---|
773 | |
---|
774 | //gapping starts |
---|
775 | for (int i=lev; i!=k ; ) { |
---|
776 | Node v=level_list[++i]; |
---|
777 | while ( v!=INVALID ) { |
---|
778 | level.set(v,n); |
---|
779 | v=right[v]; |
---|
780 | } |
---|
781 | level_list[i]=INVALID; |
---|
782 | if ( !what_heur ) first[i]=INVALID; |
---|
783 | } |
---|
784 | |
---|
785 | level.set(w,n); |
---|
786 | b=lev-1; |
---|
787 | k=b; |
---|
788 | //gapping ends |
---|
789 | |
---|
790 | } else { |
---|
791 | |
---|
792 | if ( newlevel == n ) level.set(w,n); |
---|
793 | else { |
---|
794 | level.set(w,++newlevel); |
---|
795 | next.set(w,first[newlevel]); |
---|
796 | first[newlevel]=w; |
---|
797 | if ( what_heur ) b=newlevel; |
---|
798 | if ( k < newlevel ) ++k; //now k=newlevel |
---|
799 | Node z=level_list[newlevel]; |
---|
800 | if ( z!=INVALID ) left.set(z,w); |
---|
801 | right.set(w,z); |
---|
802 | left.set(w,INVALID); |
---|
803 | level_list[newlevel]=w; |
---|
804 | } |
---|
805 | } |
---|
806 | } //relabel |
---|
807 | |
---|
808 | }; |
---|
809 | } //namespace hugo |
---|
810 | |
---|
811 | #endif //HUGO_PREFLOW_H |
---|
812 | |
---|
813 | |
---|
814 | |
---|
815 | |
---|