2 #ifndef HUGO_EDMONDS_KARP_H
3 #define HUGO_EDMONDS_KARP_H
9 #include <bfs_iterator.h>
11 #include <graph_wrapper.h>
16 // template<typename Graph, typename Number, typename CapacityMap, typename FlowMap>
19 // typedef typename Graph::Node Node;
20 // typedef typename Graph::NodeIt NodeIt;
22 // typedef typename Graph::SymEdgeIt OldSymEdgeIt;
24 // const CapacityMap& capacity;
27 // ResGraph(const Graph& _G, const CapacityMap& _capacity, FlowMap& _flow) :
28 // G(_G), capacity(_capacity), flow(_flow) { }
33 // friend class OutEdgeIt;
36 // friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
38 // const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
42 // //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
43 // Number free() const {
44 // if (resG->G.aNode(sym)==resG->G.tail(sym)) {
45 // return (resG->capacity.get(sym)-resG->flow.get(sym));
47 // return (resG->flow.get(sym));
50 // bool valid() const { return sym.valid(); }
51 // void augment(Number a) const {
52 // if (resG->G.aNode(sym)==resG->G.tail(sym)) {
53 // resG->flow.set(sym, resG->flow.get(sym)+a);
54 // //resG->flow[sym]+=a;
56 // resG->flow.set(sym, resG->flow.get(sym)-a);
57 // //resG->flow[sym]-=a;
62 // class OutEdgeIt : public Edge {
63 // friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
66 // //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
68 // OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
70 // sym=resG->G.template first<OldSymEdgeIt>(v);
71 // while( sym.valid() && !(free()>0) ) { ++sym; }
74 // OutEdgeIt& operator++() {
76 // while( sym.valid() && !(free()>0) ) { ++sym; }
81 // void /*getF*/first(OutEdgeIt& e, Node v) const {
82 // e=OutEdgeIt(*this, v);
84 // void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
86 // template< typename It >
93 // template< typename It >
94 // It first(Node v) const {
96 // /*getF*/first(e, v);
100 // Node tail(Edge e) const { return G.aNode(e.sym); }
101 // Node head(Edge e) const { return G.bNode(e.sym); }
103 // Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
104 // Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
106 // int id(Node v) const { return G.id(v); }
108 // template <typename S>
110 // typename Graph::NodeMap<S> node_map;
112 // NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
113 // NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
114 // void set(Node nit, S a) { node_map.set(nit, a); }
115 // S get(Node nit) const { return node_map.get(nit); }
116 // S& operator[](Node nit) { return node_map[nit]; }
117 // const S& operator[](Node nit) const { return node_map[nit]; }
123 // template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
126 // typedef typename Graph::Node Node;
127 // typedef typename Graph::NodeIt NodeIt;
129 // //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
130 // typedef typename Graph::OutEdgeIt OldOutEdgeIt;
131 // typedef typename Graph::InEdgeIt OldInEdgeIt;
135 // const CapacityMap& capacity;
137 // ResGraph2(const Graph& _G, FlowMap& _flow,
138 // const CapacityMap& _capacity) :
139 // G(_G), flow(_flow), capacity(_capacity) { }
143 // friend class Edge;
144 // friend class OutEdgeIt;
147 // friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
149 // const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
150 // //OldSymEdgeIt sym;
153 // bool out_or_in; //true, iff out
155 // Edge() : out_or_in(true) { }
156 // Number free() const {
158 // return (resG->capacity.get(out)-resG->flow.get(out));
160 // return (resG->flow.get(in));
163 // bool valid() const {
164 // return out_or_in && out.valid() || in.valid(); }
165 // void augment(Number a) const {
167 // resG->flow.set(out, resG->flow.get(out)+a);
169 // resG->flow.set(in, resG->flow.get(in)-a);
174 // class OutEdgeIt : public Edge {
175 // friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
179 // OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
181 // out=resG->G.template first<OldOutEdgeIt>(v);
182 // while( out.valid() && !(free()>0) ) { ++out; }
183 // if (!out.valid()) {
185 // in=resG->G.template first<OldInEdgeIt>(v);
186 // while( in.valid() && !(free()>0) ) { ++in; }
190 // OutEdgeIt& operator++() {
192 // Node v=resG->G.aNode(out);
194 // while( out.valid() && !(free()>0) ) { ++out; }
195 // if (!out.valid()) {
197 // in=resG->G.template first<OldInEdgeIt>(v);
198 // while( in.valid() && !(free()>0) ) { ++in; }
202 // while( in.valid() && !(free()>0) ) { ++in; }
208 // void /*getF*/first(OutEdgeIt& e, Node v) const {
209 // e=OutEdgeIt(*this, v);
211 // void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
213 // template< typename It >
214 // It first() const {
220 // template< typename It >
221 // It first(Node v) const {
223 // /*getF*/first(e, v);
227 // Node tail(Edge e) const {
228 // return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
229 // Node head(Edge e) const {
230 // return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
232 // Node aNode(OutEdgeIt e) const {
233 // return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
234 // Node bNode(OutEdgeIt e) const {
235 // return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
237 // int id(Node v) const { return G.id(v); }
239 // template <typename S>
241 // typename Graph::NodeMap<S> node_map;
243 // NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
244 // NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
245 // void set(Node nit, S a) { node_map.set(nit, a); }
246 // S get(Node nit) const { return node_map.get(nit); }
251 template <typename Graph, typename Number,
252 typename CapacityMap, typename FlowMap>
255 typedef typename Graph::Node Node;
256 typedef typename Graph::Edge Edge;
257 typedef typename Graph::EdgeIt EdgeIt;
258 typedef typename Graph::OutEdgeIt OutEdgeIt;
259 typedef typename Graph::InEdgeIt InEdgeIt;
263 const CapacityMap* capacity;
265 typedef ResGraphWrapper<const Graph, Number, CapacityMap, FlowMap> ResGW;
266 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
267 typedef typename ResGW::Edge ResGWEdge;
270 MaxFlow(const Graph& _g, Node _s, Node _t, const CapacityMap& _capacity,
272 g(&_g), s(_s), t(_t), capacity(&_capacity), flow(&_flow) { }
274 bool augmentOnShortestPath() {
275 ResGW res_graph(*g, *capacity, *flow);
278 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
279 bfs.pushAndSetReached(s);
281 typename ResGW::NodeMap<ResGWEdge> pred(res_graph);
282 pred.set(s, INVALID);
284 typename ResGW::NodeMap<Number> free(res_graph);
286 //searching for augmenting path
287 while ( !bfs.finished() ) {
288 ResGWOutEdgeIt e=bfs;
289 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
290 Node v=res_graph.tail(e);
291 Node w=res_graph.head(e);
293 if (res_graph.valid(pred[v])) {
294 free.set(w, std::min(free[v], res_graph.resCap(e)));
296 free.set(w, res_graph.resCap(e));
298 if (res_graph.head(e)==t) { _augment=true; break; }
302 } //end of searching augmenting path
306 Number augment_value=free[t];
307 while (res_graph.valid(pred[n])) {
309 res_graph.augment(e, augment_value);
317 template<typename MapGraphWrapper>
320 const MapGraphWrapper* g;
321 typename MapGraphWrapper::NodeMap<int> dist;
323 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
324 void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; }
325 int operator[](const typename MapGraphWrapper::Node& n)
327 // int get(const typename MapGraphWrapper::Node& n) const {
329 // bool get(const typename MapGraphWrapper::Edge& e) const {
330 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
331 bool operator[](const typename MapGraphWrapper::Edge& e) const {
332 return (dist[g->tail(e)]<dist[g->head(e)]);
336 template<typename MutableGraph> bool augmentOnBlockingFlow() {
337 typedef MutableGraph MG;
340 ResGW res_graph(*g, *capacity, *flow);
342 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
344 bfs.pushAndSetReached(s);
345 //typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
346 DistanceMap<ResGW> dist(res_graph);
347 while ( !bfs.finished() ) {
348 ResGWOutEdgeIt e=bfs;
349 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
350 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
353 } //computing distances from s in the residual graph
356 ConstMap<typename ResGW::Node, bool> true_map(true);
357 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
358 DistanceMap<ResGW> > FilterResGW;
359 FilterResGW filter_res_graph(res_graph, true_map, dist);
360 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
362 typename ResGW::NodeIt n;
363 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
364 res_graph_to_F.set(n, F.addNode());
368 typename MG::Node sF=res_graph_to_F[s];
369 typename MG::Node tF=res_graph_to_F[t];
370 typename MG::EdgeMap<ResGWEdge> original_edge(F);
371 typename MG::EdgeMap<Number> residual_capacity(F);
373 //Making F to the graph containing the edges of the residual graph
374 //which are in some shortest paths
376 typename FilterResGW::EdgeIt e;
377 for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
378 //if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
379 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
380 original_edge.update();
381 original_edge.set(f, e);
382 residual_capacity.update();
383 residual_capacity.set(f, res_graph.resCap(e));
392 //computing blocking flow with dfs
394 DfsIterator5< MG, typename MG::NodeMap<bool> > dfs(F);
395 typename MG::NodeMap<typename MG::Edge> pred(F);
396 pred.set(sF, INVALID);
397 //invalid iterators for sources
399 typename MG::NodeMap<Number> free(F);
401 dfs.pushAndSetReached(sF);
402 while (!dfs.finished()) {
404 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
405 if (dfs.isBNodeNewlyReached()) {
406 typename MG::Node v=F.aNode(dfs);
407 typename MG::Node w=F.bNode(dfs);
409 if (F.valid(pred[v])) {
410 free.set(w, std::min(free[v], residual_capacity[dfs]));
412 free.set(w, residual_capacity[dfs]);
421 F.erase(/*typename MG::OutEdgeIt*/(dfs));
427 typename MG::Node n=tF;
428 Number augment_value=free[tF];
429 while (F.valid(pred[n])) {
430 typename MG::Edge e=pred[n];
431 res_graph.augment(original_edge[e], augment_value);
433 if (residual_capacity[e]==augment_value)
436 residual_capacity.set(e, residual_capacity[e]-augment_value);
445 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
446 typedef MutableGraph MG;
449 ResGW res_graph(*g, *capacity, *flow);
451 //bfs for distances on the residual graph
452 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
453 bfs.pushAndSetReached(s);
454 typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
456 //F will contain the physical copy of the residual graph
457 //with the set of edges which are on shortest paths
459 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
461 typename ResGW::NodeIt n;
462 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
463 res_graph_to_F.set(n, F.addNode());
467 typename MG::Node sF=res_graph_to_F[s];
468 typename MG::Node tF=res_graph_to_F[t];
469 typename MG::EdgeMap<ResGWEdge> original_edge(F);
470 typename MG::EdgeMap<Number> residual_capacity(F);
472 while ( !bfs.finished() ) {
473 ResGWOutEdgeIt e=bfs;
474 if (res_graph.valid(e)) {
475 if (bfs.isBNodeNewlyReached()) {
476 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
477 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
478 original_edge.update();
479 original_edge.set(f, e);
480 residual_capacity.update();
481 residual_capacity.set(f, res_graph.resCap(e));
483 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
484 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
485 original_edge.update();
486 original_edge.set(f, e);
487 residual_capacity.update();
488 residual_capacity.set(f, res_graph.resCap(e));
493 } //computing distances from s in the residual graph
499 //computing blocking flow with dfs
500 DfsIterator5< MG, typename MG::NodeMap<bool> > dfs(F);
501 typename MG::NodeMap<typename MG::Edge> pred(F);
502 pred.set(sF, INVALID);
503 //invalid iterators for sources
505 typename MG::NodeMap<Number> free(F);
507 dfs.pushAndSetReached(sF);
508 while (!dfs.finished()) {
510 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
511 if (dfs.isBNodeNewlyReached()) {
512 typename MG::Node v=F.aNode(dfs);
513 typename MG::Node w=F.bNode(dfs);
515 if (F.valid(pred[v])) {
516 free.set(w, std::min(free[v], residual_capacity[dfs]));
518 free.set(w, residual_capacity[dfs]);
527 F.erase(/*typename MG::OutEdgeIt*/(dfs));
533 typename MG::Node n=tF;
534 Number augment_value=free[tF];
535 while (F.valid(pred[n])) {
536 typename MG::Edge e=pred[n];
537 res_graph.augment(original_edge[e], augment_value);
539 if (residual_capacity[e]==augment_value)
542 residual_capacity.set(e, residual_capacity[e]-augment_value);
551 bool augmentOnBlockingFlow2() {
554 ResGW res_graph(*g, *capacity, *flow);
556 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
558 bfs.pushAndSetReached(s);
559 DistanceMap<ResGW> dist(res_graph);
560 while ( !bfs.finished() ) {
561 ResGWOutEdgeIt e=bfs;
562 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
563 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
566 } //computing distances from s in the residual graph
568 //Subgraph containing the edges on some shortest paths
569 ConstMap<typename ResGW::Node, bool> true_map(true);
570 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
571 DistanceMap<ResGW> > FilterResGW;
572 FilterResGW filter_res_graph(res_graph, true_map, dist);
574 //Subgraph, which is able to delete edges which are already
576 typename FilterResGW::NodeMap<typename FilterResGW::OutEdgeIt>
577 first_out_edges(filter_res_graph);
578 typename FilterResGW::NodeIt v;
579 for(filter_res_graph.first(v); filter_res_graph.valid(v);
580 filter_res_graph.next(v))
582 typename FilterResGW::OutEdgeIt e;
583 filter_res_graph.first(e, v);
584 first_out_edges.set(v, e);
586 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
587 NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
588 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
595 //computing blocking flow with dfs
596 DfsIterator5< ErasingResGW, typename ErasingResGW::NodeMap<bool> >
597 dfs(erasing_res_graph);
598 typename ErasingResGW::NodeMap<typename ErasingResGW::OutEdgeIt>
599 pred(erasing_res_graph);
600 pred.set(s, INVALID);
601 //invalid iterators for sources
603 typename ErasingResGW::NodeMap<Number> free1(erasing_res_graph);
605 dfs.pushAndSetReached(
606 typename ErasingResGW::Node(
607 typename FilterResGW::Node(
608 typename ResGW::Node(s)
612 while (!dfs.finished()) {
614 if (erasing_res_graph.valid(
615 typename ErasingResGW::OutEdgeIt(dfs)))
617 if (dfs.isBNodeNewlyReached()) {
619 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
620 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
622 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
623 if (erasing_res_graph.valid(pred[v])) {
624 free1.set(w, std::min(free1[v], res_graph.resCap(
625 typename ErasingResGW::OutEdgeIt(dfs))));
627 free1.set(w, res_graph.resCap(
628 typename ErasingResGW::OutEdgeIt(dfs)));
637 erasing_res_graph.erase(dfs);
643 typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
644 // typename ResGW::NodeMap<Number> a(res_graph);
645 // typename ResGW::Node b;
647 // typename FilterResGW::NodeMap<Number> a1(filter_res_graph);
648 // typename FilterResGW::Node b1;
650 // typename ErasingResGW::NodeMap<Number> a2(erasing_res_graph);
651 // typename ErasingResGW::Node b2;
653 Number augment_value=free1[n];
654 while (erasing_res_graph.valid(pred[n])) {
655 typename ErasingResGW::OutEdgeIt e=pred[n];
656 res_graph.augment(e, augment_value);
657 n=erasing_res_graph.tail(e);
658 if (res_graph.resCap(e)==0)
659 erasing_res_graph.erase(e);
663 } //while (__augment)
669 //int num_of_augmentations=0;
670 while (augmentOnShortestPath()) {
671 //while (augmentOnBlockingFlow<MutableGraph>()) {
672 //std::cout << ++num_of_augmentations << " ";
673 //std::cout<<std::endl;
677 template<typename MutableGraph> void run() {
678 //int num_of_augmentations=0;
679 //while (augmentOnShortestPath()) {
680 while (augmentOnBlockingFlow<MutableGraph>()) {
681 //std::cout << ++num_of_augmentations << " ";
682 //std::cout<<std::endl;
689 for(g->first(e, s); g->valid(e); g->next(e)) {
698 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
699 // class MaxMatching {
701 // typedef typename Graph::Node Node;
702 // typedef typename Graph::NodeIt NodeIt;
703 // typedef typename Graph::Edge Edge;
704 // typedef typename Graph::EdgeIt EdgeIt;
705 // typedef typename Graph::OutEdgeIt OutEdgeIt;
706 // typedef typename Graph::InEdgeIt InEdgeIt;
708 // typedef typename Graph::NodeMap<bool> SMap;
709 // typedef typename Graph::NodeMap<bool> TMap;
717 // const CapacityMap* capacity;
718 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
719 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
720 // typedef typename AugGraph::Edge AugEdge;
721 // typename Graph::NodeMap<int> used; //0
724 // MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
725 // G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
726 // bool augmentOnShortestPath() {
727 // AugGraph res_graph(*G, *flow, *capacity);
728 // bool _augment=false;
730 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
731 // BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
732 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
733 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
734 // if ((S->get(s)) && (used.get(s)<1) ) {
736 // //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
737 // //u+=flow->get(e);
739 // bfs.pushAndSetReached(s);
740 // pred.set(s, AugEdge(INVALID));
745 // typename AugGraph::NodeMap<Number> free(res_graph);
748 // //searching for augmenting path
749 // while ( !bfs.finished() ) {
750 // AugOutEdgeIt e=bfs;
751 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
752 // Node v=res_graph.tail(e);
753 // Node w=res_graph.head(e);
755 // if (res_graph.valid(pred.get(v))) {
756 // free.set(w, std::min(free.get(v), res_graph.free(e)));
758 // free.set(w, res_graph.free(e));
760 // n=res_graph.head(e);
761 // if (T->get(n) && (used.get(n)<1) ) {
763 // //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
764 // //u+=flow->get(f);
773 // } //end of searching augmenting path
777 // used.set(n, 1); //mind2 vegen jav
778 // Number augment_value=free.get(n);
779 // while (res_graph.valid(pred.get(n))) {
780 // AugEdge e=pred.get(n);
781 // res_graph.augment(e, augment_value);
782 // n=res_graph.tail(e);
784 // used.set(n, 1); //mind2 vegen jav
790 // // template<typename MutableGraph> bool augmentOnBlockingFlow() {
791 // // bool _augment=false;
793 // // AugGraph res_graph(*G, *flow, *capacity);
795 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
796 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
802 // // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
803 // // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
804 // // if (S->get(s)) {
806 // // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
807 // // u+=flow->get(e);
809 // // bfs.pushAndSetReached(s);
810 // // //pred.set(s, AugEdge(INVALID));
818 // // //bfs.pushAndSetReached(s);
819 // // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
820 // // while ( !bfs.finished() ) {
821 // // AugOutEdgeIt e=bfs;
822 // // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
823 // // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
827 // // } //computing distances from s in the residual graph
829 // // MutableGraph F;
830 // // typename AugGraph::NodeMap<typename MutableGraph::Node>
831 // // res_graph_to_F(res_graph);
832 // // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
833 // // res_graph_to_F.set(n, F.addNode());
836 // // typename MutableGraph::Node sF=res_graph_to_F.get(s);
837 // // typename MutableGraph::Node tF=res_graph_to_F.get(t);
839 // // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
840 // // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
842 // // //Making F to the graph containing the edges of the residual graph
843 // // //which are in some shortest paths
844 // // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
845 // // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
846 // // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
847 // // original_edge.update();
848 // // original_edge.set(f, e);
849 // // residual_capacity.update();
850 // // residual_capacity.set(f, res_graph.free(e));
854 // // bool __augment=true;
856 // // while (__augment) {
857 // // __augment=false;
858 // // //computing blocking flow with dfs
859 // // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
860 // // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
861 // // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
862 // // pred.set(sF, typename MutableGraph::Edge(INVALID));
863 // // //invalid iterators for sources
865 // // typename MutableGraph::NodeMap<Number> free(F);
867 // // dfs.pushAndSetReached(sF);
868 // // while (!dfs.finished()) {
870 // // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
871 // // if (dfs.isBNodeNewlyReached()) {
872 // // typename MutableGraph::Node v=F.aNode(dfs);
873 // // typename MutableGraph::Node w=F.bNode(dfs);
874 // // pred.set(w, dfs);
875 // // if (F.valid(pred.get(v))) {
876 // // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
878 // // free.set(w, residual_capacity.get(dfs));
881 // // __augment=true;
887 // // F.erase(typename MutableGraph::OutEdgeIt(dfs));
892 // // if (__augment) {
893 // // typename MutableGraph::Node n=tF;
894 // // Number augment_value=free.get(tF);
895 // // while (F.valid(pred.get(n))) {
896 // // typename MutableGraph::Edge e=pred.get(n);
897 // // res_graph.augment(original_edge.get(e), augment_value);
899 // // if (residual_capacity.get(e)==augment_value)
902 // // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
908 // // return _augment;
910 // bool augmentOnBlockingFlow2() {
911 // bool _augment=false;
913 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
914 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
915 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
916 // typedef typename EAugGraph::Edge EAugEdge;
918 // EAugGraph res_graph(*G, *flow, *capacity);
920 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
922 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
923 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
924 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
927 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
928 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
931 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
934 // bfs.pushAndSetReached(s);
935 // //pred.set(s, AugEdge(INVALID));
941 // //bfs.pushAndSetReached(s);
943 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
944 // NodeMap<int>& dist=res_graph.dist;
946 // while ( !bfs.finished() ) {
947 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
948 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
949 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
952 // } //computing distances from s in the residual graph
954 // bool __augment=true;
956 // while (__augment) {
959 // //computing blocking flow with dfs
960 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
961 // DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
963 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
964 // //pred.set(s, EAugEdge(INVALID));
965 // //invalid iterators for sources
967 // typename EAugGraph::NodeMap<Number> free(res_graph);
970 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
971 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
974 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
977 // dfs.pushAndSetReached(s);
978 // //pred.set(s, AugEdge(INVALID));
985 // //dfs.pushAndSetReached(s);
986 // typename EAugGraph::Node n;
987 // while (!dfs.finished()) {
989 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
990 // if (dfs.isBNodeNewlyReached()) {
992 // typename EAugGraph::Node v=res_graph.aNode(dfs);
993 // typename EAugGraph::Node w=res_graph.bNode(dfs);
995 // pred.set(w, EAugOutEdgeIt(dfs));
996 // if (res_graph.valid(pred.get(v))) {
997 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
999 // free.set(w, res_graph.free(dfs));
1005 // for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
1014 // res_graph.erase(dfs);
1021 // // typename EAugGraph::Node n=t;
1022 // Number augment_value=free.get(n);
1023 // while (res_graph.valid(pred.get(n))) {
1024 // EAugEdge e=pred.get(n);
1025 // res_graph.augment(e, augment_value);
1026 // n=res_graph.tail(e);
1027 // if (res_graph.free(e)==0)
1028 // res_graph.erase(e);
1037 // //int num_of_augmentations=0;
1038 // while (augmentOnShortestPath()) {
1039 // //while (augmentOnBlockingFlow<MutableGraph>()) {
1040 // //std::cout << ++num_of_augmentations << " ";
1041 // //std::cout<<std::endl;
1044 // // template<typename MutableGraph> void run() {
1045 // // //int num_of_augmentations=0;
1046 // // //while (augmentOnShortestPath()) {
1047 // // while (augmentOnBlockingFlow<MutableGraph>()) {
1048 // // //std::cout << ++num_of_augmentations << " ";
1049 // // //std::cout<<std::endl;
1052 // Number flowValue() {
1055 // for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1067 // // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1068 // // class MaxFlow2 {
1070 // // typedef typename Graph::Node Node;
1071 // // typedef typename Graph::Edge Edge;
1072 // // typedef typename Graph::EdgeIt EdgeIt;
1073 // // typedef typename Graph::OutEdgeIt OutEdgeIt;
1074 // // typedef typename Graph::InEdgeIt InEdgeIt;
1076 // // const Graph& G;
1077 // // std::list<Node>& S;
1078 // // std::list<Node>& T;
1079 // // FlowMap& flow;
1080 // // const CapacityMap& capacity;
1081 // // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1082 // // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1083 // // typedef typename AugGraph::Edge AugEdge;
1084 // // typename Graph::NodeMap<bool> SMap;
1085 // // typename Graph::NodeMap<bool> TMap;
1087 // // 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) {
1088 // // for(typename std::list<Node>::const_iterator i=S.begin();
1089 // // i!=S.end(); ++i) {
1090 // // SMap.set(*i, true);
1092 // // for (typename std::list<Node>::const_iterator i=T.begin();
1093 // // i!=T.end(); ++i) {
1094 // // TMap.set(*i, true);
1097 // // bool augment() {
1098 // // AugGraph res_graph(G, flow, capacity);
1099 // // bool _augment=false;
1100 // // Node reached_t_node;
1102 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1103 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1104 // // for(typename std::list<Node>::const_iterator i=S.begin();
1105 // // i!=S.end(); ++i) {
1106 // // bfs.pushAndSetReached(*i);
1108 // // //bfs.pushAndSetReached(s);
1110 // // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1111 // // //filled up with invalid iterators
1113 // // typename AugGraph::NodeMap<Number> free(res_graph);
1115 // // //searching for augmenting path
1116 // // while ( !bfs.finished() ) {
1117 // // AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1118 // // if (e.valid() && bfs.isBNodeNewlyReached()) {
1119 // // Node v=res_graph.tail(e);
1120 // // Node w=res_graph.head(e);
1121 // // pred.set(w, e);
1122 // // if (pred.get(v).valid()) {
1123 // // free.set(w, std::min(free.get(v), e.free()));
1125 // // free.set(w, e.free());
1127 // // if (TMap.get(res_graph.head(e))) {
1128 // // _augment=true;
1129 // // reached_t_node=res_graph.head(e);
1135 // // } //end of searching augmenting path
1137 // // if (_augment) {
1138 // // Node n=reached_t_node;
1139 // // Number augment_value=free.get(reached_t_node);
1140 // // while (pred.get(n).valid()) {
1141 // // AugEdge e=pred.get(n);
1142 // // e.augment(augment_value);
1143 // // n=res_graph.tail(e);
1147 // // return _augment;
1150 // // while (augment()) { }
1152 // // Number flowValue() {
1154 // // for(typename std::list<Node>::const_iterator i=S.begin();
1155 // // i!=S.end(); ++i) {
1156 // // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1157 // // a+=flow.get(e);
1159 // // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1160 // // a-=flow.get(e);
1170 #endif //HUGO_EDMONDS_KARP_H