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