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 'dound 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 template <typename Graph, typename T,
50 typename FlowMap=typename Graph::EdgeMap<T>,
51 typename CapMap=typename Graph::EdgeMap<T> >
54 typedef typename Graph::NodeIt NodeIt;
55 typedef typename Graph::EdgeIt EdgeIt;
56 typedef typename Graph::EachNodeIt EachNodeIt;
57 typedef typename Graph::OutEdgeIt OutEdgeIt;
58 typedef typename Graph::InEdgeIt InEdgeIt;
69 preflow(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
70 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity)
73 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
75 int heur0=(int)(H0*n); //time while running 'bound decrease'
76 int heur1=(int)(H1*n); //time while running 'highest label'
77 int heur=heur1; //starting time interval (#of relabels)
80 what_heur is 0 in case 'bound decrease'
81 and 1 in case 'highest label'
85 Needed for 'bound decrease', 'true'
86 means no active nodes are above bound b.
89 int k=n-2; //bound on the highest level under n containing a node
90 int b=k; //bound on the highest level under n of an active node
92 typename Graph::NodeMap<int> level(G,n);
93 typename Graph::NodeMap<T> excess(G);
95 std::vector<NodeIt> active(n);
96 typename Graph::NodeMap<NodeIt> next(G);
97 //Stack of the active nodes in level i < n.
98 //We use it in both phases.
100 typename Graph::NodeMap<NodeIt> left(G);
101 typename Graph::NodeMap<NodeIt> right(G);
102 std::vector<NodeIt> level_list(n);
104 List of the nodes in level i<n.
107 /*Reverse_bfs from t, to find the starting level.*/
109 std::queue<NodeIt> bfs_queue;
112 while (!bfs_queue.empty()) {
114 NodeIt v=bfs_queue.front();
116 int l=level.get(v)+1;
118 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
120 if ( level.get(w) == n && w != s ) {
122 NodeIt first=level_list[l];
123 if ( first != 0 ) left.set(first,w);
134 /* Starting flow. It is everywhere 0 at the moment. */
135 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
138 if ( c == 0 ) continue;
140 if ( level.get(w) < n ) {
141 if ( excess.get(w) == 0 && w!=t ) {
142 next.set(w,active[level.get(w)]);
143 active[level.get(w)]=w;
146 excess.set(w, excess.get(w)+c);
157 Push/relabel on the highest level active nodes.
164 if ( !what_heur && !end && k > 0 ) {
171 std::queue<NodeIt> bfs_queue;
174 while (!bfs_queue.empty()) {
176 NodeIt v=bfs_queue.front();
178 int l=level.get(v)+1;
180 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
181 if ( capacity.get(e) == flow.get(e) ) continue;
183 if ( level.get(u) >= n ) {
186 if ( excess.get(u) > 0 ) {
187 next.set(u,active[l]);
193 for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
194 if ( 0 == flow.get(e) ) continue;
196 if ( level.get(u) >= n ) {
199 if ( excess.get(u) > 0 ) {
200 next.set(u,active[l]);
212 if ( active[b] == 0 ) --b;
217 active[b]=next.get(w);
218 int lev=level.get(w);
220 int newlevel=n; //bound on the next level of w
222 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
224 if ( flow.get(e) == capacity.get(e) ) continue;
228 if( lev > level.get(v) ) {
229 /*Push is allowed now*/
231 if ( excess.get(v)==0 && v!=t && v!=s ) {
232 int lev_v=level.get(v);
233 next.set(v,active[lev_v]);
237 T cap=capacity.get(e);
241 if ( remcap >= exc ) {
242 /*A nonsaturating push.*/
244 flow.set(e, flo+exc);
245 excess.set(v, excess.get(v)+exc);
250 /*A saturating push.*/
253 excess.set(v, excess.get(v)+remcap);
256 } else if ( newlevel > level.get(v) ){
257 newlevel = level.get(v);
264 for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
266 if( flow.get(e) == 0 ) continue;
270 if( lev > level.get(v) ) {
271 /*Push is allowed now*/
273 if ( excess.get(v)==0 && v!=t && v!=s ) {
274 int lev_v=level.get(v);
275 next.set(v,active[lev_v]);
282 /*A nonsaturating push.*/
284 flow.set(e, flo-exc);
285 excess.set(v, excess.get(v)+exc);
289 /*A saturating push.*/
291 excess.set(v, excess.get(v)+flo);
295 } else if ( newlevel > level.get(v) ) {
296 newlevel = level.get(v);
300 } // if w still has excess after the out edge for cycle
310 //now 'lev' is the old level of w
313 level.set(w,++newlevel);
314 next.set(w,active[newlevel]);
319 NodeIt right_n=right.get(w);
320 NodeIt left_n=left.get(w);
322 if ( right_n != 0 ) {
324 right.set(left_n, right_n);
325 left.set(right_n, left_n);
327 level_list[lev]=right_n;
328 left.set(right_n, 0);
332 right.set(left_n, 0);
341 if ( level_list[lev]==0 ) {
343 for (int i=lev; i!=k ; ) {
344 NodeIt v=level_list[++i];
350 if ( !what_heur ) active[i]=0;
359 if ( newlevel == n ) level.set(w,n);
361 level.set(w,++newlevel);
362 next.set(w,active[newlevel]);
364 if ( what_heur ) b=newlevel;
365 if ( k < newlevel ) ++k;
366 NodeIt first=level_list[newlevel];
367 if ( first != 0 ) left.set(first,w);
370 level_list[newlevel]=w;
376 if ( relabel >= heur ) {
394 } // if stack[b] is nonempty
399 value = excess.get(t);
409 Returns the maximum value of a flow.
419 For the maximum flow x found by the algorithm,
420 it returns the flow value on edge e, i.e. x(e).
423 T flowOnEdge(EdgeIt e) {
435 void Flow(FlowMap& _flow ) {
436 for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
437 _flow.set(v,flow.get(v));
443 Returns the minimum min cut, by a bfs from s in the residual graph.
446 template<typename _CutMap>
447 void minMinCut(_CutMap& M) {
449 std::queue<NodeIt> queue;
454 while (!queue.empty()) {
455 NodeIt w=queue.front();
458 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
460 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
466 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
468 if (!M.get(v) && flow.get(e) > 0 ) {
479 Returns the maximum min cut, by a reverse bfs
480 from t in the residual graph.
483 template<typename _CutMap>
484 void maxMinCut(_CutMap& M) {
486 std::queue<NodeIt> queue;
491 while (!queue.empty()) {
492 NodeIt w=queue.front();
495 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
497 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
503 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
505 if (!M.get(v) && flow.get(e) > 0 ) {
512 for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
520 template<typename CutMap>
521 void minCut(CutMap& M) {