src/work/marci/leda/bipartite_matching_leda.cc
author marci
Thu, 18 Nov 2004 14:37:22 +0000
changeset 1007 a7d5fe18d8f9
parent 769 eb61fbc64c16
permissions -rw-r--r--
MergeNodeGraphWrapper
     1 // -*- c++ -*-
     2 #include <iostream>
     3 #include <fstream>
     4 #include <vector>
     5 #include <cstdlib>
     6 
     7 #include <LEDA/graph.h>
     8 #include <LEDA/mcb_matching.h>
     9 #include <LEDA/list.h>
    10 #include <LEDA/graph_gen.h>
    11 
    12 #include <leda_graph_wrapper.h>
    13 #include <sage_graph.h>
    14 //#include <smart_graph.h>
    15 //#include <dimacs.h>
    16 #include <lemon/time_measure.h>
    17 #include <for_each_macros.h>
    18 #include <lemon/graph_wrapper.h>
    19 #include <bipartite_graph_wrapper.h>
    20 #include <lemon/maps.h>
    21 #include <lemon/max_flow.h>
    22 
    23 /**
    24  * Inicializalja a veletlenszamgeneratort.
    25  * Figyelem, ez nem jo igazi random szamokhoz,
    26  * erre ne bizzad a titkaidat!
    27  */
    28 void random_init()
    29 {
    30 	unsigned int seed = getpid();
    31 	seed |= seed << 15;
    32 	seed ^= time(0);
    33 
    34 	srand(seed);
    35 }
    36 
    37 /**
    38  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    39  */
    40 int random(int m)
    41 {
    42   return int( double(m) * rand() / (RAND_MAX + 1.0) );
    43 }
    44 
    45 using namespace lemon;
    46 
    47 int main() {
    48   //for leda graph
    49   leda::graph lg;
    50   //lg.make_undirected();
    51   typedef LedaGraphWrapper<leda::graph> Graph;
    52   Graph g(lg);
    53 
    54   //for UndirSageGraph
    55   //typedef UndirSageGraph Graph; 
    56   //Graph g;
    57 
    58   typedef Graph::Node Node;
    59   typedef Graph::NodeIt NodeIt;
    60   typedef Graph::Edge Edge;
    61   typedef Graph::EdgeIt EdgeIt;
    62   typedef Graph::OutEdgeIt OutEdgeIt;
    63 
    64   std::vector<Graph::Node> s_nodes;
    65   std::vector<Graph::Node> t_nodes;
    66 
    67   int a;
    68   std::cout << "number of nodes in the first color class=";
    69   std::cin >> a; 
    70   int b;
    71   std::cout << "number of nodes in the second color class=";
    72   std::cin >> b; 
    73   int m;
    74   std::cout << "number of edges=";
    75   std::cin >> m; 
    76   
    77 
    78   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
    79   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
    80 
    81   random_init();
    82   for(int i=0; i<m; ++i) {
    83     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
    84   }
    85 
    86   Graph::NodeMap<int> ref_map(g, -1);
    87 
    88   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
    89   for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
    90   for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
    91 
    92   typedef BipartiteGraphWrapper<Graph> BGW;
    93   BGW bgw(g, bipartite_map);
    94 
    95   BGW::NodeMap<int> dbyj(bgw);
    96   BGW::EdgeMap<int> dbyxcj(bgw);
    97 
    98   typedef stBipartiteGraphWrapper<BGW> stGW;
    99   stGW stgw(bgw);
   100   ConstMap<stGW::Edge, int> const1map(1);
   101 
   102   Timer ts;
   103   stGW::EdgeMap<int> flow(stgw);
   104   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   105     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
   106 
   107   ts.reset();
   108   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
   109 //  while (max_flow_test.augmentOnShortestPath()) { }
   110   typedef SageGraph MutableGraph;
   111 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   112 //   while (max_flow_test.augmentOnBlockingFlow2()) {
   113 //    std::cout << max_flow_test.flowValue() << std::endl;
   114 //   }
   115   max_flow_test.run();
   116   std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
   117   std::cout << "elapsed time: " << ts << std::endl;
   118 
   119   ts.reset();
   120   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
   121   max_flow_test.run();
   122   std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
   123   std::cout << "elapsed time: " << ts << std::endl;
   124 
   125   ts.reset();  
   126   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
   127   std::cout << "leda matching value: " << ml.size() << std::endl;
   128   std::cout << "elapsed time: " << ts << std::endl;
   129 
   130   return 0;
   131 }