Converted the "minlengthpaths" alg. to the new style graph_wrappers.
2 #ifndef HUGO_EDMONDS_KARP_H
3 #define HUGO_EDMONDS_KARP_H
9 #include <bfs_iterator.h>
11 #include <graph_wrapper.h>
15 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
18 typedef typename Graph::Node Node;
19 typedef typename Graph::NodeIt NodeIt;
21 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
24 const CapacityMap& capacity;
26 ResGraph(const Graph& _G, FlowMap& _flow,
27 const CapacityMap& _capacity) :
28 G(_G), flow(_flow), capacity(_capacity) { }
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) { }
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);
56 resG->flow.set(sym, resG->flow.get(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 {
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) { }
144 friend class OutEdgeIt;
147 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
149 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
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));
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; }
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; }
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 >
220 template< typename It >
221 It first(Node v) const {
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, typename FlowMap, typename CapacityMap>
254 typedef typename Graph::Node Node;
255 typedef typename Graph::Edge Edge;
256 typedef typename Graph::EdgeIt EdgeIt;
257 typedef typename Graph::OutEdgeIt OutEdgeIt;
258 typedef typename Graph::InEdgeIt InEdgeIt;
263 const CapacityMap* capacity;
264 typedef ResGraphWrapper<const Graph, Number, FlowMap, CapacityMap > ResGW;
265 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
266 typedef typename ResGW::Edge ResGWEdge;
269 MaxFlow(const Graph& _g, Node _s, Node _t, FlowMap& _flow, const CapacityMap& _capacity) :
270 g(&_g), s(_s), t(_t), flow(&_flow), capacity(&_capacity) { }
272 bool augmentOnShortestPath() {
273 ResGW res_graph(*g, *flow, *capacity);
276 typedef typename ResGW::NodeMap<bool> ReachedMap;
277 BfsIterator5< ResGW, ReachedMap > 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 typedef typename ResGW::NodeMap<bool> ReachedMap;
342 BfsIterator5< ResGW, ReachedMap > 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 typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
357 FilterResGW filter_res_graph(res_graph, dist);
358 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
360 typename ResGW::NodeIt n;
361 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
362 res_graph_to_F.set(n, F.addNode());
366 typename MG::Node sF=res_graph_to_F[s];
367 typename MG::Node tF=res_graph_to_F[t];
368 typename MG::EdgeMap<ResGWEdge> original_edge(F);
369 typename MG::EdgeMap<Number> residual_capacity(F);
371 //Making F to the graph containing the edges of the residual graph
372 //which are in some shortest paths
374 typename FilterResGW::EdgeIt e;
375 for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
376 //if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
377 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
378 original_edge.update();
379 original_edge.set(f, e);
380 residual_capacity.update();
381 residual_capacity.set(f, res_graph.resCap(e));
390 //computing blocking flow with dfs
391 typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
392 DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > dfs(F);
393 typename MG::NodeMap<typename MG::Edge> pred(F);
394 pred.set(sF, INVALID);
395 //invalid iterators for sources
397 typename MG::NodeMap<Number> free(F);
399 dfs.pushAndSetReached(sF);
400 while (!dfs.finished()) {
402 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
403 if (dfs.isBNodeNewlyReached()) {
404 typename MG::Node v=F.aNode(dfs);
405 typename MG::Node w=F.bNode(dfs);
407 if (F.valid(pred[v])) {
408 free.set(w, std::min(free[v], residual_capacity[dfs]));
410 free.set(w, residual_capacity[dfs]);
419 F.erase(/*typename MG::OutEdgeIt*/(dfs));
425 typename MG::Node n=tF;
426 Number augment_value=free[tF];
427 while (F.valid(pred[n])) {
428 typename MG::Edge e=pred[n];
429 res_graph.augment(original_edge[e], augment_value);
431 if (residual_capacity[e]==augment_value)
434 residual_capacity.set(e, residual_capacity[e]-augment_value);
443 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
444 typedef MutableGraph MG;
447 ResGW res_graph(*g, *flow, *capacity);
449 //bfs for distances on the residual graph
450 typedef typename ResGW::NodeMap<bool> ReachedMap;
451 BfsIterator5< ResGW, ReachedMap > 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 typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
500 DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > 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, *flow, *capacity);
556 typedef typename ResGW::NodeMap<bool> ReachedMap;
557 BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
559 bfs.pushAndSetReached(s);
560 DistanceMap<ResGW> dist(res_graph);
561 while ( !bfs.finished() ) {
562 ResGWOutEdgeIt e=bfs;
563 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
564 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
567 } //computing distances from s in the residual graph
569 //Subgraph containing the edges on some shortest paths
570 typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
571 FilterResGW filter_res_graph(res_graph, 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 typedef typename ErasingResGW::NodeMap<bool> BlockingReachedMap;
596 DfsIterator5< ErasingResGW, BlockingReachedMap >
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> free(erasing_res_graph);
605 dfs.pushAndSetReached(s);
606 while (!dfs.finished()) {
608 if (erasing_res_graph.valid(
609 /*typename ErasingResGW::OutEdgeIt*/(dfs)))
611 if (dfs.isBNodeNewlyReached()) {
613 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
614 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
616 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
617 if (erasing_res_graph.valid(pred[v])) {
618 free.set(w, std::min(free[v], res_graph.resCap(dfs)));
620 free.set(w, res_graph.resCap(dfs));
629 erasing_res_graph.erase(dfs);
635 typename ErasingResGW::Node n=t;
636 Number augment_value=free[n];
637 while (erasing_res_graph.valid(pred[n])) {
638 typename ErasingResGW::OutEdgeIt e=pred[n];
639 res_graph.augment(e, augment_value);
640 n=erasing_res_graph.tail(e);
641 if (res_graph.resCap(e)==0)
642 erasing_res_graph.erase(e);
646 } //while (__augment)
652 //int num_of_augmentations=0;
653 while (augmentOnShortestPath()) {
654 //while (augmentOnBlockingFlow<MutableGraph>()) {
655 //std::cout << ++num_of_augmentations << " ";
656 //std::cout<<std::endl;
660 template<typename MutableGraph> void run() {
661 //int num_of_augmentations=0;
662 //while (augmentOnShortestPath()) {
663 while (augmentOnBlockingFlow<MutableGraph>()) {
664 //std::cout << ++num_of_augmentations << " ";
665 //std::cout<<std::endl;
672 for(g->first(e, s); g->valid(e); g->next(e)) {
681 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
682 // class MaxMatching {
684 // typedef typename Graph::Node Node;
685 // typedef typename Graph::NodeIt NodeIt;
686 // typedef typename Graph::Edge Edge;
687 // typedef typename Graph::EdgeIt EdgeIt;
688 // typedef typename Graph::OutEdgeIt OutEdgeIt;
689 // typedef typename Graph::InEdgeIt InEdgeIt;
691 // typedef typename Graph::NodeMap<bool> SMap;
692 // typedef typename Graph::NodeMap<bool> TMap;
700 // const CapacityMap* capacity;
701 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
702 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
703 // typedef typename AugGraph::Edge AugEdge;
704 // typename Graph::NodeMap<int> used; //0
707 // MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
708 // G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
709 // bool augmentOnShortestPath() {
710 // AugGraph res_graph(*G, *flow, *capacity);
711 // bool _augment=false;
713 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
714 // BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
715 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
716 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
717 // if ((S->get(s)) && (used.get(s)<1) ) {
719 // //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
720 // //u+=flow->get(e);
722 // bfs.pushAndSetReached(s);
723 // pred.set(s, AugEdge(INVALID));
728 // typename AugGraph::NodeMap<Number> free(res_graph);
731 // //searching for augmenting path
732 // while ( !bfs.finished() ) {
733 // AugOutEdgeIt e=bfs;
734 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
735 // Node v=res_graph.tail(e);
736 // Node w=res_graph.head(e);
738 // if (res_graph.valid(pred.get(v))) {
739 // free.set(w, std::min(free.get(v), res_graph.free(e)));
741 // free.set(w, res_graph.free(e));
743 // n=res_graph.head(e);
744 // if (T->get(n) && (used.get(n)<1) ) {
746 // //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
747 // //u+=flow->get(f);
756 // } //end of searching augmenting path
760 // used.set(n, 1); //mind2 vegen jav
761 // Number augment_value=free.get(n);
762 // while (res_graph.valid(pred.get(n))) {
763 // AugEdge e=pred.get(n);
764 // res_graph.augment(e, augment_value);
765 // n=res_graph.tail(e);
767 // used.set(n, 1); //mind2 vegen jav
773 // // template<typename MutableGraph> bool augmentOnBlockingFlow() {
774 // // bool _augment=false;
776 // // AugGraph res_graph(*G, *flow, *capacity);
778 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
779 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
785 // // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
786 // // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
787 // // if (S->get(s)) {
789 // // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
790 // // u+=flow->get(e);
792 // // bfs.pushAndSetReached(s);
793 // // //pred.set(s, AugEdge(INVALID));
801 // // //bfs.pushAndSetReached(s);
802 // // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
803 // // while ( !bfs.finished() ) {
804 // // AugOutEdgeIt e=bfs;
805 // // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
806 // // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
810 // // } //computing distances from s in the residual graph
812 // // MutableGraph F;
813 // // typename AugGraph::NodeMap<typename MutableGraph::Node>
814 // // res_graph_to_F(res_graph);
815 // // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
816 // // res_graph_to_F.set(n, F.addNode());
819 // // typename MutableGraph::Node sF=res_graph_to_F.get(s);
820 // // typename MutableGraph::Node tF=res_graph_to_F.get(t);
822 // // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
823 // // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
825 // // //Making F to the graph containing the edges of the residual graph
826 // // //which are in some shortest paths
827 // // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
828 // // if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
829 // // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
830 // // original_edge.update();
831 // // original_edge.set(f, e);
832 // // residual_capacity.update();
833 // // residual_capacity.set(f, res_graph.free(e));
837 // // bool __augment=true;
839 // // while (__augment) {
840 // // __augment=false;
841 // // //computing blocking flow with dfs
842 // // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
843 // // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
844 // // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
845 // // pred.set(sF, typename MutableGraph::Edge(INVALID));
846 // // //invalid iterators for sources
848 // // typename MutableGraph::NodeMap<Number> free(F);
850 // // dfs.pushAndSetReached(sF);
851 // // while (!dfs.finished()) {
853 // // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
854 // // if (dfs.isBNodeNewlyReached()) {
855 // // typename MutableGraph::Node v=F.aNode(dfs);
856 // // typename MutableGraph::Node w=F.bNode(dfs);
857 // // pred.set(w, dfs);
858 // // if (F.valid(pred.get(v))) {
859 // // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
861 // // free.set(w, residual_capacity.get(dfs));
864 // // __augment=true;
870 // // F.erase(typename MutableGraph::OutEdgeIt(dfs));
875 // // if (__augment) {
876 // // typename MutableGraph::Node n=tF;
877 // // Number augment_value=free.get(tF);
878 // // while (F.valid(pred.get(n))) {
879 // // typename MutableGraph::Edge e=pred.get(n);
880 // // res_graph.augment(original_edge.get(e), augment_value);
882 // // if (residual_capacity.get(e)==augment_value)
885 // // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
891 // // return _augment;
893 // bool augmentOnBlockingFlow2() {
894 // bool _augment=false;
896 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
897 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
898 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
899 // typedef typename EAugGraph::Edge EAugEdge;
901 // EAugGraph res_graph(*G, *flow, *capacity);
903 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
905 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
906 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
907 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
910 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
911 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
914 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
917 // bfs.pushAndSetReached(s);
918 // //pred.set(s, AugEdge(INVALID));
924 // //bfs.pushAndSetReached(s);
926 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
927 // NodeMap<int>& dist=res_graph.dist;
929 // while ( !bfs.finished() ) {
930 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
931 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
932 // dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
935 // } //computing distances from s in the residual graph
937 // bool __augment=true;
939 // while (__augment) {
942 // //computing blocking flow with dfs
943 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
944 // DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
946 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
947 // //pred.set(s, EAugEdge(INVALID));
948 // //invalid iterators for sources
950 // typename EAugGraph::NodeMap<Number> free(res_graph);
953 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
954 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
957 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
960 // dfs.pushAndSetReached(s);
961 // //pred.set(s, AugEdge(INVALID));
968 // //dfs.pushAndSetReached(s);
969 // typename EAugGraph::Node n;
970 // while (!dfs.finished()) {
972 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
973 // if (dfs.isBNodeNewlyReached()) {
975 // typename EAugGraph::Node v=res_graph.aNode(dfs);
976 // typename EAugGraph::Node w=res_graph.bNode(dfs);
978 // pred.set(w, EAugOutEdgeIt(dfs));
979 // if (res_graph.valid(pred.get(v))) {
980 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
982 // free.set(w, res_graph.free(dfs));
988 // for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
997 // res_graph.erase(dfs);
1004 // // typename EAugGraph::Node n=t;
1005 // Number augment_value=free.get(n);
1006 // while (res_graph.valid(pred.get(n))) {
1007 // EAugEdge e=pred.get(n);
1008 // res_graph.augment(e, augment_value);
1009 // n=res_graph.tail(e);
1010 // if (res_graph.free(e)==0)
1011 // res_graph.erase(e);
1020 // //int num_of_augmentations=0;
1021 // while (augmentOnShortestPath()) {
1022 // //while (augmentOnBlockingFlow<MutableGraph>()) {
1023 // //std::cout << ++num_of_augmentations << " ";
1024 // //std::cout<<std::endl;
1027 // // template<typename MutableGraph> void run() {
1028 // // //int num_of_augmentations=0;
1029 // // //while (augmentOnShortestPath()) {
1030 // // while (augmentOnBlockingFlow<MutableGraph>()) {
1031 // // //std::cout << ++num_of_augmentations << " ";
1032 // // //std::cout<<std::endl;
1035 // Number flowValue() {
1038 // for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1050 // // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1051 // // class MaxFlow2 {
1053 // // typedef typename Graph::Node Node;
1054 // // typedef typename Graph::Edge Edge;
1055 // // typedef typename Graph::EdgeIt EdgeIt;
1056 // // typedef typename Graph::OutEdgeIt OutEdgeIt;
1057 // // typedef typename Graph::InEdgeIt InEdgeIt;
1059 // // const Graph& G;
1060 // // std::list<Node>& S;
1061 // // std::list<Node>& T;
1062 // // FlowMap& flow;
1063 // // const CapacityMap& capacity;
1064 // // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1065 // // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1066 // // typedef typename AugGraph::Edge AugEdge;
1067 // // typename Graph::NodeMap<bool> SMap;
1068 // // typename Graph::NodeMap<bool> TMap;
1070 // // 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) {
1071 // // for(typename std::list<Node>::const_iterator i=S.begin();
1072 // // i!=S.end(); ++i) {
1073 // // SMap.set(*i, true);
1075 // // for (typename std::list<Node>::const_iterator i=T.begin();
1076 // // i!=T.end(); ++i) {
1077 // // TMap.set(*i, true);
1080 // // bool augment() {
1081 // // AugGraph res_graph(G, flow, capacity);
1082 // // bool _augment=false;
1083 // // Node reached_t_node;
1085 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1086 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1087 // // for(typename std::list<Node>::const_iterator i=S.begin();
1088 // // i!=S.end(); ++i) {
1089 // // bfs.pushAndSetReached(*i);
1091 // // //bfs.pushAndSetReached(s);
1093 // // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1094 // // //filled up with invalid iterators
1096 // // typename AugGraph::NodeMap<Number> free(res_graph);
1098 // // //searching for augmenting path
1099 // // while ( !bfs.finished() ) {
1100 // // AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1101 // // if (e.valid() && bfs.isBNodeNewlyReached()) {
1102 // // Node v=res_graph.tail(e);
1103 // // Node w=res_graph.head(e);
1104 // // pred.set(w, e);
1105 // // if (pred.get(v).valid()) {
1106 // // free.set(w, std::min(free.get(v), e.free()));
1108 // // free.set(w, e.free());
1110 // // if (TMap.get(res_graph.head(e))) {
1111 // // _augment=true;
1112 // // reached_t_node=res_graph.head(e);
1118 // // } //end of searching augmenting path
1120 // // if (_augment) {
1121 // // Node n=reached_t_node;
1122 // // Number augment_value=free.get(reached_t_node);
1123 // // while (pred.get(n).valid()) {
1124 // // AugEdge e=pred.get(n);
1125 // // e.augment(augment_value);
1126 // // n=res_graph.tail(e);
1130 // // return _augment;
1133 // // while (augment()) { }
1135 // // Number flowValue() {
1137 // // for(typename std::list<Node>::const_iterator i=S.begin();
1138 // // i!=S.end(); ++i) {
1139 // // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1140 // // a+=flow.get(e);
1142 // // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1143 // // a-=flow.get(e);
1153 #endif //HUGO_EDMONDS_KARP_H