3 preflow.h with 'j_graph interface'
8 list 'level_list' on the nodes on level i implemented by hand
9 stack 'active' on the active nodes on level i implemented by hand
10 runs heuristic 'highest label' for H1*n relabels
11 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
13 Parameters H0 and H1 are initialized to 20 and 10.
15 The best preflow I could ever write.
17 The constructor runs the algorithm.
21 T maxFlow() : returns the value of a maximum flow
23 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e)
25 FlowMap Flow() : returns the fixed maximum flow x
27 void minMinCut(CutMap& M) : sets M to the characteristic vector of the
28 minimum min cut. M should be a map of bools initialized to false.
30 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
31 maximum min cut. M should be a map of bools initialized to false.
33 void minCut(CutMap& M) : sets M to the characteristic vector of
34 a min cut. M should be a map of bools initialized to false.
49 #include <time_measure.h>
53 template <typename Graph, typename T,
54 typename FlowMap=typename Graph::EdgeMap<T>,
55 typename CapMap=typename Graph::EdgeMap<T> >
58 typedef typename Graph::TrivNodeIt NodeIt;
59 typedef typename Graph::TrivEdgeIt EdgeIt;
60 typedef typename Graph::NodeIt EachNodeIt;
61 typedef typename Graph::OutEdgeIt OutEdgeIt;
62 typedef typename Graph::InEdgeIt InEdgeIt;
73 preflow(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
74 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity)
77 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
79 int heur0=(int)(H0*n); //time while running 'bound decrease'
80 int heur1=(int)(H1*n); //time while running 'highest label'
81 int heur=heur1; //starting time interval (#of relabels)
84 what_heur is 0 in case 'bound decrease'
85 and 1 in case 'highest label'
89 Needed for 'bound decrease', 'true'
90 means no active nodes are above bound b.
93 int k=n-2; //bound on the highest level under n containing a node
94 int b=k; //bound on the highest level under n of an active node
96 typename Graph::NodeMap<int> level(G,n);
97 typename Graph::NodeMap<T> excess(G);
99 std::vector<NodeIt> active(n);
100 typename Graph::NodeMap<NodeIt> next(G);
101 //Stack of the active nodes in level i < n.
102 //We use it in both phases.
104 typename Graph::NodeMap<NodeIt> left(G);
105 typename Graph::NodeMap<NodeIt> right(G);
106 std::vector<NodeIt> level_list(n);
108 List of the nodes in level i<n.
111 /*Reverse_bfs from t, to find the starting level.*/
113 std::queue<NodeIt> bfs_queue;
116 while (!bfs_queue.empty()) {
118 NodeIt v=bfs_queue.front();
120 int l=level.get(v)+1;
122 for(InEdgeIt e=G.firstIn(v); e; G.next(e)) {
124 if ( level.get(w) == n && w != s ) {
126 NodeIt first=level_list[l];
127 if ( first ) left.set(first,w);
138 /* Starting flow. It is everywhere 0 at the moment. */
139 for(OutEdgeIt e=G.firstOut(s); e; G.next(e))
142 if ( c == 0 ) continue;
144 if ( level.get(w) < n ) {
145 if ( excess.get(w) == 0 && w!=t ) {
146 next.set(w,active[level.get(w)]);
147 active[level.get(w)]=w;
150 excess.set(w, excess.get(w)+c);
161 Push/relabel on the highest level active nodes.
168 if ( !what_heur && !end && k > 0 ) {
175 std::queue<NodeIt> bfs_queue;
178 while (!bfs_queue.empty()) {
180 NodeIt v=bfs_queue.front();
182 int l=level.get(v)+1;
184 for(InEdgeIt e=G.firstIn(v); e; G.next(e)) {
185 if ( capacity.get(e) == flow.get(e) ) continue;
187 if ( level.get(u) >= n ) {
190 if ( excess.get(u) > 0 ) {
191 next.set(u,active[l]);
197 for(OutEdgeIt e=G.firstOut(v); e; G.next(e)) {
198 if ( 0 == flow.get(e) ) continue;
200 if ( level.get(u) >= n ) {
203 if ( excess.get(u) > 0 ) {
204 next.set(u,active[l]);
216 if ( !active[b] ) --b;
221 active[b]=next.get(w);
222 int lev=level.get(w);
224 int newlevel=n; //bound on the next level of w
226 for(OutEdgeIt e=G.firstOut(w); e; G.next(e)) {
228 if ( flow.get(e) == capacity.get(e) ) continue;
232 if( lev > level.get(v) ) {
233 /*Push is allowed now*/
235 if ( excess.get(v)==0 && v!=t && v!=s ) {
236 int lev_v=level.get(v);
237 next.set(v,active[lev_v]);
241 T cap=capacity.get(e);
245 if ( remcap >= exc ) {
246 /*A nonsaturating push.*/
248 flow.set(e, flo+exc);
249 excess.set(v, excess.get(v)+exc);
254 /*A saturating push.*/
257 excess.set(v, excess.get(v)+remcap);
260 } else if ( newlevel > level.get(v) ){
261 newlevel = level.get(v);
268 for( InEdgeIt e=G.firstIn(w); e; G.next(e)) {
270 if( flow.get(e) == 0 ) continue;
274 if( lev > level.get(v) ) {
275 /*Push is allowed now*/
277 if ( excess.get(v)==0 && v!=t && v!=s ) {
278 int lev_v=level.get(v);
279 next.set(v,active[lev_v]);
286 /*A nonsaturating push.*/
288 flow.set(e, flo-exc);
289 excess.set(v, excess.get(v)+exc);
293 /*A saturating push.*/
295 excess.set(v, excess.get(v)+flo);
299 } else if ( newlevel > level.get(v) ) {
300 newlevel = level.get(v);
304 } // if w still has excess after the out edge for cycle
314 //now 'lev' is the old level of w
317 level.set(w,++newlevel);
318 next.set(w,active[newlevel]);
323 NodeIt right_n=right.get(w);
324 NodeIt left_n=left.get(w);
328 right.set(left_n, right_n);
329 left.set(right_n, left_n);
331 level_list[lev]=right_n;
332 left.set(right_n, NodeIt());
336 right.set(left_n, NodeIt());
338 level_list[lev]=NodeIt();
345 if ( !level_list[lev] ) {
347 for (int i=lev; i!=k ; ) {
348 NodeIt v=level_list[++i];
353 level_list[i]=NodeIt();
354 if ( !what_heur ) active[i]=NodeIt();
363 if ( newlevel == n ) level.set(w,n);
365 level.set(w,++newlevel);
366 next.set(w,active[newlevel]);
368 if ( what_heur ) b=newlevel;
369 if ( k < newlevel ) ++k;
370 NodeIt first=level_list[newlevel];
371 if ( first ) left.set(first,w);
373 left.set(w,NodeIt());
374 level_list[newlevel]=w;
380 if ( relabel >= heur ) {
398 } // if stack[b] is nonempty
403 value = excess.get(t);
413 Returns the maximum value of a flow.
423 For the maximum flow x found by the algorithm,
424 it returns the flow value on edge e, i.e. x(e).
427 T flowOnEdge(EdgeIt e) {
439 void Flow(FlowMap& _flow ) {
440 for(EachNodeIt v=G.firstNode() ; v; G.next(v))
441 _flow.set(v,flow.get(v));
447 Returns the minimum min cut, by a bfs from s in the residual graph.
450 template<typename _CutMap>
451 void minMinCut(_CutMap& M) {
453 std::queue<NodeIt> queue;
458 while (!queue.empty()) {
459 NodeIt w=queue.front();
462 for(OutEdgeIt e=G.firstOut(w) ; e; G.next(e)) {
464 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
470 for(InEdgeIt e=G.firstIn(w) ; e; G.next(e)) {
472 if (!M.get(v) && flow.get(e) > 0 ) {
483 Returns the maximum min cut, by a reverse bfs
484 from t in the residual graph.
487 template<typename _CutMap>
488 void maxMinCut(_CutMap& M) {
490 std::queue<NodeIt> queue;
495 while (!queue.empty()) {
496 NodeIt w=queue.front();
499 for(InEdgeIt e=G.firstIn(w) ; e; G.next(e)) {
501 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
507 for(OutEdgeIt e=G.firstOut(w) ; e; G.next(e)) {
509 if (!M.get(v) && flow.get(e) > 0 ) {
516 for(EachNodeIt v=G.firstNode() ; v; G.next(v)) {
524 template<typename CutMap>
525 void minCut(CutMap& M) {