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