jacint@109
|
1 |
// -*- C++ -*-
|
jacint@372
|
2 |
|
jacint@109
|
3 |
/*
|
jacint@109
|
4 |
Heuristics:
|
jacint@109
|
5 |
2 phase
|
jacint@109
|
6 |
gap
|
jacint@109
|
7 |
list 'level_list' on the nodes on level i implemented by hand
|
jacint@451
|
8 |
stack 'active' on the active nodes on level i
|
jacint@109
|
9 |
runs heuristic 'highest label' for H1*n relabels
|
jacint@113
|
10 |
runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
|
jacint@109
|
11 |
|
jacint@451
|
12 |
Parameters H0 and H1 are initialized to 20 and 1.
|
jacint@109
|
13 |
|
jacint@211
|
14 |
Constructors:
|
jacint@109
|
15 |
|
jacint@372
|
16 |
Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
|
jacint@372
|
17 |
FlowMap is not constant zero, and should be true if it is
|
jacint@109
|
18 |
|
jacint@109
|
19 |
Members:
|
jacint@109
|
20 |
|
jacint@211
|
21 |
void run()
|
jacint@109
|
22 |
|
marci@468
|
23 |
Num flowValue() : returns the value of a maximum flow
|
jacint@109
|
24 |
|
jacint@109
|
25 |
void minMinCut(CutMap& M) : sets M to the characteristic vector of the
|
jacint@451
|
26 |
minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
|
jacint@109
|
27 |
|
jacint@109
|
28 |
void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
|
jacint@109
|
29 |
maximum min cut. M should be a map of bools initialized to false.
|
jacint@109
|
30 |
|
jacint@109
|
31 |
void minCut(CutMap& M) : sets M to the characteristic vector of
|
jacint@109
|
32 |
a min cut. M should be a map of bools initialized to false.
|
jacint@109
|
33 |
|
jacint@109
|
34 |
*/
|
jacint@109
|
35 |
|
jacint@211
|
36 |
#ifndef HUGO_PREFLOW_H
|
jacint@211
|
37 |
#define HUGO_PREFLOW_H
|
jacint@109
|
38 |
|
jacint@109
|
39 |
#define H0 20
|
jacint@109
|
40 |
#define H1 1
|
jacint@109
|
41 |
|
jacint@109
|
42 |
#include <vector>
|
jacint@109
|
43 |
#include <queue>
|
jacint@451
|
44 |
#include <stack>
|
jacint@109
|
45 |
|
marci@471
|
46 |
#include <graph_wrapper.h>
|
marci@472
|
47 |
#include <bfs_iterator.h>
|
marci@472
|
48 |
#include <invalid.h>
|
marci@472
|
49 |
#include <maps.h>
|
marci@472
|
50 |
#include <for_each_macros.h>
|
marci@472
|
51 |
|
marci@471
|
52 |
|
jacint@109
|
53 |
namespace hugo {
|
jacint@109
|
54 |
|
marci@468
|
55 |
template <typename Graph, typename Num,
|
marci@468
|
56 |
typename CapMap=typename Graph::template EdgeMap<Num>,
|
marci@468
|
57 |
typename FlowMap=typename Graph::template EdgeMap<Num> >
|
jacint@211
|
58 |
class Preflow {
|
jacint@109
|
59 |
|
jacint@211
|
60 |
typedef typename Graph::Node Node;
|
jacint@109
|
61 |
typedef typename Graph::NodeIt NodeIt;
|
jacint@109
|
62 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
jacint@109
|
63 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
jacint@451
|
64 |
|
jacint@451
|
65 |
typedef typename std::vector<std::stack<Node> > VecStack;
|
jacint@451
|
66 |
typedef typename Graph::template NodeMap<Node> NNMap;
|
jacint@451
|
67 |
typedef typename std::vector<Node> VecNode;
|
jacint@451
|
68 |
|
marci@466
|
69 |
const Graph* g;
|
jacint@211
|
70 |
Node s;
|
jacint@211
|
71 |
Node t;
|
marci@465
|
72 |
const CapMap* capacity;
|
jacint@451
|
73 |
FlowMap* flow;
|
jacint@451
|
74 |
int n; //the number of nodes of G
|
marci@471
|
75 |
typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
|
marci@471
|
76 |
typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
|
marci@471
|
77 |
typedef typename ResGW::Edge ResGWEdge;
|
marci@471
|
78 |
//typedef typename ResGW::template NodeMap<bool> ReachedMap;
|
marci@471
|
79 |
typedef typename Graph::template NodeMap<int> ReachedMap;
|
marci@471
|
80 |
ReachedMap level;
|
marci@471
|
81 |
//level works as a bool map in augmenting path algorithms
|
marci@471
|
82 |
//and is used by bfs for storing reached information.
|
marci@471
|
83 |
//In preflow, it shows levels of nodes.
|
marci@471
|
84 |
//typename Graph::template NodeMap<int> level;
|
marci@468
|
85 |
typename Graph::template NodeMap<Num> excess;
|
jacint@451
|
86 |
|
jacint@109
|
87 |
public:
|
jacint@451
|
88 |
|
jacint@451
|
89 |
enum flowEnum{
|
jacint@451
|
90 |
ZERO_FLOW=0,
|
jacint@451
|
91 |
GEN_FLOW=1,
|
jacint@451
|
92 |
PREFLOW=2
|
jacint@451
|
93 |
};
|
jacint@451
|
94 |
|
marci@465
|
95 |
Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
|
jacint@451
|
96 |
FlowMap& _flow) :
|
marci@466
|
97 |
g(&_G), s(_s), t(_t), capacity(&_capacity),
|
jacint@451
|
98 |
flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
|
jacint@451
|
99 |
|
jacint@451
|
100 |
void run() {
|
jacint@451
|
101 |
preflow( ZERO_FLOW );
|
jacint@451
|
102 |
}
|
jacint@372
|
103 |
|
jacint@451
|
104 |
void preflow( flowEnum fe ) {
|
jacint@451
|
105 |
preflowPhase0(fe);
|
jacint@451
|
106 |
preflowPhase1();
|
jacint@451
|
107 |
}
|
jacint@451
|
108 |
|
marci@471
|
109 |
void preflowPhase0( flowEnum fe );
|
jacint@451
|
110 |
|
marci@471
|
111 |
void preflowPhase1();
|
jacint@451
|
112 |
|
marci@471
|
113 |
bool augmentOnShortestPath();
|
jacint@109
|
114 |
|
marci@471
|
115 |
template<typename MutableGraph> bool augmentOnBlockingFlow();
|
jacint@451
|
116 |
|
marci@471
|
117 |
bool augmentOnBlockingFlow2();
|
jacint@109
|
118 |
|
marci@472
|
119 |
/// Returns the actual flow value.
|
marci@472
|
120 |
/// More precisely, it returns the negative excess of s, thus
|
marci@472
|
121 |
/// this works also for preflows.
|
marci@472
|
122 |
Num flowValue() {
|
marci@472
|
123 |
Num a=0;
|
marci@472
|
124 |
FOR_EACH_INC_LOC(OutEdgeIt, e, *g, s) a+=(*flow)[e];
|
marci@472
|
125 |
FOR_EACH_INC_LOC(InEdgeIt, e, *g, s) a-=(*flow)[e];
|
marci@472
|
126 |
return a;
|
jacint@451
|
127 |
}
|
jacint@109
|
128 |
|
jacint@451
|
129 |
//should be used only between preflowPhase0 and preflowPhase1
|
jacint@451
|
130 |
template<typename _CutMap>
|
jacint@451
|
131 |
void actMinCut(_CutMap& M) {
|
jacint@211
|
132 |
NodeIt v;
|
marci@466
|
133 |
for(g->first(v); g->valid(v); g->next(v))
|
marci@466
|
134 |
if ( level[v] < n ) {
|
marci@466
|
135 |
M.set(v,false);
|
marci@466
|
136 |
} else {
|
marci@466
|
137 |
M.set(v,true);
|
marci@466
|
138 |
}
|
jacint@211
|
139 |
}
|
jacint@109
|
140 |
|
jacint@109
|
141 |
|
jacint@109
|
142 |
|
jacint@109
|
143 |
/*
|
jacint@109
|
144 |
Returns the minimum min cut, by a bfs from s in the residual graph.
|
jacint@109
|
145 |
*/
|
jacint@109
|
146 |
template<typename _CutMap>
|
jacint@109
|
147 |
void minMinCut(_CutMap& M) {
|
jacint@109
|
148 |
|
jacint@211
|
149 |
std::queue<Node> queue;
|
jacint@109
|
150 |
|
jacint@109
|
151 |
M.set(s,true);
|
jacint@109
|
152 |
queue.push(s);
|
jacint@109
|
153 |
|
jacint@109
|
154 |
while (!queue.empty()) {
|
jacint@211
|
155 |
Node w=queue.front();
|
jacint@109
|
156 |
queue.pop();
|
jacint@109
|
157 |
|
jacint@211
|
158 |
OutEdgeIt e;
|
marci@466
|
159 |
for(g->first(e,w) ; g->valid(e); g->next(e)) {
|
marci@466
|
160 |
Node v=g->head(e);
|
jacint@451
|
161 |
if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
|
jacint@109
|
162 |
queue.push(v);
|
jacint@109
|
163 |
M.set(v, true);
|
jacint@109
|
164 |
}
|
jacint@109
|
165 |
}
|
jacint@109
|
166 |
|
jacint@211
|
167 |
InEdgeIt f;
|
marci@466
|
168 |
for(g->first(f,w) ; g->valid(f); g->next(f)) {
|
marci@466
|
169 |
Node v=g->tail(f);
|
jacint@451
|
170 |
if (!M[v] && (*flow)[f] > 0 ) {
|
jacint@109
|
171 |
queue.push(v);
|
jacint@109
|
172 |
M.set(v, true);
|
jacint@109
|
173 |
}
|
jacint@109
|
174 |
}
|
jacint@109
|
175 |
}
|
jacint@109
|
176 |
}
|
jacint@109
|
177 |
|
jacint@109
|
178 |
|
jacint@109
|
179 |
|
jacint@109
|
180 |
/*
|
jacint@109
|
181 |
Returns the maximum min cut, by a reverse bfs
|
jacint@109
|
182 |
from t in the residual graph.
|
jacint@109
|
183 |
*/
|
jacint@109
|
184 |
|
jacint@109
|
185 |
template<typename _CutMap>
|
jacint@109
|
186 |
void maxMinCut(_CutMap& M) {
|
jacint@451
|
187 |
|
jacint@451
|
188 |
NodeIt v;
|
marci@466
|
189 |
for(g->first(v) ; g->valid(v); g->next(v)) {
|
jacint@451
|
190 |
M.set(v, true);
|
jacint@451
|
191 |
}
|
jacint@451
|
192 |
|
jacint@211
|
193 |
std::queue<Node> queue;
|
jacint@109
|
194 |
|
jacint@451
|
195 |
M.set(t,false);
|
jacint@109
|
196 |
queue.push(t);
|
jacint@109
|
197 |
|
jacint@109
|
198 |
while (!queue.empty()) {
|
jacint@211
|
199 |
Node w=queue.front();
|
jacint@109
|
200 |
queue.pop();
|
jacint@109
|
201 |
|
jacint@211
|
202 |
|
jacint@211
|
203 |
InEdgeIt e;
|
marci@466
|
204 |
for(g->first(e,w) ; g->valid(e); g->next(e)) {
|
marci@466
|
205 |
Node v=g->tail(e);
|
jacint@451
|
206 |
if (M[v] && (*flow)[e] < (*capacity)[e] ) {
|
jacint@109
|
207 |
queue.push(v);
|
jacint@451
|
208 |
M.set(v, false);
|
jacint@109
|
209 |
}
|
jacint@109
|
210 |
}
|
jacint@211
|
211 |
|
jacint@211
|
212 |
OutEdgeIt f;
|
marci@466
|
213 |
for(g->first(f,w) ; g->valid(f); g->next(f)) {
|
marci@466
|
214 |
Node v=g->head(f);
|
jacint@451
|
215 |
if (M[v] && (*flow)[f] > 0 ) {
|
jacint@109
|
216 |
queue.push(v);
|
jacint@451
|
217 |
M.set(v, false);
|
jacint@109
|
218 |
}
|
jacint@109
|
219 |
}
|
jacint@109
|
220 |
}
|
jacint@109
|
221 |
}
|
jacint@109
|
222 |
|
jacint@109
|
223 |
|
jacint@109
|
224 |
template<typename CutMap>
|
jacint@109
|
225 |
void minCut(CutMap& M) {
|
jacint@109
|
226 |
minMinCut(M);
|
jacint@109
|
227 |
}
|
jacint@109
|
228 |
|
marci@468
|
229 |
void resetTarget(Node _t) {t=_t;}
|
marci@468
|
230 |
void resetSource(Node _s) {s=_s;}
|
jacint@372
|
231 |
|
marci@468
|
232 |
void resetCap(const CapMap& _cap) {
|
jacint@451
|
233 |
capacity=&_cap;
|
jacint@451
|
234 |
}
|
jacint@451
|
235 |
|
marci@468
|
236 |
void resetFlow(FlowMap& _flow) {
|
jacint@451
|
237 |
flow=&_flow;
|
jacint@372
|
238 |
}
|
jacint@372
|
239 |
|
jacint@372
|
240 |
|
jacint@451
|
241 |
private:
|
jacint@451
|
242 |
|
marci@469
|
243 |
int push(Node w, VecStack& active) {
|
jacint@451
|
244 |
|
jacint@451
|
245 |
int lev=level[w];
|
marci@468
|
246 |
Num exc=excess[w];
|
jacint@451
|
247 |
int newlevel=n; //bound on the next level of w
|
jacint@451
|
248 |
|
jacint@451
|
249 |
OutEdgeIt e;
|
marci@466
|
250 |
for(g->first(e,w); g->valid(e); g->next(e)) {
|
jacint@451
|
251 |
|
jacint@470
|
252 |
if ( (*flow)[e] >= (*capacity)[e] ) continue;
|
marci@466
|
253 |
Node v=g->head(e);
|
jacint@451
|
254 |
|
jacint@451
|
255 |
if( lev > level[v] ) { //Push is allowed now
|
jacint@451
|
256 |
|
jacint@470
|
257 |
if ( excess[v]<=0 && v!=t && v!=s ) {
|
jacint@451
|
258 |
int lev_v=level[v];
|
jacint@451
|
259 |
active[lev_v].push(v);
|
jacint@451
|
260 |
}
|
jacint@451
|
261 |
|
marci@468
|
262 |
Num cap=(*capacity)[e];
|
marci@468
|
263 |
Num flo=(*flow)[e];
|
marci@468
|
264 |
Num remcap=cap-flo;
|
jacint@451
|
265 |
|
jacint@451
|
266 |
if ( remcap >= exc ) { //A nonsaturating push.
|
jacint@451
|
267 |
|
jacint@451
|
268 |
flow->set(e, flo+exc);
|
jacint@451
|
269 |
excess.set(v, excess[v]+exc);
|
jacint@451
|
270 |
exc=0;
|
jacint@451
|
271 |
break;
|
jacint@451
|
272 |
|
jacint@451
|
273 |
} else { //A saturating push.
|
jacint@451
|
274 |
flow->set(e, cap);
|
jacint@451
|
275 |
excess.set(v, excess[v]+remcap);
|
jacint@451
|
276 |
exc-=remcap;
|
jacint@451
|
277 |
}
|
jacint@451
|
278 |
} else if ( newlevel > level[v] ) newlevel = level[v];
|
jacint@451
|
279 |
} //for out edges wv
|
jacint@451
|
280 |
|
jacint@451
|
281 |
if ( exc > 0 ) {
|
jacint@451
|
282 |
InEdgeIt e;
|
marci@466
|
283 |
for(g->first(e,w); g->valid(e); g->next(e)) {
|
jacint@451
|
284 |
|
jacint@470
|
285 |
if( (*flow)[e] <= 0 ) continue;
|
marci@466
|
286 |
Node v=g->tail(e);
|
jacint@451
|
287 |
|
jacint@451
|
288 |
if( lev > level[v] ) { //Push is allowed now
|
jacint@451
|
289 |
|
jacint@470
|
290 |
if ( excess[v]<=0 && v!=t && v!=s ) {
|
jacint@451
|
291 |
int lev_v=level[v];
|
jacint@451
|
292 |
active[lev_v].push(v);
|
jacint@451
|
293 |
}
|
jacint@451
|
294 |
|
marci@468
|
295 |
Num flo=(*flow)[e];
|
jacint@451
|
296 |
|
jacint@451
|
297 |
if ( flo >= exc ) { //A nonsaturating push.
|
jacint@451
|
298 |
|
jacint@451
|
299 |
flow->set(e, flo-exc);
|
jacint@451
|
300 |
excess.set(v, excess[v]+exc);
|
jacint@451
|
301 |
exc=0;
|
jacint@451
|
302 |
break;
|
jacint@451
|
303 |
} else { //A saturating push.
|
jacint@451
|
304 |
|
jacint@451
|
305 |
excess.set(v, excess[v]+flo);
|
jacint@451
|
306 |
exc-=flo;
|
jacint@451
|
307 |
flow->set(e,0);
|
jacint@451
|
308 |
}
|
jacint@451
|
309 |
} else if ( newlevel > level[v] ) newlevel = level[v];
|
jacint@451
|
310 |
} //for in edges vw
|
jacint@451
|
311 |
|
jacint@451
|
312 |
} // if w still has excess after the out edge for cycle
|
jacint@451
|
313 |
|
jacint@451
|
314 |
excess.set(w, exc);
|
jacint@451
|
315 |
|
jacint@451
|
316 |
return newlevel;
|
jacint@451
|
317 |
}
|
jacint@451
|
318 |
|
jacint@451
|
319 |
|
jacint@451
|
320 |
void preflowPreproc ( flowEnum fe, VecStack& active,
|
jacint@451
|
321 |
VecNode& level_list, NNMap& left, NNMap& right ) {
|
jacint@451
|
322 |
|
jacint@451
|
323 |
std::queue<Node> bfs_queue;
|
jacint@451
|
324 |
|
jacint@451
|
325 |
switch ( fe ) {
|
jacint@451
|
326 |
case ZERO_FLOW:
|
jacint@451
|
327 |
{
|
jacint@451
|
328 |
//Reverse_bfs from t, to find the starting level.
|
jacint@451
|
329 |
level.set(t,0);
|
jacint@451
|
330 |
bfs_queue.push(t);
|
jacint@451
|
331 |
|
jacint@451
|
332 |
while (!bfs_queue.empty()) {
|
jacint@451
|
333 |
|
jacint@451
|
334 |
Node v=bfs_queue.front();
|
jacint@451
|
335 |
bfs_queue.pop();
|
jacint@451
|
336 |
int l=level[v]+1;
|
jacint@451
|
337 |
|
jacint@451
|
338 |
InEdgeIt e;
|
marci@466
|
339 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
marci@466
|
340 |
Node w=g->tail(e);
|
jacint@451
|
341 |
if ( level[w] == n && w != s ) {
|
jacint@451
|
342 |
bfs_queue.push(w);
|
jacint@451
|
343 |
Node first=level_list[l];
|
marci@466
|
344 |
if ( g->valid(first) ) left.set(first,w);
|
jacint@451
|
345 |
right.set(w,first);
|
jacint@451
|
346 |
level_list[l]=w;
|
jacint@451
|
347 |
level.set(w, l);
|
jacint@451
|
348 |
}
|
jacint@451
|
349 |
}
|
jacint@451
|
350 |
}
|
jacint@451
|
351 |
|
jacint@451
|
352 |
//the starting flow
|
jacint@451
|
353 |
OutEdgeIt e;
|
marci@466
|
354 |
for(g->first(e,s); g->valid(e); g->next(e))
|
jacint@451
|
355 |
{
|
marci@468
|
356 |
Num c=(*capacity)[e];
|
jacint@470
|
357 |
if ( c <= 0 ) continue;
|
marci@466
|
358 |
Node w=g->head(e);
|
jacint@451
|
359 |
if ( level[w] < n ) {
|
jacint@470
|
360 |
if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
|
jacint@451
|
361 |
flow->set(e, c);
|
jacint@451
|
362 |
excess.set(w, excess[w]+c);
|
jacint@451
|
363 |
}
|
jacint@451
|
364 |
}
|
jacint@451
|
365 |
break;
|
jacint@451
|
366 |
}
|
jacint@451
|
367 |
|
jacint@451
|
368 |
case GEN_FLOW:
|
jacint@451
|
369 |
case PREFLOW:
|
jacint@451
|
370 |
{
|
jacint@451
|
371 |
//Reverse_bfs from t in the residual graph,
|
jacint@451
|
372 |
//to find the starting level.
|
jacint@451
|
373 |
level.set(t,0);
|
jacint@451
|
374 |
bfs_queue.push(t);
|
jacint@451
|
375 |
|
jacint@451
|
376 |
while (!bfs_queue.empty()) {
|
jacint@451
|
377 |
|
jacint@451
|
378 |
Node v=bfs_queue.front();
|
jacint@451
|
379 |
bfs_queue.pop();
|
jacint@451
|
380 |
int l=level[v]+1;
|
jacint@451
|
381 |
|
jacint@451
|
382 |
InEdgeIt e;
|
marci@466
|
383 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
jacint@470
|
384 |
if ( (*capacity)[e] <= (*flow)[e] ) continue;
|
marci@466
|
385 |
Node w=g->tail(e);
|
jacint@451
|
386 |
if ( level[w] == n && w != s ) {
|
jacint@451
|
387 |
bfs_queue.push(w);
|
jacint@451
|
388 |
Node first=level_list[l];
|
marci@466
|
389 |
if ( g->valid(first) ) left.set(first,w);
|
jacint@451
|
390 |
right.set(w,first);
|
jacint@451
|
391 |
level_list[l]=w;
|
jacint@451
|
392 |
level.set(w, l);
|
jacint@451
|
393 |
}
|
jacint@451
|
394 |
}
|
jacint@451
|
395 |
|
jacint@451
|
396 |
OutEdgeIt f;
|
marci@466
|
397 |
for(g->first(f,v); g->valid(f); g->next(f)) {
|
jacint@470
|
398 |
if ( 0 >= (*flow)[f] ) continue;
|
marci@466
|
399 |
Node w=g->head(f);
|
jacint@451
|
400 |
if ( level[w] == n && w != s ) {
|
jacint@451
|
401 |
bfs_queue.push(w);
|
jacint@451
|
402 |
Node first=level_list[l];
|
marci@466
|
403 |
if ( g->valid(first) ) left.set(first,w);
|
jacint@451
|
404 |
right.set(w,first);
|
jacint@451
|
405 |
level_list[l]=w;
|
jacint@451
|
406 |
level.set(w, l);
|
jacint@451
|
407 |
}
|
jacint@451
|
408 |
}
|
jacint@451
|
409 |
}
|
jacint@451
|
410 |
|
jacint@451
|
411 |
|
jacint@451
|
412 |
//the starting flow
|
jacint@451
|
413 |
OutEdgeIt e;
|
marci@466
|
414 |
for(g->first(e,s); g->valid(e); g->next(e))
|
jacint@451
|
415 |
{
|
marci@468
|
416 |
Num rem=(*capacity)[e]-(*flow)[e];
|
jacint@470
|
417 |
if ( rem <= 0 ) continue;
|
marci@466
|
418 |
Node w=g->head(e);
|
jacint@451
|
419 |
if ( level[w] < n ) {
|
jacint@470
|
420 |
if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
|
jacint@451
|
421 |
flow->set(e, (*capacity)[e]);
|
jacint@451
|
422 |
excess.set(w, excess[w]+rem);
|
jacint@451
|
423 |
}
|
jacint@451
|
424 |
}
|
jacint@451
|
425 |
|
jacint@451
|
426 |
InEdgeIt f;
|
marci@466
|
427 |
for(g->first(f,s); g->valid(f); g->next(f))
|
jacint@451
|
428 |
{
|
jacint@470
|
429 |
if ( (*flow)[f] <= 0 ) continue;
|
marci@466
|
430 |
Node w=g->tail(f);
|
jacint@451
|
431 |
if ( level[w] < n ) {
|
jacint@470
|
432 |
if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
|
jacint@451
|
433 |
excess.set(w, excess[w]+(*flow)[f]);
|
jacint@451
|
434 |
flow->set(f, 0);
|
jacint@451
|
435 |
}
|
jacint@451
|
436 |
}
|
jacint@451
|
437 |
break;
|
jacint@451
|
438 |
} //case PREFLOW
|
jacint@451
|
439 |
}
|
jacint@451
|
440 |
} //preflowPreproc
|
jacint@451
|
441 |
|
jacint@451
|
442 |
|
jacint@451
|
443 |
|
marci@469
|
444 |
void relabel(Node w, int newlevel, VecStack& active,
|
marci@469
|
445 |
VecNode& level_list, NNMap& left,
|
marci@472
|
446 |
NNMap& right, int& b, int& k, bool what_heur )
|
marci@472
|
447 |
{
|
jacint@451
|
448 |
|
marci@468
|
449 |
Num lev=level[w];
|
jacint@451
|
450 |
|
jacint@451
|
451 |
Node right_n=right[w];
|
jacint@451
|
452 |
Node left_n=left[w];
|
jacint@451
|
453 |
|
jacint@451
|
454 |
//unlacing starts
|
marci@466
|
455 |
if ( g->valid(right_n) ) {
|
marci@466
|
456 |
if ( g->valid(left_n) ) {
|
jacint@451
|
457 |
right.set(left_n, right_n);
|
jacint@451
|
458 |
left.set(right_n, left_n);
|
jacint@451
|
459 |
} else {
|
jacint@451
|
460 |
level_list[lev]=right_n;
|
jacint@451
|
461 |
left.set(right_n, INVALID);
|
jacint@451
|
462 |
}
|
jacint@451
|
463 |
} else {
|
marci@466
|
464 |
if ( g->valid(left_n) ) {
|
jacint@451
|
465 |
right.set(left_n, INVALID);
|
jacint@451
|
466 |
} else {
|
jacint@451
|
467 |
level_list[lev]=INVALID;
|
jacint@451
|
468 |
}
|
jacint@451
|
469 |
}
|
jacint@451
|
470 |
//unlacing ends
|
jacint@451
|
471 |
|
marci@466
|
472 |
if ( !g->valid(level_list[lev]) ) {
|
jacint@451
|
473 |
|
jacint@451
|
474 |
//gapping starts
|
jacint@451
|
475 |
for (int i=lev; i!=k ; ) {
|
jacint@451
|
476 |
Node v=level_list[++i];
|
marci@466
|
477 |
while ( g->valid(v) ) {
|
jacint@451
|
478 |
level.set(v,n);
|
jacint@451
|
479 |
v=right[v];
|
jacint@451
|
480 |
}
|
jacint@451
|
481 |
level_list[i]=INVALID;
|
jacint@451
|
482 |
if ( !what_heur ) {
|
jacint@451
|
483 |
while ( !active[i].empty() ) {
|
jacint@451
|
484 |
active[i].pop(); //FIXME: ezt szebben kene
|
jacint@451
|
485 |
}
|
jacint@451
|
486 |
}
|
jacint@451
|
487 |
}
|
jacint@451
|
488 |
|
jacint@451
|
489 |
level.set(w,n);
|
jacint@451
|
490 |
b=lev-1;
|
jacint@451
|
491 |
k=b;
|
jacint@451
|
492 |
//gapping ends
|
jacint@451
|
493 |
|
jacint@451
|
494 |
} else {
|
jacint@451
|
495 |
|
jacint@451
|
496 |
if ( newlevel == n ) level.set(w,n);
|
jacint@451
|
497 |
else {
|
jacint@451
|
498 |
level.set(w,++newlevel);
|
jacint@451
|
499 |
active[newlevel].push(w);
|
jacint@451
|
500 |
if ( what_heur ) b=newlevel;
|
jacint@451
|
501 |
if ( k < newlevel ) ++k; //now k=newlevel
|
jacint@451
|
502 |
Node first=level_list[newlevel];
|
marci@466
|
503 |
if ( g->valid(first) ) left.set(first,w);
|
jacint@451
|
504 |
right.set(w,first);
|
jacint@451
|
505 |
left.set(w,INVALID);
|
jacint@451
|
506 |
level_list[newlevel]=w;
|
jacint@451
|
507 |
}
|
jacint@451
|
508 |
}
|
jacint@451
|
509 |
|
jacint@451
|
510 |
} //relabel
|
marci@472
|
511 |
|
marci@472
|
512 |
|
marci@472
|
513 |
template<typename MapGraphWrapper>
|
marci@472
|
514 |
class DistanceMap {
|
marci@472
|
515 |
protected:
|
marci@472
|
516 |
const MapGraphWrapper* g;
|
marci@472
|
517 |
typename MapGraphWrapper::template NodeMap<int> dist;
|
marci@472
|
518 |
public:
|
marci@472
|
519 |
DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
|
marci@472
|
520 |
void set(const typename MapGraphWrapper::Node& n, int a) {
|
marci@472
|
521 |
dist.set(n, a);
|
marci@472
|
522 |
}
|
marci@472
|
523 |
int operator[](const typename MapGraphWrapper::Node& n)
|
marci@472
|
524 |
{ return dist[n]; }
|
marci@472
|
525 |
// int get(const typename MapGraphWrapper::Node& n) const {
|
marci@472
|
526 |
// return dist[n]; }
|
marci@472
|
527 |
// bool get(const typename MapGraphWrapper::Edge& e) const {
|
marci@472
|
528 |
// return (dist.get(g->tail(e))<dist.get(g->head(e))); }
|
marci@472
|
529 |
bool operator[](const typename MapGraphWrapper::Edge& e) const {
|
marci@472
|
530 |
return (dist[g->tail(e)]<dist[g->head(e)]);
|
marci@472
|
531 |
}
|
marci@472
|
532 |
};
|
jacint@451
|
533 |
|
marci@471
|
534 |
};
|
jacint@109
|
535 |
|
marci@471
|
536 |
|
marci@471
|
537 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@471
|
538 |
void Preflow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe )
|
marci@471
|
539 |
{
|
marci@471
|
540 |
|
marci@471
|
541 |
int heur0=(int)(H0*n); //time while running 'bound decrease'
|
marci@471
|
542 |
int heur1=(int)(H1*n); //time while running 'highest label'
|
marci@471
|
543 |
int heur=heur1; //starting time interval (#of relabels)
|
marci@471
|
544 |
int numrelabel=0;
|
marci@471
|
545 |
|
marci@471
|
546 |
bool what_heur=1;
|
marci@471
|
547 |
//It is 0 in case 'bound decrease' and 1 in case 'highest label'
|
marci@471
|
548 |
|
marci@471
|
549 |
bool end=false;
|
marci@471
|
550 |
//Needed for 'bound decrease', true means no active nodes are above bound b.
|
marci@471
|
551 |
|
marci@471
|
552 |
int k=n-2; //bound on the highest level under n containing a node
|
marci@471
|
553 |
int b=k; //bound on the highest level under n of an active node
|
marci@471
|
554 |
|
marci@471
|
555 |
VecStack active(n);
|
marci@471
|
556 |
|
marci@471
|
557 |
NNMap left(*g, INVALID);
|
marci@471
|
558 |
NNMap right(*g, INVALID);
|
marci@471
|
559 |
VecNode level_list(n,INVALID);
|
marci@471
|
560 |
//List of the nodes in level i<n, set to n.
|
marci@471
|
561 |
|
marci@471
|
562 |
NodeIt v;
|
marci@471
|
563 |
for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
|
marci@471
|
564 |
//setting each node to level n
|
marci@471
|
565 |
|
marci@471
|
566 |
switch ( fe ) {
|
marci@471
|
567 |
case PREFLOW:
|
marci@471
|
568 |
{
|
marci@471
|
569 |
//counting the excess
|
marci@471
|
570 |
NodeIt v;
|
marci@471
|
571 |
for(g->first(v); g->valid(v); g->next(v)) {
|
marci@471
|
572 |
Num exc=0;
|
marci@471
|
573 |
|
marci@471
|
574 |
InEdgeIt e;
|
marci@471
|
575 |
for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
|
marci@471
|
576 |
OutEdgeIt f;
|
marci@471
|
577 |
for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
|
marci@471
|
578 |
|
marci@471
|
579 |
excess.set(v,exc);
|
marci@471
|
580 |
|
marci@471
|
581 |
//putting the active nodes into the stack
|
marci@471
|
582 |
int lev=level[v];
|
marci@471
|
583 |
if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
|
marci@471
|
584 |
}
|
marci@471
|
585 |
break;
|
marci@471
|
586 |
}
|
marci@471
|
587 |
case GEN_FLOW:
|
marci@471
|
588 |
{
|
marci@471
|
589 |
//Counting the excess of t
|
marci@471
|
590 |
Num exc=0;
|
marci@471
|
591 |
|
marci@471
|
592 |
InEdgeIt e;
|
marci@471
|
593 |
for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
|
marci@471
|
594 |
OutEdgeIt f;
|
marci@471
|
595 |
for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
|
marci@471
|
596 |
|
marci@471
|
597 |
excess.set(t,exc);
|
marci@471
|
598 |
|
marci@471
|
599 |
break;
|
marci@471
|
600 |
}
|
marci@471
|
601 |
default:
|
marci@471
|
602 |
break;
|
marci@471
|
603 |
}
|
marci@471
|
604 |
|
marci@471
|
605 |
preflowPreproc( fe, active, level_list, left, right );
|
marci@471
|
606 |
//End of preprocessing
|
marci@471
|
607 |
|
marci@471
|
608 |
|
marci@471
|
609 |
//Push/relabel on the highest level active nodes.
|
marci@471
|
610 |
while ( true ) {
|
marci@471
|
611 |
if ( b == 0 ) {
|
marci@471
|
612 |
if ( !what_heur && !end && k > 0 ) {
|
marci@471
|
613 |
b=k;
|
marci@471
|
614 |
end=true;
|
marci@471
|
615 |
} else break;
|
marci@471
|
616 |
}
|
marci@471
|
617 |
|
marci@471
|
618 |
if ( active[b].empty() ) --b;
|
marci@471
|
619 |
else {
|
marci@471
|
620 |
end=false;
|
marci@471
|
621 |
Node w=active[b].top();
|
marci@471
|
622 |
active[b].pop();
|
marci@471
|
623 |
int newlevel=push(w,active);
|
marci@471
|
624 |
if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
|
marci@471
|
625 |
left, right, b, k, what_heur);
|
marci@471
|
626 |
|
marci@471
|
627 |
++numrelabel;
|
marci@471
|
628 |
if ( numrelabel >= heur ) {
|
marci@471
|
629 |
numrelabel=0;
|
marci@471
|
630 |
if ( what_heur ) {
|
marci@471
|
631 |
what_heur=0;
|
marci@471
|
632 |
heur=heur0;
|
marci@471
|
633 |
end=false;
|
marci@471
|
634 |
} else {
|
marci@471
|
635 |
what_heur=1;
|
marci@471
|
636 |
heur=heur1;
|
marci@471
|
637 |
b=k;
|
marci@471
|
638 |
}
|
marci@471
|
639 |
}
|
marci@471
|
640 |
}
|
marci@471
|
641 |
}
|
marci@471
|
642 |
}
|
marci@471
|
643 |
|
marci@471
|
644 |
|
marci@471
|
645 |
|
marci@471
|
646 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@471
|
647 |
void Preflow<Graph, Num, CapMap, FlowMap>::preflowPhase1()
|
marci@471
|
648 |
{
|
marci@471
|
649 |
|
marci@471
|
650 |
int k=n-2; //bound on the highest level under n containing a node
|
marci@471
|
651 |
int b=k; //bound on the highest level under n of an active node
|
marci@471
|
652 |
|
marci@471
|
653 |
VecStack active(n);
|
marci@471
|
654 |
level.set(s,0);
|
marci@471
|
655 |
std::queue<Node> bfs_queue;
|
marci@471
|
656 |
bfs_queue.push(s);
|
marci@471
|
657 |
|
marci@471
|
658 |
while (!bfs_queue.empty()) {
|
marci@471
|
659 |
|
marci@471
|
660 |
Node v=bfs_queue.front();
|
marci@471
|
661 |
bfs_queue.pop();
|
marci@471
|
662 |
int l=level[v]+1;
|
marci@471
|
663 |
|
marci@471
|
664 |
InEdgeIt e;
|
marci@471
|
665 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
marci@471
|
666 |
if ( (*capacity)[e] <= (*flow)[e] ) continue;
|
marci@471
|
667 |
Node u=g->tail(e);
|
marci@471
|
668 |
if ( level[u] >= n ) {
|
marci@471
|
669 |
bfs_queue.push(u);
|
marci@471
|
670 |
level.set(u, l);
|
marci@471
|
671 |
if ( excess[u] > 0 ) active[l].push(u);
|
marci@471
|
672 |
}
|
marci@471
|
673 |
}
|
marci@471
|
674 |
|
marci@471
|
675 |
OutEdgeIt f;
|
marci@471
|
676 |
for(g->first(f,v); g->valid(f); g->next(f)) {
|
marci@471
|
677 |
if ( 0 >= (*flow)[f] ) continue;
|
marci@471
|
678 |
Node u=g->head(f);
|
marci@471
|
679 |
if ( level[u] >= n ) {
|
marci@471
|
680 |
bfs_queue.push(u);
|
marci@471
|
681 |
level.set(u, l);
|
marci@471
|
682 |
if ( excess[u] > 0 ) active[l].push(u);
|
marci@471
|
683 |
}
|
marci@471
|
684 |
}
|
marci@471
|
685 |
}
|
marci@471
|
686 |
b=n-2;
|
marci@471
|
687 |
|
marci@471
|
688 |
while ( true ) {
|
marci@471
|
689 |
|
marci@471
|
690 |
if ( b == 0 ) break;
|
marci@471
|
691 |
|
marci@471
|
692 |
if ( active[b].empty() ) --b;
|
marci@471
|
693 |
else {
|
marci@471
|
694 |
Node w=active[b].top();
|
marci@471
|
695 |
active[b].pop();
|
marci@471
|
696 |
int newlevel=push(w,active);
|
marci@471
|
697 |
|
marci@471
|
698 |
//relabel
|
marci@471
|
699 |
if ( excess[w] > 0 ) {
|
marci@471
|
700 |
level.set(w,++newlevel);
|
marci@471
|
701 |
active[newlevel].push(w);
|
marci@471
|
702 |
b=newlevel;
|
marci@471
|
703 |
}
|
marci@471
|
704 |
} // if stack[b] is nonempty
|
marci@471
|
705 |
} // while(true)
|
marci@471
|
706 |
}
|
marci@471
|
707 |
|
marci@471
|
708 |
|
marci@471
|
709 |
|
marci@472
|
710 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@472
|
711 |
bool Preflow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
|
marci@472
|
712 |
{
|
marci@472
|
713 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@472
|
714 |
bool _augment=false;
|
marci@472
|
715 |
|
marci@472
|
716 |
//ReachedMap level(res_graph);
|
marci@472
|
717 |
FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
|
marci@472
|
718 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@472
|
719 |
bfs.pushAndSetReached(s);
|
marci@472
|
720 |
|
marci@472
|
721 |
typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
|
marci@472
|
722 |
pred.set(s, INVALID);
|
marci@472
|
723 |
|
marci@472
|
724 |
typename ResGW::template NodeMap<Num> free(res_graph);
|
marci@472
|
725 |
|
marci@472
|
726 |
//searching for augmenting path
|
marci@472
|
727 |
while ( !bfs.finished() ) {
|
marci@472
|
728 |
ResGWOutEdgeIt e=bfs;
|
marci@472
|
729 |
if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
|
marci@472
|
730 |
Node v=res_graph.tail(e);
|
marci@472
|
731 |
Node w=res_graph.head(e);
|
marci@472
|
732 |
pred.set(w, e);
|
marci@472
|
733 |
if (res_graph.valid(pred[v])) {
|
marci@472
|
734 |
free.set(w, std::min(free[v], res_graph.resCap(e)));
|
marci@472
|
735 |
} else {
|
marci@472
|
736 |
free.set(w, res_graph.resCap(e));
|
marci@472
|
737 |
}
|
marci@472
|
738 |
if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@472
|
739 |
}
|
marci@472
|
740 |
|
marci@472
|
741 |
++bfs;
|
marci@472
|
742 |
} //end of searching augmenting path
|
marci@471
|
743 |
|
marci@472
|
744 |
if (_augment) {
|
marci@472
|
745 |
Node n=t;
|
marci@472
|
746 |
Num augment_value=free[t];
|
marci@472
|
747 |
while (res_graph.valid(pred[n])) {
|
marci@472
|
748 |
ResGWEdge e=pred[n];
|
marci@472
|
749 |
res_graph.augment(e, augment_value);
|
marci@472
|
750 |
n=res_graph.tail(e);
|
marci@472
|
751 |
}
|
marci@472
|
752 |
}
|
marci@471
|
753 |
|
marci@472
|
754 |
return _augment;
|
marci@472
|
755 |
}
|
marci@471
|
756 |
|
marci@471
|
757 |
|
marci@471
|
758 |
|
marci@471
|
759 |
|
marci@471
|
760 |
|
marci@471
|
761 |
|
jacint@109
|
762 |
|
marci@472
|
763 |
|
marci@472
|
764 |
|
marci@472
|
765 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@472
|
766 |
template<typename MutableGraph>
|
marci@472
|
767 |
bool Preflow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
|
marci@472
|
768 |
{
|
marci@472
|
769 |
typedef MutableGraph MG;
|
marci@472
|
770 |
bool _augment=false;
|
marci@472
|
771 |
|
marci@472
|
772 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@472
|
773 |
|
marci@472
|
774 |
//bfs for distances on the residual graph
|
marci@472
|
775 |
//ReachedMap level(res_graph);
|
marci@472
|
776 |
FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
|
marci@472
|
777 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@472
|
778 |
bfs.pushAndSetReached(s);
|
marci@472
|
779 |
typename ResGW::template NodeMap<int>
|
marci@472
|
780 |
dist(res_graph); //filled up with 0's
|
marci@472
|
781 |
|
marci@472
|
782 |
//F will contain the physical copy of the residual graph
|
marci@472
|
783 |
//with the set of edges which are on shortest paths
|
marci@472
|
784 |
MG F;
|
marci@472
|
785 |
typename ResGW::template NodeMap<typename MG::Node>
|
marci@472
|
786 |
res_graph_to_F(res_graph);
|
marci@472
|
787 |
{
|
marci@472
|
788 |
typename ResGW::NodeIt n;
|
marci@472
|
789 |
for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
|
marci@472
|
790 |
res_graph_to_F.set(n, F.addNode());
|
marci@472
|
791 |
}
|
marci@472
|
792 |
}
|
marci@472
|
793 |
|
marci@472
|
794 |
typename MG::Node sF=res_graph_to_F[s];
|
marci@472
|
795 |
typename MG::Node tF=res_graph_to_F[t];
|
marci@472
|
796 |
typename MG::template EdgeMap<ResGWEdge> original_edge(F);
|
marci@472
|
797 |
typename MG::template EdgeMap<Num> residual_capacity(F);
|
marci@472
|
798 |
|
marci@472
|
799 |
while ( !bfs.finished() ) {
|
marci@472
|
800 |
ResGWOutEdgeIt e=bfs;
|
marci@472
|
801 |
if (res_graph.valid(e)) {
|
marci@472
|
802 |
if (bfs.isBNodeNewlyReached()) {
|
marci@472
|
803 |
dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
|
marci@472
|
804 |
typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
|
marci@472
|
805 |
original_edge.update();
|
marci@472
|
806 |
original_edge.set(f, e);
|
marci@472
|
807 |
residual_capacity.update();
|
marci@472
|
808 |
residual_capacity.set(f, res_graph.resCap(e));
|
marci@472
|
809 |
} else {
|
marci@472
|
810 |
if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
|
marci@472
|
811 |
typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
|
marci@472
|
812 |
original_edge.update();
|
marci@472
|
813 |
original_edge.set(f, e);
|
marci@472
|
814 |
residual_capacity.update();
|
marci@472
|
815 |
residual_capacity.set(f, res_graph.resCap(e));
|
marci@472
|
816 |
}
|
marci@472
|
817 |
}
|
marci@472
|
818 |
}
|
marci@472
|
819 |
++bfs;
|
marci@472
|
820 |
} //computing distances from s in the residual graph
|
marci@472
|
821 |
|
marci@472
|
822 |
bool __augment=true;
|
marci@472
|
823 |
|
marci@472
|
824 |
while (__augment) {
|
marci@472
|
825 |
__augment=false;
|
marci@472
|
826 |
//computing blocking flow with dfs
|
marci@472
|
827 |
DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
|
marci@472
|
828 |
typename MG::template NodeMap<typename MG::Edge> pred(F);
|
marci@472
|
829 |
pred.set(sF, INVALID);
|
marci@472
|
830 |
//invalid iterators for sources
|
marci@472
|
831 |
|
marci@472
|
832 |
typename MG::template NodeMap<Num> free(F);
|
marci@472
|
833 |
|
marci@472
|
834 |
dfs.pushAndSetReached(sF);
|
marci@472
|
835 |
while (!dfs.finished()) {
|
marci@472
|
836 |
++dfs;
|
marci@472
|
837 |
if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
|
marci@472
|
838 |
if (dfs.isBNodeNewlyReached()) {
|
marci@472
|
839 |
typename MG::Node v=F.aNode(dfs);
|
marci@472
|
840 |
typename MG::Node w=F.bNode(dfs);
|
marci@472
|
841 |
pred.set(w, dfs);
|
marci@472
|
842 |
if (F.valid(pred[v])) {
|
marci@472
|
843 |
free.set(w, std::min(free[v], residual_capacity[dfs]));
|
marci@472
|
844 |
} else {
|
marci@472
|
845 |
free.set(w, residual_capacity[dfs]);
|
marci@472
|
846 |
}
|
marci@472
|
847 |
if (w==tF) {
|
marci@472
|
848 |
__augment=true;
|
marci@472
|
849 |
_augment=true;
|
marci@472
|
850 |
break;
|
marci@472
|
851 |
}
|
marci@472
|
852 |
|
marci@472
|
853 |
} else {
|
marci@472
|
854 |
F.erase(/*typename MG::OutEdgeIt*/(dfs));
|
marci@472
|
855 |
}
|
marci@472
|
856 |
}
|
marci@472
|
857 |
}
|
marci@472
|
858 |
|
marci@472
|
859 |
if (__augment) {
|
marci@472
|
860 |
typename MG::Node n=tF;
|
marci@472
|
861 |
Num augment_value=free[tF];
|
marci@472
|
862 |
while (F.valid(pred[n])) {
|
marci@472
|
863 |
typename MG::Edge e=pred[n];
|
marci@472
|
864 |
res_graph.augment(original_edge[e], augment_value);
|
marci@472
|
865 |
n=F.tail(e);
|
marci@472
|
866 |
if (residual_capacity[e]==augment_value)
|
marci@472
|
867 |
F.erase(e);
|
marci@472
|
868 |
else
|
marci@472
|
869 |
residual_capacity.set(e, residual_capacity[e]-augment_value);
|
marci@472
|
870 |
}
|
marci@472
|
871 |
}
|
marci@472
|
872 |
|
marci@472
|
873 |
}
|
marci@472
|
874 |
|
marci@472
|
875 |
return _augment;
|
marci@472
|
876 |
}
|
marci@472
|
877 |
|
marci@472
|
878 |
|
marci@472
|
879 |
|
marci@472
|
880 |
|
marci@472
|
881 |
|
marci@472
|
882 |
|
marci@472
|
883 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@472
|
884 |
bool Preflow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
|
marci@472
|
885 |
{
|
marci@472
|
886 |
bool _augment=false;
|
marci@472
|
887 |
|
marci@472
|
888 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@472
|
889 |
|
marci@472
|
890 |
//ReachedMap level(res_graph);
|
marci@472
|
891 |
FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
|
marci@472
|
892 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@472
|
893 |
|
marci@472
|
894 |
bfs.pushAndSetReached(s);
|
marci@472
|
895 |
DistanceMap<ResGW> dist(res_graph);
|
marci@472
|
896 |
while ( !bfs.finished() ) {
|
marci@472
|
897 |
ResGWOutEdgeIt e=bfs;
|
marci@472
|
898 |
if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
|
marci@472
|
899 |
dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
|
marci@472
|
900 |
}
|
marci@472
|
901 |
++bfs;
|
marci@472
|
902 |
} //computing distances from s in the residual graph
|
marci@472
|
903 |
|
marci@472
|
904 |
//Subgraph containing the edges on some shortest paths
|
marci@472
|
905 |
ConstMap<typename ResGW::Node, bool> true_map(true);
|
marci@472
|
906 |
typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
|
marci@472
|
907 |
DistanceMap<ResGW> > FilterResGW;
|
marci@472
|
908 |
FilterResGW filter_res_graph(res_graph, true_map, dist);
|
marci@472
|
909 |
|
marci@472
|
910 |
//Subgraph, which is able to delete edges which are already
|
marci@472
|
911 |
//met by the dfs
|
marci@472
|
912 |
typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
|
marci@472
|
913 |
first_out_edges(filter_res_graph);
|
marci@472
|
914 |
typename FilterResGW::NodeIt v;
|
marci@472
|
915 |
for(filter_res_graph.first(v); filter_res_graph.valid(v);
|
marci@472
|
916 |
filter_res_graph.next(v))
|
marci@472
|
917 |
{
|
marci@472
|
918 |
typename FilterResGW::OutEdgeIt e;
|
marci@472
|
919 |
filter_res_graph.first(e, v);
|
marci@472
|
920 |
first_out_edges.set(v, e);
|
marci@472
|
921 |
}
|
marci@472
|
922 |
typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
|
marci@472
|
923 |
template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
|
marci@472
|
924 |
ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
|
marci@472
|
925 |
|
marci@472
|
926 |
bool __augment=true;
|
marci@472
|
927 |
|
marci@472
|
928 |
while (__augment) {
|
marci@472
|
929 |
|
marci@472
|
930 |
__augment=false;
|
marci@472
|
931 |
//computing blocking flow with dfs
|
marci@472
|
932 |
DfsIterator< ErasingResGW,
|
marci@472
|
933 |
typename ErasingResGW::template NodeMap<bool> >
|
marci@472
|
934 |
dfs(erasing_res_graph);
|
marci@472
|
935 |
typename ErasingResGW::
|
marci@472
|
936 |
template NodeMap<typename ErasingResGW::OutEdgeIt>
|
marci@472
|
937 |
pred(erasing_res_graph);
|
marci@472
|
938 |
pred.set(s, INVALID);
|
marci@472
|
939 |
//invalid iterators for sources
|
marci@472
|
940 |
|
marci@472
|
941 |
typename ErasingResGW::template NodeMap<Num>
|
marci@472
|
942 |
free1(erasing_res_graph);
|
marci@472
|
943 |
|
marci@472
|
944 |
dfs.pushAndSetReached(
|
marci@472
|
945 |
typename ErasingResGW::Node(
|
marci@472
|
946 |
typename FilterResGW::Node(
|
marci@472
|
947 |
typename ResGW::Node(s)
|
marci@472
|
948 |
)
|
marci@472
|
949 |
)
|
marci@472
|
950 |
);
|
marci@472
|
951 |
while (!dfs.finished()) {
|
marci@472
|
952 |
++dfs;
|
marci@472
|
953 |
if (erasing_res_graph.valid(
|
marci@472
|
954 |
typename ErasingResGW::OutEdgeIt(dfs)))
|
marci@472
|
955 |
{
|
marci@472
|
956 |
if (dfs.isBNodeNewlyReached()) {
|
marci@472
|
957 |
|
marci@472
|
958 |
typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
|
marci@472
|
959 |
typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
|
marci@472
|
960 |
|
marci@472
|
961 |
pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
|
marci@472
|
962 |
if (erasing_res_graph.valid(pred[v])) {
|
marci@472
|
963 |
free1.set(w, std::min(free1[v], res_graph.resCap(
|
marci@472
|
964 |
typename ErasingResGW::OutEdgeIt(dfs))));
|
marci@472
|
965 |
} else {
|
marci@472
|
966 |
free1.set(w, res_graph.resCap(
|
marci@472
|
967 |
typename ErasingResGW::OutEdgeIt(dfs)));
|
marci@472
|
968 |
}
|
marci@472
|
969 |
|
marci@472
|
970 |
if (w==t) {
|
marci@472
|
971 |
__augment=true;
|
marci@472
|
972 |
_augment=true;
|
marci@472
|
973 |
break;
|
marci@472
|
974 |
}
|
marci@472
|
975 |
} else {
|
marci@472
|
976 |
erasing_res_graph.erase(dfs);
|
marci@472
|
977 |
}
|
marci@472
|
978 |
}
|
marci@472
|
979 |
}
|
marci@472
|
980 |
|
marci@472
|
981 |
if (__augment) {
|
marci@472
|
982 |
typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
|
marci@472
|
983 |
// typename ResGW::NodeMap<Num> a(res_graph);
|
marci@472
|
984 |
// typename ResGW::Node b;
|
marci@472
|
985 |
// Num j=a[b];
|
marci@472
|
986 |
// typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
|
marci@472
|
987 |
// typename FilterResGW::Node b1;
|
marci@472
|
988 |
// Num j1=a1[b1];
|
marci@472
|
989 |
// typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
|
marci@472
|
990 |
// typename ErasingResGW::Node b2;
|
marci@472
|
991 |
// Num j2=a2[b2];
|
marci@472
|
992 |
Num augment_value=free1[n];
|
marci@472
|
993 |
while (erasing_res_graph.valid(pred[n])) {
|
marci@472
|
994 |
typename ErasingResGW::OutEdgeIt e=pred[n];
|
marci@472
|
995 |
res_graph.augment(e, augment_value);
|
marci@472
|
996 |
n=erasing_res_graph.tail(e);
|
marci@472
|
997 |
if (res_graph.resCap(e)==0)
|
marci@472
|
998 |
erasing_res_graph.erase(e);
|
marci@472
|
999 |
}
|
marci@472
|
1000 |
}
|
marci@472
|
1001 |
|
marci@472
|
1002 |
} //while (__augment)
|
marci@472
|
1003 |
|
marci@472
|
1004 |
return _augment;
|
marci@472
|
1005 |
}
|
marci@472
|
1006 |
|
marci@472
|
1007 |
|
marci@472
|
1008 |
|
marci@472
|
1009 |
|
marci@174
|
1010 |
} //namespace hugo
|
jacint@109
|
1011 |
|
jacint@451
|
1012 |
#endif //HUGO_PREFLOW_H
|
jacint@109
|
1013 |
|
jacint@109
|
1014 |
|
marci@174
|
1015 |
|
marci@174
|
1016 |
|