1 #ifndef EDMONDS_KARP_HH
2 #define EDMONDS_KARP_HH
8 #include <bfs_iterator.hh>
12 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
14 typedef typename Graph::NodeIt NodeIt;
15 typedef typename Graph::EachNodeIt EachNodeIt;
16 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
19 const CapacityMap& capacity;
21 ResGraph(const Graph& _G, FlowMap& _flow,
22 const CapacityMap& _capacity) :
23 G(_G), flow(_flow), capacity(_capacity) { }
28 friend class OutEdgeIt;
31 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
33 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
37 //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
39 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
40 return (resG->capacity.get(sym)-resG->flow.get(sym));
42 return (resG->flow.get(sym));
45 bool valid() const { return sym.valid(); }
46 void augment(Number a) const {
47 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
48 resG->flow.set(sym, resG->flow.get(sym)+a);
51 resG->flow.set(sym, resG->flow.get(sym)-a);
57 class OutEdgeIt : public EdgeIt {
58 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
61 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
63 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
65 sym=resG->G.template first<OldSymEdgeIt>(v);
66 while( sym.valid() && !(free()>0) ) { ++sym; }
69 OutEdgeIt& operator++() {
71 while( sym.valid() && !(free()>0) ) { ++sym; }
76 void getFirst(OutEdgeIt& e, NodeIt v) const {
77 e=OutEdgeIt(*this, v);
79 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
81 template< typename It >
88 template< typename It >
89 It first(NodeIt v) const {
95 NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
96 NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
98 NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
99 NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
101 int id(NodeIt v) const { return G.id(v); }
103 template <typename S>
105 typename Graph::NodeMap<S> node_map;
107 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
108 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
109 void set(NodeIt nit, S a) { node_map.set(nit, a); }
110 S get(NodeIt nit) const { return node_map.get(nit); }
111 S& operator[](NodeIt nit) { return node_map[nit]; }
112 const S& operator[](NodeIt nit) const { return node_map[nit]; }
118 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
120 typedef typename Graph::NodeIt NodeIt;
121 typedef typename Graph::EachNodeIt EachNodeIt;
122 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
123 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
124 typedef typename Graph::InEdgeIt OldInEdgeIt;
128 const CapacityMap& capacity;
130 ResGraph2(const Graph& _G, FlowMap& _flow,
131 const CapacityMap& _capacity) :
132 G(_G), flow(_flow), capacity(_capacity) { }
137 friend class OutEdgeIt;
140 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
142 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
146 bool out_or_in; //1, iff out
148 EdgeIt() : out_or_in(1) { }
149 Number free() const {
151 return (resG->capacity.get(out)-resG->flow.get(out));
153 return (resG->flow.get(in));
157 return out_or_in && out.valid() || in.valid(); }
158 void augment(Number a) const {
160 resG->flow.set(out, resG->flow.get(out)+a);
162 resG->flow.set(in, resG->flow.get(in)-a);
167 class OutEdgeIt : public EdgeIt {
168 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
172 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
174 out=resG->G.template first<OldOutEdgeIt>(v);
175 while( out.valid() && !(free()>0) ) { ++out; }
178 in=resG->G.template first<OldInEdgeIt>(v);
179 while( in.valid() && !(free()>0) ) { ++in; }
183 OutEdgeIt& operator++() {
185 NodeIt v=resG->G.aNode(out);
187 while( out.valid() && !(free()>0) ) { ++out; }
190 in=resG->G.template first<OldInEdgeIt>(v);
191 while( in.valid() && !(free()>0) ) { ++in; }
195 while( in.valid() && !(free()>0) ) { ++in; }
201 void getFirst(OutEdgeIt& e, NodeIt v) const {
202 e=OutEdgeIt(*this, v);
204 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
206 template< typename It >
213 template< typename It >
214 It first(NodeIt v) const {
220 NodeIt tail(EdgeIt e) const {
221 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
222 NodeIt head(EdgeIt e) const {
223 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
225 NodeIt aNode(OutEdgeIt e) const {
226 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
227 NodeIt bNode(OutEdgeIt e) const {
228 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
230 int id(NodeIt v) const { return G.id(v); }
232 template <typename S>
234 typename Graph::NodeMap<S> node_map;
236 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
237 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
238 void set(NodeIt nit, S a) { node_map.set(nit, a); }
239 S get(NodeIt nit) const { return node_map.get(nit); }
243 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
246 typedef typename Graph::NodeIt NodeIt;
247 typedef typename Graph::EachNodeIt EachNodeIt;
248 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
249 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
250 typedef typename Graph::InEdgeIt OldInEdgeIt;
255 const CapacityMap& capacity;
257 ResGraph3(const Graph& _G, FlowMap& _flow,
258 const CapacityMap& _capacity) :
259 G(_G), flow(_flow), capacity(_capacity) { }
264 friend class OutEdgeIt;
267 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
269 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
272 const CapacityMap* capacity;
276 bool out_or_in; //1, iff out
278 EdgeIt() : out_or_in(1) { }
279 Number free() const {
281 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
283 return (/*resG->*/flow->get(in));
287 return out_or_in && out.valid() || in.valid(); }
288 void augment(Number a) const {
290 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
292 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
297 class OutEdgeIt : public EdgeIt {
298 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
302 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
306 out=/*resG->*/G->template first<OldOutEdgeIt>(v);
307 while( out.valid() && !(free()>0) ) { ++out; }
310 in=/*resG->*/G->template first<OldInEdgeIt>(v);
311 while( in.valid() && !(free()>0) ) { ++in; }
315 OutEdgeIt& operator++() {
317 NodeIt v=/*resG->*/G->aNode(out);
319 while( out.valid() && !(free()>0) ) { ++out; }
322 in=/*resG->*/G->template first<OldInEdgeIt>(v);
323 while( in.valid() && !(free()>0) ) { ++in; }
327 while( in.valid() && !(free()>0) ) { ++in; }
333 void getFirst(OutEdgeIt& e, NodeIt v) const {
334 e=OutEdgeIt(G, v, flow, capacity);
336 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
338 template< typename It >
345 template< typename It >
346 It first(NodeIt v) const {
352 NodeIt tail(EdgeIt e) const {
353 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
354 NodeIt head(EdgeIt e) const {
355 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
357 NodeIt aNode(OutEdgeIt e) const {
358 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
359 NodeIt bNode(OutEdgeIt e) const {
360 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
362 int id(NodeIt v) const { return G.id(v); }
364 template <typename S>
366 typename Graph::NodeMap<S> node_map;
368 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
369 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
370 void set(NodeIt nit, S a) { node_map.set(nit, a); }
371 S get(NodeIt nit) const { return node_map.get(nit); }
377 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
379 typedef typename Graph::NodeIt NodeIt;
380 typedef typename Graph::EdgeIt EdgeIt;
381 typedef typename Graph::EachEdgeIt EachEdgeIt;
382 typedef typename Graph::OutEdgeIt OutEdgeIt;
383 typedef typename Graph::InEdgeIt InEdgeIt;
388 const CapacityMap& capacity;
389 typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
390 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
391 typedef typename AugGraph::EdgeIt AugEdgeIt;
393 //AugGraph res_graph;
394 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
395 //typename AugGraph::NodeMap<AugEdgeIt> pred;
396 //typename AugGraph::NodeMap<int> free;
398 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
399 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
400 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
403 AugGraph res_graph(G, flow, capacity);
406 typedef typename AugGraph::NodeMap<bool> ReachedMap;
407 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
408 res_bfs.pushAndSetReached(s);
410 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
411 //filled up with invalid iterators
412 //pred.set(s, AugEdgeIt());
414 typename AugGraph::NodeMap<int> free(res_graph);
416 //searching for augmenting path
417 while ( !res_bfs.finished() ) {
418 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
419 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
420 NodeIt v=res_graph.tail(e);
421 NodeIt w=res_graph.head(e);
423 if (pred.get(v).valid()) {
424 free.set(w, std::min(free.get(v), e.free()));
426 free.set(w, e.free());
428 if (res_graph.head(e)==t) { _augment=true; break; }
432 } //end of searching augmenting path
436 Number augment_value=free.get(t);
437 while (pred.get(n).valid()) {
438 AugEdgeIt e=pred.get(n);
439 e.augment(augment_value);
447 while (augment()) { }
451 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
460 template <typename Graph>
461 class IteratorBoolNodeMap {
462 typedef typename Graph::NodeIt NodeIt;
463 typedef typename Graph::EachNodeIt EachNodeIt;
469 BoolItem() : value(false), prev(), next() {}
476 typename Graph::NodeMap<BoolItem> container;
478 typedef bool ValueType;
479 typedef NodeIt KeyType;
480 IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
481 //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
482 //BoolItem b=container.get(e);
486 G.getFirst(first_false);
488 for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
489 container[e].prev=e_prev;
490 if (e_prev.valid()) container[e_prev].next=e;
494 //NodeMap(const Graph& _G, T a) :
495 // G(_G), container(G.node_id, a) { }
497 void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
498 T get(NodeIt nit) const { return container[G.id(nit)]; }
499 //void resize() { container.resize(G.node_id); }
500 //void resize(T a) { container.resize(G.node_id, a); }
505 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
507 typedef typename Graph::NodeIt NodeIt;
508 typedef typename Graph::EdgeIt EdgeIt;
509 typedef typename Graph::EachEdgeIt EachEdgeIt;
510 typedef typename Graph::OutEdgeIt OutEdgeIt;
511 typedef typename Graph::InEdgeIt InEdgeIt;
513 std::list<NodeIt>& S;
514 std::list<NodeIt>& T;
516 const CapacityMap& capacity;
517 typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
518 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
519 typedef typename AugGraph::EdgeIt AugEdgeIt;
520 typename Graph::NodeMap<bool> SMap;
521 typename Graph::NodeMap<bool> TMap;
523 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) {
524 for(typename std::list<NodeIt>::const_iterator i=S.begin();
528 for (typename std::list<NodeIt>::const_iterator i=T.begin();
534 AugGraph res_graph(G, flow, capacity);
536 NodeIt reached_t_node;
538 typedef typename AugGraph::NodeMap<bool> ReachedMap;
539 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
540 for(typename std::list<NodeIt>::const_iterator i=S.begin();
542 res_bfs.pushAndSetReached(*i);
544 //res_bfs.pushAndSetReached(s);
546 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
547 //filled up with invalid iterators
549 typename AugGraph::NodeMap<int> free(res_graph);
551 //searching for augmenting path
552 while ( !res_bfs.finished() ) {
553 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
554 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
555 NodeIt v=res_graph.tail(e);
556 NodeIt w=res_graph.head(e);
558 if (pred.get(v).valid()) {
559 free.set(w, std::min(free.get(v), e.free()));
561 free.set(w, e.free());
563 if (TMap.get(res_graph.head(e))) {
565 reached_t_node=res_graph.head(e);
571 } //end of searching augmenting path
574 NodeIt n=reached_t_node;
575 Number augment_value=free.get(reached_t_node);
576 while (pred.get(n).valid()) {
577 AugEdgeIt e=pred.get(n);
578 e.augment(augment_value);
586 while (augment()) { }
590 for(typename std::list<NodeIt>::const_iterator i=S.begin();
592 for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
595 for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
607 #endif //EDMONDS_KARP_HH