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