jacint@78
|
1 |
/*
|
jacint@78
|
2 |
preflow_push_max_flow_h
|
jacint@78
|
3 |
by jacint.
|
jacint@78
|
4 |
Runs a preflow push algorithm with the modification,
|
jacint@78
|
5 |
that we do not push on Nodes with level at least n.
|
jacint@78
|
6 |
Moreover, if a level gets empty, we.set all Nodes above that
|
jacint@78
|
7 |
level to level n. Hence, in the end, we arrive at a maximum preflow
|
jacint@78
|
8 |
with value of a max flow value. An empty level gives a minimum cut.
|
jacint@78
|
9 |
|
jacint@78
|
10 |
Member functions:
|
jacint@78
|
11 |
|
jacint@78
|
12 |
void run() : runs the algorithm
|
jacint@78
|
13 |
|
jacint@78
|
14 |
The following functions should be used after run() was already run.
|
jacint@78
|
15 |
|
jacint@78
|
16 |
T maxflow() : returns the value of a maximum flow
|
jacint@78
|
17 |
|
jacint@78
|
18 |
NodeMap<Graph, bool> mincut(): returns a
|
jacint@78
|
19 |
characteristic vector of a minimum cut.
|
jacint@78
|
20 |
*/
|
jacint@78
|
21 |
|
jacint@78
|
22 |
#ifndef PREFLOW_PUSH_MAX_FLOW_H
|
jacint@78
|
23 |
#define PREFLOW_PUSH_MAX_FLOW_H
|
jacint@78
|
24 |
|
jacint@78
|
25 |
#include <algorithm>
|
jacint@78
|
26 |
#include <vector>
|
jacint@78
|
27 |
#include <stack>
|
jacint@78
|
28 |
|
jacint@78
|
29 |
#include <list_graph.hh>
|
jacint@78
|
30 |
#include <reverse_bfs.h>
|
jacint@78
|
31 |
|
jacint@78
|
32 |
|
jacint@78
|
33 |
namespace marci {
|
jacint@78
|
34 |
|
jacint@78
|
35 |
template <typename Graph, typename T>
|
jacint@78
|
36 |
class preflow_push_max_flow {
|
jacint@78
|
37 |
|
jacint@78
|
38 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@78
|
39 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
jacint@78
|
40 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
jacint@78
|
41 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
jacint@78
|
42 |
|
jacint@78
|
43 |
Graph& G;
|
jacint@78
|
44 |
NodeIt s;
|
jacint@78
|
45 |
NodeIt t;
|
jacint@78
|
46 |
typename Graph::EdgeMap<T>& capacity;
|
jacint@78
|
47 |
T value;
|
jacint@78
|
48 |
typename Graph::NodeMap<bool> mincutvector;
|
jacint@78
|
49 |
|
jacint@78
|
50 |
|
jacint@78
|
51 |
|
jacint@78
|
52 |
public:
|
jacint@78
|
53 |
|
jacint@78
|
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) { }
|
jacint@78
|
55 |
|
jacint@78
|
56 |
|
jacint@78
|
57 |
/*
|
jacint@78
|
58 |
The run() function runs a modified version of the highest label preflow-push, which only
|
jacint@78
|
59 |
finds a maximum preflow, hence giving the value of a maximum flow.
|
jacint@78
|
60 |
*/
|
jacint@78
|
61 |
void run() {
|
jacint@78
|
62 |
|
jacint@78
|
63 |
typename Graph::EdgeMap<T> flow(G, 0); //the flow value, 0 everywhere
|
jacint@78
|
64 |
typename Graph::NodeMap<int> level(G); //level of Node
|
jacint@78
|
65 |
typename Graph::NodeMap<T> excess(G); //excess of Node
|
jacint@78
|
66 |
|
jacint@78
|
67 |
int n=G.nodeNum(); //number of Nodes
|
jacint@78
|
68 |
int b=n-2;
|
jacint@78
|
69 |
/*b is a bound on the highest level of an active Node. In the beginning it is at most n-2.*/
|
jacint@78
|
70 |
|
jacint@78
|
71 |
std::vector<int> numb(n); //The number of Nodes on level i < n.
|
jacint@78
|
72 |
|
jacint@78
|
73 |
std::vector<std::stack<NodeIt> > stack(2*n-1); //Stack of the active Nodes in level i.
|
jacint@78
|
74 |
|
jacint@78
|
75 |
|
jacint@78
|
76 |
|
jacint@78
|
77 |
/*Reverse_bfs from t, to find the starting level.*/
|
jacint@78
|
78 |
|
jacint@78
|
79 |
reverse_bfs<Graph> bfs(G, t);
|
jacint@78
|
80 |
bfs.run();
|
jacint@78
|
81 |
for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
|
jacint@78
|
82 |
{
|
jacint@78
|
83 |
int dist=bfs.dist(v);
|
jacint@78
|
84 |
level.set(v, dist);
|
jacint@78
|
85 |
++numb[dist];
|
jacint@78
|
86 |
}
|
jacint@78
|
87 |
|
jacint@78
|
88 |
/*The level of s is fixed to n*/
|
jacint@78
|
89 |
level.set(s,n);
|
jacint@78
|
90 |
|
jacint@78
|
91 |
|
jacint@78
|
92 |
/* Starting flow. It is everywhere 0 at the moment. */
|
jacint@78
|
93 |
|
jacint@78
|
94 |
for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i)
|
jacint@78
|
95 |
{
|
jacint@78
|
96 |
NodeIt w=G.head(i);
|
jacint@78
|
97 |
flow.set(i, capacity.get(i));
|
jacint@78
|
98 |
stack[bfs.dist(w)].push(w);
|
jacint@78
|
99 |
excess.set(w, capacity.get(i));
|
jacint@78
|
100 |
}
|
jacint@78
|
101 |
|
jacint@78
|
102 |
|
jacint@78
|
103 |
/*
|
jacint@78
|
104 |
End of preprocessing
|
jacint@78
|
105 |
*/
|
jacint@78
|
106 |
|
jacint@78
|
107 |
|
jacint@78
|
108 |
|
jacint@78
|
109 |
|
jacint@78
|
110 |
/*
|
jacint@78
|
111 |
Push/relabel on the highest level active Nodes.
|
jacint@78
|
112 |
*/
|
jacint@78
|
113 |
|
jacint@78
|
114 |
/*While there exists an active Node.*/
|
jacint@78
|
115 |
while (b) {
|
jacint@78
|
116 |
|
jacint@78
|
117 |
/*We decrease the bound if there is no active Node of level b.*/
|
jacint@78
|
118 |
if (stack[b].empty()) {
|
jacint@78
|
119 |
--b;
|
jacint@78
|
120 |
} else {
|
jacint@78
|
121 |
|
jacint@78
|
122 |
NodeIt w=stack[b].top(); //w is the highest label active Node.
|
jacint@78
|
123 |
stack[b].pop(); //We delete w from the stack.
|
jacint@78
|
124 |
|
jacint@78
|
125 |
int newlevel=2*n-2; //In newlevel we maintain the next level of w.
|
jacint@78
|
126 |
|
jacint@78
|
127 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
|
jacint@78
|
128 |
NodeIt v=G.head(e);
|
jacint@78
|
129 |
/*e is the Edge wv.*/
|
jacint@78
|
130 |
|
jacint@78
|
131 |
if (flow.get(e)<capacity.get(e)) {
|
jacint@78
|
132 |
/*e is an Edge of the residual graph */
|
jacint@78
|
133 |
|
jacint@78
|
134 |
if(level.get(w)==level.get(v)+1) {
|
jacint@78
|
135 |
/*Push is allowed now*/
|
jacint@78
|
136 |
|
jacint@78
|
137 |
if (capacity.get(e)-flow.get(e) > excess.get(w)) {
|
jacint@78
|
138 |
/*A nonsaturating push.*/
|
jacint@78
|
139 |
|
jacint@78
|
140 |
if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
|
jacint@78
|
141 |
/*v becomes active.*/
|
jacint@78
|
142 |
|
jacint@78
|
143 |
flow.set(e, flow.get(e)+excess.get(w));
|
jacint@78
|
144 |
excess.set(v, excess.get(v)+excess.get(w));
|
jacint@78
|
145 |
excess.set(w,0);
|
jacint@78
|
146 |
//std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl;
|
jacint@78
|
147 |
break;
|
jacint@78
|
148 |
} else {
|
jacint@78
|
149 |
/*A saturating push.*/
|
jacint@78
|
150 |
|
jacint@78
|
151 |
if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
|
jacint@78
|
152 |
/*v becomes active.*/
|
jacint@78
|
153 |
|
jacint@78
|
154 |
excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
|
jacint@78
|
155 |
excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
|
jacint@78
|
156 |
flow.set(e, capacity.get(e));
|
jacint@78
|
157 |
//std::cout << w <<" " << v <<" elore elen sat pump " << std::endl;
|
jacint@78
|
158 |
if (excess.get(w)==0) break;
|
jacint@78
|
159 |
/*If w is not active any more, then we go on to the next Node.*/
|
jacint@78
|
160 |
|
jacint@78
|
161 |
} // if (capacity.get(e)-flow.get(e) > excess.get(w))
|
jacint@78
|
162 |
} // if (level.get(w)==level.get(v)+1)
|
jacint@78
|
163 |
|
jacint@78
|
164 |
else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
|
jacint@78
|
165 |
|
jacint@78
|
166 |
} //if (flow.get(e)<capacity.get(e))
|
jacint@78
|
167 |
|
jacint@78
|
168 |
} //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e)
|
jacint@78
|
169 |
|
jacint@78
|
170 |
|
jacint@78
|
171 |
|
jacint@78
|
172 |
for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
|
jacint@78
|
173 |
NodeIt v=G.tail(e);
|
jacint@78
|
174 |
/*e is the Edge vw.*/
|
jacint@78
|
175 |
|
jacint@78
|
176 |
if (excess.get(w)==0) break;
|
jacint@78
|
177 |
/*It may happen, that w became inactive in the first 'for' cycle.*/
|
jacint@78
|
178 |
|
jacint@78
|
179 |
if(flow.get(e)>0) {
|
jacint@78
|
180 |
/*e is an Edge of the residual graph */
|
jacint@78
|
181 |
|
jacint@78
|
182 |
if(level.get(w)==level.get(v)+1) {
|
jacint@78
|
183 |
/*Push is allowed now*/
|
jacint@78
|
184 |
|
jacint@78
|
185 |
if (flow.get(e) > excess.get(w)) {
|
jacint@78
|
186 |
/*A nonsaturating push.*/
|
jacint@78
|
187 |
|
jacint@78
|
188 |
if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
|
jacint@78
|
189 |
/*v becomes active.*/
|
jacint@78
|
190 |
|
jacint@78
|
191 |
flow.set(e, flow.get(e)-excess.get(w));
|
jacint@78
|
192 |
excess.set(v, excess.get(v)+excess.get(w));
|
jacint@78
|
193 |
excess.set(w,0);
|
jacint@78
|
194 |
//std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl;
|
jacint@78
|
195 |
break;
|
jacint@78
|
196 |
} else {
|
jacint@78
|
197 |
/*A saturating push.*/
|
jacint@78
|
198 |
|
jacint@78
|
199 |
if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v);
|
jacint@78
|
200 |
/*v becomes active.*/
|
jacint@78
|
201 |
|
jacint@78
|
202 |
flow.set(e,0);
|
jacint@78
|
203 |
excess.set(v, excess.get(v)+flow.get(e));
|
jacint@78
|
204 |
excess.set(w, excess.get(w)-flow.get(e));
|
jacint@78
|
205 |
//std::cout << v <<" " << w << " vissza elen sat pump " << std::endl;
|
jacint@78
|
206 |
if (excess.get(w)==0) { break;}
|
jacint@78
|
207 |
} //if (flow.get(e) > excess.get(v))
|
jacint@78
|
208 |
} //if(level.get(w)==level.get(v)+1)
|
jacint@78
|
209 |
|
jacint@78
|
210 |
else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);}
|
jacint@78
|
211 |
//std::cout << "Leveldecrease of Node " << w << " to " << newlevel << std::endl;
|
jacint@78
|
212 |
|
jacint@78
|
213 |
} //if (flow.get(e)>0)
|
jacint@78
|
214 |
|
jacint@78
|
215 |
} //for in-Edge
|
jacint@78
|
216 |
|
jacint@78
|
217 |
|
jacint@78
|
218 |
|
jacint@78
|
219 |
|
jacint@78
|
220 |
/*
|
jacint@78
|
221 |
Relabel
|
jacint@78
|
222 |
*/
|
jacint@78
|
223 |
if (excess.get(w)>0) {
|
jacint@78
|
224 |
/*Now newlevel <= n*/
|
jacint@78
|
225 |
|
jacint@78
|
226 |
int l=level.get(w); //l is the old level of w.
|
jacint@78
|
227 |
--numb[l];
|
jacint@78
|
228 |
|
jacint@78
|
229 |
if (newlevel == n) {
|
jacint@78
|
230 |
level.set(w,n);
|
jacint@78
|
231 |
|
jacint@78
|
232 |
} else {
|
jacint@78
|
233 |
|
jacint@78
|
234 |
if (numb[l]) {
|
jacint@78
|
235 |
/*If the level of w remains nonempty.*/
|
jacint@78
|
236 |
|
jacint@78
|
237 |
level.set(w,++newlevel);
|
jacint@78
|
238 |
++numb[newlevel];
|
jacint@78
|
239 |
stack[newlevel].push(w);
|
jacint@78
|
240 |
b=newlevel;
|
jacint@78
|
241 |
} else {
|
jacint@78
|
242 |
/*If the level of w gets empty.*/
|
jacint@78
|
243 |
|
jacint@78
|
244 |
for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
|
jacint@78
|
245 |
if (level.get(v) >= l ) {
|
jacint@78
|
246 |
level.set(v,n);
|
jacint@78
|
247 |
}
|
jacint@78
|
248 |
}
|
jacint@78
|
249 |
|
jacint@78
|
250 |
for (int i=l+1 ; i!=n ; ++i) numb[i]=0;
|
jacint@78
|
251 |
} //if (numb[l])
|
jacint@78
|
252 |
|
jacint@78
|
253 |
} // if (newlevel = n)
|
jacint@78
|
254 |
|
jacint@78
|
255 |
} // if (excess.get(w)>0)
|
jacint@78
|
256 |
|
jacint@78
|
257 |
|
jacint@78
|
258 |
} //else
|
jacint@78
|
259 |
|
jacint@78
|
260 |
} //while(b)
|
jacint@78
|
261 |
|
jacint@78
|
262 |
value=excess.get(t);
|
jacint@78
|
263 |
/*Max flow value.*/
|
jacint@78
|
264 |
|
jacint@78
|
265 |
|
jacint@78
|
266 |
|
jacint@78
|
267 |
/*
|
jacint@78
|
268 |
We find an empty level, e. The Nodes above this level give
|
jacint@78
|
269 |
a minimum cut.
|
jacint@78
|
270 |
*/
|
jacint@78
|
271 |
|
jacint@78
|
272 |
int e=1;
|
jacint@78
|
273 |
|
jacint@78
|
274 |
while(e) {
|
jacint@78
|
275 |
if(numb[e]) ++e;
|
jacint@78
|
276 |
else break;
|
jacint@78
|
277 |
}
|
jacint@78
|
278 |
for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) {
|
jacint@78
|
279 |
if (level.get(v) > e) mincutvector.set(v, true);
|
jacint@78
|
280 |
}
|
jacint@78
|
281 |
|
jacint@78
|
282 |
|
jacint@78
|
283 |
} // void run()
|
jacint@78
|
284 |
|
jacint@78
|
285 |
|
jacint@78
|
286 |
|
jacint@78
|
287 |
/*
|
jacint@78
|
288 |
Returns the maximum value of a flow.
|
jacint@78
|
289 |
*/
|
jacint@78
|
290 |
|
jacint@78
|
291 |
T maxflow() {
|
jacint@78
|
292 |
return value;
|
jacint@78
|
293 |
}
|
jacint@78
|
294 |
|
jacint@78
|
295 |
|
jacint@78
|
296 |
|
jacint@78
|
297 |
/*
|
jacint@78
|
298 |
Returns a minimum cut.
|
jacint@78
|
299 |
*/
|
jacint@78
|
300 |
|
jacint@78
|
301 |
typename Graph::NodeMap<bool> mincut() {
|
jacint@78
|
302 |
return mincutvector;
|
jacint@78
|
303 |
}
|
jacint@78
|
304 |
|
jacint@78
|
305 |
|
jacint@78
|
306 |
};
|
jacint@78
|
307 |
}//namespace marci
|
jacint@78
|
308 |
#endif
|
jacint@78
|
309 |
|
jacint@78
|
310 |
|
jacint@78
|
311 |
|
jacint@78
|
312 |
|
jacint@78
|
313 |
|