# HG changeset patch # User marci # Date 1075898793 0 # Node ID 41c7f9c09a12514265b3320af1f41c4f10323770 # Parent f71840c04b2ab7439e439e1e71b99a940c8eea4b . diff -r f71840c04b2a -r 41c7f9c09a12 src/work/edmonds_karp.hh --- a/src/work/edmonds_karp.hh Wed Feb 04 12:45:32 2004 +0000 +++ b/src/work/edmonds_karp.hh Wed Feb 04 12:46:33 2004 +0000 @@ -1,5 +1,5 @@ -#ifndef MARCI_MAX_FLOW_HH -#define MARCI_MAX_FLOW_HH +#ifndef EDMONDS_KARP_HH +#define EDMONDS_KARP_HH #include @@ -7,17 +7,17 @@ namespace marci { - template + template class ResGraph { typedef typename Graph::NodeIt NodeIt; typedef typename Graph::EachNodeIt EachNodeIt; typedef typename Graph::SymEdgeIt OldSymEdgeIt; const Graph& G; - typename Graph::EdgeMap& flow; - const typename Graph::EdgeMap& capacity; + FlowMap& flow; + const CapacityMap& capacity; public: - ResGraph(const Graph& _G, typename Graph::EdgeMap& _flow, - const typename Graph::EdgeMap& _capacity) : + ResGraph(const Graph& _G, FlowMap& _flow, + const CapacityMap& _capacity) : G(_G), flow(_flow), capacity(_capacity) { } class EdgeIt; @@ -26,9 +26,9 @@ friend class OutEdgeIt; class EdgeIt { - friend class ResGraph; + friend class ResGraph; protected: - const ResGraph* resG; + const ResGraph* resG; OldSymEdgeIt sym; public: EdgeIt() { } @@ -51,12 +51,12 @@ }; class OutEdgeIt : public EdgeIt { - friend class ResGraph; + friend class ResGraph; public: OutEdgeIt() { } //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; } private: - OutEdgeIt(const ResGraph& _resG, const NodeIt v) { + OutEdgeIt(const ResGraph& _resG, const NodeIt v) { resG=&_resG; sym=resG->G.template first(v); while( sym.valid() && !(free()>0) ) { ++sym; } @@ -98,109 +98,106 @@ template class NodeMap { - //const ResGraph& G; typename Graph::NodeMap node_map; public: - NodeMap(const ResGraph& _G) : node_map(_G.G)/*: G(_G)*/ { } - NodeMap(const ResGraph& _G, const ValueType a) : node_map(_G.G, a) /*: G(_G)*/ { } + NodeMap(const ResGraph& _G) : node_map(_G.G) { } + NodeMap(const ResGraph& _G, const ValueType a) : node_map(_G.G, a) { } void set(const NodeIt nit, const ValueType a) { node_map.set(nit, a); } ValueType get(const NodeIt nit) const { return node_map.get(nit); } }; }; - template - struct max_flow_type { + template + class MaxFlow { 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; const Graph& G; - NodeIt s; - NodeIt t; - typename Graph::EdgeMap flow; - const typename Graph::EdgeMap& capacity; + const NodeIt s; + const NodeIt t; + FlowMap& flow; + const CapacityMap& capacity; + typedef ResGraph AugGraph; + typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; + typedef typename AugGraph::EdgeIt AugEdgeIt; + public: + MaxFlow(const Graph& _G, const NodeIt _s, const NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) { } + bool augment() { + AugGraph res_graph(G, flow, capacity); + bool _augment=false; + + typedef typename AugGraph::NodeMap ReachedMap; + BfsIterator2< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); + res_bfs.pushAndSetReached(s); + + typename AugGraph::NodeMap pred(res_graph); + //filled with invalid iterators + + typename AugGraph::NodeMap free(res_graph); + + //searching for augmenting path + while ( !res_bfs.finished() ) { + //std::queue bfs_copy(res_bfs.getBfsQueue()); + //while (!bfs_copy.empty()) { + // AugOutEdgeIt e=bfs_copy.front(); + // bfs_copy.pop(); + // if (e.valid()) { + // std::cout<<"queue:"<"<"<<" "; + // } + //} + //std::cout<"<"<"<& _capacity) : G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { } + if (_augment) { + NodeIt n=t; + T augment_value=free.get(t); + //std::cout<<"augmentation: "; + while (pred.get(n).valid()) { + AugEdgeIt e=pred.get(n); + e.augment(augment_value); + //std::cout<<"("<"<(); e.valid(); ++e) { + //std::cout<<"("<"< AugGraph; - AugGraph res_graph(G, flow, capacity); - - bool augment; - do { - augment=false; - - typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; - typedef typename AugGraph::EdgeIt AugEdgeIt; - typedef std::queue BfsQueue; - BfsQueue bfs_queue; - bfs_queue.push(res_graph.template first(s)); - - typedef typename AugGraph::NodeMap ReachedMap; - ReachedMap reached(res_graph, false); - reached.set(s, true); - - bfs_iterator1< AugGraph, ReachedMap > - res_bfs(res_graph, bfs_queue, reached); - - typedef typename AugGraph::NodeMap PredMap; - PredMap pred(res_graph); - //typename AugGraph::EdgeIt a; //invalid - //a.makeInvalid(); - //pred.set(s, a); - - typedef typename AugGraph::NodeMap FreeMap; - FreeMap free(res_graph); - - //searching for augmenting path - while ( res_bfs.valid() ) { - //std::cout<<"KULSO ciklus itt jar: "<"<"<"<(); e.valid(); ++e) { - std::cout<<"("<"< reached(G, false); + //reached.set(s, true); + //std::queue bfs_queue; + //bfs_queue.push(G.first(s)); + BfsIterator2< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap > bfs(G); + bfs.pushAndSetReached(s); + while (!bfs.finished()) { + if (OutEdgeIt(bfs).valid()) { + std::cout << "OutEdgeIt: " << bfs; + std::cout << " aNode: " << G.aNode(bfs); + std::cout << " bNode: " << G.bNode(bfs) << " "; + } else { + std::cout << "OutEdgeIt: " << "invalid"; + std::cout << " aNode: " << G.aNode(bfs); + std::cout << " bNode: " << "invalid" << " "; + } + if (bfs.isBNodeNewlyReached()) { + std::cout << "bNodeIsNewlyReached "; + } else { + std::cout << "bNodeIsNotNewlyReached "; + } + if (bfs.isANodeExamined()) { + std::cout << "aNodeIsExamined "; + } else { + std::cout << "aNodeIsNotExamined "; + } + std::cout< namespace marci { template - int number_of(It _it) { - int i=0; - for( ; _it.valid(); ++_it) { ++i; } - return i; - } - - template int count(It it) { int i=0; for( ; it.valid(); ++it) { ++i; } @@ -20,9 +13,12 @@ } class ListGraph { + class node_item; class edge_item; + public: + class NodeIt; class EachNodeIt; class EdgeIt; @@ -30,70 +26,38 @@ class OutEdgeIt; class InEdgeIt; class SymEdgeIt; + template class NodeMap; + template class EdgeMap; + + private: + + template friend class NodeMap; + template friend class EdgeMap; - template + template class NodeMap { - //typedef typename Graph::NodeIt NodeIt; - //typedef typename Graph::EachNodeIt EachNodeIt; const ListGraph& G; - std::vector container; + std::vector container; public: - NodeMap(const ListGraph& _G) : G(_G) { - int i=0; - for(EachNodeIt it=G.template first(); it.valid(); ++it) ++i; - container.resize(i); - } - NodeMap(const ListGraph& _G, const T a) : G(_G) { - for(EachNodeIt it=G.template first(); it.valid(); ++it) { - container.push_back(a); - } - } - void set(const NodeIt nit, const T a) { container[G.id(nit)]=a; } - T get(const NodeIt nit) const { return container[G.id(nit)]; } + NodeMap(const ListGraph& _G) : G(_G), container(_G.node_id) { } + NodeMap(const ListGraph& _G, const ValueType a) : + G(_G), container(_G.node_id, a) { } + void set(const NodeIt nit, const ValueType a) { container[G.id(nit)]=a; } + ValueType get(const NodeIt nit) const { return container[G.id(nit)]; } }; - template + template class EdgeMap { - //typedef typename Graph::EdgeIt EdgeIt; - //typedef typename Graph::EachEdgeIt EachEdgeIt; const ListGraph& G; - std::vector container; + std::vector container; public: - EdgeMap(const ListGraph& _G) : G(_G) { - int i=0; - for(EachEdgeIt it=G.template first(); it.valid(); ++it) ++i; - container.resize(i); - } - EdgeMap(const ListGraph& _G, const T a) : G(_G) { - for(EachEdgeIt it=G.template first(); it.valid(); ++it) { - container.push_back(a); - } - } - void set(const EdgeIt eit, const T a) { container[G.id(eit)]=a; } - T get(const EdgeIt eit) const { return container[G.id(eit)]; } + EdgeMap(const ListGraph& _G) : G(_G), container(_G.edge_id) { } + EdgeMap(const ListGraph& _G, const ValueType a) : + G(_G), container(_G.edge_id, a) { } + void set(const EdgeIt eit, const ValueType a) { container[G.id(eit)]=a; } + ValueType get(const EdgeIt eit) const { return container[G.id(eit)]; } }; - //typedef template NodePropertyVector NodeMap; - //template - //typedef NodePropertyVector NodeMap; - //template - //typedef typename NodePropertyVector NodeMap; - //template - //typedef NodePropertyVector NodeMap; - //template - //typedef typename NodePropertyVector NodeMap; - //template - //typedef template NodePropertyVector NodeMap; - //template - //typedef template typename NodePropertyVector NodeMap; - //template - //typedef template NodePropertyVector NodeMap; - //template - //typedef template typename NodePropertyVector NodeMap; - //template - //typedef EdgePropertyVector EdgeMap; - - private: int node_id; int edge_id; int _node_num; @@ -113,7 +77,7 @@ friend class SymEdgeIt; friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); - ListGraph* G; + //ListGraph* G; int id; edge_item* _first_out_edge; edge_item* _last_out_edge; @@ -135,7 +99,7 @@ friend class InEdgeIt; friend class SymEdgeIt; friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); - ListGraph* G; + //ListGraph* G; int id; node_item* _tail; node_item* _head; @@ -256,17 +220,12 @@ /* functions to construct iterators from the graph, or from each other */ - EachNodeIt firstNode() const { return EachNodeIt(_first_node); } - EachEdgeIt firstEdge() const { - node_item* v=_first_node; - edge_item* edge=v->_first_out_edge; - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } - return EachEdgeIt(v, edge); - } + //EachNodeIt firstNode() const { return EachNodeIt(*this); } + //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); } //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); } - InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); } - SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); } + //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); } + //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); } NodeIt tail(const EdgeIt e) const { return e.tailNode(); } NodeIt head(const EdgeIt e) const { return e.headNode(); } @@ -287,8 +246,8 @@ /* same methods in other style */ /* for experimental purpose */ - void getFirst(EachNodeIt& v) const { v=EachNodeIt(_first_node); } - void getFirst(EachEdgeIt& e) const { e=firstEdge(); } + void getFirst(EachNodeIt& v) const { v=EachNodeIt(*this); } + void getFirst(EachEdgeIt& e) const { e=EachEdgeIt(*this); } void getFirst(OutEdgeIt& e, const NodeIt& v) const { e=OutEdgeIt(v); } void getFirst(InEdgeIt& e, const NodeIt& v) const { e=InEdgeIt(v); } void getFirst(SymEdgeIt& e, const NodeIt& v) const { e=SymEdgeIt(v); } @@ -386,10 +345,11 @@ class EachNodeIt : public NodeIt { friend class ListGraph; + protected: + EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { } public: EachNodeIt() : NodeIt() { } EachNodeIt(node_item* v) : NodeIt(v) { } - EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { } EachNodeIt& operator++() { node=node->_next_node; return *this; } }; @@ -422,16 +382,17 @@ class EachEdgeIt : public EdgeIt { friend class ListGraph; - node_item* v; - public: - EachEdgeIt() : EdgeIt(), v(0) { } - EachEdgeIt(node_item* _v, edge_item* _e) : EdgeIt(_e), v(_v) { } + protected: EachEdgeIt(const ListGraph& G) { - v=G._first_node; - edge=v->_first_out_edge; + node_item* v=G._first_node; + if (v) edge=v->_first_out_edge; else edge=0; while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } } + public: + EachEdgeIt() : EdgeIt() { } + EachEdgeIt(edge_item* _e) : EdgeIt(_e) { } EachEdgeIt& operator++() { + node_item* v=edge->_tail; edge=edge->_next_out; while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } return *this; @@ -441,11 +402,10 @@ class OutEdgeIt : public EdgeIt { friend class ListGraph; node_item* v; - public: - OutEdgeIt() : EdgeIt(), v(0) { } protected: OutEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_out_edge; } public: + OutEdgeIt() : EdgeIt(), v(0) { } OutEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_out_edge; } OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } protected: @@ -457,13 +417,10 @@ class InEdgeIt : public EdgeIt { friend class ListGraph; node_item* v; + protected: + InEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_in_edge; } public: InEdgeIt() : EdgeIt(), v(0) { } - protected: - InEdgeIt(const NodeIt& _v) : v(_v.node) { - edge=v->_first_in_edge; - } - public: InEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_in_edge; } InEdgeIt& operator++() { edge=edge->_next_in; return *this; } protected: @@ -476,8 +433,6 @@ friend class ListGraph; bool out_or_in; //1 iff out, 0 iff in node_item* v; - public: - SymEdgeIt() : EdgeIt(), v(0) { } protected: SymEdgeIt(const NodeIt& _v) : v(_v.node) { out_or_in=1; @@ -485,6 +440,7 @@ if (!edge) { edge=v->_first_in_edge; out_or_in=0; } } public: + SymEdgeIt() : EdgeIt(), v(0) { } SymEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { out_or_in=1; edge=v->_first_out_edge; @@ -549,4 +505,4 @@ } //namespace marci -#endif //MARCI_LIST_GRAPH_HH +#endif //LIST_GRAPH_HH