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@133
|
9 |
//#include <time_measure.h>
|
marci@43
|
10 |
|
alpar@107
|
11 |
namespace hugo {
|
marci@43
|
12 |
|
marci@75
|
13 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@43
|
14 |
class ResGraph {
|
klao@127
|
15 |
public:
|
marci@43
|
16 |
typedef typename Graph::NodeIt NodeIt;
|
marci@43
|
17 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
klao@127
|
18 |
private:
|
marci@43
|
19 |
typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@43
|
20 |
const Graph& G;
|
marci@59
|
21 |
FlowMap& flow;
|
marci@59
|
22 |
const CapacityMap& capacity;
|
marci@43
|
23 |
public:
|
marci@59
|
24 |
ResGraph(const Graph& _G, FlowMap& _flow,
|
marci@59
|
25 |
const CapacityMap& _capacity) :
|
marci@43
|
26 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@43
|
27 |
|
marci@43
|
28 |
class EdgeIt;
|
marci@43
|
29 |
class OutEdgeIt;
|
marci@43
|
30 |
friend class EdgeIt;
|
marci@43
|
31 |
friend class OutEdgeIt;
|
marci@43
|
32 |
|
marci@43
|
33 |
class EdgeIt {
|
marci@75
|
34 |
friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
|
marci@43
|
35 |
protected:
|
marci@75
|
36 |
const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@43
|
37 |
OldSymEdgeIt sym;
|
marci@43
|
38 |
public:
|
marci@43
|
39 |
EdgeIt() { }
|
marci@43
|
40 |
//EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
|
marci@75
|
41 |
Number free() const {
|
marci@43
|
42 |
if (resG->G.aNode(sym)==resG->G.tail(sym)) {
|
marci@43
|
43 |
return (resG->capacity.get(sym)-resG->flow.get(sym));
|
marci@43
|
44 |
} else {
|
marci@43
|
45 |
return (resG->flow.get(sym));
|
marci@43
|
46 |
}
|
marci@43
|
47 |
}
|
marci@43
|
48 |
bool valid() const { return sym.valid(); }
|
marci@75
|
49 |
void augment(Number a) const {
|
marci@43
|
50 |
if (resG->G.aNode(sym)==resG->G.tail(sym)) {
|
marci@43
|
51 |
resG->flow.set(sym, resG->flow.get(sym)+a);
|
marci@75
|
52 |
//resG->flow[sym]+=a;
|
marci@43
|
53 |
} else {
|
marci@43
|
54 |
resG->flow.set(sym, resG->flow.get(sym)-a);
|
marci@75
|
55 |
//resG->flow[sym]-=a;
|
marci@43
|
56 |
}
|
marci@43
|
57 |
}
|
marci@43
|
58 |
};
|
marci@43
|
59 |
|
marci@43
|
60 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
61 |
friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
|
marci@43
|
62 |
public:
|
marci@43
|
63 |
OutEdgeIt() { }
|
marci@43
|
64 |
//OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
|
marci@43
|
65 |
private:
|
marci@75
|
66 |
OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
|
marci@43
|
67 |
resG=&_resG;
|
marci@43
|
68 |
sym=resG->G.template first<OldSymEdgeIt>(v);
|
marci@43
|
69 |
while( sym.valid() && !(free()>0) ) { ++sym; }
|
marci@43
|
70 |
}
|
marci@43
|
71 |
public:
|
marci@43
|
72 |
OutEdgeIt& operator++() {
|
marci@43
|
73 |
++sym;
|
marci@43
|
74 |
while( sym.valid() && !(free()>0) ) { ++sym; }
|
marci@43
|
75 |
return *this;
|
marci@43
|
76 |
}
|
marci@43
|
77 |
};
|
marci@43
|
78 |
|
marci@64
|
79 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@43
|
80 |
e=OutEdgeIt(*this, v);
|
marci@43
|
81 |
}
|
marci@43
|
82 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@43
|
83 |
|
marci@43
|
84 |
template< typename It >
|
marci@43
|
85 |
It first() const {
|
marci@75
|
86 |
It e;
|
marci@75
|
87 |
getFirst(e);
|
marci@75
|
88 |
return e;
|
marci@75
|
89 |
}
|
marci@75
|
90 |
|
marci@75
|
91 |
template< typename It >
|
marci@75
|
92 |
It first(NodeIt v) const {
|
marci@75
|
93 |
It e;
|
marci@75
|
94 |
getFirst(e, v);
|
marci@75
|
95 |
return e;
|
marci@75
|
96 |
}
|
marci@75
|
97 |
|
marci@75
|
98 |
NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
|
marci@75
|
99 |
NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
|
marci@75
|
100 |
|
marci@75
|
101 |
NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
|
marci@75
|
102 |
NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
|
marci@75
|
103 |
|
marci@75
|
104 |
int id(NodeIt v) const { return G.id(v); }
|
marci@75
|
105 |
|
marci@75
|
106 |
template <typename S>
|
marci@75
|
107 |
class NodeMap {
|
marci@75
|
108 |
typename Graph::NodeMap<S> node_map;
|
marci@75
|
109 |
public:
|
marci@75
|
110 |
NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@75
|
111 |
NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
|
marci@75
|
112 |
void set(NodeIt nit, S a) { node_map.set(nit, a); }
|
marci@75
|
113 |
S get(NodeIt nit) const { return node_map.get(nit); }
|
marci@75
|
114 |
S& operator[](NodeIt nit) { return node_map[nit]; }
|
marci@75
|
115 |
const S& operator[](NodeIt nit) const { return node_map[nit]; }
|
marci@75
|
116 |
};
|
marci@75
|
117 |
|
marci@75
|
118 |
};
|
marci@75
|
119 |
|
marci@75
|
120 |
|
marci@75
|
121 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
122 |
class ResGraph2 {
|
klao@127
|
123 |
public:
|
marci@75
|
124 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
125 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
klao@127
|
126 |
private:
|
marci@75
|
127 |
//typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@75
|
128 |
typedef typename Graph::OutEdgeIt OldOutEdgeIt;
|
marci@75
|
129 |
typedef typename Graph::InEdgeIt OldInEdgeIt;
|
marci@75
|
130 |
|
marci@75
|
131 |
const Graph& G;
|
marci@75
|
132 |
FlowMap& flow;
|
marci@75
|
133 |
const CapacityMap& capacity;
|
marci@75
|
134 |
public:
|
marci@75
|
135 |
ResGraph2(const Graph& _G, FlowMap& _flow,
|
marci@75
|
136 |
const CapacityMap& _capacity) :
|
marci@75
|
137 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@75
|
138 |
|
marci@75
|
139 |
class EdgeIt;
|
marci@75
|
140 |
class OutEdgeIt;
|
marci@75
|
141 |
friend class EdgeIt;
|
marci@75
|
142 |
friend class OutEdgeIt;
|
marci@75
|
143 |
|
marci@75
|
144 |
class EdgeIt {
|
marci@75
|
145 |
friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
146 |
protected:
|
marci@75
|
147 |
const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@75
|
148 |
//OldSymEdgeIt sym;
|
marci@75
|
149 |
OldOutEdgeIt out;
|
marci@75
|
150 |
OldInEdgeIt in;
|
marci@75
|
151 |
bool out_or_in; //1, iff out
|
marci@75
|
152 |
public:
|
marci@75
|
153 |
EdgeIt() : out_or_in(1) { }
|
marci@75
|
154 |
Number free() const {
|
marci@75
|
155 |
if (out_or_in) {
|
marci@75
|
156 |
return (resG->capacity.get(out)-resG->flow.get(out));
|
marci@75
|
157 |
} else {
|
marci@75
|
158 |
return (resG->flow.get(in));
|
marci@75
|
159 |
}
|
marci@75
|
160 |
}
|
marci@75
|
161 |
bool valid() const {
|
marci@75
|
162 |
return out_or_in && out.valid() || in.valid(); }
|
marci@75
|
163 |
void augment(Number a) const {
|
marci@75
|
164 |
if (out_or_in) {
|
marci@75
|
165 |
resG->flow.set(out, resG->flow.get(out)+a);
|
marci@75
|
166 |
} else {
|
marci@75
|
167 |
resG->flow.set(in, resG->flow.get(in)-a);
|
marci@75
|
168 |
}
|
marci@75
|
169 |
}
|
marci@75
|
170 |
};
|
marci@75
|
171 |
|
marci@75
|
172 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
173 |
friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
174 |
public:
|
marci@75
|
175 |
OutEdgeIt() { }
|
marci@75
|
176 |
private:
|
marci@75
|
177 |
OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
|
marci@75
|
178 |
resG=&_resG;
|
marci@75
|
179 |
out=resG->G.template first<OldOutEdgeIt>(v);
|
marci@75
|
180 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
181 |
if (!out.valid()) {
|
marci@75
|
182 |
out_or_in=0;
|
marci@75
|
183 |
in=resG->G.template first<OldInEdgeIt>(v);
|
marci@75
|
184 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
185 |
}
|
marci@75
|
186 |
}
|
marci@75
|
187 |
public:
|
marci@75
|
188 |
OutEdgeIt& operator++() {
|
marci@75
|
189 |
if (out_or_in) {
|
marci@75
|
190 |
NodeIt v=resG->G.aNode(out);
|
marci@75
|
191 |
++out;
|
marci@75
|
192 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
193 |
if (!out.valid()) {
|
marci@75
|
194 |
out_or_in=0;
|
marci@75
|
195 |
in=resG->G.template first<OldInEdgeIt>(v);
|
marci@75
|
196 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
197 |
}
|
marci@75
|
198 |
} else {
|
marci@75
|
199 |
++in;
|
marci@75
|
200 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
201 |
}
|
marci@75
|
202 |
return *this;
|
marci@75
|
203 |
}
|
marci@75
|
204 |
};
|
marci@75
|
205 |
|
marci@75
|
206 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@75
|
207 |
e=OutEdgeIt(*this, v);
|
marci@75
|
208 |
}
|
marci@75
|
209 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@75
|
210 |
|
marci@75
|
211 |
template< typename It >
|
marci@75
|
212 |
It first() const {
|
marci@43
|
213 |
It e;
|
marci@43
|
214 |
getFirst(e);
|
marci@43
|
215 |
return e;
|
marci@43
|
216 |
}
|
marci@43
|
217 |
|
marci@43
|
218 |
template< typename It >
|
marci@64
|
219 |
It first(NodeIt v) const {
|
marci@43
|
220 |
It e;
|
marci@43
|
221 |
getFirst(e, v);
|
marci@43
|
222 |
return e;
|
marci@43
|
223 |
}
|
marci@43
|
224 |
|
marci@75
|
225 |
NodeIt tail(EdgeIt e) const {
|
marci@75
|
226 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
227 |
NodeIt head(EdgeIt e) const {
|
marci@75
|
228 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@43
|
229 |
|
marci@75
|
230 |
NodeIt aNode(OutEdgeIt e) const {
|
marci@75
|
231 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
232 |
NodeIt bNode(OutEdgeIt e) const {
|
marci@75
|
233 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@43
|
234 |
|
marci@64
|
235 |
int id(NodeIt v) const { return G.id(v); }
|
marci@43
|
236 |
|
marci@69
|
237 |
template <typename S>
|
marci@43
|
238 |
class NodeMap {
|
marci@69
|
239 |
typename Graph::NodeMap<S> node_map;
|
marci@43
|
240 |
public:
|
marci@75
|
241 |
NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@75
|
242 |
NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
|
marci@75
|
243 |
void set(NodeIt nit, S a) { node_map.set(nit, a); }
|
marci@75
|
244 |
S get(NodeIt nit) const { return node_map.get(nit); }
|
marci@75
|
245 |
};
|
marci@75
|
246 |
};
|
marci@75
|
247 |
|
marci@75
|
248 |
template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
249 |
class ResGraph3 {
|
klao@127
|
250 |
public:
|
marci@75
|
251 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
252 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
klao@127
|
253 |
|
klao@127
|
254 |
private:
|
marci@75
|
255 |
//typedef typename Graph::SymEdgeIt OldSymEdgeIt;
|
marci@75
|
256 |
typedef typename Graph::OutEdgeIt OldOutEdgeIt;
|
marci@75
|
257 |
typedef typename Graph::InEdgeIt OldInEdgeIt;
|
marci@75
|
258 |
const Graph& G;
|
marci@75
|
259 |
FlowMap& flow;
|
marci@75
|
260 |
const CapacityMap& capacity;
|
marci@75
|
261 |
public:
|
marci@75
|
262 |
ResGraph3(const Graph& _G, FlowMap& _flow,
|
marci@75
|
263 |
const CapacityMap& _capacity) :
|
marci@75
|
264 |
G(_G), flow(_flow), capacity(_capacity) { }
|
marci@75
|
265 |
|
marci@75
|
266 |
class EdgeIt;
|
marci@75
|
267 |
class OutEdgeIt;
|
marci@75
|
268 |
friend class EdgeIt;
|
marci@75
|
269 |
friend class OutEdgeIt;
|
marci@75
|
270 |
|
marci@75
|
271 |
class EdgeIt {
|
marci@75
|
272 |
friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
273 |
protected:
|
marci@75
|
274 |
//const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
|
marci@75
|
275 |
const Graph* G;
|
marci@75
|
276 |
FlowMap* flow;
|
marci@75
|
277 |
const CapacityMap* capacity;
|
marci@75
|
278 |
//OldSymEdgeIt sym;
|
marci@75
|
279 |
OldOutEdgeIt out;
|
marci@75
|
280 |
OldInEdgeIt in;
|
marci@75
|
281 |
bool out_or_in; //1, iff out
|
marci@75
|
282 |
public:
|
marci@75
|
283 |
EdgeIt() : out_or_in(1) { }
|
marci@133
|
284 |
//EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { }
|
marci@75
|
285 |
Number free() const {
|
marci@75
|
286 |
if (out_or_in) {
|
marci@75
|
287 |
return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
|
marci@75
|
288 |
} else {
|
marci@75
|
289 |
return (/*resG->*/flow->get(in));
|
marci@75
|
290 |
}
|
marci@75
|
291 |
}
|
marci@75
|
292 |
bool valid() const {
|
marci@75
|
293 |
return out_or_in && out.valid() || in.valid(); }
|
marci@75
|
294 |
void augment(Number a) const {
|
marci@75
|
295 |
if (out_or_in) {
|
marci@75
|
296 |
/*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
|
marci@75
|
297 |
} else {
|
marci@75
|
298 |
/*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
|
marci@75
|
299 |
}
|
marci@75
|
300 |
}
|
marci@133
|
301 |
void print() {
|
marci@133
|
302 |
if (out_or_in) {
|
marci@133
|
303 |
std::cout << "out ";
|
marci@133
|
304 |
if (out.valid())
|
marci@133
|
305 |
std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out));
|
marci@133
|
306 |
else
|
marci@133
|
307 |
std::cout << "invalid";
|
marci@133
|
308 |
}
|
marci@133
|
309 |
else {
|
marci@133
|
310 |
std::cout << "in ";
|
marci@133
|
311 |
if (in.valid())
|
marci@133
|
312 |
std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in));
|
marci@133
|
313 |
else
|
marci@133
|
314 |
std::cout << "invalid";
|
marci@133
|
315 |
}
|
marci@133
|
316 |
std::cout << std::endl;
|
marci@133
|
317 |
}
|
marci@75
|
318 |
};
|
marci@75
|
319 |
|
marci@75
|
320 |
class OutEdgeIt : public EdgeIt {
|
marci@75
|
321 |
friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
|
marci@75
|
322 |
public:
|
marci@75
|
323 |
OutEdgeIt() { }
|
marci@75
|
324 |
private:
|
marci@75
|
325 |
OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
|
marci@75
|
326 |
G=&_G;
|
marci@75
|
327 |
flow=&_flow;
|
marci@75
|
328 |
capacity=&_capacity;
|
marci@133
|
329 |
//out=/*resG->*/G->template first<OldOutEdgeIt>(v);
|
marci@133
|
330 |
G->getFirst(out, v);
|
marci@75
|
331 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
332 |
if (!out.valid()) {
|
marci@75
|
333 |
out_or_in=0;
|
marci@133
|
334 |
//in=/*resG->*/G->template first<OldInEdgeIt>(v);
|
marci@133
|
335 |
G->getFirst(in, v);
|
marci@75
|
336 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
337 |
}
|
marci@75
|
338 |
}
|
marci@75
|
339 |
public:
|
marci@75
|
340 |
OutEdgeIt& operator++() {
|
marci@75
|
341 |
if (out_or_in) {
|
marci@75
|
342 |
NodeIt v=/*resG->*/G->aNode(out);
|
marci@75
|
343 |
++out;
|
marci@75
|
344 |
while( out.valid() && !(free()>0) ) { ++out; }
|
marci@75
|
345 |
if (!out.valid()) {
|
marci@75
|
346 |
out_or_in=0;
|
marci@133
|
347 |
G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
|
marci@75
|
348 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
349 |
}
|
marci@75
|
350 |
} else {
|
marci@75
|
351 |
++in;
|
marci@75
|
352 |
while( in.valid() && !(free()>0) ) { ++in; }
|
marci@75
|
353 |
}
|
marci@75
|
354 |
return *this;
|
marci@75
|
355 |
}
|
marci@75
|
356 |
};
|
marci@75
|
357 |
|
marci@133
|
358 |
class EachEdgeIt : public EdgeIt {
|
marci@133
|
359 |
typename Graph::EachNodeIt v;
|
marci@133
|
360 |
public:
|
marci@133
|
361 |
EachEdgeIt() { }
|
marci@133
|
362 |
//EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
|
marci@133
|
363 |
EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) {
|
marci@133
|
364 |
G=&_G;
|
marci@133
|
365 |
flow=&_flow;
|
marci@133
|
366 |
capacity=&_capacity;
|
marci@133
|
367 |
out_or_in=1;
|
marci@133
|
368 |
G->getFirst(v);
|
marci@133
|
369 |
if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
|
marci@133
|
370 |
while (out.valid() && !(free()>0) ) { ++out; }
|
marci@133
|
371 |
while (v.valid() && !out.valid()) {
|
marci@133
|
372 |
++v;
|
marci@133
|
373 |
if (v.valid()) G->getFirst(out, v);
|
marci@133
|
374 |
while (out.valid() && !(free()>0) ) { ++out; }
|
marci@133
|
375 |
}
|
marci@133
|
376 |
if (!out.valid()) {
|
marci@133
|
377 |
out_or_in=0;
|
marci@133
|
378 |
G->getFirst(v);
|
marci@133
|
379 |
if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
|
marci@133
|
380 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
381 |
while (v.valid() && !in.valid()) {
|
marci@133
|
382 |
++v;
|
marci@133
|
383 |
if (v.valid()) G->getFirst(in, v);
|
marci@133
|
384 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
385 |
}
|
marci@133
|
386 |
}
|
marci@133
|
387 |
}
|
marci@133
|
388 |
EachEdgeIt& operator++() {
|
marci@133
|
389 |
if (out_or_in) {
|
marci@133
|
390 |
++out;
|
marci@133
|
391 |
while (out.valid() && !(free()>0) ) { ++out; }
|
marci@133
|
392 |
while (v.valid() && !out.valid()) {
|
marci@133
|
393 |
++v;
|
marci@133
|
394 |
if (v.valid()) G->getFirst(out, v);
|
marci@133
|
395 |
while (out.valid() && !(free()>0) ) { ++out; }
|
marci@133
|
396 |
}
|
marci@133
|
397 |
if (!out.valid()) {
|
marci@133
|
398 |
out_or_in=0;
|
marci@133
|
399 |
G->getFirst(v);
|
marci@133
|
400 |
if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
|
marci@133
|
401 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
402 |
while (v.valid() && !in.valid()) {
|
marci@133
|
403 |
++v;
|
marci@133
|
404 |
if (v.valid()) G->getFirst(in, v);
|
marci@133
|
405 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
406 |
}
|
marci@133
|
407 |
}
|
marci@133
|
408 |
} else {
|
marci@133
|
409 |
++in;
|
marci@133
|
410 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
411 |
while (v.valid() && !in.valid()) {
|
marci@133
|
412 |
++v;
|
marci@133
|
413 |
if (v.valid()) G->getFirst(in, v);
|
marci@133
|
414 |
while (in.valid() && !(free()>0) ) { ++in; }
|
marci@133
|
415 |
}
|
marci@133
|
416 |
}
|
marci@133
|
417 |
return *this;
|
marci@133
|
418 |
}
|
marci@133
|
419 |
};
|
marci@133
|
420 |
|
marci@75
|
421 |
void getFirst(OutEdgeIt& e, NodeIt v) const {
|
marci@75
|
422 |
e=OutEdgeIt(G, v, flow, capacity);
|
marci@75
|
423 |
}
|
marci@133
|
424 |
void getFirst(EachEdgeIt& e) const {
|
marci@133
|
425 |
e=EachEdgeIt(G, flow, capacity);
|
marci@133
|
426 |
}
|
marci@75
|
427 |
void getFirst(EachNodeIt& v) const { G.getFirst(v); }
|
marci@75
|
428 |
|
marci@75
|
429 |
template< typename It >
|
marci@75
|
430 |
It first() const {
|
marci@75
|
431 |
It e;
|
marci@75
|
432 |
getFirst(e);
|
marci@75
|
433 |
return e;
|
marci@75
|
434 |
}
|
marci@75
|
435 |
|
marci@75
|
436 |
template< typename It >
|
marci@75
|
437 |
It first(NodeIt v) const {
|
marci@75
|
438 |
It e;
|
marci@75
|
439 |
getFirst(e, v);
|
marci@75
|
440 |
return e;
|
marci@75
|
441 |
}
|
marci@75
|
442 |
|
marci@75
|
443 |
NodeIt tail(EdgeIt e) const {
|
marci@75
|
444 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
445 |
NodeIt head(EdgeIt e) const {
|
marci@75
|
446 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@75
|
447 |
|
marci@75
|
448 |
NodeIt aNode(OutEdgeIt e) const {
|
marci@75
|
449 |
return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
|
marci@75
|
450 |
NodeIt bNode(OutEdgeIt e) const {
|
marci@75
|
451 |
return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
|
marci@75
|
452 |
|
marci@75
|
453 |
int id(NodeIt v) const { return G.id(v); }
|
marci@75
|
454 |
|
marci@137
|
455 |
template <typename T>
|
marci@75
|
456 |
class NodeMap {
|
marci@137
|
457 |
typename Graph::NodeMap<T> node_map;
|
marci@75
|
458 |
public:
|
marci@75
|
459 |
NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
|
marci@137
|
460 |
NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(_G.G, a) { }
|
marci@137
|
461 |
void set(NodeIt nit, T a) { node_map.set(nit, a); }
|
marci@137
|
462 |
T get(NodeIt nit) const { return node_map.get(nit); }
|
marci@137
|
463 |
};
|
marci@137
|
464 |
|
marci@137
|
465 |
template <typename T>
|
marci@137
|
466 |
class EdgeMap {
|
marci@137
|
467 |
typename Graph::EdgeMap<T> forward_map, backward_map;
|
marci@137
|
468 |
public:
|
marci@137
|
469 |
EdgeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.G), backward_map(_G.G) { }
|
marci@137
|
470 |
EdgeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.G, a), backward_map(_G.G, a) { }
|
marci@137
|
471 |
void set(EdgeIt e, T a) {
|
marci@137
|
472 |
if (e.out_or_in)
|
marci@137
|
473 |
forward_map.set(e.out, a);
|
marci@137
|
474 |
else
|
marci@137
|
475 |
backward_map.set(e.in, a);
|
marci@137
|
476 |
}
|
marci@137
|
477 |
T get(EdgeIt e) {
|
marci@137
|
478 |
if (e.out_or_in)
|
marci@137
|
479 |
return forward_map.get(e.out);
|
marci@137
|
480 |
else
|
marci@137
|
481 |
return backward_map.get(e.in);
|
marci@137
|
482 |
}
|
marci@43
|
483 |
};
|
marci@43
|
484 |
|
marci@43
|
485 |
};
|
marci@43
|
486 |
|
marci@75
|
487 |
template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@59
|
488 |
class MaxFlow {
|
klao@127
|
489 |
public:
|
marci@43
|
490 |
typedef typename Graph::NodeIt NodeIt;
|
marci@43
|
491 |
typedef typename Graph::EdgeIt EdgeIt;
|
marci@43
|
492 |
typedef typename Graph::EachEdgeIt EachEdgeIt;
|
marci@43
|
493 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
marci@43
|
494 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
klao@127
|
495 |
|
klao@127
|
496 |
private:
|
marci@43
|
497 |
const Graph& G;
|
marci@64
|
498 |
NodeIt s;
|
marci@64
|
499 |
NodeIt t;
|
marci@59
|
500 |
FlowMap& flow;
|
marci@59
|
501 |
const CapacityMap& capacity;
|
marci@75
|
502 |
typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
|
marci@59
|
503 |
typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
|
marci@59
|
504 |
typedef typename AugGraph::EdgeIt AugEdgeIt;
|
marci@75
|
505 |
|
marci@75
|
506 |
//AugGraph res_graph;
|
marci@75
|
507 |
//typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
508 |
//typename AugGraph::NodeMap<AugEdgeIt> pred;
|
marci@133
|
509 |
//typename AugGraph::NodeMap<Number> free;
|
marci@59
|
510 |
public:
|
marci@75
|
511 |
MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
|
marci@75
|
512 |
G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
|
marci@75
|
513 |
//res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
|
marci@75
|
514 |
{ }
|
marci@133
|
515 |
bool augmentOnShortestPath() {
|
marci@59
|
516 |
AugGraph res_graph(G, flow, capacity);
|
marci@59
|
517 |
bool _augment=false;
|
marci@59
|
518 |
|
marci@59
|
519 |
typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
520 |
BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
|
marci@59
|
521 |
res_bfs.pushAndSetReached(s);
|
marci@59
|
522 |
|
marci@59
|
523 |
typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
|
marci@75
|
524 |
//filled up with invalid iterators
|
marci@75
|
525 |
//pred.set(s, AugEdgeIt());
|
marci@59
|
526 |
|
marci@133
|
527 |
typename AugGraph::NodeMap<Number> free(res_graph);
|
marci@59
|
528 |
|
marci@59
|
529 |
//searching for augmenting path
|
marci@59
|
530 |
while ( !res_bfs.finished() ) {
|
marci@59
|
531 |
AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
|
marci@59
|
532 |
if (e.valid() && res_bfs.isBNodeNewlyReached()) {
|
marci@59
|
533 |
NodeIt v=res_graph.tail(e);
|
marci@59
|
534 |
NodeIt w=res_graph.head(e);
|
marci@59
|
535 |
pred.set(w, e);
|
marci@59
|
536 |
if (pred.get(v).valid()) {
|
marci@59
|
537 |
free.set(w, std::min(free.get(v), e.free()));
|
marci@59
|
538 |
} else {
|
marci@59
|
539 |
free.set(w, e.free());
|
marci@59
|
540 |
}
|
marci@59
|
541 |
if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@59
|
542 |
}
|
marci@59
|
543 |
|
marci@59
|
544 |
++res_bfs;
|
marci@59
|
545 |
} //end of searching augmenting path
|
marci@43
|
546 |
|
marci@59
|
547 |
if (_augment) {
|
marci@59
|
548 |
NodeIt n=t;
|
marci@75
|
549 |
Number augment_value=free.get(t);
|
marci@59
|
550 |
while (pred.get(n).valid()) {
|
marci@59
|
551 |
AugEdgeIt e=pred.get(n);
|
marci@59
|
552 |
e.augment(augment_value);
|
marci@59
|
553 |
n=res_graph.tail(e);
|
marci@59
|
554 |
}
|
marci@59
|
555 |
}
|
marci@59
|
556 |
|
marci@59
|
557 |
return _augment;
|
marci@59
|
558 |
}
|
marci@133
|
559 |
template<typename MutableGraph> bool augmentOnBlockingFlow() {
|
marci@133
|
560 |
bool _augment=false;
|
marci@133
|
561 |
|
marci@133
|
562 |
AugGraph res_graph(G, flow, capacity);
|
marci@133
|
563 |
|
marci@133
|
564 |
typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@133
|
565 |
BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
|
marci@133
|
566 |
|
marci@100
|
567 |
bfs.pushAndSetReached(s);
|
marci@133
|
568 |
typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
|
marci@100
|
569 |
while ( !bfs.finished() ) {
|
marci@133
|
570 |
AugOutEdgeIt e=AugOutEdgeIt(bfs);
|
marci@100
|
571 |
if (e.valid() && bfs.isBNodeNewlyReached()) {
|
marci@133
|
572 |
dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
|
marci@100
|
573 |
}
|
marci@100
|
574 |
|
marci@100
|
575 |
++bfs;
|
marci@133
|
576 |
} //computing distances from s in the residual graph
|
marci@100
|
577 |
|
marci@100
|
578 |
MutableGraph F;
|
marci@133
|
579 |
typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
|
marci@133
|
580 |
for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); n.valid(); ++n) {
|
marci@133
|
581 |
res_graph_to_F.set(n, F.addNode());
|
marci@100
|
582 |
}
|
marci@133
|
583 |
|
marci@133
|
584 |
typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
|
marci@133
|
585 |
typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
|
marci@133
|
586 |
|
marci@133
|
587 |
typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
|
marci@133
|
588 |
typename MutableGraph::EdgeMap<Number> free_on_edge(F);
|
marci@133
|
589 |
|
marci@133
|
590 |
//Making F to the graph containing the edges of the residual graph
|
marci@133
|
591 |
//which are in some shortest paths
|
marci@133
|
592 |
for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); e.valid(); ++e) {
|
marci@133
|
593 |
if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
|
marci@133
|
594 |
typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
|
marci@133
|
595 |
original_edge.update();
|
marci@133
|
596 |
original_edge.set(f, e);
|
marci@133
|
597 |
free_on_edge.update();
|
marci@133
|
598 |
free_on_edge.set(f, e.free());
|
marci@133
|
599 |
}
|
marci@133
|
600 |
}
|
marci@133
|
601 |
|
marci@133
|
602 |
bool __augment=true;
|
marci@133
|
603 |
|
marci@133
|
604 |
while (__augment) {
|
marci@133
|
605 |
__augment=false;
|
marci@133
|
606 |
//computing blocking flow with dfs
|
marci@133
|
607 |
typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
|
marci@133
|
608 |
DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
|
marci@133
|
609 |
typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
|
marci@133
|
610 |
typename MutableGraph::NodeMap<Number> free(F);
|
marci@133
|
611 |
|
marci@133
|
612 |
dfs.pushAndSetReached(sF);
|
marci@133
|
613 |
while (!dfs.finished()) {
|
marci@133
|
614 |
++dfs;
|
marci@133
|
615 |
if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
|
marci@133
|
616 |
//std::cout << "OutEdgeIt: " << dfs;
|
marci@133
|
617 |
//std::cout << " aNode: " << F.aNode(dfs);
|
marci@133
|
618 |
//std::cout << " bNode: " << F.bNode(dfs) << " ";
|
marci@133
|
619 |
|
marci@133
|
620 |
typename MutableGraph::NodeIt v=F.aNode(dfs);
|
marci@133
|
621 |
typename MutableGraph::NodeIt w=F.bNode(dfs);
|
marci@133
|
622 |
pred.set(w, dfs);
|
marci@133
|
623 |
if (pred.get(v).valid()) {
|
marci@133
|
624 |
free.set(w, std::min(free.get(v), free_on_edge.get(dfs)));
|
marci@133
|
625 |
} else {
|
marci@133
|
626 |
free.set(w, free_on_edge.get(dfs)/*original_edge.get(dfs).free()*/);
|
marci@133
|
627 |
}
|
marci@133
|
628 |
if (w==tF) {
|
marci@133
|
629 |
//std::cout << "AUGMENTATION"<<std::endl;
|
marci@133
|
630 |
__augment=true;
|
marci@133
|
631 |
_augment=true;
|
marci@133
|
632 |
break;
|
marci@133
|
633 |
}
|
marci@133
|
634 |
} else {
|
marci@133
|
635 |
//std::cout << "OutEdgeIt: " << "invalid";
|
marci@133
|
636 |
//std::cout << " aNode: " << dfs.aNode();
|
marci@133
|
637 |
//std::cout << " bNode: " << "invalid" << " ";
|
marci@133
|
638 |
}
|
marci@133
|
639 |
if (dfs.isBNodeNewlyReached()) {
|
marci@133
|
640 |
//std::cout << "bNodeIsNewlyReached ";
|
marci@133
|
641 |
} else {
|
marci@133
|
642 |
//std::cout << "bNodeIsNotNewlyReached ";
|
marci@133
|
643 |
if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
|
marci@133
|
644 |
//std::cout << "DELETE ";
|
marci@133
|
645 |
F.erase(typename MutableGraph::OutEdgeIt(dfs));
|
marci@133
|
646 |
}
|
marci@133
|
647 |
}
|
marci@133
|
648 |
//if (dfs.isANodeExamined()) {
|
marci@133
|
649 |
//std::cout << "aNodeIsExamined ";
|
marci@133
|
650 |
//} else {
|
marci@133
|
651 |
//std::cout << "aNodeIsNotExamined ";
|
marci@133
|
652 |
//}
|
marci@133
|
653 |
//std::cout<<std::endl;
|
marci@100
|
654 |
}
|
marci@133
|
655 |
|
marci@133
|
656 |
if (__augment) {
|
marci@133
|
657 |
typename MutableGraph::NodeIt n=tF;
|
marci@133
|
658 |
Number augment_value=free.get(tF);
|
marci@133
|
659 |
while (pred.get(n).valid()) {
|
marci@133
|
660 |
typename MutableGraph::EdgeIt e=pred.get(n);
|
marci@133
|
661 |
original_edge.get(e).augment(augment_value);
|
marci@133
|
662 |
n=F.tail(e);
|
marci@135
|
663 |
if (free_on_edge.get(e)==augment_value)
|
marci@135
|
664 |
F.erase(e);
|
marci@135
|
665 |
else
|
marci@135
|
666 |
free_on_edge.set(e, free_on_edge.get(e)-augment_value);
|
marci@133
|
667 |
}
|
marci@133
|
668 |
}
|
marci@135
|
669 |
|
marci@100
|
670 |
}
|
marci@133
|
671 |
|
marci@133
|
672 |
return _augment;
|
marci@100
|
673 |
}
|
marci@43
|
674 |
void run() {
|
marci@100
|
675 |
//int num_of_augmentations=0;
|
marci@133
|
676 |
while (augmentOnShortestPath()) {
|
marci@133
|
677 |
//while (augmentOnBlockingFlow<MutableGraph>()) {
|
marci@133
|
678 |
//std::cout << ++num_of_augmentations << " ";
|
marci@133
|
679 |
//std::cout<<std::endl;
|
marci@133
|
680 |
}
|
marci@133
|
681 |
}
|
marci@133
|
682 |
template<typename MutableGraph> void run() {
|
marci@133
|
683 |
//int num_of_augmentations=0;
|
marci@133
|
684 |
//while (augmentOnShortestPath()) {
|
marci@133
|
685 |
while (augmentOnBlockingFlow<MutableGraph>()) {
|
marci@100
|
686 |
//std::cout << ++num_of_augmentations << " ";
|
marci@100
|
687 |
//std::cout<<std::endl;
|
marci@100
|
688 |
}
|
marci@43
|
689 |
}
|
marci@75
|
690 |
Number flowValue() {
|
marci@75
|
691 |
Number a=0;
|
marci@69
|
692 |
for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
|
marci@69
|
693 |
a+=flow.get(i);
|
marci@69
|
694 |
}
|
marci@69
|
695 |
return a;
|
marci@69
|
696 |
}
|
marci@43
|
697 |
};
|
marci@43
|
698 |
|
marci@75
|
699 |
|
marci@75
|
700 |
/*
|
marci@75
|
701 |
template <typename Graph>
|
marci@75
|
702 |
class IteratorBoolNodeMap {
|
marci@75
|
703 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
704 |
typedef typename Graph::EachNodeIt EachNodeIt;
|
marci@75
|
705 |
class BoolItem {
|
marci@75
|
706 |
public:
|
marci@75
|
707 |
bool value;
|
marci@75
|
708 |
NodeIt prev;
|
marci@75
|
709 |
NodeIt next;
|
marci@75
|
710 |
BoolItem() : value(false), prev(), next() {}
|
marci@75
|
711 |
};
|
marci@75
|
712 |
NodeIt first_true;
|
marci@75
|
713 |
//NodeIt last_true;
|
marci@75
|
714 |
NodeIt first_false;
|
marci@75
|
715 |
//NodeIt last_false;
|
marci@75
|
716 |
const Graph& G;
|
marci@75
|
717 |
typename Graph::NodeMap<BoolItem> container;
|
marci@75
|
718 |
public:
|
marci@75
|
719 |
typedef bool ValueType;
|
marci@75
|
720 |
typedef NodeIt KeyType;
|
marci@75
|
721 |
IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
|
marci@75
|
722 |
//for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
|
marci@75
|
723 |
//BoolItem b=container.get(e);
|
marci@75
|
724 |
//b.me=e;
|
marci@75
|
725 |
//container.set(b);
|
marci@75
|
726 |
//}
|
marci@75
|
727 |
G.getFirst(first_false);
|
marci@75
|
728 |
NodeIt e_prev;
|
marci@75
|
729 |
for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
|
marci@75
|
730 |
container[e].prev=e_prev;
|
marci@75
|
731 |
if (e_prev.valid()) container[e_prev].next=e;
|
marci@75
|
732 |
e_prev=e;
|
marci@75
|
733 |
}
|
marci@75
|
734 |
}
|
marci@75
|
735 |
//NodeMap(const Graph& _G, T a) :
|
marci@75
|
736 |
// G(_G), container(G.node_id, a) { }
|
marci@75
|
737 |
//FIXME
|
marci@75
|
738 |
void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
|
marci@75
|
739 |
T get(NodeIt nit) const { return container[G.id(nit)]; }
|
alpar@107
|
740 |
//void update() { container.resize(G.node_id); }
|
alpar@107
|
741 |
//void update(T a) { container.resize(G.node_id, a); }
|
marci@75
|
742 |
};
|
marci@75
|
743 |
*/
|
marci@75
|
744 |
|
marci@75
|
745 |
|
marci@75
|
746 |
template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
|
marci@75
|
747 |
class MaxFlow2 {
|
klao@127
|
748 |
public:
|
marci@75
|
749 |
typedef typename Graph::NodeIt NodeIt;
|
marci@75
|
750 |
typedef typename Graph::EdgeIt EdgeIt;
|
marci@75
|
751 |
typedef typename Graph::EachEdgeIt EachEdgeIt;
|
marci@75
|
752 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
marci@75
|
753 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
klao@127
|
754 |
private:
|
marci@75
|
755 |
const Graph& G;
|
marci@75
|
756 |
std::list<NodeIt>& S;
|
marci@75
|
757 |
std::list<NodeIt>& T;
|
marci@75
|
758 |
FlowMap& flow;
|
marci@75
|
759 |
const CapacityMap& capacity;
|
marci@75
|
760 |
typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
|
marci@75
|
761 |
typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
|
marci@75
|
762 |
typedef typename AugGraph::EdgeIt AugEdgeIt;
|
marci@75
|
763 |
typename Graph::NodeMap<bool> SMap;
|
marci@75
|
764 |
typename Graph::NodeMap<bool> TMap;
|
marci@75
|
765 |
public:
|
marci@75
|
766 |
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
|
767 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
768 |
i!=S.end(); ++i) {
|
marci@75
|
769 |
SMap.set(*i, true);
|
marci@75
|
770 |
}
|
marci@75
|
771 |
for (typename std::list<NodeIt>::const_iterator i=T.begin();
|
marci@75
|
772 |
i!=T.end(); ++i) {
|
marci@75
|
773 |
TMap.set(*i, true);
|
marci@75
|
774 |
}
|
marci@75
|
775 |
}
|
marci@75
|
776 |
bool augment() {
|
marci@75
|
777 |
AugGraph res_graph(G, flow, capacity);
|
marci@75
|
778 |
bool _augment=false;
|
marci@75
|
779 |
NodeIt reached_t_node;
|
marci@75
|
780 |
|
marci@75
|
781 |
typedef typename AugGraph::NodeMap<bool> ReachedMap;
|
marci@75
|
782 |
BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
|
marci@75
|
783 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
784 |
i!=S.end(); ++i) {
|
marci@75
|
785 |
res_bfs.pushAndSetReached(*i);
|
marci@75
|
786 |
}
|
marci@75
|
787 |
//res_bfs.pushAndSetReached(s);
|
marci@75
|
788 |
|
marci@75
|
789 |
typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
|
marci@75
|
790 |
//filled up with invalid iterators
|
marci@75
|
791 |
|
marci@133
|
792 |
typename AugGraph::NodeMap<Number> free(res_graph);
|
marci@75
|
793 |
|
marci@75
|
794 |
//searching for augmenting path
|
marci@75
|
795 |
while ( !res_bfs.finished() ) {
|
marci@75
|
796 |
AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
|
marci@75
|
797 |
if (e.valid() && res_bfs.isBNodeNewlyReached()) {
|
marci@75
|
798 |
NodeIt v=res_graph.tail(e);
|
marci@75
|
799 |
NodeIt w=res_graph.head(e);
|
marci@75
|
800 |
pred.set(w, e);
|
marci@75
|
801 |
if (pred.get(v).valid()) {
|
marci@75
|
802 |
free.set(w, std::min(free.get(v), e.free()));
|
marci@75
|
803 |
} else {
|
marci@75
|
804 |
free.set(w, e.free());
|
marci@75
|
805 |
}
|
marci@75
|
806 |
if (TMap.get(res_graph.head(e))) {
|
marci@75
|
807 |
_augment=true;
|
marci@75
|
808 |
reached_t_node=res_graph.head(e);
|
marci@75
|
809 |
break;
|
marci@75
|
810 |
}
|
marci@75
|
811 |
}
|
marci@75
|
812 |
|
marci@75
|
813 |
++res_bfs;
|
marci@75
|
814 |
} //end of searching augmenting path
|
marci@75
|
815 |
|
marci@75
|
816 |
if (_augment) {
|
marci@75
|
817 |
NodeIt n=reached_t_node;
|
marci@75
|
818 |
Number augment_value=free.get(reached_t_node);
|
marci@75
|
819 |
while (pred.get(n).valid()) {
|
marci@75
|
820 |
AugEdgeIt e=pred.get(n);
|
marci@75
|
821 |
e.augment(augment_value);
|
marci@75
|
822 |
n=res_graph.tail(e);
|
marci@75
|
823 |
}
|
marci@75
|
824 |
}
|
marci@75
|
825 |
|
marci@75
|
826 |
return _augment;
|
marci@75
|
827 |
}
|
marci@75
|
828 |
void run() {
|
marci@75
|
829 |
while (augment()) { }
|
marci@75
|
830 |
}
|
marci@75
|
831 |
Number flowValue() {
|
marci@75
|
832 |
Number a=0;
|
marci@75
|
833 |
for(typename std::list<NodeIt>::const_iterator i=S.begin();
|
marci@75
|
834 |
i!=S.end(); ++i) {
|
marci@75
|
835 |
for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
|
marci@75
|
836 |
a+=flow.get(e);
|
marci@75
|
837 |
}
|
marci@75
|
838 |
for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
|
marci@75
|
839 |
a-=flow.get(e);
|
marci@75
|
840 |
}
|
marci@75
|
841 |
}
|
marci@75
|
842 |
return a;
|
marci@75
|
843 |
}
|
marci@75
|
844 |
};
|
marci@75
|
845 |
|
marci@75
|
846 |
|
marci@75
|
847 |
|
alpar@107
|
848 |
} // namespace hugo
|
marci@43
|
849 |
|
marci@59
|
850 |
#endif //EDMONDS_KARP_HH
|