src/work/marci/leda/comparison.cc
changeset 639 a11a4377a816
child 648 8c13444bccf6
equal deleted inserted replaced
-1:000000000000 0:f0af8cd9e589
       
     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 <list_graph.h>
       
    14 //#include <smart_graph.h>
       
    15 //#include <dimacs.h>
       
    16 #include <hugo/time_measure.h>
       
    17 #include <for_each_macros.h>
       
    18 #include <hugo/graph_wrapper.h>
       
    19 #include <bipartite_graph_wrapper.h>
       
    20 #include <hugo/maps.h>
       
    21 #include <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 hugo;
       
    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 UndirListGraph
       
    55   //typedef UndirListGraph 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   int k;
       
    77   std::cout << "A bipartite graph is a random group graph if the color classes \nA and B are partitiones to A_0, A_1, ..., A_{k-1} and B_0, B_1, ..., B_{k-1} \nas equally as possible \nand the edges from A_i goes to A_{i-1 mod k} and A_{i+1 mod k}.\n";
       
    78   std::cout << "number of groups in LEDA random group graph=";
       
    79   std::cin >> k; 
       
    80   std::cout << std::endl;
       
    81   
       
    82   leda_list<leda_node> lS;
       
    83   leda_list<leda_node> lT;
       
    84   random_bigraph(lg, a, b, m, lS, lT, k);
       
    85 
       
    86   Graph::NodeMap<int> ref_map(g, -1);
       
    87   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
       
    88 
       
    89   //generating leda random group graph
       
    90   leda_node ln;
       
    91   forall(ln, lS) bipartite_map.insert(ln, false);
       
    92   forall(ln, lT) bipartite_map.insert(ln, true);
       
    93 
       
    94   //making bipartite graph
       
    95   typedef BipartiteGraphWrapper<Graph> BGW;
       
    96   BGW bgw(g, bipartite_map);
       
    97 
       
    98 
       
    99   //st-wrapper
       
   100   typedef stGraphWrapper<BGW> stGW;
       
   101   stGW stgw(bgw);
       
   102   ConstMap<stGW::Edge, int> const1map(1);
       
   103   stGW::EdgeMap<int> flow(stgw);
       
   104 
       
   105   Timer ts;
       
   106 
       
   107   ts.reset();
       
   108   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
       
   109   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
       
   110     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
       
   111   max_flow_test.run();
       
   112   std::cout << "HUGO max matching algorithm based on preflow." << std::endl 
       
   113 	    << "Size of matching: " 
       
   114 	    << max_flow_test.flowValue() << std::endl;
       
   115   std::cout << "elapsed time: " << ts << std::endl << std::endl;
       
   116 
       
   117   ts.reset();  
       
   118   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
       
   119   std::cout << "LEDA max matching algorithm." << std::endl 
       
   120 	    << "Size of matching: " 
       
   121 	    << ml.size() << std::endl;
       
   122   std::cout << "elapsed time: " << ts << std::endl << std::endl;
       
   123 
       
   124 //   ts.reset();
       
   125 //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
       
   126 //   typedef ListGraph MutableGraph;
       
   127 //   while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { }
       
   128 //   std::cout << "HUGO max matching algorithm based on blocking flow augmentation." 
       
   129 // 	    << std::endl << "Matching size: " 
       
   130 // 	    << max_flow_test.flowValue() << std::endl;
       
   131 //   std::cout << "elapsed time: " << ts << std::endl << std::endl;
       
   132 
       
   133   {
       
   134   ListGraph hg;
       
   135   ListGraph::Node s=hg.addNode();  
       
   136   ListGraph::Node t=hg.addNode();
       
   137   BGW::NodeMap<ListGraph::Node> b_s_nodes(bgw);  
       
   138   BGW::NodeMap<ListGraph::Node> b_t_nodes(bgw);
       
   139   
       
   140   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::S_CLASS) {
       
   141     b_s_nodes.set(n, hg.addNode());
       
   142     hg.addEdge(s, b_s_nodes[n]);
       
   143   }
       
   144   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::T_CLASS) {
       
   145     b_t_nodes.set(n, hg.addNode());
       
   146     hg.addEdge(b_t_nodes[n], t);
       
   147   }
       
   148 
       
   149   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) 
       
   150     hg.addEdge(b_s_nodes[bgw.tail(e)], b_t_nodes[bgw.head(e)]);
       
   151 
       
   152   ConstMap<ListGraph::Edge, int> cm(1);
       
   153   ListGraph::EdgeMap<int> flow(hg); //0
       
   154   
       
   155   Timer ts;
       
   156 
       
   157   ts.reset();
       
   158   MaxFlow<ListGraph, int, ConstMap<ListGraph::Edge, int>, 
       
   159     ListGraph::EdgeMap<int> > 
       
   160     max_flow_test(hg, s, t, cm, flow);
       
   161   max_flow_test.run();
       
   162   std::cout << "HUGO max matching algorithm on ListGraph by copying the graph, based on preflow." 
       
   163 	    << std::endl 
       
   164 	    << "Size of matching: " 
       
   165 	    << max_flow_test.flowValue() << std::endl;
       
   166   std::cout << "elapsed time: " << ts << std::endl << std::endl;
       
   167   }
       
   168 
       
   169   return 0;
       
   170 }