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<Graph, 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 <list_graph.hh>
30 #include <reverse_bfs.h>
35 template <typename Graph, typename T>
36 class preflow_push_max_flow {
38 typedef typename Graph::NodeIt NodeIt;
39 typedef typename Graph::EachNodeIt EachNodeIt;
40 typedef typename Graph::OutEdgeIt OutEdgeIt;
41 typedef typename Graph::InEdgeIt InEdgeIt;
46 typename Graph::EdgeMap<T>& capacity;
48 typename Graph::NodeMap<bool> mincutvector;
54 preflow_push_max_flow(Graph& _G, NodeIt _s, NodeIt _t, typename Graph::EdgeMap<T>& _capacity) : G(_G), s(_s), t(_t), capacity(_capacity), mincutvector(_G, false) { }
58 The run() function runs a modified version of the highest label preflow-push, which only
59 finds a maximum preflow, hence giving the value of a maximum flow.
63 typename Graph::EdgeMap<T> flow(G, 0); //the flow value, 0 everywhere
64 typename Graph::NodeMap<int> level(G); //level of Node
65 typename Graph::NodeMap<T> excess(G); //excess of Node
67 int n=G.nodeNum(); //number of Nodes
69 /*b is a bound on the highest level of an active Node. In the beginning it is at most n-2.*/
71 std::vector<int> numb(n); //The number of Nodes on level i < n.
73 std::vector<std::stack<NodeIt> > stack(2*n-1); //Stack of the active Nodes in level i.
77 /*Reverse_bfs from t, to find the starting level.*/
79 reverse_bfs<Graph> bfs(G, t);
81 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
88 /*The level of s is fixed to n*/
92 /* Starting flow. It is everywhere 0 at the moment. */
94 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i)
97 flow.set(i, capacity.get(i));
98 stack[bfs.dist(w)].push(w);
99 excess.set(w, capacity.get(i));
111 Push/relabel on the highest level active Nodes.
114 /*While there exists an active Node.*/
117 /*We decrease the bound if there is no active Node of level b.*/
118 if (stack[b].empty()) {
122 NodeIt w=stack[b].top(); //w is the highest label active Node.
123 stack[b].pop(); //We delete w from the stack.
125 int newlevel=2*n-2; //In newlevel we maintain the next level of w.
127 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
129 /*e is the Edge wv.*/
131 if (flow.get(e)<capacity.get(e)) {
132 /*e is an Edge of the residual graph */
134 if(level.get(w)==level.get(v)+1) {
135 /*Push is allowed now*/
137 if (capacity.get(e)-flow.get(e) > excess.get(w)) {
138 /*A nonsaturating push.*/
140 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
141 /*v becomes active.*/
143 flow.set(e, flow.get(e)+excess.get(w));
144 excess.set(v, excess.get(v)+excess.get(w));
146 //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
149 /*A saturating push.*/
151 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
152 /*v becomes active.*/
154 excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
155 excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
156 flow.set(e, capacity.get(e));
157 //std::cout << w <<" " << v <<" elore elen sat pump " << std::endl;
158 if (excess.get(w)==0) break;
159 /*If w is not active any more, then we go on to the next Node.*/
161 } // if (capacity.get(e)-flow.get(e) > excess.get(w))
162 } // if (level.get(w)==level.get(v)+1)
164 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
166 } //if (flow.get(e)<capacity.get(e))
168 } //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e)
172 for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
174 /*e is the Edge vw.*/
176 if (excess.get(w)==0) break;
177 /*It may happen, that w became inactive in the first 'for' cycle.*/
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 (flow.get(e) > excess.get(w)) {
186 /*A nonsaturating push.*/
188 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
189 /*v becomes active.*/
191 flow.set(e, flow.get(e)-excess.get(w));
192 excess.set(v, excess.get(v)+excess.get(w));
194 //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
197 /*A saturating push.*/
199 if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
200 /*v becomes active.*/
203 excess.set(v, excess.get(v)+flow.get(e));
204 excess.set(w, excess.get(w)-flow.get(e));
205 //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
206 if (excess.get(w)==0) { break;}
207 } //if (flow.get(e) > excess.get(v))
208 } //if(level.get(w)==level.get(v)+1)
210 else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
211 //std::cout << "Leveldecrease of Node " << w << " to " << newlevel << std::endl;
213 } //if (flow.get(e)>0)
223 if (excess.get(w)>0) {
224 /*Now newlevel <= n*/
226 int l=level.get(w); //l is the old level of w.
235 /*If the level of w remains nonempty.*/
237 level.set(w,++newlevel);
239 stack[newlevel].push(w);
242 /*If the level of w gets empty.*/
244 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
245 if (level.get(v) >= l ) {
250 for (int i=l+1 ; i!=n ; ++i) numb[i]=0;
253 } // if (newlevel = n)
255 } // if (excess.get(w)>0)
268 We find an empty level, e. The Nodes above this level give
278 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) {
279 if (level.get(v) > e) mincutvector.set(v, true);
288 Returns the maximum value of a flow.
298 Returns a minimum cut.
301 typename Graph::NodeMap<bool> mincut() {