marci@59
|
1 |
#ifndef EDMONDS_KARP_HH
|
marci@59
|
2 |
#define EDMONDS_KARP_HH
|
marci@43
|
3 |
|
marci@43
|
4 |
#include <algorithm>
|
marci@75
|
5 |
#include <list>
|
marci@75
|
6 |
#include <iterator>
|
marci@43
|
7 |
|
marci@43
|
8 |
#include <bfs_iterator.hh>
|
marci@100
|
9 |
#include <time_measure.h>
|
marci@43
|
10 |
|
marci@43
|
11 |
namespace marci {
|
marci@43
|
12 |
|
marci@75
|
13 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@43
|
14 |
class ResGraph {
|
marci@43
|
15 |
typedef typename Graph::NodeIt NodeIt;
|
marci@43
|
16 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
marci@43
|
17 |
typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@43
|
18 |
const Graph& G;
|
marci@59
|
19 |
FlowMap& flow;
|
marci@59
|
20 |
const CapacityMap& capacity;
|
marci@43
|
21 |
public:
|
marci@59
|
22 |
ResGraph(const Graph& _G, FlowMap& _flow,
|
marci@59
|
23 |
const CapacityMap& _capacity) :
|
marci@43
|
24 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@43
|
25 |
|
marci@43
|
26 |
class EdgeIt;
|
marci@43
|
27 |
class OutEdgeIt;
|
marci@43
|
28 |
friend class EdgeIt;
|
marci@43
|
29 |
friend class OutEdgeIt;
|
marci@43
|
30 |
|
marci@43
|
31 |
class EdgeIt {
|
marci@75
|
32 |
friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
|
marci@43
|
33 |
protected:
|
marci@75
|
34 |
const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@43
|
35 |
OldSymEdgeIt sym;
|
marci@43
|
36 |
public:
|
marci@43
|
37 |
EdgeIt() { }
|
marci@43
|
38 |
//EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
|
marci@75
|
39 |
Number free() const {
|
marci@43
|
40 |
if (resG->G.aNode(sym)==resG->G.tail(sym)) {
|
marci@43
|
41 |
return (resG->capacity.get(sym)-resG->flow.get(sym));
|
marci@43
|
42 |
} else {
|
marci@43
|
43 |
return (resG->flow.get(sym));
|
marci@43
|
44 |
}
|
marci@43
|
45 |
}
|
marci@43
|
46 |
bool valid() const { return sym.valid(); }
|
marci@75
|
47 |
void augment(Number a) const {
|
marci@43
|
48 |
if (resG->G.aNode(sym)==resG->G.tail(sym)) {
|
marci@43
|
49 |
resG->flow.set(sym, resG->flow.get(sym)+a);
|
marci@75
|
50 |
//resG->flow[sym]+=a;
|
marci@43
|
51 |
} else {
|
marci@43
|
52 |
resG->flow.set(sym, resG->flow.get(sym)-a);
|
marci@75
|
53 |
//resG->flow[sym]-=a;
|
marci@43
|
54 |
}
|
marci@43
|
55 |
}
|
marci@43
|
56 |
};
|
marci@43
|
57 |
|
marci@43
|
58 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
59 |
friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
|
marci@43
|
60 |
public:
|
marci@43
|
61 |
OutEdgeIt() { }
|
marci@43
|
62 |
//OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
|
marci@43
|
63 |
private:
|
marci@75
|
64 |
OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
|
marci@43
|
65 |
resG=&_resG;
|
marci@43
|
66 |
sym=resG->G.template first<OldSymEdgeIt>(v);
|
marci@43
|
67 |
while( sym.valid() && !(free()>0) ) { ++sym; }
|
marci@43
|
68 |
}
|
marci@43
|
69 |
public:
|
marci@43
|
70 |
OutEdgeIt& operator++() {
|
marci@43
|
71 |
++sym;
|
marci@43
|
72 |
while( sym.valid() && !(free()>0) ) { ++sym; }
|
marci@43
|
73 |
return *this;
|
marci@43
|
74 |
}
|
marci@43
|
75 |
};
|
marci@43
|
76 |
|
marci@64
|
77 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@43
|
78 |
e=OutEdgeIt(*this, v);
|
marci@43
|
79 |
}
|
marci@43
|
80 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@43
|
81 |
|
marci@43
|
82 |
template< typename It >
|
marci@43
|
83 |
It first() const {
|
marci@75
|
84 |
It e;
|
marci@75
|
85 |
getFirst(e);
|
marci@75
|
86 |
return e;
|
marci@75
|
87 |
}
|
marci@75
|
88 |
|
marci@75
|
89 |
template< typename It >
|
marci@75
|
90 |
It first(NodeIt v) const {
|
marci@75
|
91 |
It e;
|
marci@75
|
92 |
getFirst(e, v);
|
marci@75
|
93 |
return e;
|
marci@75
|
94 |
}
|
marci@75
|
95 |
|
marci@75
|
96 |
NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
|
marci@75
|
97 |
NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
|
marci@75
|
98 |
|
marci@75
|
99 |
NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
|
marci@75
|
100 |
NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
|
marci@75
|
101 |
|
marci@75
|
102 |
int id(NodeIt v) const { return G.id(v); }
|
marci@75
|
103 |
|
marci@75
|
104 |
template <typename S>
|
marci@75
|
105 |
class NodeMap {
|
marci@75
|
106 |
typename Graph::NodeMap<S> node_map;
|
marci@75
|
107 |
public:
|
marci@75
|
108 |
NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@75
|
109 |
NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
|
marci@75
|
110 |
void set(NodeIt nit, S a) { node_map.set(nit, a); }
|
marci@75
|
111 |
S get(NodeIt nit) const { return node_map.get(nit); }
|
marci@75
|
112 |
S& operator[](NodeIt nit) { return node_map[nit]; }
|
marci@75
|
113 |
const S& operator[](NodeIt nit) const { return node_map[nit]; }
|
marci@75
|
114 |
};
|
marci@75
|
115 |
|
marci@75
|
116 |
};
|
marci@75
|
117 |
|
marci@75
|
118 |
|
marci@75
|
119 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
120 |
class ResGraph2 {
|
marci@75
|
121 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
122 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
marci@75
|
123 |
//typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@75
|
124 |
typedef typename Graph::OutEdgeIt OldOutEdgeIt;
|
marci@75
|
125 |
typedef typename Graph::InEdgeIt OldInEdgeIt;
|
marci@75
|
126 |
|
marci@75
|
127 |
const Graph& G;
|
marci@75
|
128 |
FlowMap& flow;
|
marci@75
|
129 |
const CapacityMap& capacity;
|
marci@75
|
130 |
public:
|
marci@75
|
131 |
ResGraph2(const Graph& _G, FlowMap& _flow,
|
marci@75
|
132 |
const CapacityMap& _capacity) :
|
marci@75
|
133 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@75
|
134 |
|
marci@75
|
135 |
class EdgeIt;
|
marci@75
|
136 |
class OutEdgeIt;
|
marci@75
|
137 |
friend class EdgeIt;
|
marci@75
|
138 |
friend class OutEdgeIt;
|
marci@75
|
139 |
|
marci@75
|
140 |
class EdgeIt {
|
marci@75
|
141 |
friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
142 |
protected:
|
marci@75
|
143 |
const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@75
|
144 |
//OldSymEdgeIt sym;
|
marci@75
|
145 |
OldOutEdgeIt out;
|
marci@75
|
146 |
OldInEdgeIt in;
|
marci@75
|
147 |
bool out_or_in; //1, iff out
|
marci@75
|
148 |
public:
|
marci@75
|
149 |
EdgeIt() : out_or_in(1) { }
|
marci@75
|
150 |
Number free() const {
|
marci@75
|
151 |
if (out_or_in) {
|
marci@75
|
152 |
return (resG->capacity.get(out)-resG->flow.get(out));
|
marci@75
|
153 |
} else {
|
marci@75
|
154 |
return (resG->flow.get(in));
|
marci@75
|
155 |
}
|
marci@75
|
156 |
}
|
marci@75
|
157 |
bool valid() const {
|
marci@75
|
158 |
return out_or_in && out.valid() || in.valid(); }
|
marci@75
|
159 |
void augment(Number a) const {
|
marci@75
|
160 |
if (out_or_in) {
|
marci@75
|
161 |
resG->flow.set(out, resG->flow.get(out)+a);
|
marci@75
|
162 |
} else {
|
marci@75
|
163 |
resG->flow.set(in, resG->flow.get(in)-a);
|
marci@75
|
164 |
}
|
marci@75
|
165 |
}
|
marci@75
|
166 |
};
|
marci@75
|
167 |
|
marci@75
|
168 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
169 |
friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
170 |
public:
|
marci@75
|
171 |
OutEdgeIt() { }
|
marci@75
|
172 |
private:
|
marci@75
|
173 |
OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
|
marci@75
|
174 |
resG=&_resG;
|
marci@75
|
175 |
out=resG->G.template first<OldOutEdgeIt>(v);
|
marci@75
|
176 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
177 |
if (!out.valid()) {
|
marci@75
|
178 |
out_or_in=0;
|
marci@75
|
179 |
in=resG->G.template first<OldInEdgeIt>(v);
|
marci@75
|
180 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
181 |
}
|
marci@75
|
182 |
}
|
marci@75
|
183 |
public:
|
marci@75
|
184 |
OutEdgeIt& operator++() {
|
marci@75
|
185 |
if (out_or_in) {
|
marci@75
|
186 |
NodeIt v=resG->G.aNode(out);
|
marci@75
|
187 |
++out;
|
marci@75
|
188 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
189 |
if (!out.valid()) {
|
marci@75
|
190 |
out_or_in=0;
|
marci@75
|
191 |
in=resG->G.template first<OldInEdgeIt>(v);
|
marci@75
|
192 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
193 |
}
|
marci@75
|
194 |
} else {
|
marci@75
|
195 |
++in;
|
marci@75
|
196 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
197 |
}
|
marci@75
|
198 |
return *this;
|
marci@75
|
199 |
}
|
marci@75
|
200 |
};
|
marci@75
|
201 |
|
marci@75
|
202 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@75
|
203 |
e=OutEdgeIt(*this, v);
|
marci@75
|
204 |
}
|
marci@75
|
205 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@75
|
206 |
|
marci@75
|
207 |
template< typename It >
|
marci@75
|
208 |
It first() const {
|
marci@43
|
209 |
It e;
|
marci@43
|
210 |
getFirst(e);
|
marci@43
|
211 |
return e;
|
marci@43
|
212 |
}
|
marci@43
|
213 |
|
marci@43
|
214 |
template< typename It >
|
marci@64
|
215 |
It first(NodeIt v) const {
|
marci@43
|
216 |
It e;
|
marci@43
|
217 |
getFirst(e, v);
|
marci@43
|
218 |
return e;
|
marci@43
|
219 |
}
|
marci@43
|
220 |
|
marci@75
|
221 |
NodeIt tail(EdgeIt e) const {
|
marci@75
|
222 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
223 |
NodeIt head(EdgeIt e) const {
|
marci@75
|
224 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@43
|
225 |
|
marci@75
|
226 |
NodeIt aNode(OutEdgeIt e) const {
|
marci@75
|
227 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
228 |
NodeIt bNode(OutEdgeIt e) const {
|
marci@75
|
229 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@43
|
230 |
|
marci@64
|
231 |
int id(NodeIt v) const { return G.id(v); }
|
marci@43
|
232 |
|
marci@69
|
233 |
template <typename S>
|
marci@43
|
234 |
class NodeMap {
|
marci@69
|
235 |
typename Graph::NodeMap<S> node_map;
|
marci@43
|
236 |
public:
|
marci@75
|
237 |
NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@75
|
238 |
NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
|
marci@75
|
239 |
void set(NodeIt nit, S a) { node_map.set(nit, a); }
|
marci@75
|
240 |
S get(NodeIt nit) const { return node_map.get(nit); }
|
marci@75
|
241 |
};
|
marci@75
|
242 |
};
|
marci@75
|
243 |
|
marci@75
|
244 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
245 |
class ResGraph3 {
|
alpar@94
|
246 |
public:
|
marci@75
|
247 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
248 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
marci@75
|
249 |
//typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@75
|
250 |
typedef typename Graph::OutEdgeIt OldOutEdgeIt;
|
marci@75
|
251 |
typedef typename Graph::InEdgeIt OldInEdgeIt;
|
marci@75
|
252 |
|
alpar@94
|
253 |
private:
|
marci@75
|
254 |
const Graph& G;
|
marci@75
|
255 |
FlowMap& flow;
|
marci@75
|
256 |
const CapacityMap& capacity;
|
marci@75
|
257 |
public:
|
marci@75
|
258 |
ResGraph3(const Graph& _G, FlowMap& _flow,
|
marci@75
|
259 |
const CapacityMap& _capacity) :
|
marci@75
|
260 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@75
|
261 |
|
marci@75
|
262 |
class EdgeIt;
|
marci@75
|
263 |
class OutEdgeIt;
|
marci@75
|
264 |
friend class EdgeIt;
|
marci@75
|
265 |
friend class OutEdgeIt;
|
marci@75
|
266 |
|
marci@75
|
267 |
class EdgeIt {
|
marci@75
|
268 |
friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
269 |
protected:
|
marci@75
|
270 |
//const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@75
|
271 |
const Graph* G;
|
marci@75
|
272 |
FlowMap* flow;
|
marci@75
|
273 |
const CapacityMap* capacity;
|
marci@75
|
274 |
//OldSymEdgeIt sym;
|
marci@75
|
275 |
OldOutEdgeIt out;
|
marci@75
|
276 |
OldInEdgeIt in;
|
marci@75
|
277 |
bool out_or_in; //1, iff out
|
marci@75
|
278 |
public:
|
marci@75
|
279 |
EdgeIt() : out_or_in(1) { }
|
marci@75
|
280 |
Number free() const {
|
marci@75
|
281 |
if (out_or_in) {
|
marci@75
|
282 |
return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
|
marci@75
|
283 |
} else {
|
marci@75
|
284 |
return (/*resG->*/flow->get(in));
|
marci@75
|
285 |
}
|
marci@75
|
286 |
}
|
marci@75
|
287 |
bool valid() const {
|
marci@75
|
288 |
return out_or_in && out.valid() || in.valid(); }
|
marci@75
|
289 |
void augment(Number a) const {
|
marci@75
|
290 |
if (out_or_in) {
|
marci@75
|
291 |
/*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
|
marci@75
|
292 |
} else {
|
marci@75
|
293 |
/*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
|
marci@75
|
294 |
}
|
marci@75
|
295 |
}
|
marci@75
|
296 |
};
|
marci@75
|
297 |
|
marci@75
|
298 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
299 |
friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
300 |
public:
|
marci@75
|
301 |
OutEdgeIt() { }
|
marci@75
|
302 |
private:
|
marci@75
|
303 |
OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
|
marci@75
|
304 |
G=&_G;
|
marci@75
|
305 |
flow=&_flow;
|
marci@75
|
306 |
capacity=&_capacity;
|
marci@75
|
307 |
out=/*resG->*/G->template first<OldOutEdgeIt>(v);
|
marci@75
|
308 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
309 |
if (!out.valid()) {
|
marci@75
|
310 |
out_or_in=0;
|
marci@75
|
311 |
in=/*resG->*/G->template first<OldInEdgeIt>(v);
|
marci@75
|
312 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
313 |
}
|
marci@75
|
314 |
}
|
marci@75
|
315 |
public:
|
marci@75
|
316 |
OutEdgeIt& operator++() {
|
marci@75
|
317 |
if (out_or_in) {
|
marci@75
|
318 |
NodeIt v=/*resG->*/G->aNode(out);
|
marci@75
|
319 |
++out;
|
marci@75
|
320 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
321 |
if (!out.valid()) {
|
marci@75
|
322 |
out_or_in=0;
|
marci@75
|
323 |
in=/*resG->*/G->template first<OldInEdgeIt>(v);
|
marci@75
|
324 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
325 |
}
|
marci@75
|
326 |
} else {
|
marci@75
|
327 |
++in;
|
marci@75
|
328 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
329 |
}
|
marci@75
|
330 |
return *this;
|
marci@75
|
331 |
}
|
marci@75
|
332 |
};
|
marci@75
|
333 |
|
marci@75
|
334 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@75
|
335 |
e=OutEdgeIt(G, v, flow, capacity);
|
marci@75
|
336 |
}
|
marci@75
|
337 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@75
|
338 |
|
marci@75
|
339 |
template< typename It >
|
marci@75
|
340 |
It first() const {
|
marci@75
|
341 |
It e;
|
marci@75
|
342 |
getFirst(e);
|
marci@75
|
343 |
return e;
|
marci@75
|
344 |
}
|
marci@75
|
345 |
|
marci@75
|
346 |
template< typename It >
|
marci@75
|
347 |
It first(NodeIt v) const {
|
marci@75
|
348 |
It e;
|
marci@75
|
349 |
getFirst(e, v);
|
marci@75
|
350 |
return e;
|
marci@75
|
351 |
}
|
marci@75
|
352 |
|
marci@75
|
353 |
NodeIt tail(EdgeIt e) const {
|
marci@75
|
354 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
355 |
NodeIt head(EdgeIt e) const {
|
marci@75
|
356 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@75
|
357 |
|
marci@75
|
358 |
NodeIt aNode(OutEdgeIt e) const {
|
marci@75
|
359 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
360 |
NodeIt bNode(OutEdgeIt e) const {
|
marci@75
|
361 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@75
|
362 |
|
marci@75
|
363 |
int id(NodeIt v) const { return G.id(v); }
|
marci@75
|
364 |
|
marci@75
|
365 |
template <typename S>
|
marci@75
|
366 |
class NodeMap {
|
marci@75
|
367 |
typename Graph::NodeMap<S> node_map;
|
marci@75
|
368 |
public:
|
marci@75
|
369 |
NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@75
|
370 |
NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
|
marci@69
|
371 |
void set(NodeIt nit, S a) { node_map.set(nit, a); }
|
marci@69
|
372 |
S get(NodeIt nit) const { return node_map.get(nit); }
|
marci@43
|
373 |
};
|
marci@43
|
374 |
|
marci@43
|
375 |
};
|
marci@43
|
376 |
|
marci@75
|
377 |
|
marci@75
|
378 |
template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@59
|
379 |
class MaxFlow {
|
marci@43
|
380 |
typedef typename Graph::NodeIt NodeIt;
|
marci@43
|
381 |
typedef typename Graph::EdgeIt EdgeIt;
|
marci@43
|
382 |
typedef typename Graph::EachEdgeIt EachEdgeIt;
|
marci@43
|
383 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
marci@43
|
384 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
marci@43
|
385 |
const Graph& G;
|
marci@64
|
386 |
NodeIt s;
|
marci@64
|
387 |
NodeIt t;
|
marci@59
|
388 |
FlowMap& flow;
|
marci@59
|
389 |
const CapacityMap& capacity;
|
marci@75
|
390 |
typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
|
marci@59
|
391 |
typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
|
marci@59
|
392 |
typedef typename AugGraph::EdgeIt AugEdgeIt;
|
marci@75
|
393 |
|
marci@75
|
394 |
//AugGraph res_graph;
|
marci@75
|
395 |
//typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
396 |
//typename AugGraph::NodeMap<AugEdgeIt> pred;
|
marci@75
|
397 |
//typename AugGraph::NodeMap<int> free;
|
marci@59
|
398 |
public:
|
marci@75
|
399 |
MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
|
marci@75
|
400 |
G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
|
marci@75
|
401 |
//res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
|
marci@75
|
402 |
{ }
|
marci@59
|
403 |
bool augment() {
|
marci@59
|
404 |
AugGraph res_graph(G, flow, capacity);
|
marci@59
|
405 |
bool _augment=false;
|
marci@59
|
406 |
|
marci@59
|
407 |
typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
408 |
BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
|
marci@59
|
409 |
res_bfs.pushAndSetReached(s);
|
marci@59
|
410 |
|
marci@59
|
411 |
typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
|
marci@75
|
412 |
//filled up with invalid iterators
|
marci@75
|
413 |
//pred.set(s, AugEdgeIt());
|
marci@59
|
414 |
|
marci@59
|
415 |
typename AugGraph::NodeMap<int> free(res_graph);
|
marci@59
|
416 |
|
marci@59
|
417 |
//searching for augmenting path
|
marci@59
|
418 |
while ( !res_bfs.finished() ) {
|
marci@59
|
419 |
AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
|
marci@59
|
420 |
if (e.valid() && res_bfs.isBNodeNewlyReached()) {
|
marci@59
|
421 |
NodeIt v=res_graph.tail(e);
|
marci@59
|
422 |
NodeIt w=res_graph.head(e);
|
marci@59
|
423 |
pred.set(w, e);
|
marci@59
|
424 |
if (pred.get(v).valid()) {
|
marci@59
|
425 |
free.set(w, std::min(free.get(v), e.free()));
|
marci@59
|
426 |
} else {
|
marci@59
|
427 |
free.set(w, e.free());
|
marci@59
|
428 |
}
|
marci@59
|
429 |
if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@59
|
430 |
}
|
marci@59
|
431 |
|
marci@59
|
432 |
++res_bfs;
|
marci@59
|
433 |
} //end of searching augmenting path
|
marci@43
|
434 |
|
marci@59
|
435 |
if (_augment) {
|
marci@59
|
436 |
NodeIt n=t;
|
marci@75
|
437 |
Number augment_value=free.get(t);
|
marci@59
|
438 |
while (pred.get(n).valid()) {
|
marci@59
|
439 |
AugEdgeIt e=pred.get(n);
|
marci@59
|
440 |
e.augment(augment_value);
|
marci@59
|
441 |
n=res_graph.tail(e);
|
marci@59
|
442 |
}
|
marci@59
|
443 |
}
|
marci@59
|
444 |
|
marci@59
|
445 |
return _augment;
|
marci@59
|
446 |
}
|
marci@100
|
447 |
bool augmentWithBlockingFlow() {
|
marci@100
|
448 |
BfsIterator4< Graph, OutEdgeIt, typename Graph::NodeMap<bool> > bfs(G);
|
marci@100
|
449 |
bfs.pushAndSetReached(s);
|
marci@100
|
450 |
typename Graph::NodeMap<int> dist(G); //filled up with 0's
|
marci@100
|
451 |
while ( !bfs.finished() ) {
|
marci@100
|
452 |
OutEdgeIt e=OutEdgeIt(bfs);
|
marci@100
|
453 |
if (e.valid() && bfs.isBNodeNewlyReached()) {
|
marci@100
|
454 |
dist.set(G.head(e), dist.get(G.tail(e))+1);
|
marci@100
|
455 |
//NodeIt v=res_graph.tail(e);
|
marci@100
|
456 |
//NodeIt w=res_graph.head(e);
|
marci@100
|
457 |
//pred.set(w, e);
|
marci@100
|
458 |
//if (pred.get(v).valid()) {
|
marci@100
|
459 |
// free.set(w, std::min(free.get(v), e.free()));
|
marci@100
|
460 |
//} else {
|
marci@100
|
461 |
// free.set(w, e.free());
|
marci@100
|
462 |
//}
|
marci@100
|
463 |
//if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@100
|
464 |
}
|
marci@100
|
465 |
|
marci@100
|
466 |
++bfs;
|
marci@100
|
467 |
} //end of searching augmenting path
|
marci@100
|
468 |
|
marci@100
|
469 |
double pre_time_copy=currTime();
|
marci@100
|
470 |
typedef Graph MutableGraph;
|
marci@100
|
471 |
MutableGraph F;
|
marci@100
|
472 |
typename Graph::NodeMap<NodeIt> G_to_F(G);
|
marci@100
|
473 |
for(typename Graph::EachNodeIt n=G.template first<typename Graph::EachNodeIt>(); n.valid(); ++n) {
|
marci@100
|
474 |
G_to_F.set(n, F.addNode());
|
marci@100
|
475 |
}
|
marci@100
|
476 |
for(typename Graph::EachEdgeIt e=G.template first<typename Graph::EachEdgeIt>(); e.valid(); ++e) {
|
marci@100
|
477 |
if (dist.get(G.head(e))==dist.get(G.tail(e))+1) {
|
marci@100
|
478 |
F.addEdge(G_to_F.get(G.tail(e)), G_to_F.get(G.head(e)));
|
marci@100
|
479 |
}
|
marci@100
|
480 |
}
|
marci@100
|
481 |
double post_time_copy=currTime();
|
marci@100
|
482 |
std::cout << "copy time: " << post_time_copy-pre_time_copy << " sec"<< std::endl;
|
marci@100
|
483 |
|
marci@100
|
484 |
return 0;
|
marci@100
|
485 |
}
|
marci@43
|
486 |
void run() {
|
marci@100
|
487 |
//int num_of_augmentations=0;
|
marci@100
|
488 |
while (augment()) {
|
marci@100
|
489 |
//std::cout << ++num_of_augmentations << " ";
|
marci@100
|
490 |
//std::cout<<std::endl;
|
marci@100
|
491 |
}
|
marci@43
|
492 |
}
|
marci@75
|
493 |
Number flowValue() {
|
marci@75
|
494 |
Number a=0;
|
marci@69
|
495 |
for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
|
marci@69
|
496 |
a+=flow.get(i);
|
marci@69
|
497 |
}
|
marci@69
|
498 |
return a;
|
marci@69
|
499 |
}
|
marci@43
|
500 |
};
|
marci@43
|
501 |
|
marci@75
|
502 |
|
marci@75
|
503 |
/*
|
marci@75
|
504 |
template <typename Graph>
|
marci@75
|
505 |
class IteratorBoolNodeMap {
|
marci@75
|
506 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
507 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
marci@75
|
508 |
class BoolItem {
|
marci@75
|
509 |
public:
|
marci@75
|
510 |
bool value;
|
marci@75
|
511 |
NodeIt prev;
|
marci@75
|
512 |
NodeIt next;
|
marci@75
|
513 |
BoolItem() : value(false), prev(), next() {}
|
marci@75
|
514 |
};
|
marci@75
|
515 |
NodeIt first_true;
|
marci@75
|
516 |
//NodeIt last_true;
|
marci@75
|
517 |
NodeIt first_false;
|
marci@75
|
518 |
//NodeIt last_false;
|
marci@75
|
519 |
const Graph& G;
|
marci@75
|
520 |
typename Graph::NodeMap<BoolItem> container;
|
marci@75
|
521 |
public:
|
marci@75
|
522 |
typedef bool ValueType;
|
marci@75
|
523 |
typedef NodeIt KeyType;
|
marci@75
|
524 |
IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
|
marci@75
|
525 |
//for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
|
marci@75
|
526 |
//BoolItem b=container.get(e);
|
marci@75
|
527 |
//b.me=e;
|
marci@75
|
528 |
//container.set(b);
|
marci@75
|
529 |
//}
|
marci@75
|
530 |
G.getFirst(first_false);
|
marci@75
|
531 |
NodeIt e_prev;
|
marci@75
|
532 |
for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
|
marci@75
|
533 |
container[e].prev=e_prev;
|
marci@75
|
534 |
if (e_prev.valid()) container[e_prev].next=e;
|
marci@75
|
535 |
e_prev=e;
|
marci@75
|
536 |
}
|
marci@75
|
537 |
}
|
marci@75
|
538 |
//NodeMap(const Graph& _G, T a) :
|
marci@75
|
539 |
// G(_G), container(G.node_id, a) { }
|
marci@75
|
540 |
//FIXME
|
marci@75
|
541 |
void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
|
marci@75
|
542 |
T get(NodeIt nit) const { return container[G.id(nit)]; }
|
marci@75
|
543 |
//void resize() { container.resize(G.node_id); }
|
marci@75
|
544 |
//void resize(T a) { container.resize(G.node_id, a); }
|
marci@75
|
545 |
};
|
marci@75
|
546 |
*/
|
marci@75
|
547 |
|
marci@75
|
548 |
|
marci@75
|
549 |
template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
550 |
class MaxFlow2 {
|
marci@75
|
551 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
552 |
typedef typename Graph::EdgeIt EdgeIt;
|
marci@75
|
553 |
typedef typename Graph::EachEdgeIt EachEdgeIt;
|
marci@75
|
554 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
marci@75
|
555 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
marci@75
|
556 |
const Graph& G;
|
marci@75
|
557 |
std::list<NodeIt>& S;
|
marci@75
|
558 |
std::list<NodeIt>& T;
|
marci@75
|
559 |
FlowMap& flow;
|
marci@75
|
560 |
const CapacityMap& capacity;
|
marci@75
|
561 |
typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
|
marci@75
|
562 |
typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
|
marci@75
|
563 |
typedef typename AugGraph::EdgeIt AugEdgeIt;
|
marci@75
|
564 |
typename Graph::NodeMap<bool> SMap;
|
marci@75
|
565 |
typename Graph::NodeMap<bool> TMap;
|
marci@75
|
566 |
public:
|
marci@75
|
567 |
MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
|
marci@75
|
568 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
569 |
i!=S.end(); ++i) {
|
marci@75
|
570 |
SMap.set(*i, true);
|
marci@75
|
571 |
}
|
marci@75
|
572 |
for (typename std::list<NodeIt>::const_iterator i=T.begin();
|
marci@75
|
573 |
i!=T.end(); ++i) {
|
marci@75
|
574 |
TMap.set(*i, true);
|
marci@75
|
575 |
}
|
marci@75
|
576 |
}
|
marci@75
|
577 |
bool augment() {
|
marci@75
|
578 |
AugGraph res_graph(G, flow, capacity);
|
marci@75
|
579 |
bool _augment=false;
|
marci@75
|
580 |
NodeIt reached_t_node;
|
marci@75
|
581 |
|
marci@75
|
582 |
typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
583 |
BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
|
marci@75
|
584 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
585 |
i!=S.end(); ++i) {
|
marci@75
|
586 |
res_bfs.pushAndSetReached(*i);
|
marci@75
|
587 |
}
|
marci@75
|
588 |
//res_bfs.pushAndSetReached(s);
|
marci@75
|
589 |
|
marci@75
|
590 |
typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
|
marci@75
|
591 |
//filled up with invalid iterators
|
marci@75
|
592 |
|
marci@75
|
593 |
typename AugGraph::NodeMap<int> free(res_graph);
|
marci@75
|
594 |
|
marci@75
|
595 |
//searching for augmenting path
|
marci@75
|
596 |
while ( !res_bfs.finished() ) {
|
marci@75
|
597 |
AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
|
marci@75
|
598 |
if (e.valid() && res_bfs.isBNodeNewlyReached()) {
|
marci@75
|
599 |
NodeIt v=res_graph.tail(e);
|
marci@75
|
600 |
NodeIt w=res_graph.head(e);
|
marci@75
|
601 |
pred.set(w, e);
|
marci@75
|
602 |
if (pred.get(v).valid()) {
|
marci@75
|
603 |
free.set(w, std::min(free.get(v), e.free()));
|
marci@75
|
604 |
} else {
|
marci@75
|
605 |
free.set(w, e.free());
|
marci@75
|
606 |
}
|
marci@75
|
607 |
if (TMap.get(res_graph.head(e))) {
|
marci@75
|
608 |
_augment=true;
|
marci@75
|
609 |
reached_t_node=res_graph.head(e);
|
marci@75
|
610 |
break;
|
marci@75
|
611 |
}
|
marci@75
|
612 |
}
|
marci@75
|
613 |
|
marci@75
|
614 |
++res_bfs;
|
marci@75
|
615 |
} //end of searching augmenting path
|
marci@75
|
616 |
|
marci@75
|
617 |
if (_augment) {
|
marci@75
|
618 |
NodeIt n=reached_t_node;
|
marci@75
|
619 |
Number augment_value=free.get(reached_t_node);
|
marci@75
|
620 |
while (pred.get(n).valid()) {
|
marci@75
|
621 |
AugEdgeIt e=pred.get(n);
|
marci@75
|
622 |
e.augment(augment_value);
|
marci@75
|
623 |
n=res_graph.tail(e);
|
marci@75
|
624 |
}
|
marci@75
|
625 |
}
|
marci@75
|
626 |
|
marci@75
|
627 |
return _augment;
|
marci@75
|
628 |
}
|
marci@75
|
629 |
void run() {
|
marci@75
|
630 |
while (augment()) { }
|
marci@75
|
631 |
}
|
marci@75
|
632 |
Number flowValue() {
|
marci@75
|
633 |
Number a=0;
|
marci@75
|
634 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
635 |
i!=S.end(); ++i) {
|
marci@75
|
636 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
|
marci@75
|
637 |
a+=flow.get(e);
|
marci@75
|
638 |
}
|
marci@75
|
639 |
for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
|
marci@75
|
640 |
a-=flow.get(e);
|
marci@75
|
641 |
}
|
marci@75
|
642 |
}
|
marci@75
|
643 |
return a;
|
marci@75
|
644 |
}
|
marci@75
|
645 |
};
|
marci@75
|
646 |
|
marci@75
|
647 |
|
marci@75
|
648 |
|
marci@43
|
649 |
} // namespace marci
|
marci@43
|
650 |
|
marci@59
|
651 |
#endif //EDMONDS_KARP_HH
|