# HG changeset patch # User marci # Date 1079014507 0 # Node ID 27fbd1559fb7edbd60e1fc006afa8a6eb7fa5c63 # Parent 7949a29a334e0fd8267d68da67090f5ea37b26fa graph wrapper improvements, blocking flow on fly diff -r 7949a29a334e -r 27fbd1559fb7 src/work/bfs_iterator.hh --- a/src/work/bfs_iterator.hh Thu Mar 11 12:55:50 2004 +0000 +++ b/src/work/bfs_iterator.hh Thu Mar 11 14:15:07 2004 +0000 @@ -562,10 +562,15 @@ own_reached_map(true) { } ~BfsIterator4() { if (own_reached_map) delete &reached; } void pushAndSetReached(NodeIt s) { + //std::cout << "mimi" << &reached << std::endl; reached.set(s, true); + //std::cout << "mumus" << std::endl; if (bfs_queue.empty()) { + //std::cout << "bibi1" << std::endl; bfs_queue.push(s); + //std::cout << "zizi" << std::endl; G.getFirst(actual_edge, s); + //std::cout << "kiki" << std::endl; if (G.valid(actual_edge)/*.valid()*/) { NodeIt w=G.bNode(actual_edge); if (!reached.get(w)) { @@ -577,6 +582,7 @@ } } } else { + //std::cout << "bibi2" << std::endl; bfs_queue.push(s); } } diff -r 7949a29a334e -r 27fbd1559fb7 src/work/edmonds_karp.hh --- a/src/work/edmonds_karp.hh Thu Mar 11 12:55:50 2004 +0000 +++ b/src/work/edmonds_karp.hh Thu Mar 11 14:15:07 2004 +0000 @@ -279,7 +279,7 @@ bool _augment=false; typedef typename AugGraph::NodeMap ReachedMap; - BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); + BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph); res_bfs.pushAndSetReached(s); typename AugGraph::NodeMap pred(res_graph); @@ -296,9 +296,9 @@ NodeIt w=res_graph.head(e); pred.set(w, e); if (res_graph.valid(pred.get(v))) { - free.set(w, std::min(free.get(v), e.free())); + free.set(w, std::min(free.get(v), res_graph.free(e))); } else { - free.set(w, e.free()); + free.set(w, res_graph.free(e)); } if (res_graph.head(e)==t) { _augment=true; break; } } @@ -311,7 +311,8 @@ Number augment_value=free.get(t); while (res_graph.valid(pred.get(n))) { AugEdgeIt e=pred.get(n); - e.augment(augment_value); + res_graph.augment(e, augment_value); + //e.augment(augment_value); n=res_graph.tail(e); } } @@ -358,7 +359,7 @@ original_edge.update(); original_edge.set(f, e); residual_capacity.update(); - residual_capacity.set(f, e.free()); + residual_capacity.set(f, res_graph.free(e)); } } @@ -376,44 +377,30 @@ while (!dfs.finished()) { ++dfs; if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) { - //std::cout << "OutEdgeIt: " << dfs; - //std::cout << " aNode: " << F.aNode(dfs); - //std::cout << " bNode: " << F.bNode(dfs) << " "; + if (dfs.isBNodeNewlyReached()) { +// std::cout << "OutEdgeIt: " << dfs; +// std::cout << " aNode: " << F.aNode(dfs); +// std::cout << " bNode: " << F.bNode(dfs) << " "; - typename MutableGraph::NodeIt v=F.aNode(dfs); - typename MutableGraph::NodeIt 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))); + typename MutableGraph::NodeIt v=F.aNode(dfs); + typename MutableGraph::NodeIt 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) { + //std::cout << "AUGMENTATION"< EAugGraph; + typedef FilterGraphWrapper< ErasingResGraphWrapper > EAugGraph; + typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt; + typedef typename EAugGraph::EdgeIt EAugEdgeIt; + + EAugGraph res_graph(*G, *flow, *capacity); + + //std::cout << "meg jo1" << std::endl; + + //typedef typename EAugGraph::NodeMap ReachedMap; + BfsIterator4< + ErasingResGraphWrapper, + ErasingResGraphWrapper::OutEdgeIt, + ErasingResGraphWrapper::NodeMap > bfs(res_graph); + + //std::cout << "meg jo2" << std::endl; + + bfs.pushAndSetReached(s); + //std::cout << "meg jo2.5" << std::endl; + + //typename EAugGraph::NodeMap dist(res_graph); //filled up with 0's + typename ErasingResGraphWrapper:: + NodeMap& dist=res_graph.dist; + //std::cout << "meg jo2.6" << std::endl; + + while ( !bfs.finished() ) { + ErasingResGraphWrapper::OutEdgeIt e=bfs; +// EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); + //if (res_graph.valid(e)) { + // std::cout<<"a:"<(); res_graph.valid(n); res_graph.next(n)) { +// std::cout << "dist: " << dist.get(n) << std::endl; +// } + + bool __augment=true; + + while (__augment) { +// std::cout << "new iteration"<< std::endl; + + __augment=false; + //computing blocking flow with dfs + typedef typename EAugGraph::NodeMap BlockingReachedMap; + DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > + dfs(res_graph); + typename EAugGraph::NodeMap pred(res_graph); //invalid iterators + typename EAugGraph::NodeMap free(res_graph); + + dfs.pushAndSetReached(s); + while (!dfs.finished()) { + ++dfs; + if (res_graph.valid(EAugOutEdgeIt(dfs))) { + if (dfs.isBNodeNewlyReached()) { +// std::cout << "OutEdgeIt: " << dfs; +// std::cout << " aNode: " << res_graph.aNode(dfs); +// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free(); +// std::cout << " bNode: " << res_graph.bNode(dfs) << " "; + + typename EAugGraph::NodeIt v=res_graph.aNode(dfs); + typename EAugGraph::NodeIt w=res_graph.bNode(dfs); + + pred.set(w, EAugOutEdgeIt(dfs)); + + //std::cout << EAugOutEdgeIt(dfs).free() << std::endl; + if (res_graph.valid(pred.get(v))) { + free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs)))); + } else { + free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs))); + } + + if (w==t) { +// std::cout << "t is reached, AUGMENTATION"<> "; + + res_graph.erase(dfs); + } + } + + } + + if (__augment) { + typename EAugGraph::NodeIt n=t; + Number augment_value=free.get(t); +// std::cout << "av:" << augment_value << std::endl; + while (res_graph.valid(pred.get(n))) { + EAugEdgeIt e=pred.get(n); + res_graph.augment(e, augment_value); + //e.augment(augment_value); + n=res_graph.tail(e); + if (res_graph.free(e)==0) + res_graph.erase(e); + } + } } diff -r 7949a29a334e -r 27fbd1559fb7 src/work/marci/edmonds_karp_demo.cc --- a/src/work/marci/edmonds_karp_demo.cc Thu Mar 11 12:55:50 2004 +0000 +++ b/src/work/marci/edmonds_karp_demo.cc Thu Mar 11 14:15:07 2004 +0000 @@ -87,7 +87,42 @@ MaxFlow, ListGraph::EdgeMap > max_flow_test(G, s, t, flow, cap); //max_flow_test.augmentWithBlockingFlow(); int i=0; - while (max_flow_test.augmentOnBlockingFlow()) { ++i; } + while (max_flow_test.augmentOnBlockingFlow()) { +// for(EachEdgeIt e=G.template first(); e.valid(); ++e) { +// std::cout<<"("<"<(); e.valid(); ++e) { + // std::cout<<"("<"< flow(G); //0 flow + + Timer ts; + ts.reset(); + //double pre_time=currTime(); + MaxFlow, ListGraph::EdgeMap > max_flow_test(G, s, t, flow, cap); + //max_flow_test.augmentWithBlockingFlow(); + int i=0; + while (max_flow_test.augmentOnBlockingFlow2()) { +// for(EachEdgeIt e=G.template first(); e.valid(); ++e) { +// std::cout<<"("<"<, ListGraph::EdgeMap > max_flow_test(G, s, t, flow, cap); //max_flow_test.augmentWithBlockingFlow(); int i=0; - while (max_flow_test.augmentOnShortestPath()) { ++i; } + while (max_flow_test.augmentOnShortestPath()) { +// for(EachEdgeIt e=G.template first(); e.valid(); ++e) { +// std::cout<<"("<"< I& getFirst(I& i) const { return graph->getFirst(i); } template I& getFirst(I& i, const P& p) const { @@ -66,6 +72,7 @@ NodeMap(const TrivGraphWrapper& _G, T a) : Graph::NodeMap(_G.getGraph(), a) { } }; + template class EdgeMap : public Graph::EdgeMap { public: EdgeMap(const TrivGraphWrapper& _G) : @@ -73,12 +80,6 @@ EdgeMap(const TrivGraphWrapper& _G, T a) : Graph::EdgeMap(_G.getGraph(), a) { } }; - - void setGraph(Graph& _graph) { graph = &_graph; } - Graph& getGraph() const { return (*graph); } - - //TrivGraphWrapper() : graph(0) { } - TrivGraphWrapper(Graph& _graph) : graph(&_graph) { } }; template @@ -97,6 +98,12 @@ typedef typename Graph::InEdgeIt OutEdgeIt; //typedef typename Graph::SymEdgeIt SymEdgeIt; typedef typename Graph::EachEdgeIt EachEdgeIt; + + //RevGraphWrapper() : graph(0) { } + RevGraphWrapper(Graph& _graph) : graph(&_graph) { } + + void setGraph(Graph& _graph) { graph = &_graph; } + Graph& getGraph() const { return (*graph); } template I& getFirst(I& i) const { return graph->getFirst(i); } template I& getFirst(I& i, const P& p) const { @@ -144,6 +151,7 @@ NodeMap(const RevGraphWrapper& _G, T a) : Graph::NodeMap(_G.getGraph(), a) { } }; + template class EdgeMap : public Graph::EdgeMap { public: EdgeMap(const RevGraphWrapper& _G) : @@ -151,12 +159,6 @@ EdgeMap(const RevGraphWrapper& _G, T a) : Graph::EdgeMap(_G.getGraph(), a) { } }; - - void setGraph(Graph& _graph) { graph = &_graph; } - Graph& getGraph() const { return (*graph); } - - //RevGraphWrapper() : graph(0) { } - RevGraphWrapper(Graph& _graph) : graph(&_graph) { } }; @@ -182,6 +184,12 @@ typedef typename Graph::InEdgeIt GraphInEdgeIt; //public: + //UndirGraphWrapper() : graph(0) { } + UndirGraphWrapper(Graph& _graph) : graph(&_graph) { } + + void setGraph(Graph& _graph) { graph = &_graph; } + Graph& getGraph() const { return (*graph); } + class EdgeIt { friend class UndirGraphWrapper; bool out_or_in; //true iff out @@ -196,9 +204,6 @@ class OutEdgeIt : public EdgeIt { friend class UndirGraphWrapper; - //bool out_or_in; //true iff out - //GraphOutEdgeIt out; - //GraphInEdgeIt in; public: OutEdgeIt() : EdgeIt() { } OutEdgeIt(const UndirGraphWrapper& _G, const NodeIt& n) : EdgeIt() { @@ -287,6 +292,7 @@ NodeMap(const UndirGraphWrapper& _G, T a) : Graph::NodeMap(_G.getGraph(), a) { } }; + template class EdgeMap : public Graph::EdgeMap { public: EdgeMap(const UndirGraphWrapper& _G) : @@ -294,12 +300,6 @@ EdgeMap(const UndirGraphWrapper& _G, T a) : Graph::EdgeMap(_G.getGraph(), a) { } }; - - void setGraph(Graph& _graph) { graph = &_graph; } - Graph& getGraph() const { return (*graph); } - - //TrivGraphWrapper() : graph(0) { } - UndirGraphWrapper(Graph& _graph) : graph(&_graph) { } }; @@ -386,13 +386,16 @@ FlowMap* flow; const CapacityMap* capacity; public: + ResGraphWrapper(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : G(&_G), flow(&_flow), capacity(&_capacity) { } // ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : // G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { } - void setGraph(Graph& _graph) { graph = &_graph; } - Graph& getGraph() const { return (*graph); } + + void setGraph(const Graph& _graph) { graph = &_graph; } + const Graph& getGraph() const { return (*G); } + class EdgeIt; class OutEdgeIt; friend class EdgeIt; @@ -401,94 +404,49 @@ class EdgeIt { friend class ResGraphWrapper; protected: - //const ResGraph3* resG; - const Graph* G; - FlowMap* flow; - const CapacityMap* capacity; - //OldSymEdgeIt sym; + bool out_or_in; //true, iff out OldOutEdgeIt out; OldInEdgeIt in; - bool out_or_in; //true, iff out public: EdgeIt() : out_or_in(true) { } - EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : - G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { } - //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) { } - Number free() const { - if (out_or_in) { - return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); - } else { - return (/*resG->*/flow->get(in)); - } - } - bool valid() const { - return out_or_in && out.valid() || in.valid(); } - void augment(Number a) const { - if (out_or_in) { - /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a); - } else { - /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a); - } - } - void print() { - if (out_or_in) { - std::cout << "out "; - if (out.valid()) - std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out)); - else - std::cout << "invalid"; - } - else { - std::cout << "in "; - if (in.valid()) - std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in)); - else - std::cout << "invalid"; - } - std::cout << std::endl; - } +// bool valid() const { +// return out_or_in && out.valid() || in.valid(); } }; - Number free(OldOutEdgeIt out) const { - return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); - } - Number free(OldInEdgeIt in) const { - return (/*resG->*/flow->get(in)); - } class OutEdgeIt : public EdgeIt { friend class ResGraphWrapper; public: OutEdgeIt() { } + //FIXME + OutEdgeIt(const EdgeIt& e) : EdgeIt(e) { } private: - OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { - //out=/*resG->*/G->template first(v); - G->getFirst(out, v); - while( out.valid() && !(EdgeIt::free()>0) ) { ++out; } + OutEdgeIt(const ResGraphWrapper& resG, NodeIt v) : EdgeIt() { + resG.G->getFirst(out, v); + while( out.valid() && !(resG.free(out)>0) ) { ++out; } if (!out.valid()) { out_or_in=0; - //in=/*resG->*/G->template first(v); - G->getFirst(in, v); - while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } + resG.G->getFirst(in, v); + while( in.valid() && !(resG.free(in)>0) ) { ++in; } } } - public: - OutEdgeIt& operator++() { - if (out_or_in) { - NodeIt v=/*resG->*/G->aNode(out); - ++out; - while( out.valid() && !(EdgeIt::free()>0) ) { ++out; } - if (!out.valid()) { - out_or_in=0; - G->getFirst(in, v); //=/*resG->*/G->template first(v); - while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } - } - } else { - ++in; - while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } - } - return *this; - } +// public: +// OutEdgeIt& operator++() { +// if (out_or_in) { +// NodeIt v=/*resG->*/G->aNode(out); +// ++out; +// while( out.valid() && !(EdgeIt::free()>0) ) { ++out; } +// if (!out.valid()) { +// out_or_in=0; +// G->getFirst(in, v); +// while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// } +// } else { +// ++in; +// while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// } +// return *this; +// } }; class EachEdgeIt : public EdgeIt { @@ -497,67 +455,66 @@ public: EachEdgeIt() { } //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { } - EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { - out_or_in=true; - G->getFirst(v); - if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt(); - while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } + EachEdgeIt(const ResGraphWrapper& resG) : EdgeIt() { + resG.G->getFirst(v); + if (v.valid()) resG.G->getFirst(out, v); else out=OldOutEdgeIt(); + while (out.valid() && !(resG.free(out)>0) ) { ++out; } while (v.valid() && !out.valid()) { ++v; - if (v.valid()) G->getFirst(out, v); - while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } + if (v.valid()) resG.G->getFirst(out, v); + while (out.valid() && !(resG.free(out)>0) ) { ++out; } } if (!out.valid()) { out_or_in=0; - G->getFirst(v); - if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } + resG.G->getFirst(v); + if (v.valid()) resG.G->getFirst(in, v); else in=OldInEdgeIt(); + while (in.valid() && !(resG.free(in)>0) ) { ++in; } while (v.valid() && !in.valid()) { ++v; - if (v.valid()) G->getFirst(in, v); - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } + if (v.valid()) resG.G->getFirst(in, v); + while (in.valid() && !(resG.free(in)>0) ) { ++in; } } } } - EachEdgeIt& operator++() { - if (out_or_in) { - ++out; - while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } - while (v.valid() && !out.valid()) { - ++v; - if (v.valid()) G->getFirst(out, v); - while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } - } - if (!out.valid()) { - out_or_in=0; - G->getFirst(v); - if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } - while (v.valid() && !in.valid()) { - ++v; - if (v.valid()) G->getFirst(in, v); - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } - } - } - } else { - ++in; - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } - while (v.valid() && !in.valid()) { - ++v; - if (v.valid()) G->getFirst(in, v); - while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } - } - } - return *this; - } +// EachEdgeIt& operator++() { +// if (out_or_in) { +// ++out; +// while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } +// while (v.valid() && !out.valid()) { +// ++v; +// if (v.valid()) G->getFirst(out, v); +// while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } +// } +// if (!out.valid()) { +// out_or_in=0; +// G->getFirst(v); +// if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); +// while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// while (v.valid() && !in.valid()) { +// ++v; +// if (v.valid()) G->getFirst(in, v); +// while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// } +// } +// } else { +// ++in; +// while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// while (v.valid() && !in.valid()) { +// ++v; +// if (v.valid()) G->getFirst(in, v); +// while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } +// } +// } +// return *this; +// } }; - void getFirst(EachNodeIt& v) const { G->getFirst(v); } - void getFirst(OutEdgeIt& e, NodeIt v) const { - e=OutEdgeIt(*G, v, *flow, *capacity); + EachNodeIt& getFirst(EachNodeIt& v) const { G->getFirst(v); } + OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const { + e=OutEdgeIt(*this, v); } - void getFirst(EachEdgeIt& e) const { - e=EachEdgeIt(*G, *flow, *capacity); + EachEdgeIt& getFirst(EachEdgeIt& e) const { + e=EachEdgeIt(*this); } EachNodeIt& next(EachNodeIt& n) const { return G->next(n); } @@ -566,15 +523,15 @@ if (e.out_or_in) { NodeIt v=G->aNode(e.out); ++(e.out); - while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } + while( G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); } if (!G->valid(e.out)) { e.out_or_in=0; - G->getFirst(e.in, v); //=/*resG->*/G->template first(v); - while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + G->getFirst(e.in, v); + while( G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } } } else { ++(e.in); - while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + while( G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } } return e; } @@ -582,30 +539,30 @@ EachEdgeIt& next(EachEdgeIt& e) const { if (e.out_or_in) { ++(e.out); - while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } + while (G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); } while (G->valid(e.v) && !G->valid(e.out)) { ++(e.v); if (G->valid(e.v)) G->getFirst(e.out, e.v); - while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } + while (G->valid(e.out) && !(free(e.out)>0) ) { ++(e.out); } } if (!G->valid(e.out)) { e.out_or_in=0; G->getFirst(e.v); if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt(); - while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } while (G->valid(e.v) && !G->valid(e.in)) { ++(e.v); if (G->valid(e.v)) G->getFirst(e.in, e.v); - while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } } } } else { ++(e.in); - while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } while (G->valid(e.v) && !G->valid(e.in)) { ++(e.v); if (G->valid(e.v)) G->getFirst(e.in, e.v); - while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } + while (G->valid(e.in) && !(free(e.in)>0) ) { ++(e.in); } } } return e; @@ -642,6 +599,28 @@ bool valid(EdgeIt e) const { return e.out_or_in ? G->valid(e.out) : G->valid(e.in); } + void augment(const EdgeIt& e, Number a) const { + if (e.out_or_in) + flow->set(e.out, flow->get(e.out)+a); + else + flow->set(e.in, flow->get(e.in)-a); + } + + Number free(const EdgeIt& e) const { + if (e.out_or_in) + return (capacity->get(e.out)-flow->get(e.out)); + else + return (flow->get(e.in)); + } + + Number free(OldOutEdgeIt out) const { + return (capacity->get(out)-flow->get(out)); + } + + Number free(OldInEdgeIt in) const { + return (flow->get(in)); + } + template class NodeMap : public Graph::NodeMap { public: NodeMap(const ResGraphWrapper& _G) @@ -679,6 +658,243 @@ return backward_map.get(e.in); } }; + }; + + template + class ErasingResGraphWrapper : public ResGraphWrapper { + protected: + ResGraphWrapper::NodeMap::OutEdgeIt> first_out_edges; + //ResGraphWrapper::NodeMap dist; + public: + ErasingResGraphWrapper(const Graph& _G, FlowMap& _flow, + const CapacityMap& _capacity) : + ResGraphWrapper(_G, _flow, _capacity), + first_out_edges(*this) /*, dist(*this)*/ { + for(EachNodeIt n=this->template first(); this->valid(n); this->next(n)) { + OutEdgeIt e; + ResGraphWrapper::getFirst(e, n); + first_out_edges.set(n, e); + } + } + + //void setGraph(Graph& _graph) { graph = &_graph; } + //Graph& getGraph() const { return (*graph); } + + //TrivGraphWrapper() : graph(0) { } + //ErasingResGraphWrapper(Graph& _graph) : graph(&_graph) { } + + //typedef Graph BaseGraph; + + //typedef typename Graph::NodeIt NodeIt; + //typedef typename Graph::EachNodeIt EachNodeIt; + + //typedef typename Graph::EdgeIt EdgeIt; + //typedef typename Graph::OutEdgeIt OutEdgeIt; + //typedef typename Graph::InEdgeIt InEdgeIt; + //typedef typename Graph::SymEdgeIt SymEdgeIt; + //typedef typename Graph::EachEdgeIt EachEdgeIt; + + typedef typename ResGraphWrapper::NodeIt NodeIt; + typedef typename ResGraphWrapper::EachNodeIt EachNodeIt; + + typedef typename ResGraphWrapper::EdgeIt EdgeIt; + typedef typename ResGraphWrapper::OutEdgeIt OutEdgeIt; + //typedef typename ResGraphWrapper::InEdgeIt InEdgeIt; + //typedef typename Graph::SymEdgeIt SymEdgeIt; + //typedef typename ResGraphWrapper::EachEdgeIt EachEdgeIt; + + EachNodeIt& getFirst(EachNodeIt& n) const { + return ResGraphWrapper::getFirst(n); + } + + OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const { + e=first_out_edges.get(n); + return e; + } + + //ROSSZ template I& getFirst(I& i) const { return getFirst(i); } + //ROSSZ template I& getFirst(I& i, const P& p) const { + // return getFirst(i, p); } + + //template I getNext(const I& i) const { + // return graph->getNext(i); } + //template I& next(I &i) const { return graph->next(i); } + + template< typename It > It first() const { + It e; getFirst(e); return e; } + + template< typename It > It first(const NodeIt& v) const { + It e; getFirst(e, v); return e; } + + //NodeIt head(const EdgeIt& e) const { return graph->head(e); } + //NodeIt tail(const EdgeIt& e) const { return graph->tail(e); } + + //template bool valid(const I& i) const + // { return graph->valid(i); } + + //int nodeNum() const { return graph->nodeNum(); } + //int edgeNum() const { return graph->edgeNum(); } + + //template NodeIt aNode(const I& e) const { + // return graph->aNode(e); } + //template NodeIt bNode(const I& e) const { + // return graph->bNode(e); } + + //NodeIt addNode() const { return graph->addNode(); } + //EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { + // return graph->addEdge(tail, head); } + + //void erase(const OutEdgeIt& e) { + // first_out_edge(this->tail(e))=e; + //} + void erase(const EdgeIt& e) { + OutEdgeIt f(e); + next(f); + first_out_edges.set(this->tail(e), f); + } + //template void erase(const I& i) const { graph->erase(i); } + + //void clear() const { graph->clear(); } + + template class NodeMap : public ResGraphWrapper::NodeMap { + public: + NodeMap(const ErasingResGraphWrapper& _G) : + ResGraphWrapper::NodeMap(_G /*_G.getGraph()*/) { } + NodeMap(const ErasingResGraphWrapper& _G, T a) : + ResGraphWrapper::NodeMap(_G /*_G.getGraph()*/, a) { } + }; + + template class EdgeMap : public ResGraphWrapper::EdgeMap { + public: + EdgeMap(const ErasingResGraphWrapper& _G) : + ResGraphWrapper::EdgeMap(_G /*_G.getGraph()*/) { } + EdgeMap(const ErasingResGraphWrapper& _G, T a) : + ResGraphWrapper::EdgeMap(_G /*_G.getGraph()*/, a) { } + }; + }; + + template + class FilterGraphWrapper { + }; + + template + class FilterGraphWrapper > : public ErasingResGraphWrapper { + + //Graph* graph; + + public: + //typedef Graph BaseGraph; + + typedef typename ErasingResGraphWrapper::NodeIt NodeIt; + typedef typename ErasingResGraphWrapper::EachNodeIt EachNodeIt; + + typedef typename ErasingResGraphWrapper::EdgeIt EdgeIt; + typedef typename ErasingResGraphWrapper::OutEdgeIt OutEdgeIt; + //typedef typename ErasingResGraphWrapper::InEdgeIt InEdgeIt; + //typedef typename Graph::SymEdgeIt SymEdgeIt; + typedef typename ErasingResGraphWrapper::EachEdgeIt EachEdgeIt; + + //FilterGraphWrapper::NodeMap::OutEdgeIt> first_out_edges; + + public: + FilterGraphWrapper(const Graph& _G, FlowMap& _flow, + const CapacityMap& _capacity) : + ErasingResGraphWrapper(_G, _flow, _capacity), dist(*this) { + } + + OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt& n) const { + ErasingResGraphWrapper::getFirst(e, n); + while (valid(e) && (dist.get(tail(e))+1!=dist.get(head(e)))) + ErasingResGraphWrapper::next(e); + return e; + } + + EachNodeIt& next(EachNodeIt& e) const { + return ErasingResGraphWrapper::next(e); + } + + OutEdgeIt& next(OutEdgeIt& e) const { + ErasingResGraphWrapper::next(e); + while (valid(e) && (dist.get(tail(e))+1!=dist.get(head(e)))) + ErasingResGraphWrapper::next(e); + return e; + } + + EachNodeIt& getFirst(EachNodeIt& n) const { + return ErasingResGraphWrapper::getFirst(n); + } + + void erase(const EdgeIt& e) { + OutEdgeIt f(e); + ErasingResGraphWrapper::next(f); + while (valid(f) && (dist.get(tail(f))+1!=dist.get(head(f)))) + ErasingResGraphWrapper::next(f); + first_out_edges.set(this->tail(e), f); + } + + //TrivGraphWrapper() : graph(0) { } + //TrivGraphWrapper(Graph& _graph) : graph(&_graph) { } + + //void setGraph(Graph& _graph) { graph = &_graph; } + //Graph& getGraph() const { return (*graph); } + + //template I& getFirst(I& i) const { return graph->getFirst(i); } + //template I& getFirst(I& i, const P& p) const { + // return graph->getFirst(i, p); } + + //template I getNext(const I& i) const { + // return graph->getNext(i); } + //template I& next(I &i) const { return graph->next(i); } + + template< typename It > It first() const { + It e; getFirst(e); return e; } + + template< typename It > It first(const NodeIt& v) const { + It e; getFirst(e, v); return e; } + + //NodeIt head(const EdgeIt& e) const { return graph->head(e); } + //NodeIt tail(const EdgeIt& e) const { return graph->tail(e); } + + //template bool valid(const I& i) const + // { return graph->valid(i); } + + //template void setInvalid(const I &i); + //{ return graph->setInvalid(i); } + + //int nodeNum() const { return graph->nodeNum(); } + //int edgeNum() const { return graph->edgeNum(); } + + //template NodeIt aNode(const I& e) const { + // return graph->aNode(e); } + //template NodeIt bNode(const I& e) const { + // return graph->bNode(e); } + + //NodeIt addNode() const { return graph->addNode(); } + //EdgeIt addEdge(const NodeIt& tail, const NodeIt& head) const { + // return graph->addEdge(tail, head); } + + //template void erase(const I& i) const { graph->erase(i); } + + //void clear() const { graph->clear(); } + + template class NodeMap : public ErasingResGraphWrapper::NodeMap { + public: + NodeMap(const FilterGraphWrapper >& _G) : + ErasingResGraphWrapper::NodeMap(_G /*_G.getGraph()*/) { } + NodeMap(const FilterGraphWrapper >& _G, T a) : + ErasingResGraphWrapper::NodeMap(_G /*_G.getGraph()*/, a) { } + }; + + template class EdgeMap : public ErasingResGraphWrapper::EdgeMap { + public: + EdgeMap(const FilterGraphWrapper >& _G) : + ErasingResGraphWrapper::EdgeMap(_G /*_G.getGraph()*/) { } + EdgeMap(const FilterGraphWrapper >& _G, T a) : + ErasingResGraphWrapper::EdgeMap(_G /*_G.getGraph()*/, a) { } + }; + + public: + ErasingResGraphWrapper::NodeMap dist; }; diff -r 7949a29a334e -r 27fbd1559fb7 src/work/marci/makefile --- a/src/work/marci/makefile Thu Mar 11 12:55:50 2004 +0000 +++ b/src/work/marci/makefile Thu Mar 11 14:15:07 2004 +0000 @@ -16,8 +16,8 @@ sinclude .depend edmonds_karp_demo: - $(CXX3) $(CXXFLAGS) -O3 -I. -I.. -o edmonds_karp_demo edmonds_karp_demo.cc - $(CXX3) $(CXXFLAGS) -g -pg -O3 -I. -I.. -o edmonds_karp_demo_prof edmonds_karp_demo.cc + $(CXX3) $(CXXFLAGS) -g -O3 -I. -I.. -o edmonds_karp_demo edmonds_karp_demo.cc + $(CXX3) $(CXXFLAGS) -g -pg -O3 -I. -I.. -o edmonds_karp_demo_prof edmonds_karp_demo_prof.cc edmonds_karp_demo_alpar: $(CXX3) $(CXXFLAGS) -O3 -I. -I.. -I../alpar -o edmonds_karp_demo_alpar edmonds_karp_demo_alpar.cc diff -r 7949a29a334e -r 27fbd1559fb7 src/work/marci_graph_demo.cc --- a/src/work/marci_graph_demo.cc Thu Mar 11 12:55:50 2004 +0000 +++ b/src/work/marci_graph_demo.cc Thu Mar 11 14:15:07 2004 +0000 @@ -236,13 +236,15 @@ std::cout<<"("<"<(); e.valid(); ++e) { - std::cout<<"("<"<(); e.valid(); ++e) { + std::cout<<"("<"<