Adding GraphEdgeSet and GraphNodeSet classes to graph_utils.h.
2 #ifndef LEMON_EDMONDS_KARP_H
3 #define LEMON_EDMONDS_KARP_H
9 #include <bfs_iterator.h>
14 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
17 typedef typename Graph::Node Node;
18 typedef typename Graph::NodeIt NodeIt;
20 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
23 const CapacityMap& capacity;
25 ResGraph(const Graph& _G, FlowMap& _flow,
26 const CapacityMap& _capacity) :
27 G(_G), flow(_flow), capacity(_capacity) { }
32 friend class OutEdgeIt;
35 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
37 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
41 //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
43 if (resG->G.aNode(sym)==resG->G.source(sym)) {
44 return (resG->capacity.get(sym)-resG->flow.get(sym));
46 return (resG->flow.get(sym));
49 bool valid() const { return sym.valid(); }
50 void augment(Number a) const {
51 if (resG->G.aNode(sym)==resG->G.source(sym)) {
52 resG->flow.set(sym, resG->flow.get(sym)+a);
55 resG->flow.set(sym, resG->flow.get(sym)-a);
61 class OutEdgeIt : public Edge {
62 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
65 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
67 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
69 sym=resG->G.template first<OldSymEdgeIt>(v);
70 while( sym.valid() && !(free()>0) ) { ++sym; }
73 OutEdgeIt& operator++() {
75 while( sym.valid() && !(free()>0) ) { ++sym; }
80 void /*getF*/first(OutEdgeIt& e, Node v) const {
81 e=OutEdgeIt(*this, v);
83 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
85 template< typename It >
92 template< typename It >
93 It first(Node v) const {
99 Node source(Edge e) const { return G.aNode(e.sym); }
100 Node target(Edge e) const { return G.bNode(e.sym); }
102 Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
103 Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
105 int id(Node v) const { return G.id(v); }
107 template <typename S>
109 typename Graph::NodeMap<S> node_map;
111 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
112 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
113 void set(Node nit, S a) { node_map.set(nit, a); }
114 S get(Node nit) const { return node_map.get(nit); }
115 S& operator[](Node nit) { return node_map[nit]; }
116 const S& operator[](Node nit) const { return node_map[nit]; }
122 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
125 typedef typename Graph::Node Node;
126 typedef typename Graph::NodeIt NodeIt;
128 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
129 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
130 typedef typename Graph::InEdgeIt OldInEdgeIt;
134 const CapacityMap& capacity;
136 ResGraph2(const Graph& _G, FlowMap& _flow,
137 const CapacityMap& _capacity) :
138 G(_G), flow(_flow), capacity(_capacity) { }
143 friend class OutEdgeIt;
146 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
148 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
152 bool out_or_in; //true, iff out
154 Edge() : out_or_in(true) { }
155 Number free() const {
157 return (resG->capacity.get(out)-resG->flow.get(out));
159 return (resG->flow.get(in));
163 return out_or_in && out.valid() || in.valid(); }
164 void augment(Number a) const {
166 resG->flow.set(out, resG->flow.get(out)+a);
168 resG->flow.set(in, resG->flow.get(in)-a);
173 class OutEdgeIt : public Edge {
174 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
178 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
180 out=resG->G.template first<OldOutEdgeIt>(v);
181 while( out.valid() && !(free()>0) ) { ++out; }
184 in=resG->G.template first<OldInEdgeIt>(v);
185 while( in.valid() && !(free()>0) ) { ++in; }
189 OutEdgeIt& operator++() {
191 Node v=resG->G.aNode(out);
193 while( out.valid() && !(free()>0) ) { ++out; }
196 in=resG->G.template first<OldInEdgeIt>(v);
197 while( in.valid() && !(free()>0) ) { ++in; }
201 while( in.valid() && !(free()>0) ) { ++in; }
207 void /*getF*/first(OutEdgeIt& e, Node v) const {
208 e=OutEdgeIt(*this, v);
210 void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
212 template< typename It >
219 template< typename It >
220 It first(Node v) const {
226 Node source(Edge e) const {
227 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
228 Node target(Edge e) const {
229 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
231 Node aNode(OutEdgeIt e) const {
232 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
233 Node bNode(OutEdgeIt e) const {
234 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
236 int id(Node v) const { return G.id(v); }
238 template <typename S>
240 typename Graph::NodeMap<S> node_map;
242 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
243 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
244 void set(Node nit, S a) { node_map.set(nit, a); }
245 S get(Node nit) const { return node_map.get(nit); }
250 template <typename GraphWrapper, typename Number, typename FlowMap, typename CapacityMap>
253 typedef GraphWrapper GW;
254 typedef typename GW::Node Node;
255 typedef typename GW::Edge Edge;
256 typedef typename GW::EdgeIt EdgeIt;
257 typedef typename GW::OutEdgeIt OutEdgeIt;
258 typedef typename GW::InEdgeIt InEdgeIt;
264 const CapacityMap* capacity;
265 typedef ResGraphWrapper<GW, Number, FlowMap, CapacityMap > ResGW;
266 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
267 typedef typename ResGW::Edge ResGWEdge;
270 MaxFlow(const GW& _gw, Node _s, Node _t, FlowMap& _flow, const CapacityMap& _capacity) :
271 gw(_gw), s(_s), t(_t), flow(&_flow), capacity(&_capacity) { }
273 bool augmentOnShortestPath() {
274 ResGW res_graph(gw, *flow, *capacity);
277 typedef typename ResGW::NodeMap<bool> ReachedMap;
278 BfsIterator5< ResGW, ReachedMap > 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.source(e);
291 Node w=res_graph.target(e);
293 if (res_graph.valid(pred.get(v))) {
294 free.set(w, std::min(free.get(v), res_graph.resCap(e)));
296 free.set(w, res_graph.resCap(e));
298 if (res_graph.target(e)==t) { _augment=true; break; }
302 } //end of searching augmenting path
306 Number augment_value=free.get(t);
307 while (res_graph.valid(pred.get(n))) {
308 ResGWEdge e=pred.get(n);
309 res_graph.augment(e, augment_value);
310 n=res_graph.source(e);
317 template<typename MapGraphWrapper>
321 typename MapGraphWrapper::NodeMap<int> dist;
323 DistanceMap(MapGraphWrapper& _gw) : gw(_gw), dist(_gw, _gw.nodeNum()) { }
324 void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; }
325 int get(const typename MapGraphWrapper::Node& n) const { return dist[n]; }
326 bool get(const typename MapGraphWrapper::Edge& e) const {
327 return (dist.get(gw.source(e))<dist.get(gw.target(e)));
331 template<typename MutableGraph> bool augmentOnBlockingFlow() {
332 typedef MutableGraph MG;
335 ResGW res_graph(gw, *flow, *capacity);
337 typedef typename ResGW::NodeMap<bool> ReachedMap;
338 BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
340 bfs.pushAndSetReached(s);
341 //typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
342 DistanceMap<ResGW> dist(res_graph);
343 while ( !bfs.finished() ) {
344 ResGWOutEdgeIt e=bfs;
345 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
346 dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
349 } //computing distances from s in the residual graph
352 typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
353 FilterResGW filter_res_graph(res_graph, dist);
354 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
356 typename ResGW::NodeIt n;
357 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
358 res_graph_to_F.set(n, F.addNode());
362 typename MG::Node sF=res_graph_to_F.get(s);
363 typename MG::Node tF=res_graph_to_F.get(t);
364 typename MG::EdgeMap<ResGWEdge> original_edge(F);
365 typename MG::EdgeMap<Number> residual_capacity(F);
367 //Making F to the graph containing the edges of the residual graph
368 //which are in some shortest paths
370 typename FilterResGW::EdgeIt e;
371 for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
372 //if (dist.get(res_graph.target(e))==dist.get(res_graph.source(e))+1) {
373 typename MG::Edge f=F.addEdge(res_graph_to_F.get(res_graph.source(e)), res_graph_to_F.get(res_graph.target(e)));
374 original_edge.update();
375 original_edge.set(f, e);
376 residual_capacity.update();
377 residual_capacity.set(f, res_graph.resCap(e));
386 //computing blocking flow with dfs
387 typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
388 DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > dfs(F);
389 typename MG::NodeMap<typename MG::Edge> pred(F);
390 pred.set(sF, INVALID);
391 //invalid iterators for sources
393 typename MG::NodeMap<Number> free(F);
395 dfs.pushAndSetReached(sF);
396 while (!dfs.finished()) {
398 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
399 if (dfs.isBNodeNewlyReached()) {
400 typename MG::Node v=F.aNode(dfs);
401 typename MG::Node w=F.bNode(dfs);
403 if (F.valid(pred.get(v))) {
404 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
406 free.set(w, residual_capacity.get(dfs));
415 F.erase(/*typename MG::OutEdgeIt*/(dfs));
421 typename MG::Node n=tF;
422 Number augment_value=free.get(tF);
423 while (F.valid(pred.get(n))) {
424 typename MG::Edge e=pred.get(n);
425 res_graph.augment(original_edge.get(e), augment_value);
427 if (residual_capacity.get(e)==augment_value)
430 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
439 template<typename MutableGraph> bool augmentOnBlockingFlow1() {
440 typedef MutableGraph MG;
443 ResGW res_graph(gw, *flow, *capacity);
445 //bfs for distances on the residual graph
446 typedef typename ResGW::NodeMap<bool> ReachedMap;
447 BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
448 bfs.pushAndSetReached(s);
449 typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
451 //F will contain the physical copy of the residual graph
452 //with the set of edges which are on shortest paths
454 typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
456 typename ResGW::NodeIt n;
457 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
458 res_graph_to_F.set(n, F.addNode());
462 typename MG::Node sF=res_graph_to_F.get(s);
463 typename MG::Node tF=res_graph_to_F.get(t);
464 typename MG::EdgeMap<ResGWEdge> original_edge(F);
465 typename MG::EdgeMap<Number> residual_capacity(F);
467 while ( !bfs.finished() ) {
468 ResGWOutEdgeIt e=bfs;
469 if (res_graph.valid(e)) {
470 if (bfs.isBNodeNewlyReached()) {
471 dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
472 typename MG::Edge f=F.addEdge(res_graph_to_F.get(res_graph.source(e)), res_graph_to_F.get(res_graph.target(e)));
473 original_edge.update();
474 original_edge.set(f, e);
475 residual_capacity.update();
476 residual_capacity.set(f, res_graph.resCap(e));
478 if (dist.get(res_graph.target(e))==(dist.get(res_graph.source(e))+1)) {
479 typename MG::Edge f=F.addEdge(res_graph_to_F.get(res_graph.source(e)), res_graph_to_F.get(res_graph.target(e)));
480 original_edge.update();
481 original_edge.set(f, e);
482 residual_capacity.update();
483 residual_capacity.set(f, res_graph.resCap(e));
488 } //computing distances from s in the residual graph
494 //computing blocking flow with dfs
495 typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
496 DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > dfs(F);
497 typename MG::NodeMap<typename MG::Edge> pred(F);
498 pred.set(sF, INVALID);
499 //invalid iterators for sources
501 typename MG::NodeMap<Number> free(F);
503 dfs.pushAndSetReached(sF);
504 while (!dfs.finished()) {
506 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
507 if (dfs.isBNodeNewlyReached()) {
508 typename MG::Node v=F.aNode(dfs);
509 typename MG::Node w=F.bNode(dfs);
511 if (F.valid(pred.get(v))) {
512 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
514 free.set(w, residual_capacity.get(dfs));
523 F.erase(/*typename MG::OutEdgeIt*/(dfs));
529 typename MG::Node n=tF;
530 Number augment_value=free.get(tF);
531 while (F.valid(pred.get(n))) {
532 typename MG::Edge e=pred.get(n);
533 res_graph.augment(original_edge.get(e), augment_value);
535 if (residual_capacity.get(e)==augment_value)
538 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
547 bool augmentOnBlockingFlow2() {
550 ResGW res_graph(gw, *flow, *capacity);
552 typedef typename ResGW::NodeMap<bool> ReachedMap;
553 BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
555 bfs.pushAndSetReached(s);
556 DistanceMap<ResGW> dist(res_graph);
557 while ( !bfs.finished() ) {
558 ResGWOutEdgeIt e=bfs;
559 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
560 dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
563 } //computing distances from s in the residual graph
565 //Subgraph containing the edges on some shortest paths
566 typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
567 FilterResGW filter_res_graph(res_graph, dist);
569 //Subgraph, which is able to delete edges which are already
571 typename FilterResGW::NodeMap<typename FilterResGW::OutEdgeIt>
572 first_out_edges(filter_res_graph);
573 typename FilterResGW::NodeIt v;
574 for(filter_res_graph.first(v); filter_res_graph.valid(v);
575 filter_res_graph.next(v))
577 typename FilterResGW::OutEdgeIt e;
578 filter_res_graph.first(e, v);
579 first_out_edges.set(v, e);
581 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
582 NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
583 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
590 //computing blocking flow with dfs
591 typedef typename ErasingResGW::NodeMap<bool> BlockingReachedMap;
592 DfsIterator5< ErasingResGW, BlockingReachedMap >
593 dfs(erasing_res_graph);
594 typename ErasingResGW::NodeMap<typename ErasingResGW::OutEdgeIt>
595 pred(erasing_res_graph);
596 pred.set(s, INVALID);
597 //invalid iterators for sources
599 typename ErasingResGW::NodeMap<Number> free(erasing_res_graph);
601 dfs.pushAndSetReached(s);
602 while (!dfs.finished()) {
604 if (erasing_res_graph.valid(
605 /*typename ErasingResGW::OutEdgeIt*/(dfs)))
607 if (dfs.isBNodeNewlyReached()) {
609 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
610 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
612 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
613 if (erasing_res_graph.valid(pred.get(v))) {
614 free.set(w, std::min(free.get(v), res_graph.resCap(dfs)));
616 free.set(w, res_graph.resCap(dfs));
625 erasing_res_graph.erase(dfs);
631 typename ErasingResGW::Node n=t;
632 Number augment_value=free.get(n);
633 while (erasing_res_graph.valid(pred.get(n))) {
634 typename ErasingResGW::OutEdgeIt e=pred.get(n);
635 res_graph.augment(e, augment_value);
636 n=erasing_res_graph.source(e);
637 if (res_graph.resCap(e)==0)
638 erasing_res_graph.erase(e);
642 } //while (__augment)
647 // bool augmentOnBlockingFlow2() {
648 // bool _augment=false;
650 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
651 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
652 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
653 // typedef typename EAugGraph::Edge EAugEdge;
655 // EAugGraph res_graph(*G, *flow, *capacity);
657 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
659 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
660 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
661 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
663 // bfs.pushAndSetReached(s);
665 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
666 // NodeMap<int>& dist=res_graph.dist;
668 // while ( !bfs.finished() ) {
669 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
670 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
671 // dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
674 // } //computing distances from s in the residual graph
676 // bool __augment=true;
678 // while (__augment) {
681 // //computing blocking flow with dfs
682 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
683 // DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
685 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph);
686 // pred.set(s, EAugEdge(INVALID));
687 // //invalid iterators for sources
689 // typename EAugGraph::NodeMap<Number> free(res_graph);
691 // dfs.pushAndSetReached(s);
692 // while (!dfs.finished()) {
694 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
695 // if (dfs.isBNodeNewlyReached()) {
697 // typename EAugGraph::Node v=res_graph.aNode(dfs);
698 // typename EAugGraph::Node w=res_graph.bNode(dfs);
700 // pred.set(w, EAugOutEdgeIt(dfs));
701 // if (res_graph.valid(pred.get(v))) {
702 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
704 // free.set(w, res_graph.free(dfs));
713 // res_graph.erase(dfs);
720 // typename EAugGraph::Node n=t;
721 // Number augment_value=free.get(t);
722 // while (res_graph.valid(pred.get(n))) {
723 // EAugEdge e=pred.get(n);
724 // res_graph.augment(e, augment_value);
725 // n=res_graph.source(e);
726 // if (res_graph.free(e)==0)
727 // res_graph.erase(e);
737 //int num_of_augmentations=0;
738 while (augmentOnShortestPath()) {
739 //while (augmentOnBlockingFlow<MutableGraph>()) {
740 //std::cout << ++num_of_augmentations << " ";
741 //std::cout<<std::endl;
745 template<typename MutableGraph> void run() {
746 //int num_of_augmentations=0;
747 //while (augmentOnShortestPath()) {
748 while (augmentOnBlockingFlow<MutableGraph>()) {
749 //std::cout << ++num_of_augmentations << " ";
750 //std::cout<<std::endl;
757 for(gw.first(e, s); gw.valid(e); gw.next(e)) {
766 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
767 // class MaxMatching {
769 // typedef typename Graph::Node Node;
770 // typedef typename Graph::NodeIt NodeIt;
771 // typedef typename Graph::Edge Edge;
772 // typedef typename Graph::EdgeIt EdgeIt;
773 // typedef typename Graph::OutEdgeIt OutEdgeIt;
774 // typedef typename Graph::InEdgeIt InEdgeIt;
776 // typedef typename Graph::NodeMap<bool> SMap;
777 // typedef typename Graph::NodeMap<bool> TMap;
785 // const CapacityMap* capacity;
786 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
787 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
788 // typedef typename AugGraph::Edge AugEdge;
789 // typename Graph::NodeMap<int> used; //0
792 // MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
793 // G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
794 // bool augmentOnShortestPath() {
795 // AugGraph res_graph(*G, *flow, *capacity);
796 // bool _augment=false;
798 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
799 // BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
800 // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
801 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
802 // if ((S->get(s)) && (used.get(s)<1) ) {
804 // //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
805 // //u+=flow->get(e);
807 // bfs.pushAndSetReached(s);
808 // pred.set(s, AugEdge(INVALID));
813 // typename AugGraph::NodeMap<Number> free(res_graph);
816 // //searching for augmenting path
817 // while ( !bfs.finished() ) {
818 // AugOutEdgeIt e=bfs;
819 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
820 // Node v=res_graph.source(e);
821 // Node w=res_graph.target(e);
823 // if (res_graph.valid(pred.get(v))) {
824 // free.set(w, std::min(free.get(v), res_graph.free(e)));
826 // free.set(w, res_graph.free(e));
828 // n=res_graph.target(e);
829 // if (T->get(n) && (used.get(n)<1) ) {
831 // //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
832 // //u+=flow->get(f);
841 // } //end of searching augmenting path
845 // used.set(n, 1); //mind2 vegen jav
846 // Number augment_value=free.get(n);
847 // while (res_graph.valid(pred.get(n))) {
848 // AugEdge e=pred.get(n);
849 // res_graph.augment(e, augment_value);
850 // n=res_graph.source(e);
852 // used.set(n, 1); //mind2 vegen jav
858 // // template<typename MutableGraph> bool augmentOnBlockingFlow() {
859 // // bool _augment=false;
861 // // AugGraph res_graph(*G, *flow, *capacity);
863 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
864 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
870 // // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
871 // // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
872 // // if (S->get(s)) {
874 // // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
875 // // u+=flow->get(e);
877 // // bfs.pushAndSetReached(s);
878 // // //pred.set(s, AugEdge(INVALID));
886 // // //bfs.pushAndSetReached(s);
887 // // typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
888 // // while ( !bfs.finished() ) {
889 // // AugOutEdgeIt e=bfs;
890 // // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
891 // // dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
895 // // } //computing distances from s in the residual graph
897 // // MutableGraph F;
898 // // typename AugGraph::NodeMap<typename MutableGraph::Node>
899 // // res_graph_to_F(res_graph);
900 // // for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
901 // // res_graph_to_F.set(n, F.addNode());
904 // // typename MutableGraph::Node sF=res_graph_to_F.get(s);
905 // // typename MutableGraph::Node tF=res_graph_to_F.get(t);
907 // // typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
908 // // typename MutableGraph::EdgeMap<Number> residual_capacity(F);
910 // // //Making F to the graph containing the edges of the residual graph
911 // // //which are in some shortest paths
912 // // for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
913 // // if (dist.get(res_graph.target(e))==dist.get(res_graph.source(e))+1) {
914 // // typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.source(e)), res_graph_to_F.get(res_graph.target(e)));
915 // // original_edge.update();
916 // // original_edge.set(f, e);
917 // // residual_capacity.update();
918 // // residual_capacity.set(f, res_graph.free(e));
922 // // bool __augment=true;
924 // // while (__augment) {
925 // // __augment=false;
926 // // //computing blocking flow with dfs
927 // // typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
928 // // DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
929 // // typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
930 // // pred.set(sF, typename MutableGraph::Edge(INVALID));
931 // // //invalid iterators for sources
933 // // typename MutableGraph::NodeMap<Number> free(F);
935 // // dfs.pushAndSetReached(sF);
936 // // while (!dfs.finished()) {
938 // // if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
939 // // if (dfs.isBNodeNewlyReached()) {
940 // // typename MutableGraph::Node v=F.aNode(dfs);
941 // // typename MutableGraph::Node w=F.bNode(dfs);
942 // // pred.set(w, dfs);
943 // // if (F.valid(pred.get(v))) {
944 // // free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
946 // // free.set(w, residual_capacity.get(dfs));
949 // // __augment=true;
955 // // F.erase(typename MutableGraph::OutEdgeIt(dfs));
960 // // if (__augment) {
961 // // typename MutableGraph::Node n=tF;
962 // // Number augment_value=free.get(tF);
963 // // while (F.valid(pred.get(n))) {
964 // // typename MutableGraph::Edge e=pred.get(n);
965 // // res_graph.augment(original_edge.get(e), augment_value);
967 // // if (residual_capacity.get(e)==augment_value)
970 // // residual_capacity.set(e, residual_capacity.get(e)-augment_value);
976 // // return _augment;
978 // bool augmentOnBlockingFlow2() {
979 // bool _augment=false;
981 // //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
982 // typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
983 // typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
984 // typedef typename EAugGraph::Edge EAugEdge;
986 // EAugGraph res_graph(*G, *flow, *capacity);
988 // //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
990 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
991 // /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
992 // ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
995 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
996 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
999 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
1002 // bfs.pushAndSetReached(s);
1003 // //pred.set(s, AugEdge(INVALID));
1009 // //bfs.pushAndSetReached(s);
1011 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
1012 // NodeMap<int>& dist=res_graph.dist;
1014 // while ( !bfs.finished() ) {
1015 // typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
1016 // if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1017 // dist.set(res_graph.target(e), dist.get(res_graph.source(e))+1);
1020 // } //computing distances from s in the residual graph
1022 // bool __augment=true;
1024 // while (__augment) {
1027 // //computing blocking flow with dfs
1028 // typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
1029 // DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
1031 // typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
1032 // //pred.set(s, EAugEdge(INVALID));
1033 // //invalid iterators for sources
1035 // typename EAugGraph::NodeMap<Number> free(res_graph);
1038 // //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1039 // for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
1042 // for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
1045 // dfs.pushAndSetReached(s);
1046 // //pred.set(s, AugEdge(INVALID));
1053 // //dfs.pushAndSetReached(s);
1054 // typename EAugGraph::Node n;
1055 // while (!dfs.finished()) {
1057 // if (res_graph.valid(EAugOutEdgeIt(dfs))) {
1058 // if (dfs.isBNodeNewlyReached()) {
1060 // typename EAugGraph::Node v=res_graph.aNode(dfs);
1061 // typename EAugGraph::Node w=res_graph.bNode(dfs);
1063 // pred.set(w, EAugOutEdgeIt(dfs));
1064 // if (res_graph.valid(pred.get(v))) {
1065 // free.set(w, std::min(free.get(v), res_graph.free(dfs)));
1067 // free.set(w, res_graph.free(dfs));
1073 // for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
1082 // res_graph.erase(dfs);
1089 // // typename EAugGraph::Node n=t;
1090 // Number augment_value=free.get(n);
1091 // while (res_graph.valid(pred.get(n))) {
1092 // EAugEdge e=pred.get(n);
1093 // res_graph.augment(e, augment_value);
1094 // n=res_graph.source(e);
1095 // if (res_graph.free(e)==0)
1096 // res_graph.erase(e);
1105 // //int num_of_augmentations=0;
1106 // while (augmentOnShortestPath()) {
1107 // //while (augmentOnBlockingFlow<MutableGraph>()) {
1108 // //std::cout << ++num_of_augmentations << " ";
1109 // //std::cout<<std::endl;
1112 // // template<typename MutableGraph> void run() {
1113 // // //int num_of_augmentations=0;
1114 // // //while (augmentOnShortestPath()) {
1115 // // while (augmentOnBlockingFlow<MutableGraph>()) {
1116 // // //std::cout << ++num_of_augmentations << " ";
1117 // // //std::cout<<std::endl;
1120 // Number flowValue() {
1123 // for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1135 // // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1136 // // class MaxFlow2 {
1138 // // typedef typename Graph::Node Node;
1139 // // typedef typename Graph::Edge Edge;
1140 // // typedef typename Graph::EdgeIt EdgeIt;
1141 // // typedef typename Graph::OutEdgeIt OutEdgeIt;
1142 // // typedef typename Graph::InEdgeIt InEdgeIt;
1144 // // const Graph& G;
1145 // // std::list<Node>& S;
1146 // // std::list<Node>& T;
1147 // // FlowMap& flow;
1148 // // const CapacityMap& capacity;
1149 // // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1150 // // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1151 // // typedef typename AugGraph::Edge AugEdge;
1152 // // typename Graph::NodeMap<bool> SMap;
1153 // // typename Graph::NodeMap<bool> TMap;
1155 // // 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) {
1156 // // for(typename std::list<Node>::const_iterator i=S.begin();
1157 // // i!=S.end(); ++i) {
1158 // // SMap.set(*i, true);
1160 // // for (typename std::list<Node>::const_iterator i=T.begin();
1161 // // i!=T.end(); ++i) {
1162 // // TMap.set(*i, true);
1165 // // bool augment() {
1166 // // AugGraph res_graph(G, flow, capacity);
1167 // // bool _augment=false;
1168 // // Node reached_t_node;
1170 // // typedef typename AugGraph::NodeMap<bool> ReachedMap;
1171 // // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1172 // // for(typename std::list<Node>::const_iterator i=S.begin();
1173 // // i!=S.end(); ++i) {
1174 // // bfs.pushAndSetReached(*i);
1176 // // //bfs.pushAndSetReached(s);
1178 // // typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1179 // // //filled up with invalid iterators
1181 // // typename AugGraph::NodeMap<Number> free(res_graph);
1183 // // //searching for augmenting path
1184 // // while ( !bfs.finished() ) {
1185 // // AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1186 // // if (e.valid() && bfs.isBNodeNewlyReached()) {
1187 // // Node v=res_graph.source(e);
1188 // // Node w=res_graph.target(e);
1189 // // pred.set(w, e);
1190 // // if (pred.get(v).valid()) {
1191 // // free.set(w, std::min(free.get(v), e.free()));
1193 // // free.set(w, e.free());
1195 // // if (TMap.get(res_graph.target(e))) {
1196 // // _augment=true;
1197 // // reached_t_node=res_graph.target(e);
1203 // // } //end of searching augmenting path
1205 // // if (_augment) {
1206 // // Node n=reached_t_node;
1207 // // Number augment_value=free.get(reached_t_node);
1208 // // while (pred.get(n).valid()) {
1209 // // AugEdge e=pred.get(n);
1210 // // e.augment(augment_value);
1211 // // n=res_graph.source(e);
1215 // // return _augment;
1218 // // while (augment()) { }
1220 // // Number flowValue() {
1222 // // for(typename std::list<Node>::const_iterator i=S.begin();
1223 // // i!=S.end(); ++i) {
1224 // // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1225 // // a+=flow.get(e);
1227 // // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1228 // // a-=flow.get(e);
1236 } // namespace lemon
1238 #endif //LEMON_EDMONDS_KARP_H