1 #ifndef EDMONDS_KARP_HH
2 #define EDMONDS_KARP_HH
8 #include <bfs_iterator.hh>
9 //#include <time_measure.h>
13 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
16 typedef typename Graph::NodeIt NodeIt;
17 typedef typename Graph::EachNodeIt EachNodeIt;
19 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
22 const CapacityMap& capacity;
24 ResGraph(const Graph& _G, FlowMap& _flow,
25 const CapacityMap& _capacity) :
26 G(_G), flow(_flow), capacity(_capacity) { }
31 friend class OutEdgeIt;
34 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
36 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
40 //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
42 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
43 return (resG->capacity.get(sym)-resG->flow.get(sym));
45 return (resG->flow.get(sym));
48 bool valid() const { return sym.valid(); }
49 void augment(Number a) const {
50 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
51 resG->flow.set(sym, resG->flow.get(sym)+a);
54 resG->flow.set(sym, resG->flow.get(sym)-a);
60 class OutEdgeIt : public EdgeIt {
61 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
64 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
66 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
68 sym=resG->G.template first<OldSymEdgeIt>(v);
69 while( sym.valid() && !(free()>0) ) { ++sym; }
72 OutEdgeIt& operator++() {
74 while( sym.valid() && !(free()>0) ) { ++sym; }
79 void getFirst(OutEdgeIt& e, NodeIt v) const {
80 e=OutEdgeIt(*this, v);
82 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
84 template< typename It >
91 template< typename It >
92 It first(NodeIt v) const {
98 NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
99 NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
101 NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
102 NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
104 int id(NodeIt v) const { return G.id(v); }
106 template <typename S>
108 typename Graph::NodeMap<S> node_map;
110 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
111 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
112 void set(NodeIt nit, S a) { node_map.set(nit, a); }
113 S get(NodeIt nit) const { return node_map.get(nit); }
114 S& operator[](NodeIt nit) { return node_map[nit]; }
115 const S& operator[](NodeIt nit) const { return node_map[nit]; }
121 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
124 typedef typename Graph::NodeIt NodeIt;
125 typedef typename Graph::EachNodeIt EachNodeIt;
127 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
128 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
129 typedef typename Graph::InEdgeIt OldInEdgeIt;
133 const CapacityMap& capacity;
135 ResGraph2(const Graph& _G, FlowMap& _flow,
136 const CapacityMap& _capacity) :
137 G(_G), flow(_flow), capacity(_capacity) { }
142 friend class OutEdgeIt;
145 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
147 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
151 bool out_or_in; //true, iff out
153 EdgeIt() : out_or_in(true) { }
154 Number free() const {
156 return (resG->capacity.get(out)-resG->flow.get(out));
158 return (resG->flow.get(in));
162 return out_or_in && out.valid() || in.valid(); }
163 void augment(Number a) const {
165 resG->flow.set(out, resG->flow.get(out)+a);
167 resG->flow.set(in, resG->flow.get(in)-a);
172 class OutEdgeIt : public EdgeIt {
173 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
177 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
179 out=resG->G.template first<OldOutEdgeIt>(v);
180 while( out.valid() && !(free()>0) ) { ++out; }
183 in=resG->G.template first<OldInEdgeIt>(v);
184 while( in.valid() && !(free()>0) ) { ++in; }
188 OutEdgeIt& operator++() {
190 NodeIt v=resG->G.aNode(out);
192 while( out.valid() && !(free()>0) ) { ++out; }
195 in=resG->G.template first<OldInEdgeIt>(v);
196 while( in.valid() && !(free()>0) ) { ++in; }
200 while( in.valid() && !(free()>0) ) { ++in; }
206 void getFirst(OutEdgeIt& e, NodeIt v) const {
207 e=OutEdgeIt(*this, v);
209 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
211 template< typename It >
218 template< typename It >
219 It first(NodeIt v) const {
225 NodeIt tail(EdgeIt e) const {
226 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
227 NodeIt head(EdgeIt e) const {
228 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
230 NodeIt aNode(OutEdgeIt e) const {
231 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
232 NodeIt bNode(OutEdgeIt e) const {
233 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
235 int id(NodeIt v) const { return G.id(v); }
237 template <typename S>
239 typename Graph::NodeMap<S> node_map;
241 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
242 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
243 void set(NodeIt nit, S a) { node_map.set(nit, a); }
244 S get(NodeIt nit) const { return node_map.get(nit); }
248 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
249 class ResGraphWrapper {
251 typedef typename Graph::NodeIt NodeIt;
252 typedef typename Graph::EachNodeIt EachNodeIt;
254 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
255 typedef typename Graph::InEdgeIt OldInEdgeIt;
258 const CapacityMap* capacity;
260 ResGraphWrapper(const Graph& _G, FlowMap& _flow,
261 const CapacityMap& _capacity) :
262 G(&_G), flow(&_flow), capacity(&_capacity) { }
263 // ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) :
264 // G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
268 friend class OutEdgeIt;
271 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
273 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
276 const CapacityMap* capacity;
280 bool out_or_in; //true, iff out
282 EdgeIt() : out_or_in(true) { }
283 EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) :
284 G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { }
285 //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { }
286 Number free() const {
288 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
290 return (/*resG->*/flow->get(in));
294 return out_or_in && out.valid() || in.valid(); }
295 void augment(Number a) const {
297 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
299 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
306 std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out));
308 std::cout << "invalid";
313 std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in));
315 std::cout << "invalid";
317 std::cout << std::endl;
321 Number free(OldOutEdgeIt out) const {
322 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
324 Number free(OldInEdgeIt in) const {
325 return (/*resG->*/flow->get(in));
328 class OutEdgeIt : public EdgeIt {
329 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
333 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) {
334 //out=/*resG->*/G->template first<OldOutEdgeIt>(v);
336 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
339 //in=/*resG->*/G->template first<OldInEdgeIt>(v);
341 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
345 OutEdgeIt& operator++() {
347 NodeIt v=/*resG->*/G->aNode(out);
349 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
352 G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
353 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
357 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
363 class EachEdgeIt : public EdgeIt {
364 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
365 typename Graph::EachNodeIt v;
368 //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
369 EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) {
372 if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
373 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
374 while (v.valid() && !out.valid()) {
376 if (v.valid()) G->getFirst(out, v);
377 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
382 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
383 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
384 while (v.valid() && !in.valid()) {
386 if (v.valid()) G->getFirst(in, v);
387 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
391 EachEdgeIt& operator++() {
394 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
395 while (v.valid() && !out.valid()) {
397 if (v.valid()) G->getFirst(out, v);
398 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
403 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
404 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
405 while (v.valid() && !in.valid()) {
407 if (v.valid()) G->getFirst(in, v);
408 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
413 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
414 while (v.valid() && !in.valid()) {
416 if (v.valid()) G->getFirst(in, v);
417 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
424 void getFirst(EachNodeIt& v) const { G->getFirst(v); }
425 void getFirst(OutEdgeIt& e, NodeIt v) const {
426 e=OutEdgeIt(*G, v, *flow, *capacity);
428 void getFirst(EachEdgeIt& e) const {
429 e=EachEdgeIt(*G, *flow, *capacity);
432 EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
434 OutEdgeIt& next(OutEdgeIt& e) const {
436 NodeIt v=G->aNode(e.out);
438 while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
439 if (!G->valid(e.out)) {
441 G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
442 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
446 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
451 EachEdgeIt& next(EachEdgeIt& e) const {
454 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
455 while (G->valid(e.v) && !G->valid(e.out)) {
457 if (G->valid(e.v)) G->getFirst(e.out, e.v);
458 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
460 if (!G->valid(e.out)) {
463 if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
464 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
465 while (G->valid(e.v) && !G->valid(e.in)) {
467 if (G->valid(e.v)) G->getFirst(e.in, e.v);
468 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
473 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
474 while (G->valid(e.v) && !G->valid(e.in)) {
476 if (G->valid(e.v)) G->getFirst(e.in, e.v);
477 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
484 template< typename It >
491 template< typename It >
492 It first(NodeIt v) const {
498 NodeIt tail(EdgeIt e) const {
499 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
500 NodeIt head(EdgeIt e) const {
501 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
503 NodeIt aNode(OutEdgeIt e) const {
504 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
505 NodeIt bNode(OutEdgeIt e) const {
506 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
508 int id(NodeIt v) const { return G->id(v); }
510 bool valid(NodeIt n) const { return G->valid(n); }
511 bool valid(EdgeIt e) const {
512 return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
514 template <typename T>
516 typename Graph::NodeMap<T> node_map;
518 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.G)) { }
519 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.G), a) { }
520 void set(NodeIt nit, T a) { node_map.set(nit, a); }
521 T get(NodeIt nit) const { return node_map.get(nit); }
524 template <typename T>
526 typename Graph::EdgeMap<T> forward_map, backward_map;
528 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { }
529 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { }
530 void set(EdgeIt e, T a) {
532 forward_map.set(e.out, a);
534 backward_map.set(e.in, a);
538 return forward_map.get(e.out);
540 return backward_map.get(e.in);
546 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
549 typedef typename Graph::NodeIt NodeIt;
550 typedef typename Graph::EdgeIt EdgeIt;
551 typedef typename Graph::EachEdgeIt EachEdgeIt;
552 typedef typename Graph::OutEdgeIt OutEdgeIt;
553 typedef typename Graph::InEdgeIt InEdgeIt;
560 const CapacityMap* capacity;
561 typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
562 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
563 typedef typename AugGraph::EdgeIt AugEdgeIt;
565 //AugGraph res_graph;
566 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
567 //typename AugGraph::NodeMap<AugEdgeIt> pred;
568 //typename AugGraph::NodeMap<Number> free;
570 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
571 G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,
572 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
574 bool augmentOnShortestPath() {
575 AugGraph res_graph(*G, *flow, *capacity);
578 typedef typename AugGraph::NodeMap<bool> ReachedMap;
579 BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
580 res_bfs.pushAndSetReached(s);
582 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
583 //filled up with invalid iterators
584 //pred.set(s, AugEdgeIt());
586 typename AugGraph::NodeMap<Number> free(res_graph);
588 //searching for augmenting path
589 while ( !res_bfs.finished() ) {
590 AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
591 if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
592 NodeIt v=res_graph.tail(e);
593 NodeIt w=res_graph.head(e);
595 if (res_graph.valid(pred.get(v))) {
596 free.set(w, std::min(free.get(v), e.free()));
598 free.set(w, e.free());
600 if (res_graph.head(e)==t) { _augment=true; break; }
604 } //end of searching augmenting path
608 Number augment_value=free.get(t);
609 while (res_graph.valid(pred.get(n))) {
610 AugEdgeIt e=pred.get(n);
611 e.augment(augment_value);
619 template<typename MutableGraph> bool augmentOnBlockingFlow() {
622 AugGraph res_graph(*G, *flow, *capacity);
624 typedef typename AugGraph::NodeMap<bool> ReachedMap;
625 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
627 bfs.pushAndSetReached(s);
628 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
629 while ( !bfs.finished() ) {
630 AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
631 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
632 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
636 } //computing distances from s in the residual graph
639 typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
640 for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
641 res_graph_to_F.set(n, F.addNode());
644 typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
645 typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
647 typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
648 typename MutableGraph::EdgeMap<Number> residual_capacity(F);
650 //Making F to the graph containing the edges of the residual graph
651 //which are in some shortest paths
652 for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
653 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
654 typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
655 original_edge.update();
656 original_edge.set(f, e);
657 residual_capacity.update();
658 residual_capacity.set(f, e.free());
666 //computing blocking flow with dfs
667 typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
668 DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
669 typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
670 typename MutableGraph::NodeMap<Number> free(F);
672 dfs.pushAndSetReached(sF);
673 while (!dfs.finished()) {
675 if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
676 //std::cout << "OutEdgeIt: " << dfs;
677 //std::cout << " aNode: " << F.aNode(dfs);
678 //std::cout << " bNode: " << F.bNode(dfs) << " ";
680 typename MutableGraph::NodeIt v=F.aNode(dfs);
681 typename MutableGraph::NodeIt w=F.bNode(dfs);
683 if (F.valid(pred.get(v))) {
684 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
686 free.set(w, residual_capacity.get(dfs)/*original_edge.get(dfs).free()*/);
689 //std::cout << "AUGMENTATION"<<std::endl;
695 //std::cout << "OutEdgeIt: " << "invalid";
696 //std::cout << " aNode: " << dfs.aNode();
697 //std::cout << " bNode: " << "invalid" << " ";
699 if (dfs.isBNodeNewlyReached()) {
700 //std::cout << "bNodeIsNewlyReached ";
702 //std::cout << "bNodeIsNotNewlyReached ";
703 if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
704 //std::cout << "DELETE ";
705 F.erase(typename MutableGraph::OutEdgeIt(dfs));
708 //if (dfs.isANodeExamined()) {
709 //std::cout << "aNodeIsExamined ";
711 //std::cout << "aNodeIsNotExamined ";
713 //std::cout<<std::endl;
717 typename MutableGraph::NodeIt n=tF;
718 Number augment_value=free.get(tF);
719 while (F.valid(pred.get(n))) {
720 typename MutableGraph::EdgeIt e=pred.get(n);
721 original_edge.get(e).augment(augment_value);
723 if (residual_capacity.get(e)==augment_value)
726 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
735 //int num_of_augmentations=0;
736 while (augmentOnShortestPath()) {
737 //while (augmentOnBlockingFlow<MutableGraph>()) {
738 //std::cout << ++num_of_augmentations << " ";
739 //std::cout<<std::endl;
742 template<typename MutableGraph> void run() {
743 //int num_of_augmentations=0;
744 //while (augmentOnShortestPath()) {
745 while (augmentOnBlockingFlow<MutableGraph>()) {
746 //std::cout << ++num_of_augmentations << " ";
747 //std::cout<<std::endl;
753 for(G->getFirst(e, s); G->valid(e); G->next(e)) {
761 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
764 typedef typename Graph::NodeIt NodeIt;
765 typedef typename Graph::EdgeIt EdgeIt;
766 typedef typename Graph::EachEdgeIt EachEdgeIt;
767 typedef typename Graph::OutEdgeIt OutEdgeIt;
768 typedef typename Graph::InEdgeIt InEdgeIt;
771 std::list<NodeIt>& S;
772 std::list<NodeIt>& T;
774 const CapacityMap& capacity;
775 typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
776 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
777 typedef typename AugGraph::EdgeIt AugEdgeIt;
778 typename Graph::NodeMap<bool> SMap;
779 typename Graph::NodeMap<bool> TMap;
781 MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
782 for(typename std::list<NodeIt>::const_iterator i=S.begin();
786 for (typename std::list<NodeIt>::const_iterator i=T.begin();
792 AugGraph res_graph(G, flow, capacity);
794 NodeIt reached_t_node;
796 typedef typename AugGraph::NodeMap<bool> ReachedMap;
797 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
798 for(typename std::list<NodeIt>::const_iterator i=S.begin();
800 res_bfs.pushAndSetReached(*i);
802 //res_bfs.pushAndSetReached(s);
804 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
805 //filled up with invalid iterators
807 typename AugGraph::NodeMap<Number> free(res_graph);
809 //searching for augmenting path
810 while ( !res_bfs.finished() ) {
811 AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
812 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
813 NodeIt v=res_graph.tail(e);
814 NodeIt w=res_graph.head(e);
816 if (pred.get(v).valid()) {
817 free.set(w, std::min(free.get(v), e.free()));
819 free.set(w, e.free());
821 if (TMap.get(res_graph.head(e))) {
823 reached_t_node=res_graph.head(e);
829 } //end of searching augmenting path
832 NodeIt n=reached_t_node;
833 Number augment_value=free.get(reached_t_node);
834 while (pred.get(n).valid()) {
835 AugEdgeIt e=pred.get(n);
836 e.augment(augment_value);
844 while (augment()) { }
848 for(typename std::list<NodeIt>::const_iterator i=S.begin();
850 for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
853 for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
865 #endif //EDMONDS_KARP_HH