.
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>
245 typedef typename Graph::NodeIt NodeIt;
246 typedef typename Graph::EachNodeIt EachNodeIt;
247 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
248 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
249 typedef typename Graph::InEdgeIt OldInEdgeIt;
253 const CapacityMap& capacity;
255 ResGraph3(const Graph& _G, FlowMap& _flow,
256 const CapacityMap& _capacity) :
257 G(_G), flow(_flow), capacity(_capacity) { }
262 friend class OutEdgeIt;
265 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
267 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
270 const CapacityMap* capacity;
274 bool out_or_in; //1, iff out
276 EdgeIt() : out_or_in(1) { }
277 Number free() const {
279 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
281 return (/*resG->*/flow->get(in));
285 return out_or_in && out.valid() || in.valid(); }
286 void augment(Number a) const {
288 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
290 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
295 class OutEdgeIt : public EdgeIt {
296 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
300 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
304 out=/*resG->*/G->template first<OldOutEdgeIt>(v);
305 while( out.valid() && !(free()>0) ) { ++out; }
308 in=/*resG->*/G->template first<OldInEdgeIt>(v);
309 while( in.valid() && !(free()>0) ) { ++in; }
313 OutEdgeIt& operator++() {
315 NodeIt v=/*resG->*/G->aNode(out);
317 while( out.valid() && !(free()>0) ) { ++out; }
320 in=/*resG->*/G->template first<OldInEdgeIt>(v);
321 while( in.valid() && !(free()>0) ) { ++in; }
325 while( in.valid() && !(free()>0) ) { ++in; }
331 void getFirst(OutEdgeIt& e, NodeIt v) const {
332 e=OutEdgeIt(G, v, flow, capacity);
334 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
336 template< typename It >
343 template< typename It >
344 It first(NodeIt v) const {
350 NodeIt tail(EdgeIt e) const {
351 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
352 NodeIt head(EdgeIt e) const {
353 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
355 NodeIt aNode(OutEdgeIt e) const {
356 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
357 NodeIt bNode(OutEdgeIt e) const {
358 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
360 int id(NodeIt v) const { return G.id(v); }
362 template <typename S>
364 typename Graph::NodeMap<S> node_map;
366 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
367 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
368 void set(NodeIt nit, S a) { node_map.set(nit, a); }
369 S get(NodeIt nit) const { return node_map.get(nit); }
375 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
377 typedef typename Graph::NodeIt NodeIt;
378 typedef typename Graph::EdgeIt EdgeIt;
379 typedef typename Graph::EachEdgeIt EachEdgeIt;
380 typedef typename Graph::OutEdgeIt OutEdgeIt;
381 typedef typename Graph::InEdgeIt InEdgeIt;
386 const CapacityMap& capacity;
387 typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
388 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
389 typedef typename AugGraph::EdgeIt AugEdgeIt;
391 //AugGraph res_graph;
392 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
393 //typename AugGraph::NodeMap<AugEdgeIt> pred;
394 //typename AugGraph::NodeMap<int> free;
396 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
397 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
398 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
401 AugGraph res_graph(G, flow, capacity);
404 typedef typename AugGraph::NodeMap<bool> ReachedMap;
405 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
406 res_bfs.pushAndSetReached(s);
408 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
409 //filled up with invalid iterators
410 //pred.set(s, AugEdgeIt());
412 typename AugGraph::NodeMap<int> free(res_graph);
414 //searching for augmenting path
415 while ( !res_bfs.finished() ) {
416 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
417 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
418 NodeIt v=res_graph.tail(e);
419 NodeIt w=res_graph.head(e);
421 if (pred.get(v).valid()) {
422 free.set(w, std::min(free.get(v), e.free()));
424 free.set(w, e.free());
426 if (res_graph.head(e)==t) { _augment=true; break; }
430 } //end of searching augmenting path
434 Number augment_value=free.get(t);
435 while (pred.get(n).valid()) {
436 AugEdgeIt e=pred.get(n);
437 e.augment(augment_value);
445 while (augment()) { }
449 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
458 template <typename Graph>
459 class IteratorBoolNodeMap {
460 typedef typename Graph::NodeIt NodeIt;
461 typedef typename Graph::EachNodeIt EachNodeIt;
467 BoolItem() : value(false), prev(), next() {}
474 typename Graph::NodeMap<BoolItem> container;
476 typedef bool ValueType;
477 typedef NodeIt KeyType;
478 IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
479 //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
480 //BoolItem b=container.get(e);
484 G.getFirst(first_false);
486 for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
487 container[e].prev=e_prev;
488 if (e_prev.valid()) container[e_prev].next=e;
492 //NodeMap(const Graph& _G, T a) :
493 // G(_G), container(G.node_id, a) { }
495 void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
496 T get(NodeIt nit) const { return container[G.id(nit)]; }
497 //void resize() { container.resize(G.node_id); }
498 //void resize(T a) { container.resize(G.node_id, a); }
503 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
505 typedef typename Graph::NodeIt NodeIt;
506 typedef typename Graph::EdgeIt EdgeIt;
507 typedef typename Graph::EachEdgeIt EachEdgeIt;
508 typedef typename Graph::OutEdgeIt OutEdgeIt;
509 typedef typename Graph::InEdgeIt InEdgeIt;
511 std::list<NodeIt>& S;
512 std::list<NodeIt>& T;
514 const CapacityMap& capacity;
515 typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
516 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
517 typedef typename AugGraph::EdgeIt AugEdgeIt;
518 typename Graph::NodeMap<bool> SMap;
519 typename Graph::NodeMap<bool> TMap;
521 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) {
522 for(typename std::list<NodeIt>::const_iterator i=S.begin();
526 for (typename std::list<NodeIt>::const_iterator i=T.begin();
532 AugGraph res_graph(G, flow, capacity);
534 NodeIt reached_t_node;
536 typedef typename AugGraph::NodeMap<bool> ReachedMap;
537 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
538 for(typename std::list<NodeIt>::const_iterator i=S.begin();
540 res_bfs.pushAndSetReached(*i);
542 //res_bfs.pushAndSetReached(s);
544 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
545 //filled up with invalid iterators
547 typename AugGraph::NodeMap<int> free(res_graph);
549 //searching for augmenting path
550 while ( !res_bfs.finished() ) {
551 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
552 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
553 NodeIt v=res_graph.tail(e);
554 NodeIt w=res_graph.head(e);
556 if (pred.get(v).valid()) {
557 free.set(w, std::min(free.get(v), e.free()));
559 free.set(w, e.free());
561 if (TMap.get(res_graph.head(e))) {
563 reached_t_node=res_graph.head(e);
569 } //end of searching augmenting path
572 NodeIt n=reached_t_node;
573 Number augment_value=free.get(reached_t_node);
574 while (pred.get(n).valid()) {
575 AugEdgeIt e=pred.get(n);
576 e.augment(augment_value);
584 while (augment()) { }
588 for(typename std::list<NodeIt>::const_iterator i=S.begin();
590 for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
593 for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
605 #endif //EDMONDS_KARP_HH