diff -r eb8dcb4ab78d -r 8c6292ec54c6 src/work/edmonds_karp.hh --- a/src/work/edmonds_karp.hh Thu Mar 04 15:13:43 2004 +0000 +++ b/src/work/edmonds_karp.hh Thu Mar 04 19:38:07 2004 +0000 @@ -245,303 +245,6 @@ }; }; - template - class ResGraphWrapper { - public: - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EachNodeIt EachNodeIt; - private: - typedef typename Graph::OutEdgeIt OldOutEdgeIt; - typedef typename Graph::InEdgeIt OldInEdgeIt; - const Graph* G; - 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) { } - class EdgeIt; - class OutEdgeIt; - friend class EdgeIt; - friend class OutEdgeIt; - - class EdgeIt { - friend class ResGraphWrapper; - protected: - //const ResGraph3* resG; - const Graph* G; - FlowMap* flow; - const CapacityMap* capacity; - //OldSymEdgeIt sym; - 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; - } - }; - - 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() { } - 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; } - if (!out.valid()) { - out_or_in=0; - //in=/*resG->*/G->template first(v); - G->getFirst(in, v); - while( in.valid() && !(EdgeIt::free()>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; - } - }; - - class EachEdgeIt : public EdgeIt { - friend class ResGraphWrapper; - typename Graph::EachNodeIt v; - 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; } - 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; } - } - } - } - 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); - } - void getFirst(EachEdgeIt& e) const { - e=EachEdgeIt(*G, *flow, *capacity); - } - - EachNodeIt& next(EachNodeIt& n) const { return G->next(n); } - - OutEdgeIt& next(OutEdgeIt& e) const { - if (e.out_or_in) { - NodeIt v=G->aNode(e.out); - ++(e.out); - while( G->valid(e.out) && !(e.free()>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); } - } - } else { - ++(e.in); - while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } - } - return e; - } - - 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.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); } - } - 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.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); } - } - } - } else { - ++(e.in); - while (G->valid(e.in) && !(e.free()>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); } - } - } - return e; - } - - - template< typename It > - It first() const { - It e; - getFirst(e); - return e; - } - - template< typename It > - It first(NodeIt v) const { - It e; - getFirst(e, v); - return e; - } - - NodeIt tail(EdgeIt e) const { - return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); } - NodeIt head(EdgeIt e) const { - return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); } - - NodeIt aNode(OutEdgeIt e) const { - return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); } - NodeIt bNode(OutEdgeIt e) const { - return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); } - - int id(NodeIt v) const { return G->id(v); } - - bool valid(NodeIt n) const { return G->valid(n); } - bool valid(EdgeIt e) const { - return e.out_or_in ? G->valid(e.out) : G->valid(e.in); } - - template - class NodeMap { - typename Graph::NodeMap node_map; - public: - NodeMap(const ResGraphWrapper& _G) : node_map(*(_G.G)) { } - NodeMap(const ResGraphWrapper& _G, T a) : node_map(*(_G.G), a) { } - void set(NodeIt nit, T a) { node_map.set(nit, a); } - T get(NodeIt nit) const { return node_map.get(nit); } - }; - - template - class EdgeMap { - typename Graph::EdgeMap forward_map, backward_map; - public: - EdgeMap(const ResGraphWrapper& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { } - EdgeMap(const ResGraphWrapper& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { } - void set(EdgeIt e, T a) { - if (e.out_or_in) - forward_map.set(e.out, a); - else - backward_map.set(e.in, a); - } - T get(EdgeIt e) { - if (e.out_or_in) - return forward_map.get(e.out); - else - return backward_map.get(e.in); - } - }; - - }; template class MaxFlow { @@ -758,105 +461,105 @@ }; - template - class MaxFlow2 { - public: - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::EachEdgeIt EachEdgeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - typedef typename Graph::InEdgeIt InEdgeIt; - private: - const Graph& G; - std::list& S; - std::list& T; - FlowMap& flow; - const CapacityMap& capacity; - typedef ResGraph AugGraph; - typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; - typedef typename AugGraph::EdgeIt AugEdgeIt; - typename Graph::NodeMap SMap; - typename Graph::NodeMap TMap; - public: - MaxFlow2(const Graph& _G, std::list& _S, std::list& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { - for(typename std::list::const_iterator i=S.begin(); - i!=S.end(); ++i) { - SMap.set(*i, true); - } - for (typename std::list::const_iterator i=T.begin(); - i!=T.end(); ++i) { - TMap.set(*i, true); - } - } - bool augment() { - AugGraph res_graph(G, flow, capacity); - bool _augment=false; - NodeIt reached_t_node; +// template +// class MaxFlow2 { +// public: +// typedef typename Graph::NodeIt NodeIt; +// typedef typename Graph::EdgeIt EdgeIt; +// typedef typename Graph::EachEdgeIt EachEdgeIt; +// typedef typename Graph::OutEdgeIt OutEdgeIt; +// typedef typename Graph::InEdgeIt InEdgeIt; +// private: +// const Graph& G; +// std::list& S; +// std::list& T; +// FlowMap& flow; +// const CapacityMap& capacity; +// typedef ResGraphWrapper AugGraph; +// typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; +// typedef typename AugGraph::EdgeIt AugEdgeIt; +// typename Graph::NodeMap SMap; +// typename Graph::NodeMap TMap; +// public: +// MaxFlow2(const Graph& _G, std::list& _S, std::list& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// SMap.set(*i, true); +// } +// for (typename std::list::const_iterator i=T.begin(); +// i!=T.end(); ++i) { +// TMap.set(*i, true); +// } +// } +// bool augment() { +// AugGraph res_graph(G, flow, capacity); +// bool _augment=false; +// NodeIt reached_t_node; - typedef typename AugGraph::NodeMap ReachedMap; - BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); - for(typename std::list::const_iterator i=S.begin(); - i!=S.end(); ++i) { - res_bfs.pushAndSetReached(*i); - } - //res_bfs.pushAndSetReached(s); +// typedef typename AugGraph::NodeMap ReachedMap; +// BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// res_bfs.pushAndSetReached(*i); +// } +// //res_bfs.pushAndSetReached(s); - typename AugGraph::NodeMap pred(res_graph); - //filled up with invalid iterators +// typename AugGraph::NodeMap pred(res_graph); +// //filled up with invalid iterators - typename AugGraph::NodeMap free(res_graph); +// typename AugGraph::NodeMap free(res_graph); - //searching for augmenting path - while ( !res_bfs.finished() ) { - AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); - if (e.valid() && res_bfs.isBNodeNewlyReached()) { - NodeIt v=res_graph.tail(e); - NodeIt w=res_graph.head(e); - pred.set(w, e); - if (pred.get(v).valid()) { - free.set(w, std::min(free.get(v), e.free())); - } else { - free.set(w, e.free()); - } - if (TMap.get(res_graph.head(e))) { - _augment=true; - reached_t_node=res_graph.head(e); - break; - } - } +// //searching for augmenting path +// while ( !res_bfs.finished() ) { +// AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); +// if (e.valid() && res_bfs.isBNodeNewlyReached()) { +// NodeIt v=res_graph.tail(e); +// NodeIt w=res_graph.head(e); +// pred.set(w, e); +// if (pred.get(v).valid()) { +// free.set(w, std::min(free.get(v), e.free())); +// } else { +// free.set(w, e.free()); +// } +// if (TMap.get(res_graph.head(e))) { +// _augment=true; +// reached_t_node=res_graph.head(e); +// break; +// } +// } - ++res_bfs; - } //end of searching augmenting path +// ++res_bfs; +// } //end of searching augmenting path - if (_augment) { - NodeIt n=reached_t_node; - Number augment_value=free.get(reached_t_node); - while (pred.get(n).valid()) { - AugEdgeIt e=pred.get(n); - e.augment(augment_value); - n=res_graph.tail(e); - } - } +// if (_augment) { +// NodeIt n=reached_t_node; +// Number augment_value=free.get(reached_t_node); +// while (pred.get(n).valid()) { +// AugEdgeIt e=pred.get(n); +// e.augment(augment_value); +// n=res_graph.tail(e); +// } +// } - return _augment; - } - void run() { - while (augment()) { } - } - Number flowValue() { - Number a=0; - for(typename std::list::const_iterator i=S.begin(); - i!=S.end(); ++i) { - for(OutEdgeIt e=G.template first(*i); e.valid(); ++e) { - a+=flow.get(e); - } - for(InEdgeIt e=G.template first(*i); e.valid(); ++e) { - a-=flow.get(e); - } - } - return a; - } - }; +// return _augment; +// } +// void run() { +// while (augment()) { } +// } +// Number flowValue() { +// Number a=0; +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// for(OutEdgeIt e=G.template first(*i); e.valid(); ++e) { +// a+=flow.get(e); +// } +// for(InEdgeIt e=G.template first(*i); e.valid(); ++e) { +// a-=flow.get(e); +// } +// } +// return a; +// } +// };