src/work/marci/bipartite_matching_try_3.cc
changeset 771 ad7dff9ee2fd
parent 762 511200bdb71f
equal deleted inserted replaced
10:593072769517 -1:000000000000
     1 // -*- c++ -*-
       
     2 #include <iostream>
       
     3 #include <fstream>
       
     4 #include <vector>
       
     5 
       
     6 #include <sage_graph.h>
       
     7 //#include <smart_graph.h>
       
     8 //#include <dimacs.h>
       
     9 #include <hugo/time_measure.h>
       
    10 #include <for_each_macros.h>
       
    11 #include <bfs_dfs.h>
       
    12 #include <bipartite_graph_wrapper.h>
       
    13 #include <hugo/maps.h>
       
    14 #include <hugo/max_flow.h>
       
    15 #include <graph_gen.h>
       
    16 #include <max_bipartite_matching.h>
       
    17 
       
    18 using namespace hugo;
       
    19 
       
    20 using std::cout;
       
    21 using std::endl;
       
    22 
       
    23 int main() {
       
    24   //typedef UndirListGraph Graph; 
       
    25   typedef BipartiteGraph<SageGraph> Graph;
       
    26   
       
    27   typedef Graph::Node Node;
       
    28   typedef Graph::NodeIt NodeIt;
       
    29   typedef Graph::Edge Edge;
       
    30   typedef Graph::EdgeIt EdgeIt;
       
    31   typedef Graph::OutEdgeIt OutEdgeIt;
       
    32 
       
    33   Graph g;
       
    34 
       
    35   int a;
       
    36   cout << "number of nodes in the first color class=";
       
    37   std::cin >> a; 
       
    38   int b;
       
    39   cout << "number of nodes in the second color class=";
       
    40   std::cin >> b; 
       
    41   int m;
       
    42   cout << "number of edges=";
       
    43   std::cin >> m; 
       
    44   
       
    45   cout << "Generatig a random bipartite graph..." << endl;
       
    46   random_init();
       
    47   randomBipartiteGraph(g, a, b, m);
       
    48 
       
    49 //   cout << "Edges of the bipartite graph:" << endl;
       
    50 //   FOR_EACH_LOC(EdgeIt, e, g) cout << e << " ";
       
    51 //   cout << endl;
       
    52 
       
    53 //   cout << "Nodes:" << endl;
       
    54 //   FOR_EACH_LOC(Graph::NodeIt, v, g) cout << v << " ";
       
    55 //   cout << endl;
       
    56 //   cout << "Nodes in T:" << endl;
       
    57 //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
       
    58 //   cout << endl;
       
    59 //   cout << "Nodes in S:" << endl;
       
    60 //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
       
    61 //   cout << endl;
       
    62 
       
    63 //   cout << "Erasing the first node..." << endl;
       
    64 //   NodeIt n;
       
    65 //   g.first(n);
       
    66 //   g.erase(n);
       
    67 //   cout << "Nodes of the bipartite graph:" << endl;
       
    68 //   FOR_EACH_GLOB(n, g) cout << n << " ";
       
    69 //   cout << endl;
       
    70 
       
    71 //   cout << "Nodes in T:" << endl;
       
    72 //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
       
    73 //   cout << endl;
       
    74 //   cout << "Nodes in S:" << endl;
       
    75 //   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
       
    76 //   cout << endl;
       
    77 
       
    78   typedef stBipartiteGraphWrapper<Graph> stGW;
       
    79   stGW stgw(g);
       
    80   ConstMap<stGW::Edge, int> const1map(1);
       
    81 
       
    82   Timer ts;
       
    83   cout << "max bipartite matching with stGraphWrapper..." << endl;
       
    84   ts.reset();
       
    85   stGW::EdgeMap<int> flow(stgw);
       
    86   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
       
    87     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
       
    88   max_flow_test.run();
       
    89 //  while (max_flow_test.augmentOnShortestPath()) { }
       
    90 //  typedef ListGraph MutableGraph;
       
    91 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
       
    92 //  while (max_flow_test.augmentOnBlockingFlow2()) {
       
    93 //   cout << max_flow_test.flowValue() << endl;
       
    94 //  }
       
    95   cout << "matching value: " << max_flow_test.flowValue() << endl;
       
    96   cout << "elapsed time: " << ts << endl;
       
    97 //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
       
    98 //     if (flow[e]) cout << e << endl; 
       
    99 //   }
       
   100   cout << endl;
       
   101 
       
   102   typedef ConstMap<Graph::Edge, int> EdgeCap; 
       
   103   EdgeCap ge1(1);
       
   104   typedef ConstMap<Graph::Node, int> NodeCap;
       
   105   NodeCap gn1(1);
       
   106   typedef Graph::EdgeMap<int> EdgeFlow;
       
   107   EdgeFlow gef(g); //0
       
   108   typedef Graph::NodeMap<int> NodeFlow; 
       
   109   NodeFlow gnf(g); //0 
       
   110 
       
   111   typedef stGW::EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
       
   112   typedef stGW::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; 
       
   113   CapMap cm(ge1, gn1);
       
   114   FlowMap fm(gef, gnf);
       
   115 
       
   116   //Timer ts;
       
   117   cout << "max bipartite matching with stGraphWrapper..." << endl;
       
   118   ts.reset();
       
   119   //stGW::EdgeMap<int> flow(stgw);
       
   120   MaxFlow<stGW, int, CapMap, FlowMap> 
       
   121     max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
       
   122   max_flow_test1.run();
       
   123 //  while (max_flow_test.augmentOnShortestPath()) { }
       
   124 //  typedef ListGraph MutableGraph;
       
   125 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
       
   126 //  while (max_flow_test.augmentOnBlockingFlow2()) {
       
   127 //   cout << max_flow_test.flowValue() << endl;
       
   128 //  }
       
   129   cout << "matching value: " << max_flow_test1.flowValue() << endl;
       
   130   cout << "elapsed time: " << ts << endl;
       
   131 //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
       
   132 //     if (gef[e]) cout << e << endl; 
       
   133 //   }
       
   134   cout << endl;
       
   135 
       
   136   cout << "max bipartite matching with stGraphWrapper..." << endl;
       
   137   ts.reset();
       
   138   FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
       
   139   FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
       
   140   MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
       
   141     Graph::EdgeMap<int>, Graph::NodeMap<int> > 
       
   142     matching_test(g, ge1, gn1, gef, gnf);
       
   143   matching_test.run();
       
   144 
       
   145   cout << "matching value: " << matching_test.matchingValue() << endl;
       
   146   cout << "elapsed time: " << ts << endl;
       
   147 //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
       
   148 //     if (gef[e]) cout << e << endl; 
       
   149 //   }
       
   150   cout << endl;
       
   151 
       
   152   cout << "max bipartite matching with MaxBipartiteMatching..." << endl;
       
   153   ts.reset();
       
   154   FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
       
   155   //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
       
   156   typedef MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, 
       
   157     ConstMap<Graph::Node, int>, 
       
   158     Graph::EdgeMap<int>, Graph::NodeMap<int> > MaxBipartiteMatching;
       
   159   MaxBipartiteMatching matching_test_1(g, ge1, gn1, gef/*, gnf*/);
       
   160   matching_test_1.run();
       
   161 
       
   162   cout << "matching value: " << matching_test_1.matchingValue() << endl;
       
   163   cout << "elapsed time: " << ts << endl;
       
   164 //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
       
   165 //     if (gef[e]) cout << e << endl; 
       
   166 //   }
       
   167   cout << endl;
       
   168 
       
   169   cout << "testing optimality with MaxBipartiteMatching..." << endl;
       
   170   ts.reset();
       
   171   matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING);
       
   172   cout << "matching value: " << matching_test_1.matchingValue() << endl;
       
   173   cout << "elapsed time: " << ts << endl;
       
   174 
       
   175   cout << "testing optimality with MaxBipartiteMatching..." << endl;
       
   176   ts.reset();
       
   177   matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING_WITH_GOOD_NODE_FLOW);
       
   178   cout << "matching value: " << matching_test_1.matchingValue() << endl;
       
   179   cout << "elapsed time: " << ts << endl;
       
   180 
       
   181   return 0;
       
   182 }