.
2 preflow_push_max_flow_h
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 set 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 NodeMap<bool> mincut(): returns a
19 characteristic vector of a minimum cut.
22 #ifndef PREFLOW_PUSH_MAX_FLOW_H
23 #define PREFLOW_PUSH_MAX_FLOW_H
29 #include <reverse_bfs.h>
34 template <typename Graph, typename T>
35 class preflow_push_max_flow {
37 typedef typename Graph::NodeIt NodeIt;
38 typedef typename Graph::EachNodeIt EachNodeIt;
39 typedef typename Graph::OutEdgeIt OutEdgeIt;
40 typedef typename Graph::InEdgeIt InEdgeIt;
45 typename Graph::EdgeMap<T>& capacity;
47 typename Graph::NodeMap<bool> mincutvector;
53 preflow_push_max_flow ( Graph& _G, NodeIt _s, NodeIt _t,
54 typename Graph::EdgeMap<T>& _capacity) :
55 G(_G), s(_s), t(_t), capacity(_capacity), mincutvector(_G, false) { }
59 The run() function runs a modified version of the
60 highest label preflow-push, which only
61 finds a maximum preflow, hence giving the value of a maximum flow.
65 typename Graph::EdgeMap<T> flow(G, 0);
66 typename Graph::NodeMap<int> level(G);
67 typename Graph::NodeMap<T> excess(G);
72 b is a bound on the highest level of an active Node.
73 In the beginning it is at most n-2.
76 std::vector<int> numb(n); //The number of Nodes on level i < n.
77 std::vector<std::stack<NodeIt> > stack(2*n-1);
78 //Stack of the active Nodes in level i.
80 /*Reverse_bfs from t, to find the starting level.*/
81 reverse_bfs<Graph> bfs(G, t);
83 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
92 /* Starting flow. It is everywhere 0 at the moment. */
93 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
95 if ( capacity.get(e) > 0 ) {
97 flow.set(e, capacity.get(e));
98 stack[level.get(w)].push(w);
99 excess.set(w, excess.get(w)+capacity.get(e));
110 Push/relabel on the highest level active Nodes.
113 /*While there exists an active Node.*/
116 /*We decrease the bound if there is no active node of level b.*/
117 if (stack[b].empty()) {
121 NodeIt w=stack[b].top(); //w is the highest label active Node.
122 stack[b].pop(); //We delete w from the stack.
124 int newlevel=2*n-2; //In newlevel we maintain the next level of w.
126 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
128 /*e is the Edge wv.*/
130 if (flow.get(e)<capacity.get(e)) {
131 /*e is an Edge of the residual graph */
133 if(level.get(w)==level.get(v)+1) {
134 /*Push is allowed now*/
136 if (capacity.get(e)-flow.get(e) > excess.get(w)) {
137 /*A nonsaturating push.*/
139 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
140 /*v becomes active.*/
142 flow.set(e, flow.get(e)+excess.get(w));
143 excess.set(v, excess.get(v)+excess.get(w));
145 //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
148 /*A saturating push.*/
150 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
151 /*v becomes active.*/
153 excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
154 excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
155 flow.set(e, capacity.get(e));
156 //std::cout << w <<" " << v <<" elore elen sat pump " << std::endl;
157 if (excess.get(w)==0) break;
158 /*If w is not active any more, then we go on to the next Node.*/
160 } // if (capacity.get(e)-flow.get(e) > excess.get(w))
161 } // if (level.get(w)==level.get(v)+1)
163 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
165 } //if (flow.get(e)<capacity.get(e))
167 } //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e)
171 for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
173 /*e is the Edge vw.*/
175 if (excess.get(w)==0) break;
176 /*It may happen, that w became inactive in the first 'for' cycle.*/
179 /*e is an Edge of the residual graph */
181 if(level.get(w)==level.get(v)+1) {
182 /*Push is allowed now*/
184 if (flow.get(e) > excess.get(w)) {
185 /*A nonsaturating push.*/
187 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
188 /*v becomes active.*/
190 flow.set(e, flow.get(e)-excess.get(w));
191 excess.set(v, excess.get(v)+excess.get(w));
193 //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
196 /*A saturating push.*/
198 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
199 /*v becomes active.*/
202 excess.set(v, excess.get(v)+flow.get(e));
203 excess.set(w, excess.get(w)-flow.get(e));
204 //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
205 if (excess.get(w)==0) { break;}
206 } //if (flow.get(e) > excess.get(v))
207 } //if(level.get(w)==level.get(v)+1)
209 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
210 //std::cout << "Leveldecrease of Node " << w << " to " << newlevel << std::endl;
212 } //if (flow.get(e)>0)
222 if (excess.get(w)>0) {
223 /*Now newlevel <= n*/
225 int l=level.get(w); //l is the old level of w.
234 /*If the level of w remains nonempty.*/
236 level.set(w,++newlevel);
238 stack[newlevel].push(w);
241 /*If the level of w gets empty.*/
243 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
244 if (level.get(v) >= l ) {
249 for (int i=l+1 ; i!=n ; ++i) numb[i]=0;
252 } // if (newlevel = n)
254 } // if (excess.get(w)>0)
267 We find an empty level, e. The Nodes above this level give
277 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) {
278 if (level.get(v) > e) mincutvector.set(v, true);
287 Returns the maximum value of a flow.
297 Returns a minimum cut.
300 typename Graph::NodeMap<bool> mincut() {