5 Runs the highest label variant of the preflow push algorithm with
6 running time O(n^2\sqrt(m)).
10 void run() : runs the algorithm
12 The following functions should be used after run() was already run.
14 T maxflow() : returns the value of a maximum flow
16 T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e)
18 Graph::EdgeMap<T> allflow() : returns the fixed maximum flow x
20 Graph::NodeMap<bool> mincut() : returns a
21 characteristic vector of a minimum cut. (An empty level
22 in the algorithm gives a minimum cut.)
25 #ifndef PREFLOW_PUSH_HL_H
26 #define PREFLOW_PUSH_HL_H
32 #include <list_graph.hh>
33 #include <reverse_bfs.h>
37 template <typename Graph, typename T>
38 class preflow_push_hl {
40 typedef typename Graph::NodeIt NodeIt;
41 typedef typename Graph::EdgeIt EdgeIt;
42 typedef typename Graph::EachNodeIt EachNodeIt;
43 typedef typename Graph::OutEdgeIt OutEdgeIt;
44 typedef typename Graph::InEdgeIt InEdgeIt;
49 typename Graph::EdgeMap<T> flow;
50 typename Graph::EdgeMap<T> capacity;
52 typename Graph::NodeMap<bool> mincutvector;
56 preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t,
57 typename Graph::EdgeMap<T>& _capacity) :
58 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity),
59 mincutvector(_G, true) { }
63 The run() function runs the highest label preflow-push,
64 running time: O(n^2\sqrt(m))
70 typename Graph::NodeMap<int> level(G);
71 typename Graph::NodeMap<T> excess(G);
76 b is a bound on the highest level of an active node.
77 In the beginning it is at most n-2.
80 std::vector<std::stack<NodeIt> > stack(2*n-1);
81 //Stack of the active nodes in level i.
84 /*Reverse_bfs from t, to find the starting level.*/
85 reverse_bfs<Graph> bfs(G, t);
87 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
89 level.set(v, bfs.dist(v));
92 std::cout << "the level of t is " << bfs.dist(t);//delme
97 /* Starting flow. It is everywhere 0 at the moment. */
98 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
100 if ( capacity.get(e) > 0 ) {
102 flow.set(e, capacity.get(e));
103 stack[level.get(w)].push(w);
104 excess.set(w, excess.get(w)+capacity.get(e));
116 Push/relabel on the highest level active Nodes.
119 /*While there exists active Node.*/
122 /*We decrease the bound if there is no active Node of level b.*/
123 if (stack[b].empty()) {
127 NodeIt w=stack[b].top(); //w is the highest label active Node.
128 stack[b].pop(); //We delete w from the stack.
130 int newlevel=2*n-2; //In newlevel we maintain the next level of w.
132 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
134 /*e is the Edge wv.*/
136 if (flow.get(e)<capacity.get(e)) {
137 /*e is an Edge of the residual graph */
139 if(level.get(w)==level.get(v)+1) {
140 /*Push is allowed now*/
142 if (capacity.get(e)-flow.get(e) > excess.get(w)) {
143 /*A nonsaturating push.*/
145 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
146 /*v becomes active.*/
150 flow.set(e, flow.get(e)+excess.get(w));
151 excess.set(v, excess.get(v)+excess.get(w));
153 //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
156 /*A saturating push.*/
158 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
159 /*v becomes active.*/
161 excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
162 excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
163 flow.set(e, capacity.get(e));
164 //std::cout << w<<" " <<v<<" elore elen sat pump " << std::endl;
165 if (excess.get(w)==0) break;
166 /*If w is not active any more, then we go on to the next Node.*/
170 } // if (capacity.get(e)-flow.get(e) > excess.get(w))
171 } // if(level.get(w)==level.get(v)+1)
173 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
175 } //if (flow.get(e)<capacity.get(e))
177 } //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e)
181 for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
183 /*e is the Edge vw.*/
185 if (excess.get(w)==0) break;
186 /*It may happen, that w became inactive in the first for cycle.*/
188 /*e is an Edge of the residual graph */
190 if(level.get(w)==level.get(v)+1) {
191 /*Push is allowed now*/
193 if (flow.get(e) > excess.get(w)) {
194 /*A nonsaturating push.*/
196 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
197 /*v becomes active.*/
199 flow.set(e, flow.get(e)-excess.get(w));
200 excess.set(v, excess.get(v)+excess.get(w));
202 //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
205 /*A saturating push.*/
207 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
208 /*v becomes active.*/
210 excess.set(v, excess.get(v)+flow.get(e));
211 excess.set(w, excess.get(w)-flow.get(e));
213 //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
214 if (excess.get(w)==0) { break;}
215 } //if (flow.get(e) > excess.get(v))
216 } //if(level.get(w)==level.get(v)+1)
218 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
221 } //if (flow.get(e)>0)
226 if (excess.get(w)>0) {
227 level.set(w,++newlevel);
228 stack[newlevel].push(w);
230 //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl;
238 value = excess.get(t);
251 Returns the maximum value of a flow.
261 For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e).
264 T flowonEdge(EdgeIt e) {
271 Returns the maximum flow x found by the algorithm.
274 typename Graph::EdgeMap<T> allflow() {
281 Returns a minimum cut by using a reverse bfs from t in the residual graph.
284 typename Graph::NodeMap<bool> mincut() {
286 std::queue<NodeIt> queue;
288 mincutvector.set(t,false);
291 while (!queue.empty()) {
292 NodeIt w=queue.front();
295 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
297 if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
299 mincutvector.set(v, false);
303 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
305 if (mincutvector.get(v) && flow.get(e) > 0 ) {
307 mincutvector.set(v, false);