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