# HG changeset patch # User marci # Date 1079687360 0 # Node ID 47f62d789fe79b6a24f45ef55459ffea35d81332 # Parent 992aac9c9541470ba2223c1b4aef5d90f444d408 . diff -r 992aac9c9541 -r 47f62d789fe7 src/work/edmonds_karp.h --- a/src/work/edmonds_karp.h Fri Mar 19 07:59:52 2004 +0000 +++ b/src/work/edmonds_karp.h Fri Mar 19 09:09:20 2004 +0000 @@ -413,6 +413,109 @@ return _augment; } + template bool augmentOnBlockingFlow1() { + bool _augment=false; + + AugGraph res_graph(*G, *flow, *capacity); + + //bfs for distances on the residual graph + typedef typename AugGraph::NodeMap ReachedMap; + BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); + bfs.pushAndSetReached(s); + typename AugGraph::NodeMap dist(res_graph); //filled up with 0's + + //F will contain the physical copy of the residual graph + //with the set of edges which areon shortest paths + MutableGraph F; + typename AugGraph::NodeMap + res_graph_to_F(res_graph); + for(typename AugGraph::NodeIt n=res_graph.template first(); res_graph.valid(n); res_graph.next(n)) { + res_graph_to_F.set(n, F.addNode()); + } + typename MutableGraph::Node sF=res_graph_to_F.get(s); + typename MutableGraph::Node tF=res_graph_to_F.get(t); + typename MutableGraph::EdgeMap original_edge(F); + typename MutableGraph::EdgeMap residual_capacity(F); + + while ( !bfs.finished() ) { + AugOutEdgeIt e=bfs; + if (res_graph.valid(e)) { + if (bfs.isBNodeNewlyReached()) { + dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); + typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); + original_edge.update(); + original_edge.set(f, e); + residual_capacity.update(); + residual_capacity.set(f, res_graph.free(e)); + } else { + if (dist.get(res_graph.head(e))==(dist.get(res_graph.tail(e))+1)) { + typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); + original_edge.update(); + original_edge.set(f, e); + residual_capacity.update(); + residual_capacity.set(f, res_graph.free(e)); + } + } + } + ++bfs; + } //computing distances from s in the residual graph + + bool __augment=true; + + while (__augment) { + __augment=false; + //computing blocking flow with dfs + typedef typename MutableGraph::NodeMap BlockingReachedMap; + DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F); + typename MutableGraph::NodeMap pred(F); + pred.set(sF, typename MutableGraph::Edge(INVALID)); + //invalid iterators for sources + + typename MutableGraph::NodeMap free(F); + + dfs.pushAndSetReached(sF); + while (!dfs.finished()) { + ++dfs; + if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) { + if (dfs.isBNodeNewlyReached()) { + typename MutableGraph::Node v=F.aNode(dfs); + typename MutableGraph::Node w=F.bNode(dfs); + pred.set(w, dfs); + if (F.valid(pred.get(v))) { + free.set(w, std::min(free.get(v), residual_capacity.get(dfs))); + } else { + free.set(w, residual_capacity.get(dfs)); + } + if (w==tF) { + __augment=true; + _augment=true; + break; + } + + } else { + F.erase(typename MutableGraph::OutEdgeIt(dfs)); + } + } + } + + if (__augment) { + typename MutableGraph::Node n=tF; + Number augment_value=free.get(tF); + while (F.valid(pred.get(n))) { + typename MutableGraph::Edge e=pred.get(n); + res_graph.augment(original_edge.get(e), augment_value); + n=F.tail(e); + if (residual_capacity.get(e)==augment_value) + F.erase(e); + else + residual_capacity.set(e, residual_capacity.get(e)-augment_value); + } + } + + } + + return _augment; + } bool augmentOnBlockingFlow2() { bool _augment=false; diff -r 992aac9c9541 -r 47f62d789fe7 src/work/marci/dimacs.h --- a/src/work/marci/dimacs.h Fri Mar 19 07:59:52 2004 +0000 +++ b/src/work/marci/dimacs.h Fri Mar 19 09:09:20 2004 +0000 @@ -25,9 +25,6 @@ case 'c': //comment getline(is, str); break; -// case 't': //type -// getline(is, str); -// break; case 'p': //problem definition is >> problem >> n >> m; getline(is, str); diff -r 992aac9c9541 -r 47f62d789fe7 src/work/marci/edmonds_karp_demo.cc --- a/src/work/marci/edmonds_karp_demo.cc Fri Mar 19 07:59:52 2004 +0000 +++ b/src/work/marci/edmonds_karp_demo.cc Fri Mar 19 09:09:20 2004 +0000 @@ -34,8 +34,8 @@ typedef ListGraph MutableGraph; -// typedef SmartGraph Graph; - typedef ListGraph Graph; + typedef SmartGraph Graph; + //typedef ListGraph Graph; typedef Graph::Node Node; typedef Graph::EdgeIt EdgeIt; @@ -114,6 +114,35 @@ } { + std::cout << "SmartGraph..." << std::endl; + std::cout << "edmonds karp demo (physical blocking flow 1 augmentation)..." << std::endl; + Graph::EdgeMap flow(G); //0 flow + + Timer ts; + ts.reset(); + + MaxFlow, Graph::EdgeMap > max_flow_test(G, s, t, flow, cap); + //max_flow_test.augmentWithBlockingFlow(); + int i=0; + while (max_flow_test.augmentOnBlockingFlow1()) { +// for(EdgeIt e=G.template first(); e.valid(); ++e) { +// std::cout<<"("<"<(); e.valid(); ++e) { +// std::cout<<"("<"< flow(G); //0 flow