.
2 preflow_push_max_flow_hh
4 Runs a preflow push algorithm with the modification,
5 that we do not push on nodes with level at least n.
6 Moreover, if a level gets empty, we put all nodes above that
7 level to level n. Hence, in the end, we arrive at a maximum preflow
8 with value of a max flow value. An empty level gives a minimum cut.
12 void run() : runs the algorithm
14 The following functions should be used after run() was already run.
16 T maxflow() : returns the value of a maximum flow
18 node_property_vector<graph_type, bool> mincut(): returns a
19 characteristic vector of a minimum cut.
22 #ifndef PREFLOW_PUSH_MAX_FLOW_HH
23 #define PREFLOW_PUSH_MAX_FLOW_HH
29 #include <marci_list_graph.hh>
30 #include <marci_graph_traits.hh>
31 #include <marci_property_vector.hh>
32 #include <reverse_bfs.hh>
37 template <typename graph_type, typename T>
38 class preflow_push_max_flow {
40 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
41 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
42 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
43 typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
48 edge_property_vector<graph_type, T>& capacity;
50 node_property_vector<graph_type, bool> mincutvector;
56 preflow_push_max_flow(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), capacity(_capacity), mincutvector(_G, false) { }
60 The run() function runs a modified version of the highest label preflow-push, which only
61 finds a maximum preflow, hence giving the value of a maximum flow.
65 edge_property_vector<graph_type, T> flow(G, 0); //the flow value, 0 everywhere
66 node_property_vector<graph_type, int> level(G); //level of node
67 node_property_vector<graph_type, T> excess(G); //excess of node
69 int n=number_of(G.first_node()); //number of nodes
71 /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/
73 std::vector<int> numb(n); //The number of nodes on level i < n.
75 std::vector<std::stack<node_iterator> > stack(2*n-1); //Stack of the active nodes in level i.
79 /*Reverse_bfs from t, to find the starting level.*/
81 reverse_bfs<list_graph> bfs(G, t);
83 for(each_node_iterator v=G.first_node(); v.valid(); ++v)
90 /*The level of s is fixed to n*/
94 /* Starting flow. It is everywhere 0 at the moment. */
96 for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i)
98 node_iterator w=G.head(i);
99 flow.put(i, capacity.get(i));
100 stack[bfs.dist(w)].push(w);
101 excess.put(w, capacity.get(i));
113 Push/relabel on the highest level active nodes.
116 /*While there exists an active node.*/
119 /*We decrease the bound if there is no active node of level b.*/
120 if (stack[b].empty()) {
124 node_iterator w=stack[b].top(); //w is the highest label active node.
125 stack[b].pop(); //We delete w from the stack.
127 int newlevel=2*n-2; //In newlevel we maintain the next level of w.
129 for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) {
130 node_iterator v=G.head(e);
131 /*e is the edge wv.*/
133 if (flow.get(e)<capacity.get(e)) {
134 /*e is an edge of the residual graph */
136 if(level.get(w)==level.get(v)+1) {
137 /*Push is allowed now*/
139 if (capacity.get(e)-flow.get(e) > excess.get(w)) {
140 /*A nonsaturating push.*/
142 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
143 /*v becomes active.*/
145 flow.put(e, flow.get(e)+excess.get(w));
146 excess.put(v, excess.get(v)+excess.get(w));
148 //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
151 /*A saturating push.*/
153 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
154 /*v becomes active.*/
156 excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e));
157 excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e));
158 flow.put(e, capacity.get(e));
159 //std::cout << w <<" " << v <<" elore elen sat pump " << std::endl;
160 if (excess.get(w)==0) break;
161 /*If w is not active any more, then we go on to the next node.*/
163 } // if (capacity.get(e)-flow.get(e) > excess.get(w))
164 } // if (level.get(w)==level.get(v)+1)
166 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
168 } //if (flow.get(e)<capacity.get(e))
170 } //for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e)
174 for(in_edge_iterator e=G.first_in_edge(w); e.valid(); ++e) {
175 node_iterator v=G.tail(e);
176 /*e is the edge vw.*/
178 if (excess.get(w)==0) break;
179 /*It may happen, that w became inactive in the first 'for' cycle.*/
182 /*e is an edge of the residual graph */
184 if(level.get(w)==level.get(v)+1) {
185 /*Push is allowed now*/
187 if (flow.get(e) > excess.get(w)) {
188 /*A nonsaturating push.*/
190 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
191 /*v becomes active.*/
193 flow.put(e, flow.get(e)-excess.get(w));
194 excess.put(v, excess.get(v)+excess.get(w));
196 //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
199 /*A saturating push.*/
201 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
202 /*v becomes active.*/
205 excess.put(v, excess.get(v)+flow.get(e));
206 excess.put(w, excess.get(w)-flow.get(e));
207 //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
208 if (excess.get(w)==0) { break;}
209 } //if (flow.get(e) > excess.get(v))
210 } //if(level.get(w)==level.get(v)+1)
212 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
213 //std::cout << "Leveldecrease of node " << w << " to " << newlevel << std::endl;
215 } //if (flow.get(e)>0)
225 if (excess.get(w)>0) {
226 /*Now newlevel <= n*/
228 int l=level.get(w); //l is the old level of w.
237 /*If the level of w remains nonempty.*/
239 level.put(w,++newlevel);
241 stack[newlevel].push(w);
244 /*If the level of w gets empty.*/
246 for (each_node_iterator v=G.first_node() ; v.valid() ; ++v) {
247 if (level.get(v) >= l ) {
252 for (int i=l+1 ; i!=n ; ++i) numb[i]=0;
255 } // if (newlevel = n)
257 } // if (excess.get(w)>0)
270 We find an empty level, e. The nodes above this level give
280 for (each_node_iterator v=G.first_node(); v.valid(); ++v) {
281 if (level.get(v) > e) mincutvector.put(v, true);
290 Returns the maximum value of a flow.
300 Returns a minimum cut.
303 node_property_vector<graph_type, bool> mincut() {