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 BfsIterator< ResGW, typename ResGW::template NodeMap<bool> >
280 bfs.pushAndSetReached(s);
282 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
283 pred.set(s, INVALID);
285 typename ResGW::template NodeMap<Number> free(res_graph);
287 //searching for augmenting path
288 while ( !bfs.finished() ) {
289 ResGWOutEdgeIt e=bfs;
290 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
291 Node v=res_graph.tail(e);
292 Node w=res_graph.head(e);
294 if (res_graph.valid(pred[v])) {
295 free.set(w, std::min(free[v], res_graph.resCap(e)));
297 free.set(w, res_graph.resCap(e));
299 if (res_graph.head(e)==t) { _augment=true; break; }
303 } //end of searching augmenting path
307 Number augment_value=free[t];
308 while (res_graph.valid(pred[n])) {
310 res_graph.augment(e, augment_value);
318 template<typename MapGraphWrapper>
321 const MapGraphWrapper* g;
322 typename MapGraphWrapper::template NodeMap<int> dist;
324 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
325 void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; }
326 int operator[](const typename MapGraphWrapper::Node& n)
328 // int get(const typename MapGraphWrapper::Node& n) const {
330 // bool get(const typename MapGraphWrapper::Edge& e) const {
331 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
332 bool operator[](const typename MapGraphWrapper::Edge& e) const {
333 return (dist[g->tail(e)]<dist[g->head(e)]);
337 template<typename MutableGraph> bool augmentOnBlockingFlow() {
338 typedef MutableGraph MG;
341 ResGW res_graph(*g, *capacity, *flow);
343 BfsIterator< ResGW, typename ResGW::template NodeMap<bool> >
346 bfs.pushAndSetReached(s);
347 //typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
348 DistanceMap<ResGW> dist(res_graph);
349 while ( !bfs.finished() ) {
350 ResGWOutEdgeIt e=bfs;
351 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
352 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
355 } //computing distances from s in the residual graph
358 ConstMap<typename ResGW::Node, bool> true_map(true);
359 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
360 DistanceMap<ResGW> > FilterResGW;
361 FilterResGW filter_res_graph(res_graph, true_map, dist);
362 typename ResGW::template NodeMap<typename MG::Node>
363 res_graph_to_F(res_graph);
365 typename ResGW::NodeIt n;
366 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
367 res_graph_to_F.set(n, F.addNode());
371 typename MG::Node sF=res_graph_to_F[s];
372 typename MG::Node tF=res_graph_to_F[t];
373 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
374 typename MG::template EdgeMap<Number> residual_capacity(F);
376 //Making F to the graph containing the edges of the residual graph
377 //which are in some shortest paths
379 typename FilterResGW::EdgeIt e;
380 for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
381 //if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
382 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
383 original_edge.update();
384 original_edge.set(f, e);
385 residual_capacity.update();
386 residual_capacity.set(f, res_graph.resCap(e));
395 //computing blocking flow with dfs
397 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
398 typename MG::template NodeMap<typename MG::Edge> pred(F);
399 pred.set(sF, INVALID);
400 //invalid iterators for sources
402 typename MG::template NodeMap<Number> free(F);
404 dfs.pushAndSetReached(sF);
405 while (!dfs.finished()) {
407 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
408 if (dfs.isBNodeNewlyReached()) {
409 typename MG::Node v=F.aNode(dfs);
410 typename MG::Node w=F.bNode(dfs);
412 if (F.valid(pred[v])) {
413 free.set(w, std::min(free[v], residual_capacity[dfs]));
415 free.set(w, residual_capacity[dfs]);
424 F.erase(/*typename MG::OutEdgeIt*/(dfs));
430 typename MG::Node n=tF;
431 Number augment_value=free[tF];
432 while (F.valid(pred[n])) {
433 typename MG::Edge e=pred[n];
434 res_graph.augment(original_edge[e], augment_value);
436 if (residual_capacity[e]==augment_value)
439 residual_capacity.set(e, residual_capacity[e]-augment_value);
448 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
449 typedef MutableGraph MG;
452 ResGW res_graph(*g, *capacity, *flow);
454 //bfs for distances on the residual graph
455 BfsIterator< ResGW, typename ResGW::template NodeMap<bool> >
457 bfs.pushAndSetReached(s);
458 typename ResGW::template NodeMap<int>
459 dist(res_graph); //filled up with 0's
461 //F will contain the physical copy of the residual graph
462 //with the set of edges which are on shortest paths
464 typename ResGW::template NodeMap<typename MG::Node>
465 res_graph_to_F(res_graph);
467 typename ResGW::NodeIt n;
468 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
469 res_graph_to_F.set(n, F.addNode());
473 typename MG::Node sF=res_graph_to_F[s];
474 typename MG::Node tF=res_graph_to_F[t];
475 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
476 typename MG::template EdgeMap<Number> residual_capacity(F);
478 while ( !bfs.finished() ) {
479 ResGWOutEdgeIt e=bfs;
480 if (res_graph.valid(e)) {
481 if (bfs.isBNodeNewlyReached()) {
482 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
483 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
484 original_edge.update();
485 original_edge.set(f, e);
486 residual_capacity.update();
487 residual_capacity.set(f, res_graph.resCap(e));
489 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
490 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
491 original_edge.update();
492 original_edge.set(f, e);
493 residual_capacity.update();
494 residual_capacity.set(f, res_graph.resCap(e));
499 } //computing distances from s in the residual graph
505 //computing blocking flow with dfs
506 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
507 typename MG::template NodeMap<typename MG::Edge> pred(F);
508 pred.set(sF, INVALID);
509 //invalid iterators for sources
511 typename MG::template NodeMap<Number> free(F);
513 dfs.pushAndSetReached(sF);
514 while (!dfs.finished()) {
516 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
517 if (dfs.isBNodeNewlyReached()) {
518 typename MG::Node v=F.aNode(dfs);
519 typename MG::Node w=F.bNode(dfs);
521 if (F.valid(pred[v])) {
522 free.set(w, std::min(free[v], residual_capacity[dfs]));
524 free.set(w, residual_capacity[dfs]);
533 F.erase(/*typename MG::OutEdgeIt*/(dfs));
539 typename MG::Node n=tF;
540 Number augment_value=free[tF];
541 while (F.valid(pred[n])) {
542 typename MG::Edge e=pred[n];
543 res_graph.augment(original_edge[e], augment_value);
545 if (residual_capacity[e]==augment_value)
548 residual_capacity.set(e, residual_capacity[e]-augment_value);
557 bool augmentOnBlockingFlow2() {
560 ResGW res_graph(*g, *capacity, *flow);
562 BfsIterator< ResGW, typename ResGW::template NodeMap<bool> >
565 bfs.pushAndSetReached(s);
566 DistanceMap<ResGW> dist(res_graph);
567 while ( !bfs.finished() ) {
568 ResGWOutEdgeIt e=bfs;
569 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
570 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
573 } //computing distances from s in the residual graph
575 //Subgraph containing the edges on some shortest paths
576 ConstMap<typename ResGW::Node, bool> true_map(true);
577 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
578 DistanceMap<ResGW> > FilterResGW;
579 FilterResGW filter_res_graph(res_graph, true_map, dist);
581 //Subgraph, which is able to delete edges which are already
583 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
584 first_out_edges(filter_res_graph);
585 typename FilterResGW::NodeIt v;
586 for(filter_res_graph.first(v); filter_res_graph.valid(v);
587 filter_res_graph.next(v))
589 typename FilterResGW::OutEdgeIt e;
590 filter_res_graph.first(e, v);
591 first_out_edges.set(v, e);
593 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
594 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
595 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
602 //computing blocking flow with dfs
603 DfsIterator< ErasingResGW,
604 typename ErasingResGW::template NodeMap<bool> >
605 dfs(erasing_res_graph);
606 typename ErasingResGW::
607 template NodeMap<typename ErasingResGW::OutEdgeIt>
608 pred(erasing_res_graph);
609 pred.set(s, INVALID);
610 //invalid iterators for sources
612 typename ErasingResGW::template NodeMap<Number>
613 free1(erasing_res_graph);
615 dfs.pushAndSetReached(
616 typename ErasingResGW::Node(
617 typename FilterResGW::Node(
618 typename ResGW::Node(s)
622 while (!dfs.finished()) {
624 if (erasing_res_graph.valid(
625 typename ErasingResGW::OutEdgeIt(dfs)))
627 if (dfs.isBNodeNewlyReached()) {
629 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
630 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
632 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
633 if (erasing_res_graph.valid(pred[v])) {
634 free1.set(w, std::min(free1[v], res_graph.resCap(
635 typename ErasingResGW::OutEdgeIt(dfs))));
637 free1.set(w, res_graph.resCap(
638 typename ErasingResGW::OutEdgeIt(dfs)));
647 erasing_res_graph.erase(dfs);
653 typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
654 // typename ResGW::NodeMap<Number> a(res_graph);
655 // typename ResGW::Node b;
657 // typename FilterResGW::NodeMap<Number> a1(filter_res_graph);
658 // typename FilterResGW::Node b1;
660 // typename ErasingResGW::NodeMap<Number> a2(erasing_res_graph);
661 // typename ErasingResGW::Node b2;
663 Number augment_value=free1[n];
664 while (erasing_res_graph.valid(pred[n])) {
665 typename ErasingResGW::OutEdgeIt e=pred[n];
666 res_graph.augment(e, augment_value);
667 n=erasing_res_graph.tail(e);
668 if (res_graph.resCap(e)==0)
669 erasing_res_graph.erase(e);
673 } //while (__augment)
679 //int num_of_augmentations=0;
680 while (augmentOnShortestPath()) {
681 //while (augmentOnBlockingFlow<MutableGraph>()) {
682 //std::cout << ++num_of_augmentations << " ";
683 //std::cout<<std::endl;
687 template<typename MutableGraph> void run() {
688 //int num_of_augmentations=0;
689 //while (augmentOnShortestPath()) {
690 while (augmentOnBlockingFlow<MutableGraph>()) {
691 //std::cout << ++num_of_augmentations << " ";
692 //std::cout<<std::endl;
699 for(g->first(e, s); g->valid(e); g->next(e)) {
708 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
709 // class MaxMatching {
711 // typedef typename Graph::Node Node;
712 // typedef typename Graph::NodeIt NodeIt;
713 // typedef typename Graph::Edge Edge;
714 // typedef typename Graph::EdgeIt EdgeIt;
715 // typedef typename Graph::OutEdgeIt OutEdgeIt;
716 // typedef typename Graph::InEdgeIt InEdgeIt;
718 // typedef typename Graph::NodeMap<bool> SMap;
719 // typedef typename Graph::NodeMap<bool> TMap;
727 // const CapacityMap* capacity;
728 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
729 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
730 // typedef typename AugGraph::Edge AugEdge;
731 // typename Graph::NodeMap<int> used; //0
734 // MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
735 // G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
736 // bool augmentOnShortestPath() {
737 // AugGraph res_graph(*G, *flow, *capacity);
738 // bool _augment=false;
740 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
741 // BfsIterator< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
742 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
743 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
744 // if ((S->get(s)) && (used.get(s)<1) ) {
746 // //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
747 // //u+=flow->get(e);
749 // bfs.pushAndSetReached(s);
750 // pred.set(s, AugEdge(INVALID));
755 // typename AugGraph::NodeMap<Number> free(res_graph);
758 // //searching for augmenting path
759 // while ( !bfs.finished() ) {
760 // AugOutEdgeIt e=bfs;
761 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
762 // Node v=res_graph.tail(e);
763 // Node w=res_graph.head(e);
765 // if (res_graph.valid(pred.get(v))) {
766 // free.set(w, std::min(free.get(v), res_graph.free(e)));
768 // free.set(w, res_graph.free(e));
770 // n=res_graph.head(e);
771 // if (T->get(n) && (used.get(n)<1) ) {
773 // //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
774 // //u+=flow->get(f);
783 // } //end of searching augmenting path
787 // used.set(n, 1); //mind2 vegen jav
788 // Number augment_value=free.get(n);
789 // while (res_graph.valid(pred.get(n))) {
790 // AugEdge e=pred.get(n);
791 // res_graph.augment(e, augment_value);
792 // n=res_graph.tail(e);
794 // used.set(n, 1); //mind2 vegen jav
800 // // template<typename MutableGraph> bool augmentOnBlockingFlow() {
801 // // bool _augment=false;
803 // // AugGraph res_graph(*G, *flow, *capacity);
805 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
806 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
812 // // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
813 // // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
814 // // if (S->get(s)) {
816 // // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
817 // // u+=flow->get(e);
819 // // bfs.pushAndSetReached(s);
820 // // //pred.set(s, AugEdge(INVALID));
828 // // //bfs.pushAndSetReached(s);
829 // // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
830 // // while ( !bfs.finished() ) {
831 // // AugOutEdgeIt e=bfs;
832 // // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
833 // // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
837 // // } //computing distances from s in the residual graph
839 // // MutableGraph F;
840 // // typename AugGraph::NodeMap<typename MutableGraph::Node>
841 // // res_graph_to_F(res_graph);
842 // // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
843 // // res_graph_to_F.set(n, F.addNode());
846 // // typename MutableGraph::Node sF=res_graph_to_F.get(s);
847 // // typename MutableGraph::Node tF=res_graph_to_F.get(t);
849 // // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
850 // // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
852 // // //Making F to the graph containing the edges of the residual graph
853 // // //which are in some shortest paths
854 // // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
855 // // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
856 // // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
857 // // original_edge.update();
858 // // original_edge.set(f, e);
859 // // residual_capacity.update();
860 // // residual_capacity.set(f, res_graph.free(e));
864 // // bool __augment=true;
866 // // while (__augment) {
867 // // __augment=false;
868 // // //computing blocking flow with dfs
869 // // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
870 // // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
871 // // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
872 // // pred.set(sF, typename MutableGraph::Edge(INVALID));
873 // // //invalid iterators for sources
875 // // typename MutableGraph::NodeMap<Number> free(F);
877 // // dfs.pushAndSetReached(sF);
878 // // while (!dfs.finished()) {
880 // // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
881 // // if (dfs.isBNodeNewlyReached()) {
882 // // typename MutableGraph::Node v=F.aNode(dfs);
883 // // typename MutableGraph::Node w=F.bNode(dfs);
884 // // pred.set(w, dfs);
885 // // if (F.valid(pred.get(v))) {
886 // // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
888 // // free.set(w, residual_capacity.get(dfs));
891 // // __augment=true;
897 // // F.erase(typename MutableGraph::OutEdgeIt(dfs));
902 // // if (__augment) {
903 // // typename MutableGraph::Node n=tF;
904 // // Number augment_value=free.get(tF);
905 // // while (F.valid(pred.get(n))) {
906 // // typename MutableGraph::Edge e=pred.get(n);
907 // // res_graph.augment(original_edge.get(e), augment_value);
909 // // if (residual_capacity.get(e)==augment_value)
912 // // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
918 // // return _augment;
920 // bool augmentOnBlockingFlow2() {
921 // bool _augment=false;
923 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
924 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
925 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
926 // typedef typename EAugGraph::Edge EAugEdge;
928 // EAugGraph res_graph(*G, *flow, *capacity);
930 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
932 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
933 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
934 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
937 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
938 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
941 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
944 // bfs.pushAndSetReached(s);
945 // //pred.set(s, AugEdge(INVALID));
951 // //bfs.pushAndSetReached(s);
953 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
954 // NodeMap<int>& dist=res_graph.dist;
956 // while ( !bfs.finished() ) {
957 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
958 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
959 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
962 // } //computing distances from s in the residual graph
964 // bool __augment=true;
966 // while (__augment) {
969 // //computing blocking flow with dfs
970 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
971 // DfsIterator< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
973 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
974 // //pred.set(s, EAugEdge(INVALID));
975 // //invalid iterators for sources
977 // typename EAugGraph::NodeMap<Number> free(res_graph);
980 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
981 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
984 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
987 // dfs.pushAndSetReached(s);
988 // //pred.set(s, AugEdge(INVALID));
995 // //dfs.pushAndSetReached(s);
996 // typename EAugGraph::Node n;
997 // while (!dfs.finished()) {
999 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
1000 // if (dfs.isBNodeNewlyReached()) {
1002 // typename EAugGraph::Node v=res_graph.aNode(dfs);
1003 // typename EAugGraph::Node w=res_graph.bNode(dfs);
1005 // pred.set(w, EAugOutEdgeIt(dfs));
1006 // if (res_graph.valid(pred.get(v))) {
1007 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
1009 // free.set(w, res_graph.free(dfs));
1015 // for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
1024 // res_graph.erase(dfs);
1031 // // typename EAugGraph::Node n=t;
1032 // Number augment_value=free.get(n);
1033 // while (res_graph.valid(pred.get(n))) {
1034 // EAugEdge e=pred.get(n);
1035 // res_graph.augment(e, augment_value);
1036 // n=res_graph.tail(e);
1037 // if (res_graph.free(e)==0)
1038 // res_graph.erase(e);
1047 // //int num_of_augmentations=0;
1048 // while (augmentOnShortestPath()) {
1049 // //while (augmentOnBlockingFlow<MutableGraph>()) {
1050 // //std::cout << ++num_of_augmentations << " ";
1051 // //std::cout<<std::endl;
1054 // // template<typename MutableGraph> void run() {
1055 // // //int num_of_augmentations=0;
1056 // // //while (augmentOnShortestPath()) {
1057 // // while (augmentOnBlockingFlow<MutableGraph>()) {
1058 // // //std::cout << ++num_of_augmentations << " ";
1059 // // //std::cout<<std::endl;
1062 // Number flowValue() {
1065 // for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1077 // // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1078 // // class MaxFlow2 {
1080 // // typedef typename Graph::Node Node;
1081 // // typedef typename Graph::Edge Edge;
1082 // // typedef typename Graph::EdgeIt EdgeIt;
1083 // // typedef typename Graph::OutEdgeIt OutEdgeIt;
1084 // // typedef typename Graph::InEdgeIt InEdgeIt;
1086 // // const Graph& G;
1087 // // std::list<Node>& S;
1088 // // std::list<Node>& T;
1089 // // FlowMap& flow;
1090 // // const CapacityMap& capacity;
1091 // // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1092 // // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1093 // // typedef typename AugGraph::Edge AugEdge;
1094 // // typename Graph::NodeMap<bool> SMap;
1095 // // typename Graph::NodeMap<bool> TMap;
1097 // // 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) {
1098 // // for(typename std::list<Node>::const_iterator i=S.begin();
1099 // // i!=S.end(); ++i) {
1100 // // SMap.set(*i, true);
1102 // // for (typename std::list<Node>::const_iterator i=T.begin();
1103 // // i!=T.end(); ++i) {
1104 // // TMap.set(*i, true);
1107 // // bool augment() {
1108 // // AugGraph res_graph(G, flow, capacity);
1109 // // bool _augment=false;
1110 // // Node reached_t_node;
1112 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1113 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1114 // // for(typename std::list<Node>::const_iterator i=S.begin();
1115 // // i!=S.end(); ++i) {
1116 // // bfs.pushAndSetReached(*i);
1118 // // //bfs.pushAndSetReached(s);
1120 // // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1121 // // //filled up with invalid iterators
1123 // // typename AugGraph::NodeMap<Number> free(res_graph);
1125 // // //searching for augmenting path
1126 // // while ( !bfs.finished() ) {
1127 // // AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1128 // // if (e.valid() && bfs.isBNodeNewlyReached()) {
1129 // // Node v=res_graph.tail(e);
1130 // // Node w=res_graph.head(e);
1131 // // pred.set(w, e);
1132 // // if (pred.get(v).valid()) {
1133 // // free.set(w, std::min(free.get(v), e.free()));
1135 // // free.set(w, e.free());
1137 // // if (TMap.get(res_graph.head(e))) {
1138 // // _augment=true;
1139 // // reached_t_node=res_graph.head(e);
1145 // // } //end of searching augmenting path
1147 // // if (_augment) {
1148 // // Node n=reached_t_node;
1149 // // Number augment_value=free.get(reached_t_node);
1150 // // while (pred.get(n).valid()) {
1151 // // AugEdge e=pred.get(n);
1152 // // e.augment(augment_value);
1153 // // n=res_graph.tail(e);
1157 // // return _augment;
1160 // // while (augment()) { }
1162 // // Number flowValue() {
1164 // // for(typename std::list<Node>::const_iterator i=S.begin();
1165 // // i!=S.end(); ++i) {
1166 // // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1167 // // a+=flow.get(e);
1169 // // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1170 // // a-=flow.get(e);
1180 #endif //HUGO_EDMONDS_KARP_H