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); }
249 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
252 typedef typename Graph::NodeIt NodeIt;
253 typedef typename Graph::EdgeIt EdgeIt;
254 typedef typename Graph::EachEdgeIt EachEdgeIt;
255 typedef typename Graph::OutEdgeIt OutEdgeIt;
256 typedef typename Graph::InEdgeIt InEdgeIt;
263 const CapacityMap* capacity;
264 typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
265 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
266 typedef typename AugGraph::EdgeIt AugEdgeIt;
268 //AugGraph res_graph;
269 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
270 //typename AugGraph::NodeMap<AugEdgeIt> pred;
271 //typename AugGraph::NodeMap<Number> free;
273 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
274 G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,
275 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
277 bool augmentOnShortestPath() {
278 AugGraph res_graph(*G, *flow, *capacity);
281 typedef typename AugGraph::NodeMap<bool> ReachedMap;
282 BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
283 res_bfs.pushAndSetReached(s);
285 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
286 //filled up with invalid iterators
287 //pred.set(s, AugEdgeIt());
289 typename AugGraph::NodeMap<Number> free(res_graph);
291 //searching for augmenting path
292 while ( !res_bfs.finished() ) {
293 AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
294 if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
295 NodeIt v=res_graph.tail(e);
296 NodeIt w=res_graph.head(e);
298 if (res_graph.valid(pred.get(v))) {
299 free.set(w, std::min(free.get(v), e.free()));
301 free.set(w, e.free());
303 if (res_graph.head(e)==t) { _augment=true; break; }
307 } //end of searching augmenting path
311 Number augment_value=free.get(t);
312 while (res_graph.valid(pred.get(n))) {
313 AugEdgeIt e=pred.get(n);
314 e.augment(augment_value);
322 template<typename MutableGraph> bool augmentOnBlockingFlow() {
325 AugGraph res_graph(*G, *flow, *capacity);
327 typedef typename AugGraph::NodeMap<bool> ReachedMap;
328 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
330 bfs.pushAndSetReached(s);
331 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
332 while ( !bfs.finished() ) {
333 AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
334 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
335 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
339 } //computing distances from s in the residual graph
342 typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
343 for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
344 res_graph_to_F.set(n, F.addNode());
347 typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
348 typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
350 typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
351 typename MutableGraph::EdgeMap<Number> residual_capacity(F);
353 //Making F to the graph containing the edges of the residual graph
354 //which are in some shortest paths
355 for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
356 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
357 typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
358 original_edge.update();
359 original_edge.set(f, e);
360 residual_capacity.update();
361 residual_capacity.set(f, e.free());
369 //computing blocking flow with dfs
370 typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
371 DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
372 typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
373 typename MutableGraph::NodeMap<Number> free(F);
375 dfs.pushAndSetReached(sF);
376 while (!dfs.finished()) {
378 if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
379 //std::cout << "OutEdgeIt: " << dfs;
380 //std::cout << " aNode: " << F.aNode(dfs);
381 //std::cout << " bNode: " << F.bNode(dfs) << " ";
383 typename MutableGraph::NodeIt v=F.aNode(dfs);
384 typename MutableGraph::NodeIt w=F.bNode(dfs);
386 if (F.valid(pred.get(v))) {
387 free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
389 free.set(w, residual_capacity.get(dfs)/*original_edge.get(dfs).free()*/);
392 //std::cout << "AUGMENTATION"<<std::endl;
398 //std::cout << "OutEdgeIt: " << "invalid";
399 //std::cout << " aNode: " << dfs.aNode();
400 //std::cout << " bNode: " << "invalid" << " ";
402 if (dfs.isBNodeNewlyReached()) {
403 //std::cout << "bNodeIsNewlyReached ";
405 //std::cout << "bNodeIsNotNewlyReached ";
406 if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
407 //std::cout << "DELETE ";
408 F.erase(typename MutableGraph::OutEdgeIt(dfs));
411 //if (dfs.isANodeExamined()) {
412 //std::cout << "aNodeIsExamined ";
414 //std::cout << "aNodeIsNotExamined ";
416 //std::cout<<std::endl;
420 typename MutableGraph::NodeIt n=tF;
421 Number augment_value=free.get(tF);
422 while (F.valid(pred.get(n))) {
423 typename MutableGraph::EdgeIt e=pred.get(n);
424 original_edge.get(e).augment(augment_value);
426 if (residual_capacity.get(e)==augment_value)
429 residual_capacity.set(e, residual_capacity.get(e)-augment_value);
438 //int num_of_augmentations=0;
439 while (augmentOnShortestPath()) {
440 //while (augmentOnBlockingFlow<MutableGraph>()) {
441 //std::cout << ++num_of_augmentations << " ";
442 //std::cout<<std::endl;
445 template<typename MutableGraph> void run() {
446 //int num_of_augmentations=0;
447 //while (augmentOnShortestPath()) {
448 while (augmentOnBlockingFlow<MutableGraph>()) {
449 //std::cout << ++num_of_augmentations << " ";
450 //std::cout<<std::endl;
456 for(G->getFirst(e, s); G->valid(e); G->next(e)) {
464 // template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
467 // typedef typename Graph::NodeIt NodeIt;
468 // typedef typename Graph::EdgeIt EdgeIt;
469 // typedef typename Graph::EachEdgeIt EachEdgeIt;
470 // typedef typename Graph::OutEdgeIt OutEdgeIt;
471 // typedef typename Graph::InEdgeIt InEdgeIt;
474 // std::list<NodeIt>& S;
475 // std::list<NodeIt>& T;
477 // const CapacityMap& capacity;
478 // typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
479 // typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
480 // typedef typename AugGraph::EdgeIt AugEdgeIt;
481 // typename Graph::NodeMap<bool> SMap;
482 // typename Graph::NodeMap<bool> TMap;
484 // 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) {
485 // for(typename std::list<NodeIt>::const_iterator i=S.begin();
486 // i!=S.end(); ++i) {
487 // SMap.set(*i, true);
489 // for (typename std::list<NodeIt>::const_iterator i=T.begin();
490 // i!=T.end(); ++i) {
491 // TMap.set(*i, true);
495 // AugGraph res_graph(G, flow, capacity);
496 // bool _augment=false;
497 // NodeIt reached_t_node;
499 // typedef typename AugGraph::NodeMap<bool> ReachedMap;
500 // BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
501 // for(typename std::list<NodeIt>::const_iterator i=S.begin();
502 // i!=S.end(); ++i) {
503 // res_bfs.pushAndSetReached(*i);
505 // //res_bfs.pushAndSetReached(s);
507 // typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
508 // //filled up with invalid iterators
510 // typename AugGraph::NodeMap<Number> free(res_graph);
512 // //searching for augmenting path
513 // while ( !res_bfs.finished() ) {
514 // AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
515 // if (e.valid() && res_bfs.isBNodeNewlyReached()) {
516 // NodeIt v=res_graph.tail(e);
517 // NodeIt w=res_graph.head(e);
519 // if (pred.get(v).valid()) {
520 // free.set(w, std::min(free.get(v), e.free()));
522 // free.set(w, e.free());
524 // if (TMap.get(res_graph.head(e))) {
526 // reached_t_node=res_graph.head(e);
532 // } //end of searching augmenting path
535 // NodeIt n=reached_t_node;
536 // Number augment_value=free.get(reached_t_node);
537 // while (pred.get(n).valid()) {
538 // AugEdgeIt e=pred.get(n);
539 // e.augment(augment_value);
540 // n=res_graph.tail(e);
547 // while (augment()) { }
549 // Number flowValue() {
551 // for(typename std::list<NodeIt>::const_iterator i=S.begin();
552 // i!=S.end(); ++i) {
553 // for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
556 // for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
568 #endif //EDMONDS_KARP_HH