.
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
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))
68 std::cout<<"A is "<<A<<" ";
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<int> numb(n); //The number of nodes on level i < n.
81 std::vector<std::stack<NodeIt> > stack(2*n-1);
82 //Stack of the active nodes in level i.
85 /*Reverse_bfs from t, to find the starting level.*/
86 reverse_bfs<Graph> bfs(G, t);
88 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
98 /* Starting flow. It is everywhere 0 at the moment. */
99 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
101 if ( capacity.get(e) > 0 ) {
104 if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w);
105 flow.set(e, capacity.get(e));
106 excess.set(w, excess.get(w)+capacity.get(e));
118 Push/relabel on the highest level active nodes.
121 /*While there exists an active node.*/
124 /*We decrease the bound if there is no active node of level b.*/
125 if (stack[b].empty()) {
129 NodeIt w=stack[b].top(); //w is a highest label active node.
132 int newlevel=2*n-2; //In newlevel we bound the next level of w.
134 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
136 if ( flow.get(e) < capacity.get(e) ) {
137 /*e is an edge of the residual graph */
139 NodeIt v=G.head(e); /*e is the edge wv.*/
141 if( level.get(w) == level.get(v)+1 ) {
142 /*Push is allowed now*/
144 if ( excess.get(v)==0 && v != s && v !=t ) stack[level.get(v)].push(v);
145 /*v becomes active.*/
147 if ( capacity.get(e)-flow.get(e) > excess.get(w) ) {
148 /*A nonsaturating push.*/
150 flow.set(e, flow.get(e)+excess.get(w));
151 excess.set(v, excess.get(v)+excess.get(w));
156 /*A saturating push.*/
158 excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
159 excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
160 flow.set(e, capacity.get(e));
161 if ( excess.get(w)==0 ) break;
162 /*If w is not active any more, then we go on to the next node.*/
166 newlevel = newlevel < level.get(v) ? newlevel : level.get(v);
169 } //if the out edge wv is in the res graph
174 if ( excess.get(w) > 0 ) {
176 for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
177 NodeIt v=G.tail(e); /*e is the edge vw.*/
179 if( flow.get(e) > 0 ) {
180 /*e is an edge of the residual graph */
182 if( level.get(w)==level.get(v)+1 ) {
183 /*Push is allowed now*/
185 if ( excess.get(v)==0 && v != s && v !=t) stack[level.get(v)].push(v);
186 /*v becomes active.*/
188 if ( flow.get(e) > excess.get(w) ) {
189 /*A nonsaturating push.*/
191 flow.set(e, flow.get(e)-excess.get(w));
192 excess.set(v, excess.get(v)+excess.get(w));
196 /*A saturating push.*/
198 excess.set(v, excess.get(v)+flow.get(e));
199 excess.set(w, excess.get(w)-flow.get(e));
201 if ( excess.get(w)==0 ) break;
204 newlevel = newlevel < level.get(v) ? newlevel : level.get(v);
207 } //if in edge vw is in the res graph
211 } // if w still has excess after the out edge for cycle
218 if ( excess.get(w) > 0 ) {
220 int oldlevel=level.get(w);
221 level.set(w,++newlevel);
223 if ( oldlevel < n ) {
226 if ( !numb[oldlevel] && oldlevel < A*n ) { //If the level of w gets empty.
228 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
229 if (level.get(v) > oldlevel && level.get(v) < n ) level.set(v,n);
231 for (int i=oldlevel+1 ; i!=n ; ++i) numb[i]=0;
232 if ( newlevel < n ) newlevel=n;
234 if ( newlevel < n ) ++numb[newlevel];
237 if ( newlevel < n ) ++numb[newlevel];
240 stack[newlevel].push(w);
245 } // if stack[b] is nonempty
250 value = excess.get(t);
261 Returns the maximum value of a flow.
271 For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e).
274 T flowonedge(EdgeIt e) {
281 Returns the maximum flow x found by the algorithm.
284 typename Graph::EdgeMap<T> allflow() {
291 Returns a minimum cut by using a reverse bfs from t in the residual graph.
294 typename Graph::NodeMap<bool> mincut() {
296 std::queue<NodeIt> queue;
298 mincutvector.set(t,false);
301 while (!queue.empty()) {
302 NodeIt w=queue.front();
305 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
307 if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
309 mincutvector.set(v, false);
313 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
315 if (mincutvector.get(v) && flow.get(e) > 0 ) {
317 mincutvector.set(v, false);