A new version is coming.
4 Runs the highest label variant of the preflow push algorithm with
5 running time O(n^2\sqrt(m)).
9 void run() : runs the algorithm
11 The following functions should be used after run() was already run.
13 T maxflow() : returns the value of a maximum flow
15 T flowonedge(edge_iterator e) : for a fixed maximum flow x it returns x(e)
17 edge_property_vector<graph_type, T> allflow() : returns the fixed maximum flow x
19 node_property_vector<graph_type, bool> mincut() : returns a
20 characteristic vector of a minimum cut. (An empty level
21 in the algorithm gives a minimum cut.)
24 #ifndef PREFLOW_PUSH_HL_HH
25 #define PREFLOW_PUSH_HL_HH
31 #include <marci_graph_traits.hh>
32 #include <marci_property_vector.hh>
33 #include <reverse_bfs.hh>
37 template <typename graph_type, typename T>
38 class preflow_push_hl {
40 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
41 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
42 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
43 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
44 typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
45 typedef typename graph_traits<graph_type>::each_edge_iterator each_edge_iterator;
51 edge_property_vector<graph_type, T> flow;
52 edge_property_vector<graph_type, T>& capacity;
54 node_property_vector<graph_type, bool> mincutvector;
59 preflow_push_hl(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), mincutvector(_G, true) { }
65 The run() function runs the highest label preflow-push,
66 running time: O(n^2\sqrt(m))
70 node_property_vector<graph_type, int> level(G); //level of node
71 node_property_vector<graph_type, T> excess(G); //excess of node
73 int n=number_of(G.first_node()); //number of nodes
75 /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/
77 std::vector<std::stack<node_iterator> > stack(2*n-1); //Stack of the active nodes in level i.
82 /*Reverse_bfs from t, to find the starting level.*/
84 reverse_bfs<list_graph> bfs(G, t);
86 for(each_node_iterator v=G.first_node(); v.valid(); ++v) {
87 level.put(v, bfs.dist(v));
88 //std::cout << "the level of " << v << " is " << bfs.dist(v);
91 /*The level of s is fixed to n*/
98 /* Starting flow. It is everywhere 0 at the moment. */
100 for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i)
102 node_iterator w=G.head(i);
103 flow.put(i, capacity.get(i));
104 stack[bfs.dist(w)].push(w);
105 excess.put(w, capacity.get(i));
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 node_iterator 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(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) {
133 node_iterator v=G.head(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.*/
148 flow.put(e, flow.get(e)+excess.get(w));
149 excess.put(v, excess.get(v)+excess.get(w));
151 //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
154 /*A saturating push.*/
156 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
157 /*v becomes active.*/
159 excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e));
160 excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e));
161 flow.put(e, capacity.get(e));
162 //std::cout << w<<" " <<v<<" elore elen sat pump " << std::endl;
163 if (excess.get(w)==0) break;
164 /*If w is not active any more, then we go on to the next node.*/
166 } // if (capacity.get(e)-flow.get(e) > excess.get(w))
167 } // if(level.get(w)==level.get(v)+1)
169 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
171 } //if (flow.get(e)<capacity.get(e))
173 } //for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e)
177 for(in_edge_iterator e=G.first_in_edge(w); e.valid(); ++e) {
178 node_iterator v=G.tail(e);
179 /*e is the edge vw.*/
181 if (excess.get(w)==0) break;
182 /*It may happen, that w became inactive in the first for cycle.*/
184 /*e is an edge of the residual graph */
186 if(level.get(w)==level.get(v)+1) {
187 /*Push is allowed now*/
189 if (flow.get(e) > excess.get(w)) {
190 /*A nonsaturating push.*/
192 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
193 /*v becomes active.*/
195 flow.put(e, flow.get(e)-excess.get(w));
196 excess.put(v, excess.get(v)+excess.get(w));
198 //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
201 /*A saturating push.*/
203 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
204 /*v becomes active.*/
206 excess.put(v, excess.get(v)+flow.get(e));
207 excess.put(w, excess.get(w)-flow.get(e));
209 //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
210 if (excess.get(w)==0) { break;}
211 } //if (flow.get(e) > excess.get(v))
212 } //if(level.get(w)==level.get(v)+1)
214 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
217 } //if (flow.get(e)>0)
222 if (excess.get(w)>0) {
223 level.put(w,++newlevel);
224 stack[newlevel].push(w);
226 //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl;
234 value = excess.get(t);
247 Returns the maximum value of a flow.
257 For the maximum flow x found by the algorithm, it returns the flow value on edge e, i.e. x(e).
260 T flowonedge(edge_iterator e) {
267 Returns the maximum flow x found by the algorithm.
270 edge_property_vector<graph_type, T> allflow() {
277 Returns a minimum cut by using a reverse bfs from t in the residual graph.
280 node_property_vector<graph_type, bool> mincut() {
282 std::queue<node_iterator> queue;
284 mincutvector.put(t,false);
287 while (!queue.empty()) {
288 node_iterator w=queue.front();
291 for(in_edge_iterator e=G.first_in_edge(w) ; e.valid(); ++e) {
292 node_iterator v=G.tail(e);
293 if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
295 mincutvector.put(v, false);
299 for(out_edge_iterator e=G.first_out_edge(w) ; e.valid(); ++e) {
300 node_iterator v=G.head(e);
301 if (mincutvector.get(v) && flow.get(e) > 0 ) {
303 mincutvector.put(v, false);