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 FlowMap, typename CapacityMap>
19 typedef typename Graph::Node Node;
20 typedef typename Graph::NodeIt NodeIt;
22 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
25 const CapacityMap& capacity;
27 ResGraph(const Graph& _G, FlowMap& _flow,
28 const CapacityMap& _capacity) :
29 G(_G), flow(_flow), capacity(_capacity) { }
34 friend class OutEdgeIt;
37 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
39 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
43 //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
45 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
46 return (resG->capacity.get(sym)-resG->flow.get(sym));
48 return (resG->flow.get(sym));
51 bool valid() const { return sym.valid(); }
52 void augment(Number a) const {
53 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
54 resG->flow.set(sym, resG->flow.get(sym)+a);
57 resG->flow.set(sym, resG->flow.get(sym)-a);
63 class OutEdgeIt : public Edge {
64 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
67 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
69 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
71 sym=resG->G.template first<OldSymEdgeIt>(v);
72 while( sym.valid() && !(free()>0) ) { ++sym; }
75 OutEdgeIt& operator++() {
77 while( sym.valid() && !(free()>0) ) { ++sym; }
82 void /*getF*/first(OutEdgeIt& e, Node v) const {
83 e=OutEdgeIt(*this, v);
85 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
87 template< typename It >
94 template< typename It >
95 It first(Node v) const {
101 Node tail(Edge e) const { return G.aNode(e.sym); }
102 Node head(Edge e) const { return G.bNode(e.sym); }
104 Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
105 Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
107 int id(Node v) const { return G.id(v); }
109 template <typename S>
111 typename Graph::NodeMap<S> node_map;
113 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
114 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
115 void set(Node nit, S a) { node_map.set(nit, a); }
116 S get(Node nit) const { return node_map.get(nit); }
117 S& operator[](Node nit) { return node_map[nit]; }
118 const S& operator[](Node nit) const { return node_map[nit]; }
124 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
127 typedef typename Graph::Node Node;
128 typedef typename Graph::NodeIt NodeIt;
130 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
131 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
132 typedef typename Graph::InEdgeIt OldInEdgeIt;
136 const CapacityMap& capacity;
138 ResGraph2(const Graph& _G, FlowMap& _flow,
139 const CapacityMap& _capacity) :
140 G(_G), flow(_flow), capacity(_capacity) { }
145 friend class OutEdgeIt;
148 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
150 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
154 bool out_or_in; //true, iff out
156 Edge() : out_or_in(true) { }
157 Number free() const {
159 return (resG->capacity.get(out)-resG->flow.get(out));
161 return (resG->flow.get(in));
165 return out_or_in && out.valid() || in.valid(); }
166 void augment(Number a) const {
168 resG->flow.set(out, resG->flow.get(out)+a);
170 resG->flow.set(in, resG->flow.get(in)-a);
175 class OutEdgeIt : public Edge {
176 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
180 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
182 out=resG->G.template first<OldOutEdgeIt>(v);
183 while( out.valid() && !(free()>0) ) { ++out; }
186 in=resG->G.template first<OldInEdgeIt>(v);
187 while( in.valid() && !(free()>0) ) { ++in; }
191 OutEdgeIt& operator++() {
193 Node v=resG->G.aNode(out);
195 while( out.valid() && !(free()>0) ) { ++out; }
198 in=resG->G.template first<OldInEdgeIt>(v);
199 while( in.valid() && !(free()>0) ) { ++in; }
203 while( in.valid() && !(free()>0) ) { ++in; }
209 void /*getF*/first(OutEdgeIt& e, Node v) const {
210 e=OutEdgeIt(*this, v);
212 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
214 template< typename It >
221 template< typename It >
222 It first(Node v) const {
228 Node tail(Edge e) const {
229 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
230 Node head(Edge e) const {
231 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
233 Node aNode(OutEdgeIt e) const {
234 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
235 Node bNode(OutEdgeIt e) const {
236 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
238 int id(Node v) const { return G.id(v); }
240 template <typename S>
242 typename Graph::NodeMap<S> node_map;
244 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
245 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
246 void set(Node nit, S a) { node_map.set(nit, a); }
247 S get(Node nit) const { return node_map.get(nit); }
252 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
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;
264 const CapacityMap* capacity;
265 typedef ResGraphWrapper<const Graph, Number, FlowMap, CapacityMap > ResGW;
266 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
267 typedef typename ResGW::Edge ResGWEdge;
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) { }
273 bool augmentOnShortestPath() {
274 ResGW res_graph(*g, *flow, *capacity);
277 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
278 bfs.pushAndSetReached(s);
280 typename ResGW::NodeMap<ResGWEdge> pred(res_graph);
281 pred.set(s, INVALID);
283 typename ResGW::NodeMap<Number> free(res_graph);
285 //searching for augmenting path
286 while ( !bfs.finished() ) {
287 ResGWOutEdgeIt e=bfs;
288 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
289 Node v=res_graph.tail(e);
290 Node w=res_graph.head(e);
292 if (res_graph.valid(pred[v])) {
293 free.set(w, std::min(free[v], res_graph.resCap(e)));
295 free.set(w, res_graph.resCap(e));
297 if (res_graph.head(e)==t) { _augment=true; break; }
301 } //end of searching augmenting path
305 Number augment_value=free[t];
306 while (res_graph.valid(pred[n])) {
308 res_graph.augment(e, augment_value);
316 template<typename MapGraphWrapper>
319 const MapGraphWrapper* g;
320 typename MapGraphWrapper::NodeMap<int> dist;
322 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
323 void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; }
324 int operator[](const typename MapGraphWrapper::Node& n)
326 // int get(const typename MapGraphWrapper::Node& n) const {
328 // bool get(const typename MapGraphWrapper::Edge& e) const {
329 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
330 bool operator[](const typename MapGraphWrapper::Edge& e) const {
331 return (dist[g->tail(e)]<dist[g->head(e)]);
335 template<typename MutableGraph> bool augmentOnBlockingFlow() {
336 typedef MutableGraph MG;
339 ResGW res_graph(*g, *flow, *capacity);
341 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
343 bfs.pushAndSetReached(s);
344 //typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
345 DistanceMap<ResGW> dist(res_graph);
346 while ( !bfs.finished() ) {
347 ResGWOutEdgeIt e=bfs;
348 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
349 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
352 } //computing distances from s in the residual graph
355 ConstMap<typename ResGW::Node, bool> true_map(true);
356 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
357 DistanceMap<ResGW> > FilterResGW;
358 FilterResGW filter_res_graph(res_graph, true_map, dist);
359 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
361 typename ResGW::NodeIt n;
362 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
363 res_graph_to_F.set(n, F.addNode());
367 typename MG::Node sF=res_graph_to_F[s];
368 typename MG::Node tF=res_graph_to_F[t];
369 typename MG::EdgeMap<ResGWEdge> original_edge(F);
370 typename MG::EdgeMap<Number> residual_capacity(F);
372 //Making F to the graph containing the edges of the residual graph
373 //which are in some shortest paths
375 typename FilterResGW::EdgeIt e;
376 for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
377 //if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
378 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
379 original_edge.update();
380 original_edge.set(f, e);
381 residual_capacity.update();
382 residual_capacity.set(f, res_graph.resCap(e));
391 //computing blocking flow with dfs
393 DfsIterator5< MG, typename MG::NodeMap<bool> > dfs(F);
394 typename MG::NodeMap<typename MG::Edge> pred(F);
395 pred.set(sF, INVALID);
396 //invalid iterators for sources
398 typename MG::NodeMap<Number> free(F);
400 dfs.pushAndSetReached(sF);
401 while (!dfs.finished()) {
403 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
404 if (dfs.isBNodeNewlyReached()) {
405 typename MG::Node v=F.aNode(dfs);
406 typename MG::Node w=F.bNode(dfs);
408 if (F.valid(pred[v])) {
409 free.set(w, std::min(free[v], residual_capacity[dfs]));
411 free.set(w, residual_capacity[dfs]);
420 F.erase(/*typename MG::OutEdgeIt*/(dfs));
426 typename MG::Node n=tF;
427 Number augment_value=free[tF];
428 while (F.valid(pred[n])) {
429 typename MG::Edge e=pred[n];
430 res_graph.augment(original_edge[e], augment_value);
432 if (residual_capacity[e]==augment_value)
435 residual_capacity.set(e, residual_capacity[e]-augment_value);
444 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
445 typedef MutableGraph MG;
448 ResGW res_graph(*g, *flow, *capacity);
450 //bfs for distances on the residual graph
451 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
452 bfs.pushAndSetReached(s);
453 typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
455 //F will contain the physical copy of the residual graph
456 //with the set of edges which are on shortest paths
458 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
460 typename ResGW::NodeIt n;
461 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
462 res_graph_to_F.set(n, F.addNode());
466 typename MG::Node sF=res_graph_to_F[s];
467 typename MG::Node tF=res_graph_to_F[t];
468 typename MG::EdgeMap<ResGWEdge> original_edge(F);
469 typename MG::EdgeMap<Number> residual_capacity(F);
471 while ( !bfs.finished() ) {
472 ResGWOutEdgeIt e=bfs;
473 if (res_graph.valid(e)) {
474 if (bfs.isBNodeNewlyReached()) {
475 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
476 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
477 original_edge.update();
478 original_edge.set(f, e);
479 residual_capacity.update();
480 residual_capacity.set(f, res_graph.resCap(e));
482 if (dist[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));
492 } //computing distances from s in the residual graph
498 //computing blocking flow with dfs
499 DfsIterator5< MG, typename MG::NodeMap<bool> > dfs(F);
500 typename MG::NodeMap<typename MG::Edge> pred(F);
501 pred.set(sF, INVALID);
502 //invalid iterators for sources
504 typename MG::NodeMap<Number> free(F);
506 dfs.pushAndSetReached(sF);
507 while (!dfs.finished()) {
509 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
510 if (dfs.isBNodeNewlyReached()) {
511 typename MG::Node v=F.aNode(dfs);
512 typename MG::Node w=F.bNode(dfs);
514 if (F.valid(pred[v])) {
515 free.set(w, std::min(free[v], residual_capacity[dfs]));
517 free.set(w, residual_capacity[dfs]);
526 F.erase(/*typename MG::OutEdgeIt*/(dfs));
532 typename MG::Node n=tF;
533 Number augment_value=free[tF];
534 while (F.valid(pred[n])) {
535 typename MG::Edge e=pred[n];
536 res_graph.augment(original_edge[e], augment_value);
538 if (residual_capacity[e]==augment_value)
541 residual_capacity.set(e, residual_capacity[e]-augment_value);
550 bool augmentOnBlockingFlow2() {
553 ResGW res_graph(*g, *flow, *capacity);
555 BfsIterator5< ResGW, typename ResGW::NodeMap<bool> > bfs(res_graph);
557 bfs.pushAndSetReached(s);
558 DistanceMap<ResGW> dist(res_graph);
559 while ( !bfs.finished() ) {
560 ResGWOutEdgeIt e=bfs;
561 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
562 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
565 } //computing distances from s in the residual graph
567 //Subgraph containing the edges on some shortest paths
568 ConstMap<typename ResGW::Node, bool> true_map(true);
569 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
570 DistanceMap<ResGW> > FilterResGW;
571 FilterResGW filter_res_graph(res_graph, true_map, dist);
573 //Subgraph, which is able to delete edges which are already
575 typename FilterResGW::NodeMap<typename FilterResGW::OutEdgeIt>
576 first_out_edges(filter_res_graph);
577 typename FilterResGW::NodeIt v;
578 for(filter_res_graph.first(v); filter_res_graph.valid(v);
579 filter_res_graph.next(v))
581 typename FilterResGW::OutEdgeIt e;
582 filter_res_graph.first(e, v);
583 first_out_edges.set(v, e);
585 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
586 NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
587 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
594 //computing blocking flow with dfs
595 DfsIterator5< ErasingResGW, typename ErasingResGW::NodeMap<bool> >
596 dfs(erasing_res_graph);
597 typename ErasingResGW::NodeMap<typename ErasingResGW::OutEdgeIt>
598 pred(erasing_res_graph);
599 pred.set(s, INVALID);
600 //invalid iterators for sources
602 typename ErasingResGW::NodeMap<Number> free(erasing_res_graph);
604 dfs.pushAndSetReached(s);
605 while (!dfs.finished()) {
607 if (erasing_res_graph.valid(
608 /*typename ErasingResGW::OutEdgeIt*/(dfs)))
610 if (dfs.isBNodeNewlyReached()) {
612 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
613 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
615 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
616 if (erasing_res_graph.valid(pred[v])) {
617 free.set(w, std::min(free[v], res_graph.resCap(dfs)));
619 free.set(w, res_graph.resCap(dfs));
628 erasing_res_graph.erase(dfs);
634 typename ErasingResGW::Node n=t;
635 Number augment_value=free[n];
636 while (erasing_res_graph.valid(pred[n])) {
637 typename ErasingResGW::OutEdgeIt e=pred[n];
638 res_graph.augment(e, augment_value);
639 n=erasing_res_graph.tail(e);
640 if (res_graph.resCap(e)==0)
641 erasing_res_graph.erase(e);
645 } //while (__augment)
651 //int num_of_augmentations=0;
652 while (augmentOnShortestPath()) {
653 //while (augmentOnBlockingFlow<MutableGraph>()) {
654 //std::cout << ++num_of_augmentations << " ";
655 //std::cout<<std::endl;
659 template<typename MutableGraph> void run() {
660 //int num_of_augmentations=0;
661 //while (augmentOnShortestPath()) {
662 while (augmentOnBlockingFlow<MutableGraph>()) {
663 //std::cout << ++num_of_augmentations << " ";
664 //std::cout<<std::endl;
671 for(g->first(e, s); g->valid(e); g->next(e)) {
680 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
681 // class MaxMatching {
683 // typedef typename Graph::Node Node;
684 // typedef typename Graph::NodeIt NodeIt;
685 // typedef typename Graph::Edge Edge;
686 // typedef typename Graph::EdgeIt EdgeIt;
687 // typedef typename Graph::OutEdgeIt OutEdgeIt;
688 // typedef typename Graph::InEdgeIt InEdgeIt;
690 // typedef typename Graph::NodeMap<bool> SMap;
691 // typedef typename Graph::NodeMap<bool> TMap;
699 // const CapacityMap* capacity;
700 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
701 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
702 // typedef typename AugGraph::Edge AugEdge;
703 // typename Graph::NodeMap<int> used; //0
706 // MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
707 // G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
708 // bool augmentOnShortestPath() {
709 // AugGraph res_graph(*G, *flow, *capacity);
710 // bool _augment=false;
712 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
713 // BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
714 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
715 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
716 // if ((S->get(s)) && (used.get(s)<1) ) {
718 // //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
719 // //u+=flow->get(e);
721 // bfs.pushAndSetReached(s);
722 // pred.set(s, AugEdge(INVALID));
727 // typename AugGraph::NodeMap<Number> free(res_graph);
730 // //searching for augmenting path
731 // while ( !bfs.finished() ) {
732 // AugOutEdgeIt e=bfs;
733 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
734 // Node v=res_graph.tail(e);
735 // Node w=res_graph.head(e);
737 // if (res_graph.valid(pred.get(v))) {
738 // free.set(w, std::min(free.get(v), res_graph.free(e)));
740 // free.set(w, res_graph.free(e));
742 // n=res_graph.head(e);
743 // if (T->get(n) && (used.get(n)<1) ) {
745 // //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
746 // //u+=flow->get(f);
755 // } //end of searching augmenting path
759 // used.set(n, 1); //mind2 vegen jav
760 // Number augment_value=free.get(n);
761 // while (res_graph.valid(pred.get(n))) {
762 // AugEdge e=pred.get(n);
763 // res_graph.augment(e, augment_value);
764 // n=res_graph.tail(e);
766 // used.set(n, 1); //mind2 vegen jav
772 // // template<typename MutableGraph> bool augmentOnBlockingFlow() {
773 // // bool _augment=false;
775 // // AugGraph res_graph(*G, *flow, *capacity);
777 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
778 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
784 // // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
785 // // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
786 // // if (S->get(s)) {
788 // // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
789 // // u+=flow->get(e);
791 // // bfs.pushAndSetReached(s);
792 // // //pred.set(s, AugEdge(INVALID));
800 // // //bfs.pushAndSetReached(s);
801 // // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
802 // // while ( !bfs.finished() ) {
803 // // AugOutEdgeIt e=bfs;
804 // // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
805 // // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
809 // // } //computing distances from s in the residual graph
811 // // MutableGraph F;
812 // // typename AugGraph::NodeMap<typename MutableGraph::Node>
813 // // res_graph_to_F(res_graph);
814 // // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
815 // // res_graph_to_F.set(n, F.addNode());
818 // // typename MutableGraph::Node sF=res_graph_to_F.get(s);
819 // // typename MutableGraph::Node tF=res_graph_to_F.get(t);
821 // // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
822 // // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
824 // // //Making F to the graph containing the edges of the residual graph
825 // // //which are in some shortest paths
826 // // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
827 // // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
828 // // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
829 // // original_edge.update();
830 // // original_edge.set(f, e);
831 // // residual_capacity.update();
832 // // residual_capacity.set(f, res_graph.free(e));
836 // // bool __augment=true;
838 // // while (__augment) {
839 // // __augment=false;
840 // // //computing blocking flow with dfs
841 // // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
842 // // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
843 // // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
844 // // pred.set(sF, typename MutableGraph::Edge(INVALID));
845 // // //invalid iterators for sources
847 // // typename MutableGraph::NodeMap<Number> free(F);
849 // // dfs.pushAndSetReached(sF);
850 // // while (!dfs.finished()) {
852 // // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
853 // // if (dfs.isBNodeNewlyReached()) {
854 // // typename MutableGraph::Node v=F.aNode(dfs);
855 // // typename MutableGraph::Node w=F.bNode(dfs);
856 // // pred.set(w, dfs);
857 // // if (F.valid(pred.get(v))) {
858 // // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
860 // // free.set(w, residual_capacity.get(dfs));
863 // // __augment=true;
869 // // F.erase(typename MutableGraph::OutEdgeIt(dfs));
874 // // if (__augment) {
875 // // typename MutableGraph::Node n=tF;
876 // // Number augment_value=free.get(tF);
877 // // while (F.valid(pred.get(n))) {
878 // // typename MutableGraph::Edge e=pred.get(n);
879 // // res_graph.augment(original_edge.get(e), augment_value);
881 // // if (residual_capacity.get(e)==augment_value)
884 // // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
890 // // return _augment;
892 // bool augmentOnBlockingFlow2() {
893 // bool _augment=false;
895 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
896 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
897 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
898 // typedef typename EAugGraph::Edge EAugEdge;
900 // EAugGraph res_graph(*G, *flow, *capacity);
902 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
904 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
905 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
906 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
909 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
910 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
913 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
916 // bfs.pushAndSetReached(s);
917 // //pred.set(s, AugEdge(INVALID));
923 // //bfs.pushAndSetReached(s);
925 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
926 // NodeMap<int>& dist=res_graph.dist;
928 // while ( !bfs.finished() ) {
929 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
930 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
931 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
934 // } //computing distances from s in the residual graph
936 // bool __augment=true;
938 // while (__augment) {
941 // //computing blocking flow with dfs
942 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
943 // DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
945 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
946 // //pred.set(s, EAugEdge(INVALID));
947 // //invalid iterators for sources
949 // typename EAugGraph::NodeMap<Number> free(res_graph);
952 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
953 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
956 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
959 // dfs.pushAndSetReached(s);
960 // //pred.set(s, AugEdge(INVALID));
967 // //dfs.pushAndSetReached(s);
968 // typename EAugGraph::Node n;
969 // while (!dfs.finished()) {
971 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
972 // if (dfs.isBNodeNewlyReached()) {
974 // typename EAugGraph::Node v=res_graph.aNode(dfs);
975 // typename EAugGraph::Node w=res_graph.bNode(dfs);
977 // pred.set(w, EAugOutEdgeIt(dfs));
978 // if (res_graph.valid(pred.get(v))) {
979 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
981 // free.set(w, res_graph.free(dfs));
987 // for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
996 // res_graph.erase(dfs);
1003 // // typename EAugGraph::Node n=t;
1004 // Number augment_value=free.get(n);
1005 // while (res_graph.valid(pred.get(n))) {
1006 // EAugEdge e=pred.get(n);
1007 // res_graph.augment(e, augment_value);
1008 // n=res_graph.tail(e);
1009 // if (res_graph.free(e)==0)
1010 // res_graph.erase(e);
1019 // //int num_of_augmentations=0;
1020 // while (augmentOnShortestPath()) {
1021 // //while (augmentOnBlockingFlow<MutableGraph>()) {
1022 // //std::cout << ++num_of_augmentations << " ";
1023 // //std::cout<<std::endl;
1026 // // template<typename MutableGraph> void run() {
1027 // // //int num_of_augmentations=0;
1028 // // //while (augmentOnShortestPath()) {
1029 // // while (augmentOnBlockingFlow<MutableGraph>()) {
1030 // // //std::cout << ++num_of_augmentations << " ";
1031 // // //std::cout<<std::endl;
1034 // Number flowValue() {
1037 // for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1049 // // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1050 // // class MaxFlow2 {
1052 // // typedef typename Graph::Node Node;
1053 // // typedef typename Graph::Edge Edge;
1054 // // typedef typename Graph::EdgeIt EdgeIt;
1055 // // typedef typename Graph::OutEdgeIt OutEdgeIt;
1056 // // typedef typename Graph::InEdgeIt InEdgeIt;
1058 // // const Graph& G;
1059 // // std::list<Node>& S;
1060 // // std::list<Node>& T;
1061 // // FlowMap& flow;
1062 // // const CapacityMap& capacity;
1063 // // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1064 // // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1065 // // typedef typename AugGraph::Edge AugEdge;
1066 // // typename Graph::NodeMap<bool> SMap;
1067 // // typename Graph::NodeMap<bool> TMap;
1069 // // 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) {
1070 // // for(typename std::list<Node>::const_iterator i=S.begin();
1071 // // i!=S.end(); ++i) {
1072 // // SMap.set(*i, true);
1074 // // for (typename std::list<Node>::const_iterator i=T.begin();
1075 // // i!=T.end(); ++i) {
1076 // // TMap.set(*i, true);
1079 // // bool augment() {
1080 // // AugGraph res_graph(G, flow, capacity);
1081 // // bool _augment=false;
1082 // // Node reached_t_node;
1084 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1085 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1086 // // for(typename std::list<Node>::const_iterator i=S.begin();
1087 // // i!=S.end(); ++i) {
1088 // // bfs.pushAndSetReached(*i);
1090 // // //bfs.pushAndSetReached(s);
1092 // // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1093 // // //filled up with invalid iterators
1095 // // typename AugGraph::NodeMap<Number> free(res_graph);
1097 // // //searching for augmenting path
1098 // // while ( !bfs.finished() ) {
1099 // // AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1100 // // if (e.valid() && bfs.isBNodeNewlyReached()) {
1101 // // Node v=res_graph.tail(e);
1102 // // Node w=res_graph.head(e);
1103 // // pred.set(w, e);
1104 // // if (pred.get(v).valid()) {
1105 // // free.set(w, std::min(free.get(v), e.free()));
1107 // // free.set(w, e.free());
1109 // // if (TMap.get(res_graph.head(e))) {
1110 // // _augment=true;
1111 // // reached_t_node=res_graph.head(e);
1117 // // } //end of searching augmenting path
1119 // // if (_augment) {
1120 // // Node n=reached_t_node;
1121 // // Number augment_value=free.get(reached_t_node);
1122 // // while (pred.get(n).valid()) {
1123 // // AugEdge e=pred.get(n);
1124 // // e.augment(augment_value);
1125 // // n=res_graph.tail(e);
1129 // // return _augment;
1132 // // while (augment()) { }
1134 // // Number flowValue() {
1136 // // for(typename std::list<Node>::const_iterator i=S.begin();
1137 // // i!=S.end(); ++i) {
1138 // // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1139 // // a+=flow.get(e);
1141 // // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1142 // // a-=flow.get(e);
1152 #endif //HUGO_EDMONDS_KARP_H