// -*- 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 { 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; NodeFlow* node_flow; 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), node_flow(0), flow(_edge_flow, _node_flow), mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { } MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : stgw(_g), cap(_edge_cap, _node_cap), node_flow(new NodeFlow(_g)), flow(_edge_flow, *node_flow), mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { } ~MaxMatching() { if (node_flow) delete node_flow; } void run() { mf.run(); } int matchingValue() { return mf.flowValue(); } }; 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; 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; random_init(); randomBipartiteGraph(g, a, b, m); // 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 << "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; typedef stGraphWrapper 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; 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_1(g, ge1, gn1, gef/*, gnf*/); matching_test_1.run(); std::cout << "max flow value: " << matching_test_1.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; }