# HG changeset patch # User marci # Date 1083578667 0 # Node ID 72143568cadcbfe48d5a5c021ad54093c621901c # Parent 2784b804abb3df47f1f28e6e55da8ba4fa676595 matching, flows diff -r 2784b804abb3 -r 72143568cadc src/work/jacint/max_flow.h --- a/src/work/jacint/max_flow.h Mon May 03 09:44:00 2004 +0000 +++ b/src/work/jacint/max_flow.h Mon May 03 10:04:27 2004 +0000 @@ -88,6 +88,20 @@ //In preflow, it shows levels of nodes. //typename Graph::template NodeMap level; typename Graph::template NodeMap excess; +// protected: +// MaxFlow() { } +// void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, +// FlowMap& _flow) +// { +// g=&_G; +// s=_s; +// t=_t; +// capacity=&_capacity; +// flow=&_flow; +// n=_G.nodeNum; +// level.set (_G); //kellene vmi ilyesmi fv +// excess(_G,0); //itt is +// } public: diff -r 2784b804abb3 -r 72143568cadc src/work/marci/bipartite_graph_wrapper.h --- a/src/work/marci/bipartite_graph_wrapper.h Mon May 03 09:44:00 2004 +0000 +++ b/src/work/marci/bipartite_graph_wrapper.h Mon May 03 10:04:27 2004 +0000 @@ -310,7 +310,7 @@ //(invalid, 2, vmi) //invalid, 3, invalid) template class NodeMap; - template class EdgeMap; + template class EdgeMap; // template friend class NodeMap; // template friend class EdgeMap; @@ -385,7 +385,7 @@ class Edge : public Graph::Edge { friend class GraphWrapper; friend class stGraphWrapper; - template friend class EdgeMap; + template friend class EdgeMap; int spec; typename Graph::Node n; public: @@ -412,6 +412,7 @@ // operator<<(std::ostream& os, const typename stGraphWrapper::Edge& i); friend std::ostream& operator<< (std::ostream& os, const Edge& i); int getSpec() const { return spec; } + typename Graph::Node getNode() const { return n; } }; class OutEdgeIt { @@ -702,6 +703,7 @@ template class NodeMap : public GraphWrapper::template NodeMap { typedef typename GraphWrapper::template NodeMap Parent; + protected: T s_value, t_value; public: NodeMap(const stGraphWrapper& _G) : Parent(_G), @@ -714,14 +716,11 @@ switch (n.spec) { case 0: return Parent::operator[](n); - break; case 1: return s_value; - break; case 2: default: return t_value; - break; } } void set(const Node& n, T t) { @@ -740,11 +739,50 @@ } }; - template::template EdgeMap > - class EdgeMap : public Parent { - //typedef typename GraphWrapper::template EdgeMap Parent; + /// This class is to wrap a node-map of \c Graph and two variables + /// storing values for \c S_NODE and \c T_NODE to a node-map of + /// stGraphWrapper. + template class NodeMapWrapper { + public: + typedef Node KeyType; + typedef typename NM::ValueType ValueType; + protected: + NM* nm; + ValueType* s_value, t_value; + public: + NodeMapWrapper(NM& _nm, ValueType& _s_value, ValueType& _t_value) : + nm(&_nm), s_value(&_s_value), t_value(&_t_value) { } + ValueType operator[](const Node& n) const { + switch (n.getSpec()) { + case 0: + return (*nm)[n]; + case 1: + return *s_value; + case 2: + default: + return *t_value; + } + } + void set(const Node& n, ValueType t) { + switch (n.getSpec()) { + case 0: + nm->set(n, t); + break; + case 1: + *s_value=t; + break; + case 2: + default: + *t_value=t; + break; + } + } + }; + + template + class EdgeMap : public GraphWrapper::template EdgeMap { + typedef typename GraphWrapper::template EdgeMap Parent; + protected: typename GraphWrapper::template NodeMap node_value; public: EdgeMap(const stGraphWrapper& _G) : Parent(_G), @@ -755,14 +793,11 @@ switch (e.spec) { case 0: return Parent::operator[](e); - break; case 1: return node_value[e.n]; - break; case 2: default: return node_value[e.n]; - break; } } void set(const Edge& e, T t) { @@ -781,43 +816,45 @@ } }; -// template class EdgeMap : public GraphWrapper::template EdgeMap { -// typedef typename GraphWrapper::template EdgeMap Parent; -// typename GraphWrapper::template NodeMap node_value; -// public: -// EdgeMap(const stGraphWrapper& _G) : Parent(_G), -// node_value(_G) { } -// EdgeMap(const stGraphWrapper& _G, T a) : Parent(_G, a), -// node_value(_G, a) { } -// T operator[](const Edge& e) const { -// switch (e.spec) { -// case 0: -// return Parent::operator[](e); -// break; -// case 1: -// return node_value[e.n]; -// break; -// case 2: -// default: -// return node_value[e.n]; -// break; -// } -// } -// void set(const Edge& e, T t) { -// switch (e.spec) { -// case 0: -// GraphWrapper::template EdgeMap::set(e, t); -// break; -// case 1: -// node_value.set(e.n, t); -// break; -// case 2: -// default: -// node_value.set(e.n, t); -// break; -// } -// } -// }; + /// This class is to wrap an edge-map and a node-map of \c Graph + /// to an edge-map of stGraphWrapper. + template + class EdgeMapWrapper { + public: + typedef Edge KeyType; + typedef typename EM::ValueType ValueType; + protected: + EM* em; + NM* nm; + public: + EdgeMapWrapper(EM& _em, NM& _nm) : em(&_em), nm(&_nm) { } + ValueType operator[](const Edge& e) const { + switch (e.getSpec()) { + case 0: + return (*em)[e]; + case 1: + return (*nm)[e.getNode()]; + case 2: + default: + return (*nm)[e.getNode()]; + } + } + void set(const Edge& e, ValueType t) { + switch (e.getSpec()) { + case 0: + em->set(e, t); + break; + case 1: + nm->set(e.getNode(), t); + break; + case 2: + default: + nm->set(e.getNode(), t); + break; + } + } + }; + // template friend std::ostream& @@ -840,5 +877,5 @@ } //namespace hugo -#endif //HUGO_GRAPH_WRAPPER_H +#endif //HUGO_BIPARTITE_GRAPH_WRAPPER_H diff -r 2784b804abb3 -r 72143568cadc src/work/marci/bipartite_matching_try_2.cc --- a/src/work/marci/bipartite_matching_try_2.cc Mon May 03 09:44:00 2004 +0000 +++ b/src/work/marci/bipartite_matching_try_2.cc Mon May 03 10:04:27 2004 +0000 @@ -72,34 +72,34 @@ g.addEdge(s_nodes[random(a)], t_nodes[random(b)]); } - std::cout << "Edges of the bipartite graph:" << std::endl; - FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " "; - std::cout << std::endl; +// std::cout << "Edges of the bipartite graph:" << std::endl; +// FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " "; +// std::cout << std::endl; - std::cout << "Nodes:" << std::endl; - FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " "; - std::cout << std::endl; - std::cout << "Nodes in T:" << std::endl; - FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; - std::cout << std::endl; - std::cout << "Nodes in S:" << std::endl; - FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; - std::cout << std::endl; +// std::cout << "Nodes:" << std::endl; +// FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " "; +// std::cout << std::endl; +// std::cout << "Nodes in T:" << std::endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; +// std::cout << std::endl; +// std::cout << "Nodes in S:" << std::endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; +// std::cout << std::endl; - std::cout << "Erasing the first node..." << std::endl; - NodeIt n; - g.first(n); - g.erase(n); - std::cout << "Nodes of the bipartite graph:" << std::endl; - FOR_EACH_GLOB(n, g) std::cout << n << " "; - std::cout << std::endl; +// std::cout << "Erasing the first node..." << std::endl; +// NodeIt n; +// g.first(n); +// g.erase(n); +// std::cout << "Nodes of the bipartite graph:" << std::endl; +// FOR_EACH_GLOB(n, g) std::cout << n << " "; +// std::cout << std::endl; - std::cout << "Nodes in T:" << std::endl; - FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; - std::cout << std::endl; - std::cout << "Nodes in S:" << std::endl; - FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; - std::cout << std::endl; +// std::cout << "Nodes in T:" << std::endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " "; +// std::cout << std::endl; +// std::cout << "Nodes in S:" << std::endl; +// FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " "; +// std::cout << std::endl; typedef stGraphWrapper stGW; stGW stgw(g); @@ -119,10 +119,10 @@ // } std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl; std::cout << "elapsed time: " << ts << std::endl; - FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { - if (flow[e]) std::cout << e << std::endl; - } - std::cout << std::endl; +// FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { +// if (flow[e]) std::cout << e << std::endl; +// } +// std::cout << std::endl; return 0; } diff -r 2784b804abb3 -r 72143568cadc src/work/marci/bipartite_matching_try_3.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/bipartite_matching_try_3.cc Mon May 03 10:04:27 2004 +0000 @@ -0,0 +1,233 @@ +// -*- c++ -*- +#include +#include +#include +#include + +#include +//#include +//#include +#include +#include +#include +#include +#include +#include + +using namespace hugo; + +// template +// class MaxMatching : public MaxFlow, +// stGraphWrapper:: EdgeMapWrapper, stGraphWrapper::EdgeMapWrapper > { +// typedef MaxFlow, +// stGraphWrapper::EdgeMapWrapper, +// stGraphWrapper::EdgeMapWrapper > +// Parent; +// protected: +// stGraphWrapper gw; +// stGraphWrapper::EdgeMapWrapper cap; +// stGraphWrapper::EdgeMapWrapper flow; +// //graph* g; +// //EdgeCap* edge_cap; +// //EdgeFlow* edge_flow; +// public: +// MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, +// EdgeFlow& _edge_flow, NodeFlow& _node_flow) : +// MaxFlow(), gw(_g), +// cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) { +// Parent::set(gw, cap, flow); +// } +// }; + +template +class MaxMatching { +// : public MaxFlow, +// stGraphWrapper:: EdgeMapWrapper, stGraphWrapper::EdgeMapWrapper > { +// typedef MaxFlow, +// stGraphWrapper::EdgeMapWrapper, +// stGraphWrapper::EdgeMapWrapper > +// Parent; +protected: +// EdgeCap* edge_cap; +// NodeCap* node_cap; +// EdgeFlow* edge_flow; +// NodeFlow* node_flow; + typedef stGraphWrapper stGW; + stGW stgw; + typedef typename stGW::template EdgeMapWrapper CapMap; + CapMap cap; + typedef typename stGW::template EdgeMapWrapper FlowMap; + FlowMap flow; + MaxFlow mf; + //graph* g; + //EdgeCap* edge_cap; + //EdgeFlow* edge_flow; +public: + MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, + EdgeFlow& _edge_flow, NodeFlow& _node_flow) : + stgw(_g), + cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow), + mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { } + void run() { mf.run(); } + int matchingValue() { return mf.flowValue(); } +}; + +/** + * Inicializalja a veletlenszamgeneratort. + * Figyelem, ez nem jo igazi random szamokhoz, + * erre ne bizzad a titkaidat! + */ +void random_init() +{ + unsigned int seed = getpid(); + seed |= seed << 15; + seed ^= time(0); + + srand(seed); +} + +/** + * Egy veletlen int-et ad vissza 0 es m-1 kozott. + */ +int random(int m) +{ + return int( double(m) * rand() / (RAND_MAX + 1.0) ); +} + +int main() { + //typedef UndirListGraph Graph; + typedef BipartiteGraph Graph; + + typedef Graph::Node Node; + typedef Graph::NodeIt NodeIt; + typedef Graph::Edge Edge; + typedef Graph::EdgeIt EdgeIt; + typedef Graph::OutEdgeIt OutEdgeIt; + + Graph g; + + std::vector s_nodes; + std::vector t_nodes; + + int a; + std::cout << "number of nodes in the first color class="; + std::cin >> a; + int b; + std::cout << "number of nodes in the second color class="; + std::cin >> b; + int m; + std::cout << "number of edges="; + std::cin >> m; + + std::cout << "Generatig a random bipartite graph..." << std::endl; + for (int i=0; i stGW; + stGW stgw(g); + ConstMap const1map(1); + + Timer ts; + ts.reset(); + stGW::EdgeMap flow(stgw); + MaxFlow, stGW::EdgeMap > + max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow); + max_flow_test.run(); +// while (max_flow_test.augmentOnShortestPath()) { } +// typedef ListGraph MutableGraph; +// while (max_flow_test.augmentOnBlockingFlow1()) { +// while (max_flow_test.augmentOnBlockingFlow2()) { +// std::cout << max_flow_test.flowValue() << std::endl; +// } + std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl; + std::cout << "elapsed time: " << ts << std::endl; + FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { + if (flow[e]) std::cout << e << std::endl; + } + std::cout << std::endl; + + typedef ConstMap EdgeCap; + EdgeCap ge1(1); + typedef ConstMap NodeCap; + NodeCap gn1(1); + typedef Graph::EdgeMap EdgeFlow; + EdgeFlow gef(g); //0 + typedef Graph::NodeMap NodeFlow; + NodeFlow gnf(g); //0 + + typedef stGraphWrapper::EdgeMapWrapper CapMap; + typedef stGraphWrapper::EdgeMapWrapper FlowMap; + CapMap cm(ge1, gn1); + FlowMap fm(gef, gnf); + + //Timer ts; + ts.reset(); + //stGW::EdgeMap flow(stgw); + MaxFlow + max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm); + max_flow_test1.run(); +// while (max_flow_test.augmentOnShortestPath()) { } +// typedef ListGraph MutableGraph; +// while (max_flow_test.augmentOnBlockingFlow1()) { +// while (max_flow_test.augmentOnBlockingFlow2()) { +// std::cout << max_flow_test.flowValue() << std::endl; +// } + std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl; + std::cout << "elapsed time: " << ts << std::endl; +// FOR_EACH_LOC(Graph::EdgeIt, e, g) { +// if (gef[e]) std::cout << e << std::endl; +// } + std::cout << std::endl; + + ts.reset(); + FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); + FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); + MaxMatching, ConstMap, + Graph::EdgeMap, Graph::NodeMap > + matching_test(g, ge1, gn1, gef, gnf); + matching_test.run(); + + std::cout << "max flow value: " << matching_test.matchingValue() << std::endl; + std::cout << "elapsed time: " << ts << std::endl; +// FOR_EACH_LOC(Graph::EdgeIt, e, g) { +// if (gef[e]) std::cout << e << std::endl; +// } + std::cout << std::endl; + return 0; +} diff -r 2784b804abb3 -r 72143568cadc src/work/marci/makefile --- a/src/work/marci/makefile Mon May 03 09:44:00 2004 +0000 +++ b/src/work/marci/makefile Mon May 03 10:04:27 2004 +0000 @@ -4,7 +4,7 @@ INCLUDEDIRS ?= -I../../include -I.. -I../{marci,jacint,alpar,klao,akos,athos} -I$(BOOSTROOT) LEDABINARIES = leda_graph_demo leda_bfs_dfs max_bipartite_matching_demo -BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try bipartite_matching_try_2 +BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try bipartite_matching_try_2 bipartite_matching_try_3 #gw_vs_not preflow_demo_boost edmonds_karp_demo_boost preflow_demo_jacint preflow_demo_athos edmonds_karp_demo_alpar preflow_demo_leda include ../makefile