Itt van.
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
12 The bound decrease heuristic behaves unexpectedly well.
14 The constructor runs the algorithm.
18 T maxFlow() : returns the value of a maximum flow
20 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e)
22 FlowMap Flow() : returns the fixed maximum flow x
24 void minMinCut(CutMap& M) : sets M to the characteristic vector of the
25 minimum min cut. M should be a map of bools initialized to false.
27 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
28 maximum min cut. M should be a map of bools initialized to false.
31 void minCut(CutMap& M) : sets M to the characteristic vector of
32 a min cut. M should be a map of bools initialized to false.
42 #include <time_measure.h> //for test
46 template <typename Graph, typename T,
47 typename FlowMap=typename Graph::EdgeMap<T>,
48 typename CapMap=typename Graph::EdgeMap<T> >
51 typedef typename Graph::NodeIt NodeIt;
52 typedef typename Graph::EdgeIt EdgeIt;
53 typedef typename Graph::EachNodeIt EachNodeIt;
54 typedef typename Graph::OutEdgeIt OutEdgeIt;
55 typedef typename Graph::InEdgeIt InEdgeIt;
67 preflow_hl0(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
68 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) {
70 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
74 'true' means no active nodes are above bound b.
76 int k=n-2; //bound on the highest level under n containing a node
77 int b=k; //bound on the highest level under n of an active node
79 b is a bound on the highest level of the stack.
80 k is a bound on the highest nonempty level i < n.
83 typename Graph::NodeMap<int> level(G,n);
84 typename Graph::NodeMap<T> excess(G);
86 std::vector<NodeIt> active(n);
87 typename Graph::NodeMap<NodeIt> next(G);
88 //Stack of the active nodes in level i < n.
89 //We use it in both phases.
91 typename Graph::NodeMap<NodeIt> left(G);
92 typename Graph::NodeMap<NodeIt> right(G);
93 std::vector<NodeIt> level_list(n);
95 List of the nodes in level i<n.
98 /*Reverse_bfs from t, to find the starting level.*/
100 std::queue<NodeIt> bfs_queue;
103 while (!bfs_queue.empty()) {
105 NodeIt v=bfs_queue.front();
107 int l=level.get(v)+1;
109 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
111 if ( level.get(w) == n && w != s ) {
113 NodeIt first=level_list[l];
114 if ( first != 0 ) left.set(first,w);
125 /* Starting flow. It is everywhere 0 at the moment. */
126 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
129 if ( c == 0 ) continue;
131 if ( level.get(w) < n ) {
132 if ( excess.get(w) == 0 && w!=t ) {
133 next.set(w,active[level.get(w)]);
134 active[level.get(w)]=w;
137 excess.set(w, excess.get(w)+c);
148 Push/relabel on the highest level active nodes.
155 if ( !end && k > 0 ) {
162 std::queue<NodeIt> bfs_queue;
165 while (!bfs_queue.empty()) {
167 NodeIt v=bfs_queue.front();
169 int l=level.get(v)+1;
171 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
172 if ( capacity.get(e) == flow.get(e) ) continue;
174 if ( level.get(u) >= n ) {
177 if ( excess.get(u) > 0 ) {
178 next.set(u,active[l]);
184 for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
185 if ( 0 == flow.get(e) ) continue;
187 if ( level.get(u) >= n ) {
190 if ( excess.get(u) > 0 ) {
191 next.set(u,active[l]);
203 if ( active[b] == 0 ) --b;
208 active[b]=next.get(w);
209 int lev=level.get(w);
211 int newlevel=n; //bound on the next level of w
213 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
215 if ( flow.get(e) == capacity.get(e) ) continue;
219 if( lev > level.get(v) ) {
220 /*Push is allowed now*/
222 if ( excess.get(v)==0 && v!=t && v!=s ) {
223 int lev_v=level.get(v);
224 next.set(v,active[lev_v]);
228 T cap=capacity.get(e);
232 if ( remcap >= exc ) {
233 /*A nonsaturating push.*/
235 flow.set(e, flo+exc);
236 excess.set(v, excess.get(v)+exc);
241 /*A saturating push.*/
244 excess.set(v, excess.get(v)+remcap);
247 } else if ( newlevel > level.get(v) ){
248 newlevel = level.get(v);
255 for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
257 if( flow.get(e) == 0 ) continue;
261 if( lev > level.get(v) ) {
262 /*Push is allowed now*/
264 if ( excess.get(v)==0 && v!=t && v!=s ) {
265 int lev_v=level.get(v);
266 next.set(v,active[lev_v]);
273 /*A nonsaturating push.*/
275 flow.set(e, flo-exc);
276 excess.set(v, excess.get(v)+exc);
280 /*A saturating push.*/
282 excess.set(v, excess.get(v)+flo);
286 } else if ( newlevel > level.get(v) ) {
287 newlevel = level.get(v);
291 } // if w still has excess after the out edge for cycle
301 //now 'lev' is the old level of w
304 level.set(w,++newlevel);
305 next.set(w,active[newlevel]);
310 NodeIt right_n=right.get(w);
311 NodeIt left_n=left.get(w);
313 if ( right_n != 0 ) {
315 right.set(left_n, right_n);
316 left.set(right_n, left_n);
318 level_list[lev]=right_n;
319 left.set(right_n, 0);
323 right.set(left_n, 0);
332 if ( level_list[lev]==0 ) {
334 for (int i=lev; i!=k ; ) {
335 NodeIt v=level_list[++i];
350 if ( newlevel == n ) level.set(w,n);
352 level.set(w,++newlevel);
353 next.set(w,active[newlevel]);
355 if ( k < newlevel ) ++k;
356 NodeIt first=level_list[newlevel];
357 if ( first != 0 ) left.set(first,w);
360 level_list[newlevel]=w;
371 } // if stack[b] is nonempty
376 value = excess.get(t);
386 Returns the maximum value of a flow.
396 For the maximum flow x found by the algorithm,
397 it returns the flow value on edge e, i.e. x(e).
400 T flowOnEdge(EdgeIt e) {
412 void Flow(FlowMap& _flow ) {
413 for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
414 _flow.set(v,flow.get(v));
420 Returns the minimum min cut, by a bfs from s in the residual graph.
423 template<typename _CutMap>
424 void minMinCut(_CutMap& M) {
426 std::queue<NodeIt> queue;
431 while (!queue.empty()) {
432 NodeIt w=queue.front();
435 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
437 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
443 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
445 if (!M.get(v) && flow.get(e) > 0 ) {
456 Returns the maximum min cut, by a reverse bfs
457 from t in the residual graph.
460 template<typename _CutMap>
461 void maxMinCut(_CutMap& M) {
463 std::queue<NodeIt> queue;
468 while (!queue.empty()) {
469 NodeIt w=queue.front();
472 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
474 if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
480 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
482 if (!M.get(v) && flow.get(e) > 0 ) {
489 for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
497 template<typename _CutMap>
498 void minCut(_CutMap& M) {