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; +}