2 #ifndef HUGO_EDMONDS_KARP_H
3 #define HUGO_EDMONDS_KARP_H
9 #include <bfs_iterator.h>
14 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
17 typedef typename Graph::Node Node;
18 typedef typename Graph::NodeIt NodeIt;
20 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
23 const CapacityMap& capacity;
25 ResGraph(const Graph& _G, FlowMap& _flow,
26 const CapacityMap& _capacity) :
27 G(_G), flow(_flow), capacity(_capacity) { }
32 friend class OutEdgeIt;
35 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
37 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
41 //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
43 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
44 return (resG->capacity.get(sym)-resG->flow.get(sym));
46 return (resG->flow.get(sym));
49 bool valid() const { return sym.valid(); }
50 void augment(Number a) const {
51 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
52 resG->flow.set(sym, resG->flow.get(sym)+a);
55 resG->flow.set(sym, resG->flow.get(sym)-a);
61 class OutEdgeIt : public Edge {
62 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
65 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
67 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
69 sym=resG->G.template first<OldSymEdgeIt>(v);
70 while( sym.valid() && !(free()>0) ) { ++sym; }
73 OutEdgeIt& operator++() {
75 while( sym.valid() && !(free()>0) ) { ++sym; }
80 void /*getF*/first(OutEdgeIt& e, Node v) const {
81 e=OutEdgeIt(*this, v);
83 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
85 template< typename It >
92 template< typename It >
93 It first(Node v) const {
99 Node tail(Edge e) const { return G.aNode(e.sym); }
100 Node head(Edge e) const { return G.bNode(e.sym); }
102 Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
103 Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
105 int id(Node v) const { return G.id(v); }
107 template <typename S>
109 typename Graph::NodeMap<S> node_map;
111 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
112 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
113 void set(Node nit, S a) { node_map.set(nit, a); }
114 S get(Node nit) const { return node_map.get(nit); }
115 S& operator[](Node nit) { return node_map[nit]; }
116 const S& operator[](Node nit) const { return node_map[nit]; }
122 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
125 typedef typename Graph::Node Node;
126 typedef typename Graph::NodeIt NodeIt;
128 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
129 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
130 typedef typename Graph::InEdgeIt OldInEdgeIt;
134 const CapacityMap& capacity;
136 ResGraph2(const Graph& _G, FlowMap& _flow,
137 const CapacityMap& _capacity) :
138 G(_G), flow(_flow), capacity(_capacity) { }
143 friend class OutEdgeIt;
146 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
148 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
152 bool out_or_in; //true, iff out
154 Edge() : out_or_in(true) { }
155 Number free() const {
157 return (resG->capacity.get(out)-resG->flow.get(out));
159 return (resG->flow.get(in));
163 return out_or_in && out.valid() || in.valid(); }
164 void augment(Number a) const {
166 resG->flow.set(out, resG->flow.get(out)+a);
168 resG->flow.set(in, resG->flow.get(in)-a);
173 class OutEdgeIt : public Edge {
174 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
178 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
180 out=resG->G.template first<OldOutEdgeIt>(v);
181 while( out.valid() && !(free()>0) ) { ++out; }
184 in=resG->G.template first<OldInEdgeIt>(v);
185 while( in.valid() && !(free()>0) ) { ++in; }
189 OutEdgeIt& operator++() {
191 Node v=resG->G.aNode(out);
193 while( out.valid() && !(free()>0) ) { ++out; }
196 in=resG->G.template first<OldInEdgeIt>(v);
197 while( in.valid() && !(free()>0) ) { ++in; }
201 while( in.valid() && !(free()>0) ) { ++in; }
207 void /*getF*/first(OutEdgeIt& e, Node v) const {
208 e=OutEdgeIt(*this, v);
210 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
212 template< typename It >
219 template< typename It >
220 It first(Node v) const {
226 Node tail(Edge e) const {
227 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
228 Node head(Edge e) const {
229 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
231 Node aNode(OutEdgeIt e) const {
232 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
233 Node bNode(OutEdgeIt e) const {
234 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
236 int id(Node v) const { return G.id(v); }
238 template <typename S>
240 typename Graph::NodeMap<S> node_map;
242 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
243 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
244 void set(Node nit, S a) { node_map.set(nit, a); }
245 S get(Node nit) const { return node_map.get(nit); }
250 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
253 typedef typename Graph::Node Node;
254 typedef typename Graph::Edge Edge;
255 typedef typename Graph::EdgeIt EdgeIt;
256 typedef typename Graph::OutEdgeIt OutEdgeIt;
257 typedef typename Graph::InEdgeIt InEdgeIt;
264 const CapacityMap* capacity;
265 typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
266 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
267 typedef typename AugGraph::Edge AugEdge;
270 MaxFlow(const Graph& _G, Node _s, Node _t, FlowMap& _flow, const CapacityMap& _capacity) :
271 G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) { }
272 bool augmentOnShortestPath() {
273 AugGraph res_graph(*G, *flow, *capacity);
276 typedef typename AugGraph::NodeMap<bool> ReachedMap;
277 BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
278 res_bfs.pushAndSetReached(s);
280 typename AugGraph::NodeMap<AugEdge> pred(res_graph);
281 pred.set(s, AugEdge(INVALID));
283 typename AugGraph::NodeMap<Number> free(res_graph);
285 //searching for augmenting path
286 while ( !res_bfs.finished() ) {
287 AugOutEdgeIt e=res_bfs;
288 if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
289 Node v=res_graph.tail(e);
290 Node w=res_graph.head(e);
292 if (res_graph.valid(pred.get(v))) {
293 free.set(w, std::min(free.get(v), res_graph.free(e)));
295 free.set(w, res_graph.free(e));
297 if (res_graph.head(e)==t) { _augment=true; break; }
301 } //end of searching augmenting path
305 Number augment_value=free.get(t);
306 while (res_graph.valid(pred.get(n))) {
307 AugEdge e=pred.get(n);
308 res_graph.augment(e, augment_value);
316 // template<typename MutableGraph> bool augmentOnBlockingFlow() {
317 // bool _augment=false;
319 // AugGraph res_graph(*G, *flow, *capacity);
321 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
322 // BfsIterator5< AugGraph /*, AugOutEdgeIt*/, ReachedMap > bfs(res_graph);
324 // bfs.pushAndSetReached(s);
325 // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
326 // while ( !bfs.finished() ) {
327 // AugOutEdgeIt e=bfs;
328 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
329 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
333 // } //computing distances from s in the residual graph
336 // typename AugGraph::NodeMap<typename MutableGraph::Node>
337 // res_graph_to_F(res_graph);
338 // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
339 // res_graph_to_F.set(n, F.addNode());
342 // typename MutableGraph::Node sF=res_graph_to_F.get(s);
343 // typename MutableGraph::Node tF=res_graph_to_F.get(t);
345 // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
346 // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
348 // //Making F to the graph containing the edges of the residual graph
349 // //which are in some shortest paths
350 // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
351 // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
352 // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
353 // original_edge.update();
354 // original_edge.set(f, e);
355 // residual_capacity.update();
356 // residual_capacity.set(f, res_graph.free(e));
360 // bool __augment=true;
362 // while (__augment) {
364 // //computing blocking flow with dfs
365 // typedef typename TrivGraphWrapper<MutableGraph>::NodeMap<bool> BlockingReachedMap;
366 // DfsIterator5< TrivGraphWrapper<MutableGraph>/*, typename MutableGraph::OutEdgeIt*/, BlockingReachedMap > dfs(F);
367 // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
368 // pred.set(sF, typename MutableGraph::Edge(INVALID));
369 // //invalid iterators for sources
371 // typename MutableGraph::NodeMap<Number> free(F);
373 // dfs.pushAndSetReached(sF);
374 // while (!dfs.finished()) {
376 // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
377 // if (dfs.isBNodeNewlyReached()) {
378 // typename MutableGraph::Node v=F.aNode(dfs);
379 // typename MutableGraph::Node w=F.bNode(dfs);
381 // if (F.valid(pred.get(v))) {
382 // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
384 // free.set(w, residual_capacity.get(dfs));
393 // F.erase(typename MutableGraph::OutEdgeIt(dfs));
399 // typename MutableGraph::Node n=tF;
400 // Number augment_value=free.get(tF);
401 // while (F.valid(pred.get(n))) {
402 // typename MutableGraph::Edge e=pred.get(n);
403 // res_graph.augment(original_edge.get(e), augment_value);
405 // if (residual_capacity.get(e)==augment_value)
408 // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
417 template<typename GraphWrapper>
421 typename GraphWrapper::NodeMap<int> dist;
423 DistanceMap(GraphWrapper& _gw) : gw(_gw), dist(_gw, _gw.nodeNum()) { }
424 //NodeMap(const ListGraph& _G, T a) :
425 //G(_G), container(G.node_id, a) { }
426 void set(const typename GraphWrapper::Node& n, int a) { dist[n]=a; }
427 int get(const typename GraphWrapper::Node& n) const { return dist[n]; }
428 bool get(const typename GraphWrapper::Edge& e) const {
429 return (dist.get(gw.tail(e))<dist.get(gw.head(e)));
431 //typename std::vector<T>::reference operator[](Node n) {
432 //return container[/*G.id(n)*/n.node->id]; }
433 //typename std::vector<T>::const_reference operator[](Node n) const {
434 //return container[/*G.id(n)*/n.node->id];
437 template<typename MutableGraph> bool augmentOnBlockingFlow() {
440 AugGraph res_graph(*G, *flow, *capacity);
442 typedef typename AugGraph::NodeMap<bool> ReachedMap;
443 BfsIterator5< AugGraph, ReachedMap > bfs(res_graph);
445 bfs.pushAndSetReached(s);
446 //typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
447 DistanceMap<AugGraph> dist(res_graph);
448 while ( !bfs.finished() ) {
450 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
451 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
454 } //computing distances from s in the residual graph
457 // typename AugGraph::NodeMap<typename MutableGraph::Node>
458 // res_graph_to_F(res_graph);
459 // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
460 // res_graph_to_F.set(n, F.addNode());
463 // typename MutableGraph::Node sF=res_graph_to_F.get(s);
464 // typename MutableGraph::Node tF=res_graph_to_F.get(t);
466 // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
467 // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
469 // //Making F to the graph containing the edges of the residual graph
470 // //which are in some shortest paths
471 // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
472 // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
473 // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
474 // original_edge.update();
475 // original_edge.set(f, e);
476 // residual_capacity.update();
477 // residual_capacity.set(f, res_graph.free(e));
482 typedef SubGraphWrapper<AugGraph, DistanceMap<AugGraph> > FilterResGraph;
483 FilterResGraph filter_res_graph(res_graph, dist);
484 typename AugGraph::NodeMap<typename MutableGraph::Node>
485 res_graph_to_F(res_graph);
486 for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
487 res_graph_to_F.set(n, F.addNode());
490 typename MutableGraph::Node sF=res_graph_to_F.get(s);
491 typename MutableGraph::Node tF=res_graph_to_F.get(t);
493 typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
494 typename MutableGraph::EdgeMap<Number> residual_capacity(F);
496 //Making F to the graph containing the edges of the residual graph
497 //which are in some shortest paths
498 for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
499 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
500 typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
501 original_edge.update();
502 original_edge.set(f, e);
503 residual_capacity.update();
504 residual_capacity.set(f, res_graph.free(e));
512 //computing blocking flow with dfs
513 typedef typename TrivGraphWrapper<MutableGraph>::NodeMap<bool> BlockingReachedMap;
514 DfsIterator5< TrivGraphWrapper<MutableGraph>, BlockingReachedMap > dfs(F);
515 typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
516 pred.set(sF, typename MutableGraph::Edge(INVALID));
517 //invalid iterators for sources
519 typename MutableGraph::NodeMap<Number> free(F);
521 dfs.pushAndSetReached(sF);
522 while (!dfs.finished()) {
524 if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
525 if (dfs.isBNodeNewlyReached()) {
526 typename MutableGraph::Node v=F.aNode(dfs);
527 typename MutableGraph::Node w=F.bNode(dfs);
529 if (F.valid(pred.get(v))) {
530 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
532 free.set(w, residual_capacity.get(dfs));
541 F.erase(typename MutableGraph::OutEdgeIt(dfs));
547 typename MutableGraph::Node n=tF;
548 Number augment_value=free.get(tF);
549 while (F.valid(pred.get(n))) {
550 typename MutableGraph::Edge e=pred.get(n);
551 res_graph.augment(original_edge.get(e), augment_value);
553 if (residual_capacity.get(e)==augment_value)
556 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
565 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
568 AugGraph res_graph(*G, *flow, *capacity);
570 //bfs for distances on the residual graph
571 typedef typename AugGraph::NodeMap<bool> ReachedMap;
572 BfsIterator5< AugGraph, ReachedMap > bfs(res_graph);
573 bfs.pushAndSetReached(s);
574 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
576 //F will contain the physical copy of the residual graph
577 //with the set of edges which areon shortest paths
579 typename AugGraph::NodeMap<typename MutableGraph::Node>
580 res_graph_to_F(res_graph);
581 for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
582 res_graph_to_F.set(n, F.addNode());
584 typename MutableGraph::Node sF=res_graph_to_F.get(s);
585 typename MutableGraph::Node tF=res_graph_to_F.get(t);
586 typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
587 typename MutableGraph::EdgeMap<Number> residual_capacity(F);
589 while ( !bfs.finished() ) {
591 if (res_graph.valid(e)) {
592 if (bfs.isBNodeNewlyReached()) {
593 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
594 typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
595 original_edge.update();
596 original_edge.set(f, e);
597 residual_capacity.update();
598 residual_capacity.set(f, res_graph.free(e));
600 if (dist.get(res_graph.head(e))==(dist.get(res_graph.tail(e))+1)) {
601 typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
602 original_edge.update();
603 original_edge.set(f, e);
604 residual_capacity.update();
605 residual_capacity.set(f, res_graph.free(e));
610 } //computing distances from s in the residual graph
616 //computing blocking flow with dfs
617 typedef typename TrivGraphWrapper<MutableGraph>::NodeMap<bool> BlockingReachedMap;
618 DfsIterator5< TrivGraphWrapper<MutableGraph>/*, typename MutableGraph::OutEdgeIt*/, BlockingReachedMap > dfs(F);
619 typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
620 pred.set(sF, typename MutableGraph::Edge(INVALID));
621 //invalid iterators for sources
623 typename MutableGraph::NodeMap<Number> free(F);
625 dfs.pushAndSetReached(sF);
626 while (!dfs.finished()) {
628 if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
629 if (dfs.isBNodeNewlyReached()) {
630 typename MutableGraph::Node v=F.aNode(dfs);
631 typename MutableGraph::Node w=F.bNode(dfs);
633 if (F.valid(pred.get(v))) {
634 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
636 free.set(w, residual_capacity.get(dfs));
645 F.erase(typename MutableGraph::OutEdgeIt(dfs));
651 typename MutableGraph::Node n=tF;
652 Number augment_value=free.get(tF);
653 while (F.valid(pred.get(n))) {
654 typename MutableGraph::Edge e=pred.get(n);
655 res_graph.augment(original_edge.get(e), augment_value);
657 if (residual_capacity.get(e)==augment_value)
660 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
668 bool augmentOnBlockingFlow2() {
671 //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
672 typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
673 typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
674 typedef typename EAugGraph::Edge EAugEdge;
676 EAugGraph res_graph(*G, *flow, *capacity);
678 //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
680 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
681 /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
682 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
684 bfs.pushAndSetReached(s);
686 typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
687 NodeMap<int>& dist=res_graph.dist;
689 while ( !bfs.finished() ) {
690 typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
691 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
692 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
695 } //computing distances from s in the residual graph
702 //computing blocking flow with dfs
703 typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
704 DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
706 typename EAugGraph::NodeMap<EAugEdge> pred(res_graph);
707 pred.set(s, EAugEdge(INVALID));
708 //invalid iterators for sources
710 typename EAugGraph::NodeMap<Number> free(res_graph);
712 dfs.pushAndSetReached(s);
713 while (!dfs.finished()) {
715 if (res_graph.valid(EAugOutEdgeIt(dfs))) {
716 if (dfs.isBNodeNewlyReached()) {
718 typename EAugGraph::Node v=res_graph.aNode(dfs);
719 typename EAugGraph::Node w=res_graph.bNode(dfs);
721 pred.set(w, EAugOutEdgeIt(dfs));
722 if (res_graph.valid(pred.get(v))) {
723 free.set(w, std::min(free.get(v), res_graph.free(dfs)));
725 free.set(w, res_graph.free(dfs));
734 res_graph.erase(dfs);
741 typename EAugGraph::Node n=t;
742 Number augment_value=free.get(t);
743 while (res_graph.valid(pred.get(n))) {
744 EAugEdge e=pred.get(n);
745 res_graph.augment(e, augment_value);
747 if (res_graph.free(e)==0)
757 //int num_of_augmentations=0;
758 while (augmentOnShortestPath()) {
759 //while (augmentOnBlockingFlow<MutableGraph>()) {
760 //std::cout << ++num_of_augmentations << " ";
761 //std::cout<<std::endl;
764 template<typename MutableGraph> void run() {
765 //int num_of_augmentations=0;
766 //while (augmentOnShortestPath()) {
767 while (augmentOnBlockingFlow<MutableGraph>()) {
768 //std::cout << ++num_of_augmentations << " ";
769 //std::cout<<std::endl;
775 for(G->/*getF*/first(e, s); G->valid(e); G->next(e)) {
783 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
786 typedef typename Graph::Node Node;
787 typedef typename Graph::NodeIt NodeIt;
788 typedef typename Graph::Edge Edge;
789 typedef typename Graph::EdgeIt EdgeIt;
790 typedef typename Graph::OutEdgeIt OutEdgeIt;
791 typedef typename Graph::InEdgeIt InEdgeIt;
793 typedef typename Graph::NodeMap<bool> SMap;
794 typedef typename Graph::NodeMap<bool> TMap;
802 const CapacityMap* capacity;
803 typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
804 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
805 typedef typename AugGraph::Edge AugEdge;
806 typename Graph::NodeMap<int> used; //0
809 MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
810 G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
811 bool augmentOnShortestPath() {
812 AugGraph res_graph(*G, *flow, *capacity);
815 typedef typename AugGraph::NodeMap<bool> ReachedMap;
816 BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
817 typename AugGraph::NodeMap<AugEdge> pred(res_graph);
818 for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
819 if ((S->get(s)) && (used.get(s)<1) ) {
821 //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
824 res_bfs.pushAndSetReached(s);
825 pred.set(s, AugEdge(INVALID));
830 typename AugGraph::NodeMap<Number> free(res_graph);
833 //searching for augmenting path
834 while ( !res_bfs.finished() ) {
835 AugOutEdgeIt e=res_bfs;
836 if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
837 Node v=res_graph.tail(e);
838 Node w=res_graph.head(e);
840 if (res_graph.valid(pred.get(v))) {
841 free.set(w, std::min(free.get(v), res_graph.free(e)));
843 free.set(w, res_graph.free(e));
846 if (T->get(n) && (used.get(n)<1) ) {
848 //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
858 } //end of searching augmenting path
862 used.set(n, 1); //mind2 vegen jav
863 Number augment_value=free.get(n);
864 while (res_graph.valid(pred.get(n))) {
865 AugEdge e=pred.get(n);
866 res_graph.augment(e, augment_value);
869 used.set(n, 1); //mind2 vegen jav
875 // template<typename MutableGraph> bool augmentOnBlockingFlow() {
876 // bool _augment=false;
878 // AugGraph res_graph(*G, *flow, *capacity);
880 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
881 // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
887 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
888 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
891 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
894 // res_bfs.pushAndSetReached(s);
895 // //pred.set(s, AugEdge(INVALID));
903 // //bfs.pushAndSetReached(s);
904 // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
905 // while ( !bfs.finished() ) {
906 // AugOutEdgeIt e=bfs;
907 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
908 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
912 // } //computing distances from s in the residual graph
915 // typename AugGraph::NodeMap<typename MutableGraph::Node>
916 // res_graph_to_F(res_graph);
917 // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
918 // res_graph_to_F.set(n, F.addNode());
921 // typename MutableGraph::Node sF=res_graph_to_F.get(s);
922 // typename MutableGraph::Node tF=res_graph_to_F.get(t);
924 // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
925 // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
927 // //Making F to the graph containing the edges of the residual graph
928 // //which are in some shortest paths
929 // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
930 // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
931 // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
932 // original_edge.update();
933 // original_edge.set(f, e);
934 // residual_capacity.update();
935 // residual_capacity.set(f, res_graph.free(e));
939 // bool __augment=true;
941 // while (__augment) {
943 // //computing blocking flow with dfs
944 // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
945 // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
946 // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
947 // pred.set(sF, typename MutableGraph::Edge(INVALID));
948 // //invalid iterators for sources
950 // typename MutableGraph::NodeMap<Number> free(F);
952 // dfs.pushAndSetReached(sF);
953 // while (!dfs.finished()) {
955 // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
956 // if (dfs.isBNodeNewlyReached()) {
957 // typename MutableGraph::Node v=F.aNode(dfs);
958 // typename MutableGraph::Node w=F.bNode(dfs);
960 // if (F.valid(pred.get(v))) {
961 // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
963 // free.set(w, residual_capacity.get(dfs));
972 // F.erase(typename MutableGraph::OutEdgeIt(dfs));
978 // typename MutableGraph::Node n=tF;
979 // Number augment_value=free.get(tF);
980 // while (F.valid(pred.get(n))) {
981 // typename MutableGraph::Edge e=pred.get(n);
982 // res_graph.augment(original_edge.get(e), augment_value);
984 // if (residual_capacity.get(e)==augment_value)
987 // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
995 bool augmentOnBlockingFlow2() {
998 //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
999 typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
1000 typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
1001 typedef typename EAugGraph::Edge EAugEdge;
1003 EAugGraph res_graph(*G, *flow, *capacity);
1005 //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
1007 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
1008 /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
1009 ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
1012 //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1013 for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
1016 for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
1019 bfs.pushAndSetReached(s);
1020 //pred.set(s, AugEdge(INVALID));
1026 //bfs.pushAndSetReached(s);
1028 typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
1029 NodeMap<int>& dist=res_graph.dist;
1031 while ( !bfs.finished() ) {
1032 typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
1033 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1034 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
1037 } //computing distances from s in the residual graph
1039 bool __augment=true;
1044 //computing blocking flow with dfs
1045 typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
1046 DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
1048 typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
1049 //pred.set(s, EAugEdge(INVALID));
1050 //invalid iterators for sources
1052 typename EAugGraph::NodeMap<Number> free(res_graph);
1055 //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1056 for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
1059 for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
1062 dfs.pushAndSetReached(s);
1063 //pred.set(s, AugEdge(INVALID));
1070 //dfs.pushAndSetReached(s);
1071 typename EAugGraph::Node n;
1072 while (!dfs.finished()) {
1074 if (res_graph.valid(EAugOutEdgeIt(dfs))) {
1075 if (dfs.isBNodeNewlyReached()) {
1077 typename EAugGraph::Node v=res_graph.aNode(dfs);
1078 typename EAugGraph::Node w=res_graph.bNode(dfs);
1080 pred.set(w, EAugOutEdgeIt(dfs));
1081 if (res_graph.valid(pred.get(v))) {
1082 free.set(w, std::min(free.get(v), res_graph.free(dfs)));
1084 free.set(w, res_graph.free(dfs));
1090 for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
1099 res_graph.erase(dfs);
1106 // typename EAugGraph::Node n=t;
1107 Number augment_value=free.get(n);
1108 while (res_graph.valid(pred.get(n))) {
1109 EAugEdge e=pred.get(n);
1110 res_graph.augment(e, augment_value);
1111 n=res_graph.tail(e);
1112 if (res_graph.free(e)==0)
1122 //int num_of_augmentations=0;
1123 while (augmentOnShortestPath()) {
1124 //while (augmentOnBlockingFlow<MutableGraph>()) {
1125 //std::cout << ++num_of_augmentations << " ";
1126 //std::cout<<std::endl;
1129 // template<typename MutableGraph> void run() {
1130 // //int num_of_augmentations=0;
1131 // //while (augmentOnShortestPath()) {
1132 // while (augmentOnBlockingFlow<MutableGraph>()) {
1133 // //std::cout << ++num_of_augmentations << " ";
1134 // //std::cout<<std::endl;
1137 Number flowValue() {
1140 for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1152 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1155 // typedef typename Graph::Node Node;
1156 // typedef typename Graph::Edge Edge;
1157 // typedef typename Graph::EdgeIt EdgeIt;
1158 // typedef typename Graph::OutEdgeIt OutEdgeIt;
1159 // typedef typename Graph::InEdgeIt InEdgeIt;
1162 // std::list<Node>& S;
1163 // std::list<Node>& T;
1165 // const CapacityMap& capacity;
1166 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1167 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1168 // typedef typename AugGraph::Edge AugEdge;
1169 // typename Graph::NodeMap<bool> SMap;
1170 // typename Graph::NodeMap<bool> TMap;
1172 // MaxFlow2(const Graph& _G, std::list<Node>& _S, std::list<Node>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
1173 // for(typename std::list<Node>::const_iterator i=S.begin();
1174 // i!=S.end(); ++i) {
1175 // SMap.set(*i, true);
1177 // for (typename std::list<Node>::const_iterator i=T.begin();
1178 // i!=T.end(); ++i) {
1179 // TMap.set(*i, true);
1183 // AugGraph res_graph(G, flow, capacity);
1184 // bool _augment=false;
1185 // Node reached_t_node;
1187 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1188 // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
1189 // for(typename std::list<Node>::const_iterator i=S.begin();
1190 // i!=S.end(); ++i) {
1191 // res_bfs.pushAndSetReached(*i);
1193 // //res_bfs.pushAndSetReached(s);
1195 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1196 // //filled up with invalid iterators
1198 // typename AugGraph::NodeMap<Number> free(res_graph);
1200 // //searching for augmenting path
1201 // while ( !res_bfs.finished() ) {
1202 // AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
1203 // if (e.valid() && res_bfs.isBNodeNewlyReached()) {
1204 // Node v=res_graph.tail(e);
1205 // Node w=res_graph.head(e);
1207 // if (pred.get(v).valid()) {
1208 // free.set(w, std::min(free.get(v), e.free()));
1210 // free.set(w, e.free());
1212 // if (TMap.get(res_graph.head(e))) {
1214 // reached_t_node=res_graph.head(e);
1220 // } //end of searching augmenting path
1223 // Node n=reached_t_node;
1224 // Number augment_value=free.get(reached_t_node);
1225 // while (pred.get(n).valid()) {
1226 // AugEdge e=pred.get(n);
1227 // e.augment(augment_value);
1228 // n=res_graph.tail(e);
1235 // while (augment()) { }
1237 // Number flowValue() {
1239 // for(typename std::list<Node>::const_iterator i=S.begin();
1240 // i!=S.end(); ++i) {
1241 // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1244 // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1256 #endif //HUGO_EDMONDS_KARP_H