jacint@109
|
1 |
// -*- C++ -*-
|
jacint@109
|
2 |
/*
|
jacint@109
|
3 |
preflow.h
|
jacint@109
|
4 |
by jacint.
|
jacint@109
|
5 |
Heuristics:
|
jacint@109
|
6 |
2 phase
|
jacint@109
|
7 |
gap
|
jacint@109
|
8 |
list 'level_list' on the nodes on level i implemented by hand
|
jacint@109
|
9 |
stack 'active' on the active nodes on level i implemented by hand
|
jacint@109
|
10 |
runs heuristic 'highest label' for H1*n relabels
|
jacint@113
|
11 |
runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
|
jacint@109
|
12 |
|
jacint@109
|
13 |
Parameters H0 and H1 are initialized to 20 and 10.
|
jacint@109
|
14 |
|
jacint@109
|
15 |
The best preflow I could ever write.
|
jacint@109
|
16 |
|
jacint@109
|
17 |
The constructor runs the algorithm.
|
jacint@109
|
18 |
|
jacint@109
|
19 |
Members:
|
jacint@109
|
20 |
|
jacint@109
|
21 |
T maxFlow() : returns the value of a maximum flow
|
jacint@109
|
22 |
|
jacint@109
|
23 |
T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e)
|
jacint@109
|
24 |
|
jacint@109
|
25 |
FlowMap Flow() : returns the fixed maximum flow x
|
jacint@109
|
26 |
|
jacint@109
|
27 |
void minMinCut(CutMap& M) : sets M to the characteristic vector of the
|
jacint@109
|
28 |
minimum min cut. M should be a map of bools initialized to false.
|
jacint@109
|
29 |
|
jacint@109
|
30 |
void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
|
jacint@109
|
31 |
maximum min cut. M should be a map of bools initialized to false.
|
jacint@109
|
32 |
|
jacint@109
|
33 |
void minCut(CutMap& M) : sets M to the characteristic vector of
|
jacint@109
|
34 |
a min cut. M should be a map of bools initialized to false.
|
jacint@109
|
35 |
|
jacint@109
|
36 |
*/
|
jacint@109
|
37 |
|
jacint@109
|
38 |
#ifndef PREFLOW_H
|
jacint@109
|
39 |
#define PREFLOW_H
|
jacint@109
|
40 |
|
jacint@109
|
41 |
#define H0 20
|
jacint@109
|
42 |
#define H1 1
|
jacint@109
|
43 |
|
jacint@109
|
44 |
#include <vector>
|
jacint@109
|
45 |
#include <queue>
|
jacint@109
|
46 |
|
jacint@113
|
47 |
#include <time_measure.h>
|
jacint@113
|
48 |
|
jacint@109
|
49 |
namespace hugo {
|
jacint@109
|
50 |
|
jacint@109
|
51 |
template <typename Graph, typename T,
|
jacint@109
|
52 |
typename FlowMap=typename Graph::EdgeMap<T>,
|
jacint@109
|
53 |
typename CapMap=typename Graph::EdgeMap<T> >
|
jacint@109
|
54 |
class preflow {
|
jacint@109
|
55 |
|
jacint@109
|
56 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@109
|
57 |
typedef typename Graph::EdgeIt EdgeIt;
|
jacint@109
|
58 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
jacint@109
|
59 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
jacint@109
|
60 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
jacint@109
|
61 |
|
jacint@109
|
62 |
Graph& G;
|
jacint@109
|
63 |
NodeIt s;
|
jacint@109
|
64 |
NodeIt t;
|
jacint@109
|
65 |
FlowMap flow;
|
jacint@109
|
66 |
CapMap& capacity;
|
jacint@109
|
67 |
T value;
|
jacint@109
|
68 |
|
jacint@109
|
69 |
public:
|
jacint@113
|
70 |
double time;
|
jacint@109
|
71 |
preflow(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
|
jacint@109
|
72 |
G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity)
|
jacint@109
|
73 |
{
|
jacint@109
|
74 |
|
jacint@109
|
75 |
bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
|
jacint@109
|
76 |
int n=G.nodeNum();
|
jacint@109
|
77 |
int heur0=(int)(H0*n); //time while running 'bound decrease'
|
jacint@109
|
78 |
int heur1=(int)(H1*n); //time while running 'highest label'
|
jacint@109
|
79 |
int heur=heur1; //starting time interval (#of relabels)
|
jacint@109
|
80 |
bool what_heur=1;
|
jacint@109
|
81 |
/*
|
jacint@109
|
82 |
what_heur is 0 in case 'bound decrease'
|
jacint@109
|
83 |
and 1 in case 'highest label'
|
jacint@109
|
84 |
*/
|
jacint@109
|
85 |
bool end=false;
|
jacint@109
|
86 |
/*
|
jacint@109
|
87 |
Needed for 'bound decrease', 'true'
|
jacint@109
|
88 |
means no active nodes are above bound b.
|
jacint@109
|
89 |
*/
|
jacint@109
|
90 |
int relabel=0;
|
jacint@109
|
91 |
int k=n-2; //bound on the highest level under n containing a node
|
jacint@109
|
92 |
int b=k; //bound on the highest level under n of an active node
|
jacint@109
|
93 |
|
jacint@109
|
94 |
typename Graph::NodeMap<int> level(G,n);
|
jacint@109
|
95 |
typename Graph::NodeMap<T> excess(G);
|
jacint@109
|
96 |
|
jacint@109
|
97 |
std::vector<NodeIt> active(n);
|
jacint@109
|
98 |
typename Graph::NodeMap<NodeIt> next(G);
|
jacint@109
|
99 |
//Stack of the active nodes in level i < n.
|
jacint@109
|
100 |
//We use it in both phases.
|
jacint@109
|
101 |
|
jacint@109
|
102 |
typename Graph::NodeMap<NodeIt> left(G);
|
jacint@109
|
103 |
typename Graph::NodeMap<NodeIt> right(G);
|
jacint@109
|
104 |
std::vector<NodeIt> level_list(n);
|
jacint@109
|
105 |
/*
|
jacint@109
|
106 |
List of the nodes in level i<n.
|
jacint@109
|
107 |
*/
|
jacint@109
|
108 |
|
jacint@109
|
109 |
/*Reverse_bfs from t, to find the starting level.*/
|
jacint@109
|
110 |
level.set(t,0);
|
jacint@109
|
111 |
std::queue<NodeIt> bfs_queue;
|
jacint@109
|
112 |
bfs_queue.push(t);
|
jacint@109
|
113 |
|
jacint@109
|
114 |
while (!bfs_queue.empty()) {
|
jacint@109
|
115 |
|
jacint@109
|
116 |
NodeIt v=bfs_queue.front();
|
jacint@109
|
117 |
bfs_queue.pop();
|
jacint@109
|
118 |
int l=level.get(v)+1;
|
jacint@109
|
119 |
|
jacint@109
|
120 |
for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
|
jacint@109
|
121 |
NodeIt w=G.tail(e);
|
jacint@109
|
122 |
if ( level.get(w) == n && w != s ) {
|
jacint@109
|
123 |
bfs_queue.push(w);
|
jacint@109
|
124 |
NodeIt first=level_list[l];
|
jacint@109
|
125 |
if ( first != 0 ) left.set(first,w);
|
jacint@109
|
126 |
right.set(w,first);
|
jacint@109
|
127 |
level_list[l]=w;
|
jacint@109
|
128 |
level.set(w, l);
|
jacint@109
|
129 |
}
|
jacint@109
|
130 |
}
|
jacint@109
|
131 |
}
|
jacint@109
|
132 |
|
jacint@109
|
133 |
level.set(s,n);
|
jacint@109
|
134 |
|
jacint@109
|
135 |
|
jacint@109
|
136 |
/* Starting flow. It is everywhere 0 at the moment. */
|
jacint@109
|
137 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
|
jacint@109
|
138 |
{
|
jacint@109
|
139 |
T c=capacity.get(e);
|
jacint@109
|
140 |
if ( c == 0 ) continue;
|
jacint@109
|
141 |
NodeIt w=G.head(e);
|
jacint@109
|
142 |
if ( level.get(w) < n ) {
|
jacint@109
|
143 |
if ( excess.get(w) == 0 && w!=t ) {
|
jacint@109
|
144 |
next.set(w,active[level.get(w)]);
|
jacint@109
|
145 |
active[level.get(w)]=w;
|
jacint@109
|
146 |
}
|
jacint@109
|
147 |
flow.set(e, c);
|
jacint@109
|
148 |
excess.set(w, excess.get(w)+c);
|
jacint@109
|
149 |
}
|
jacint@109
|
150 |
}
|
jacint@109
|
151 |
|
jacint@109
|
152 |
/*
|
jacint@109
|
153 |
End of preprocessing
|
jacint@109
|
154 |
*/
|
jacint@109
|
155 |
|
jacint@109
|
156 |
|
jacint@109
|
157 |
|
jacint@109
|
158 |
/*
|
jacint@109
|
159 |
Push/relabel on the highest level active nodes.
|
jacint@109
|
160 |
*/
|
jacint@109
|
161 |
while ( true ) {
|
jacint@109
|
162 |
|
jacint@109
|
163 |
if ( b == 0 ) {
|
jacint@109
|
164 |
if ( phase ) break;
|
jacint@109
|
165 |
|
jacint@109
|
166 |
if ( !what_heur && !end && k > 0 ) {
|
jacint@109
|
167 |
b=k;
|
jacint@109
|
168 |
end=true;
|
jacint@109
|
169 |
} else {
|
jacint@109
|
170 |
phase=1;
|
jacint@113
|
171 |
time=currTime();
|
jacint@109
|
172 |
level.set(s,0);
|
jacint@109
|
173 |
std::queue<NodeIt> bfs_queue;
|
jacint@109
|
174 |
bfs_queue.push(s);
|
jacint@109
|
175 |
|
jacint@109
|
176 |
while (!bfs_queue.empty()) {
|
jacint@109
|
177 |
|
jacint@109
|
178 |
NodeIt v=bfs_queue.front();
|
jacint@109
|
179 |
bfs_queue.pop();
|
jacint@109
|
180 |
int l=level.get(v)+1;
|
jacint@109
|
181 |
|
jacint@109
|
182 |
for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
|
jacint@109
|
183 |
if ( capacity.get(e) == flow.get(e) ) continue;
|
jacint@109
|
184 |
NodeIt u=G.tail(e);
|
jacint@109
|
185 |
if ( level.get(u) >= n ) {
|
jacint@109
|
186 |
bfs_queue.push(u);
|
jacint@109
|
187 |
level.set(u, l);
|
jacint@109
|
188 |
if ( excess.get(u) > 0 ) {
|
jacint@109
|
189 |
next.set(u,active[l]);
|
jacint@109
|
190 |
active[l]=u;
|
jacint@109
|
191 |
}
|
jacint@109
|
192 |
}
|
jacint@109
|
193 |
}
|
jacint@109
|
194 |
|
jacint@109
|
195 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
|
jacint@109
|
196 |
if ( 0 == flow.get(e) ) continue;
|
jacint@109
|
197 |
NodeIt u=G.head(e);
|
jacint@109
|
198 |
if ( level.get(u) >= n ) {
|
jacint@109
|
199 |
bfs_queue.push(u);
|
jacint@109
|
200 |
level.set(u, l);
|
jacint@109
|
201 |
if ( excess.get(u) > 0 ) {
|
jacint@109
|
202 |
next.set(u,active[l]);
|
jacint@109
|
203 |
active[l]=u;
|
jacint@109
|
204 |
}
|
jacint@109
|
205 |
}
|
jacint@109
|
206 |
}
|
jacint@109
|
207 |
}
|
jacint@109
|
208 |
b=n-2;
|
jacint@109
|
209 |
}
|
jacint@109
|
210 |
|
jacint@109
|
211 |
}
|
jacint@109
|
212 |
|
jacint@109
|
213 |
|
jacint@109
|
214 |
if ( active[b] == 0 ) --b;
|
jacint@109
|
215 |
else {
|
jacint@109
|
216 |
end=false;
|
jacint@109
|
217 |
|
jacint@109
|
218 |
NodeIt w=active[b];
|
jacint@109
|
219 |
active[b]=next.get(w);
|
jacint@109
|
220 |
int lev=level.get(w);
|
jacint@109
|
221 |
T exc=excess.get(w);
|
jacint@109
|
222 |
int newlevel=n; //bound on the next level of w
|
jacint@109
|
223 |
|
jacint@109
|
224 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
|
jacint@109
|
225 |
|
jacint@109
|
226 |
if ( flow.get(e) == capacity.get(e) ) continue;
|
jacint@109
|
227 |
NodeIt v=G.head(e);
|
jacint@109
|
228 |
//e=wv
|
jacint@109
|
229 |
|
jacint@109
|
230 |
if( lev > level.get(v) ) {
|
jacint@109
|
231 |
/*Push is allowed now*/
|
jacint@109
|
232 |
|
jacint@109
|
233 |
if ( excess.get(v)==0 && v!=t && v!=s ) {
|
jacint@109
|
234 |
int lev_v=level.get(v);
|
jacint@109
|
235 |
next.set(v,active[lev_v]);
|
jacint@109
|
236 |
active[lev_v]=v;
|
jacint@109
|
237 |
}
|
jacint@109
|
238 |
|
jacint@109
|
239 |
T cap=capacity.get(e);
|
jacint@109
|
240 |
T flo=flow.get(e);
|
jacint@109
|
241 |
T remcap=cap-flo;
|
jacint@109
|
242 |
|
jacint@109
|
243 |
if ( remcap >= exc ) {
|
jacint@109
|
244 |
/*A nonsaturating push.*/
|
jacint@109
|
245 |
|
jacint@109
|
246 |
flow.set(e, flo+exc);
|
jacint@109
|
247 |
excess.set(v, excess.get(v)+exc);
|
jacint@109
|
248 |
exc=0;
|
jacint@109
|
249 |
break;
|
jacint@109
|
250 |
|
jacint@109
|
251 |
} else {
|
jacint@109
|
252 |
/*A saturating push.*/
|
jacint@109
|
253 |
|
jacint@109
|
254 |
flow.set(e, cap);
|
jacint@109
|
255 |
excess.set(v, excess.get(v)+remcap);
|
jacint@109
|
256 |
exc-=remcap;
|
jacint@109
|
257 |
}
|
jacint@109
|
258 |
} else if ( newlevel > level.get(v) ){
|
jacint@109
|
259 |
newlevel = level.get(v);
|
jacint@109
|
260 |
}
|
jacint@109
|
261 |
|
jacint@109
|
262 |
} //for out edges wv
|
jacint@109
|
263 |
|
jacint@109
|
264 |
|
jacint@109
|
265 |
if ( exc > 0 ) {
|
jacint@109
|
266 |
for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
|
jacint@109
|
267 |
|
jacint@109
|
268 |
if( flow.get(e) == 0 ) continue;
|
jacint@109
|
269 |
NodeIt v=G.tail(e);
|
jacint@109
|
270 |
//e=vw
|
jacint@109
|
271 |
|
jacint@109
|
272 |
if( lev > level.get(v) ) {
|
jacint@109
|
273 |
/*Push is allowed now*/
|
jacint@109
|
274 |
|
jacint@109
|
275 |
if ( excess.get(v)==0 && v!=t && v!=s ) {
|
jacint@109
|
276 |
int lev_v=level.get(v);
|
jacint@109
|
277 |
next.set(v,active[lev_v]);
|
jacint@109
|
278 |
active[lev_v]=v;
|
jacint@109
|
279 |
}
|
jacint@109
|
280 |
|
jacint@109
|
281 |
T flo=flow.get(e);
|
jacint@109
|
282 |
|
jacint@109
|
283 |
if ( flo >= exc ) {
|
jacint@109
|
284 |
/*A nonsaturating push.*/
|
jacint@109
|
285 |
|
jacint@109
|
286 |
flow.set(e, flo-exc);
|
jacint@109
|
287 |
excess.set(v, excess.get(v)+exc);
|
jacint@109
|
288 |
exc=0;
|
jacint@109
|
289 |
break;
|
jacint@109
|
290 |
} else {
|
jacint@109
|
291 |
/*A saturating push.*/
|
jacint@109
|
292 |
|
jacint@109
|
293 |
excess.set(v, excess.get(v)+flo);
|
jacint@109
|
294 |
exc-=flo;
|
jacint@109
|
295 |
flow.set(e,0);
|
jacint@109
|
296 |
}
|
jacint@109
|
297 |
} else if ( newlevel > level.get(v) ) {
|
jacint@109
|
298 |
newlevel = level.get(v);
|
jacint@109
|
299 |
}
|
jacint@109
|
300 |
} //for in edges vw
|
jacint@109
|
301 |
|
jacint@109
|
302 |
} // if w still has excess after the out edge for cycle
|
jacint@109
|
303 |
|
jacint@109
|
304 |
excess.set(w, exc);
|
jacint@109
|
305 |
|
jacint@109
|
306 |
/*
|
jacint@109
|
307 |
Relabel
|
jacint@109
|
308 |
*/
|
jacint@109
|
309 |
|
jacint@109
|
310 |
|
jacint@109
|
311 |
if ( exc > 0 ) {
|
jacint@109
|
312 |
//now 'lev' is the old level of w
|
jacint@109
|
313 |
|
jacint@109
|
314 |
if ( phase ) {
|
jacint@109
|
315 |
level.set(w,++newlevel);
|
jacint@109
|
316 |
next.set(w,active[newlevel]);
|
jacint@109
|
317 |
active[newlevel]=w;
|
jacint@109
|
318 |
b=newlevel;
|
jacint@109
|
319 |
} else {
|
jacint@109
|
320 |
//unlacing starts
|
jacint@109
|
321 |
NodeIt right_n=right.get(w);
|
jacint@109
|
322 |
NodeIt left_n=left.get(w);
|
jacint@109
|
323 |
|
jacint@109
|
324 |
if ( right_n != 0 ) {
|
jacint@109
|
325 |
if ( left_n != 0 ) {
|
jacint@109
|
326 |
right.set(left_n, right_n);
|
jacint@109
|
327 |
left.set(right_n, left_n);
|
jacint@109
|
328 |
} else {
|
jacint@109
|
329 |
level_list[lev]=right_n;
|
jacint@109
|
330 |
left.set(right_n, 0);
|
jacint@109
|
331 |
}
|
jacint@109
|
332 |
} else {
|
jacint@109
|
333 |
if ( left_n != 0 ) {
|
jacint@109
|
334 |
right.set(left_n, 0);
|
jacint@109
|
335 |
} else {
|
jacint@109
|
336 |
level_list[lev]=0;
|
jacint@109
|
337 |
|
jacint@109
|
338 |
}
|
jacint@109
|
339 |
}
|
jacint@109
|
340 |
//unlacing ends
|
jacint@109
|
341 |
|
jacint@109
|
342 |
//gapping starts
|
jacint@109
|
343 |
if ( level_list[lev]==0 ) {
|
jacint@109
|
344 |
|
jacint@109
|
345 |
for (int i=lev; i!=k ; ) {
|
jacint@109
|
346 |
NodeIt v=level_list[++i];
|
jacint@109
|
347 |
while ( v != 0 ) {
|
jacint@109
|
348 |
level.set(v,n);
|
jacint@109
|
349 |
v=right.get(v);
|
jacint@109
|
350 |
}
|
jacint@109
|
351 |
level_list[i]=0;
|
jacint@109
|
352 |
if ( !what_heur ) active[i]=0;
|
jacint@109
|
353 |
}
|
jacint@109
|
354 |
|
jacint@109
|
355 |
level.set(w,n);
|
jacint@109
|
356 |
b=lev-1;
|
jacint@109
|
357 |
k=b;
|
jacint@109
|
358 |
//gapping ends
|
jacint@109
|
359 |
} else {
|
jacint@109
|
360 |
|
jacint@109
|
361 |
if ( newlevel == n ) level.set(w,n);
|
jacint@109
|
362 |
else {
|
jacint@109
|
363 |
level.set(w,++newlevel);
|
jacint@109
|
364 |
next.set(w,active[newlevel]);
|
jacint@109
|
365 |
active[newlevel]=w;
|
jacint@109
|
366 |
if ( what_heur ) b=newlevel;
|
jacint@109
|
367 |
if ( k < newlevel ) ++k;
|
jacint@109
|
368 |
NodeIt first=level_list[newlevel];
|
jacint@109
|
369 |
if ( first != 0 ) left.set(first,w);
|
jacint@109
|
370 |
right.set(w,first);
|
jacint@109
|
371 |
left.set(w,0);
|
jacint@109
|
372 |
level_list[newlevel]=w;
|
jacint@109
|
373 |
}
|
jacint@109
|
374 |
}
|
jacint@109
|
375 |
|
jacint@109
|
376 |
|
jacint@109
|
377 |
++relabel;
|
jacint@109
|
378 |
if ( relabel >= heur ) {
|
jacint@109
|
379 |
relabel=0;
|
jacint@109
|
380 |
if ( what_heur ) {
|
jacint@109
|
381 |
what_heur=0;
|
jacint@109
|
382 |
heur=heur0;
|
jacint@109
|
383 |
end=false;
|
jacint@109
|
384 |
} else {
|
jacint@109
|
385 |
what_heur=1;
|
jacint@109
|
386 |
heur=heur1;
|
jacint@109
|
387 |
b=k;
|
jacint@109
|
388 |
}
|
jacint@109
|
389 |
}
|
jacint@109
|
390 |
} //phase 0
|
jacint@109
|
391 |
|
jacint@109
|
392 |
|
jacint@109
|
393 |
} // if ( exc > 0 )
|
jacint@109
|
394 |
|
jacint@109
|
395 |
|
jacint@109
|
396 |
} // if stack[b] is nonempty
|
jacint@109
|
397 |
|
jacint@109
|
398 |
} // while(true)
|
jacint@109
|
399 |
|
jacint@109
|
400 |
|
jacint@109
|
401 |
value = excess.get(t);
|
jacint@109
|
402 |
/*Max flow value.*/
|
jacint@109
|
403 |
|
jacint@109
|
404 |
} //void run()
|
jacint@109
|
405 |
|
jacint@109
|
406 |
|
jacint@109
|
407 |
|
jacint@109
|
408 |
|
jacint@109
|
409 |
|
jacint@109
|
410 |
/*
|
jacint@109
|
411 |
Returns the maximum value of a flow.
|
jacint@109
|
412 |
*/
|
jacint@109
|
413 |
|
jacint@109
|
414 |
T maxFlow() {
|
jacint@109
|
415 |
return value;
|
jacint@109
|
416 |
}
|
jacint@109
|
417 |
|
jacint@109
|
418 |
|
jacint@109
|
419 |
|
jacint@109
|
420 |
/*
|
jacint@109
|
421 |
For the maximum flow x found by the algorithm,
|
jacint@109
|
422 |
it returns the flow value on edge e, i.e. x(e).
|
jacint@109
|
423 |
*/
|
jacint@109
|
424 |
|
jacint@109
|
425 |
T flowOnEdge(EdgeIt e) {
|
jacint@109
|
426 |
return flow.get(e);
|
jacint@109
|
427 |
}
|
jacint@109
|
428 |
|
jacint@109
|
429 |
|
jacint@109
|
430 |
|
jacint@109
|
431 |
FlowMap Flow() {
|
jacint@109
|
432 |
return flow;
|
jacint@109
|
433 |
}
|
jacint@109
|
434 |
|
jacint@109
|
435 |
|
jacint@109
|
436 |
|
jacint@109
|
437 |
void Flow(FlowMap& _flow ) {
|
jacint@109
|
438 |
for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
|
jacint@109
|
439 |
_flow.set(v,flow.get(v));
|
jacint@109
|
440 |
}
|
jacint@109
|
441 |
|
jacint@109
|
442 |
|
jacint@109
|
443 |
|
jacint@109
|
444 |
/*
|
jacint@109
|
445 |
Returns the minimum min cut, by a bfs from s in the residual graph.
|
jacint@109
|
446 |
*/
|
jacint@109
|
447 |
|
jacint@109
|
448 |
template<typename _CutMap>
|
jacint@109
|
449 |
void minMinCut(_CutMap& M) {
|
jacint@109
|
450 |
|
jacint@109
|
451 |
std::queue<NodeIt> queue;
|
jacint@109
|
452 |
|
jacint@109
|
453 |
M.set(s,true);
|
jacint@109
|
454 |
queue.push(s);
|
jacint@109
|
455 |
|
jacint@109
|
456 |
while (!queue.empty()) {
|
jacint@109
|
457 |
NodeIt w=queue.front();
|
jacint@109
|
458 |
queue.pop();
|
jacint@109
|
459 |
|
jacint@109
|
460 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
|
jacint@109
|
461 |
NodeIt v=G.head(e);
|
jacint@109
|
462 |
if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
|
jacint@109
|
463 |
queue.push(v);
|
jacint@109
|
464 |
M.set(v, true);
|
jacint@109
|
465 |
}
|
jacint@109
|
466 |
}
|
jacint@109
|
467 |
|
jacint@109
|
468 |
for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
|
jacint@109
|
469 |
NodeIt v=G.tail(e);
|
jacint@109
|
470 |
if (!M.get(v) && flow.get(e) > 0 ) {
|
jacint@109
|
471 |
queue.push(v);
|
jacint@109
|
472 |
M.set(v, true);
|
jacint@109
|
473 |
}
|
jacint@109
|
474 |
}
|
jacint@109
|
475 |
}
|
jacint@109
|
476 |
}
|
jacint@109
|
477 |
|
jacint@109
|
478 |
|
jacint@109
|
479 |
|
jacint@109
|
480 |
/*
|
jacint@109
|
481 |
Returns the maximum min cut, by a reverse bfs
|
jacint@109
|
482 |
from t in the residual graph.
|
jacint@109
|
483 |
*/
|
jacint@109
|
484 |
|
jacint@109
|
485 |
template<typename _CutMap>
|
jacint@109
|
486 |
void maxMinCut(_CutMap& M) {
|
jacint@109
|
487 |
|
jacint@109
|
488 |
std::queue<NodeIt> queue;
|
jacint@109
|
489 |
|
jacint@109
|
490 |
M.set(t,true);
|
jacint@109
|
491 |
queue.push(t);
|
jacint@109
|
492 |
|
jacint@109
|
493 |
while (!queue.empty()) {
|
jacint@109
|
494 |
NodeIt w=queue.front();
|
jacint@109
|
495 |
queue.pop();
|
jacint@109
|
496 |
|
jacint@109
|
497 |
for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
|
jacint@109
|
498 |
NodeIt v=G.tail(e);
|
jacint@109
|
499 |
if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
|
jacint@109
|
500 |
queue.push(v);
|
jacint@109
|
501 |
M.set(v, true);
|
jacint@109
|
502 |
}
|
jacint@109
|
503 |
}
|
jacint@109
|
504 |
|
jacint@109
|
505 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
|
jacint@109
|
506 |
NodeIt v=G.head(e);
|
jacint@109
|
507 |
if (!M.get(v) && flow.get(e) > 0 ) {
|
jacint@109
|
508 |
queue.push(v);
|
jacint@109
|
509 |
M.set(v, true);
|
jacint@109
|
510 |
}
|
jacint@109
|
511 |
}
|
jacint@109
|
512 |
}
|
jacint@109
|
513 |
|
jacint@109
|
514 |
for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
|
jacint@109
|
515 |
M.set(v, !M.get(v));
|
jacint@109
|
516 |
}
|
jacint@109
|
517 |
|
jacint@109
|
518 |
}
|
jacint@109
|
519 |
|
jacint@109
|
520 |
|
jacint@109
|
521 |
|
jacint@109
|
522 |
template<typename CutMap>
|
jacint@109
|
523 |
void minCut(CutMap& M) {
|
jacint@109
|
524 |
minMinCut(M);
|
jacint@109
|
525 |
}
|
jacint@109
|
526 |
|
jacint@109
|
527 |
|
jacint@109
|
528 |
};
|
jacint@109
|
529 |
|
marci@174
|
530 |
} //namespace hugo
|
jacint@109
|
531 |
|
marci@174
|
532 |
#endif //PREFLOW_H
|
jacint@109
|
533 |
|
jacint@109
|
534 |
|
marci@174
|
535 |
|
marci@174
|
536 |
|