marci@771: // -*- c++ -*-
marci@771: #include <iostream>
marci@771: #include <fstream>
marci@771: #include <vector>
marci@771: 
marci@771: #include <sage_graph.h>
marci@771: //#include <smart_graph.h>
marci@771: //#include <dimacs.h>
marci@771: #include <hugo/time_measure.h>
marci@771: #include <for_each_macros.h>
marci@771: #include <bfs_dfs.h>
marci@771: #include <bipartite_graph_wrapper.h>
marci@771: #include <hugo/maps.h>
marci@771: #include <hugo/max_flow.h>
marci@771: #include <graph_gen.h>
marci@771: #include <max_bipartite_matching.h>
marci@771: 
marci@771: using namespace hugo;
marci@771: 
marci@771: using std::cin;
marci@771: using std::cout;
marci@771: using std::endl;
marci@771: 
marci@771: int main() {
marci@771:   //typedef UndirListGraph Graph; 
marci@771:   typedef BipartiteGraph<SageGraph> Graph;
marci@771:   
marci@771:   typedef Graph::Node Node;
marci@771:   typedef Graph::NodeIt NodeIt;
marci@771:   typedef Graph::Edge Edge;
marci@771:   typedef Graph::EdgeIt EdgeIt;
marci@771:   typedef Graph::OutEdgeIt OutEdgeIt;
marci@771: 
marci@771:   Graph g;
marci@771: 
marci@771:   int a;
marci@771:   cout << "number of nodes in the first color class=";
marci@771:   cin >> a; 
marci@771:   int b;
marci@771:   cout << "number of nodes in the second color class=";
marci@771:   cin >> b; 
marci@771:   int m;
marci@771:   cout << "number of edges=";
marci@771:   cin >> m; 
marci@771:   
marci@771:   cout << "Generatig a random bipartite graph..." << endl;
marci@771:   random_init();
marci@771:   randomBipartiteGraph(g, a, b, m);
marci@771: 
marci@771: //   cout << "Edges of the bipartite graph:" << endl;
marci@771: //   FOR_EACH_LOC(EdgeIt, e, g) cout << e << " ";
marci@771: //   cout << endl;
marci@771: 
marci@771: //   cout << "Nodes:" << endl;
marci@771: //   FOR_EACH_LOC(Graph::NodeIt, v, g) cout << v << " ";
marci@771: //   cout << endl;
marci@771: //   cout << "Nodes in T:" << endl;
marci@771: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
marci@771: //   cout << endl;
marci@771: //   cout << "Nodes in S:" << endl;
marci@771: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
marci@771: //   cout << endl;
marci@771: 
marci@771: //   cout << "Erasing the first node..." << endl;
marci@771: //   NodeIt n;
marci@771: //   g.first(n);
marci@771: //   g.erase(n);
marci@771: //   cout << "Nodes of the bipartite graph:" << endl;
marci@771: //   FOR_EACH_GLOB(n, g) cout << n << " ";
marci@771: //   cout << endl;
marci@771: 
marci@771: //   cout << "Nodes in T:" << endl;
marci@771: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
marci@771: //   cout << endl;
marci@771: //   cout << "Nodes in S:" << endl;
marci@771: //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
marci@771: //   cout << endl;
marci@771: 
marci@771:   typedef stBipartiteGraphWrapper<Graph> stGW;
marci@771:   stGW stgw(g);
marci@771:   ConstMap<stGW::Edge, int> const1map(1);
marci@771: 
marci@771:   Timer ts;
marci@771:   cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771:   ts.reset();
marci@771:   stGW::EdgeMap<int> flow(stgw);
marci@771:   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@771:     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@771:   max_flow_test.run();
marci@771: //  while (max_flow_test.augmentOnShortestPath()) { }
marci@771: //  typedef ListGraph MutableGraph;
marci@771: //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@771: //  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@771: //   cout << max_flow_test.flowValue() << endl;
marci@771: //  }
marci@771:   cout << "matching value: " << max_flow_test.flowValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@771: //     if (flow[e]) cout << e << endl; 
marci@771: //   }
marci@771:   cout << endl;
marci@771: 
marci@771:   typedef ConstMap<Graph::Edge, int> EdgeCap; 
marci@771:   EdgeCap ge1(1);
marci@771:   typedef ConstMap<Graph::Node, int> NodeCap;
marci@771:   NodeCap gn1(1);
marci@771:   typedef Graph::EdgeMap<int> EdgeFlow;
marci@771:   EdgeFlow gef(g); //0
marci@771:   typedef Graph::NodeMap<int> NodeFlow; 
marci@771:   NodeFlow gnf(g); //0 
marci@771: 
marci@771:   typedef stGW::EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
marci@771:   typedef stGW::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; 
marci@771:   CapMap cm(ge1, gn1);
marci@771:   FlowMap fm(gef, gnf);
marci@771: 
marci@771:   //Timer ts;
marci@771:   cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771:   ts.reset();
marci@771:   //stGW::EdgeMap<int> flow(stgw);
marci@771:   MaxFlow<stGW, int, CapMap, FlowMap> 
marci@771:     max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
marci@771:   max_flow_test1.run();
marci@771: //  while (max_flow_test.augmentOnShortestPath()) { }
marci@771: //  typedef ListGraph MutableGraph;
marci@771: //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@771: //  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@771: //   cout << max_flow_test.flowValue() << endl;
marci@771: //  }
marci@771:   cout << "matching value: " << max_flow_test1.flowValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771: //     if (gef[e]) cout << e << endl; 
marci@771: //   }
marci@771:   cout << endl;
marci@771: 
marci@771:   cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771:   ts.reset();
marci@771:   FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
marci@771:   FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
marci@771:   MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
marci@771:     Graph::EdgeMap<int>, Graph::NodeMap<int> > 
marci@771:     matching_test(g, ge1, gn1, gef, gnf);
marci@771:   matching_test.run();
marci@771: 
marci@771:   cout << "matching value: " << matching_test.matchingValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771: //     if (gef[e]) cout << e << endl; 
marci@771: //   }
marci@771:   cout << endl;
marci@771: 
marci@771:   cout << "max bipartite matching with MaxBipartiteMatching..." << endl;
marci@771:   ts.reset();
marci@771:   FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
marci@771:   //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
marci@771:   typedef MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, 
marci@771:     ConstMap<Graph::Node, int>, 
marci@771:     Graph::EdgeMap<int>, Graph::NodeMap<int> > MaxBipartiteMatching;
marci@771:   MaxBipartiteMatching matching_test_1(g, ge1, gn1, gef/*, gnf*/);
marci@771:   matching_test_1.run();
marci@771: 
marci@771:   cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771: //     if (gef[e]) cout << e << endl; 
marci@771: //   }
marci@771:   cout << endl;
marci@771: 
marci@771:   cout << "testing optimality with MaxBipartiteMatching..." << endl;
marci@771:   ts.reset();
marci@771:   matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING);
marci@771:   cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: 
marci@771:   cout << "testing optimality with MaxBipartiteMatching..." << endl;
marci@771:   ts.reset();
marci@771:   matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING_WITH_GOOD_NODE_FLOW);
marci@771:   cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771:   cout << "elapsed time: " << ts << endl;
marci@771: 
marci@771:   return 0;
marci@771: }