src/work/marci/leda/bipartite_matching_leda_gen.cc
author marci
Fri, 14 May 2004 14:41:30 +0000
changeset 636 e59b0c363a9e
parent 496 7c463a7635d4
child 648 8c13444bccf6
permissions -rw-r--r--
(none)
     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;
   123   std::cout << "\n";
   124 
   125   ts.reset();
   126   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
   127   typedef ListGraph MutableGraph;
   128   while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { }
   129   std::cout << "HUGO max matching algorithm based on blocking flow augmentation." 
   130 	    << std::endl << "Matching size: " 
   131 	    << max_flow_test.flowValue() << std::endl;
   132   std::cout << "elapsed time: " << ts << std::endl;
   133 
   134   return 0;
   135 }