src/work/marci/leda/bipartite_matching_leda.cc
author marci
Wed, 28 Apr 2004 14:17:54 +0000
changeset 459 68e6873f421a
parent 446 77ef5c7a57d9
child 482 dce64ce044d6
permissions -rw-r--r--
dijstra const ref, misc
     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 <time_measure.h>
    17 #include <for_each_macros.h>
    18 //#include <bfs_iterator.h>
    19 #include <graph_wrapper.h>
    20 #include <maps.h>
    21 #include <edmonds_karp.h>
    22 #include <preflow.h>
    23 
    24 /**
    25  * Inicializalja a veletlenszamgeneratort.
    26  * Figyelem, ez nem jo igazi random szamokhoz,
    27  * erre ne bizzad a titkaidat!
    28  */
    29 void random_init()
    30 {
    31 	unsigned int seed = getpid();
    32 	seed |= seed << 15;
    33 	seed ^= time(0);
    34 
    35 	srand(seed);
    36 }
    37 
    38 /**
    39  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    40  */
    41 int random(int m)
    42 {
    43   return int( double(m) * rand() / (RAND_MAX + 1.0) );
    44 }
    45 
    46 using namespace hugo;
    47 
    48 int main() {
    49   //for leda graph
    50   leda::graph lg;
    51   //lg.make_undirected();
    52   typedef LedaGraphWrapper<leda::graph> Graph;
    53   Graph g(lg);
    54 
    55   //for UndirListGraph
    56   //typedef UndirListGraph Graph; 
    57   //Graph g;
    58 
    59   typedef Graph::Node Node;
    60   typedef Graph::NodeIt NodeIt;
    61   typedef Graph::Edge Edge;
    62   typedef Graph::EdgeIt EdgeIt;
    63   typedef Graph::OutEdgeIt OutEdgeIt;
    64 
    65   std::vector<Graph::Node> s_nodes;
    66   std::vector<Graph::Node> t_nodes;
    67 
    68   int a;
    69   std::cout << "number of nodes in the first color class=";
    70   std::cin >> a; 
    71   int b;
    72   std::cout << "number of nodes in the second color class=";
    73   std::cin >> b; 
    74   int m;
    75   std::cout << "number of edges=";
    76   std::cin >> m; 
    77   
    78 
    79   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
    80   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
    81 
    82   random_init();
    83   for(int i=0; i<m; ++i) {
    84     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
    85   }
    86 
    87   Graph::NodeMap<int> ref_map(g, -1);
    88 
    89   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
    90   for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
    91   for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
    92 
    93   typedef BipartiteGraphWrapper<Graph> BGW;
    94   BGW bgw(g, bipartite_map);
    95 
    96   BGW::NodeMap<int> dbyj(bgw);
    97   BGW::EdgeMap<int> dbyxcj(bgw);
    98 
    99   typedef stGraphWrapper<BGW> stGW;
   100   stGW stgw(bgw);
   101   ConstMap<stGW::Edge, int> const1map(1);
   102 
   103   Timer ts;
   104   ts.reset();
   105   stGW::EdgeMap<int> max_flow(stgw);
   106   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   107     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
   108 //  while (max_flow_test.augmentOnShortestPath()) { }
   109   typedef ListGraph MutableGraph;
   110 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   111   while (max_flow_test.augmentOnBlockingFlow2()) {
   112    std::cout << max_flow_test.flowValue() << std::endl;
   113   }
   114   std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
   115   std::cout << "elapsed time: " << ts << std::endl;
   116 //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   117 //     std::cout << e << ": " << max_flow[e] << "\n"; 
   118 //   }
   119 //   std::cout << "\n";
   120 
   121   ts.reset();
   122   stGW::EdgeMap<int> pre_flow(stgw);
   123   Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   124     pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/);
   125   pre_flow_test.run();
   126   std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
   127   std::cout << "elapsed time: " << ts << std::endl;
   128 //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   129 //     std::cout << e << ": " << pre_flow[e] << "\n"; 
   130 //   }
   131 //   std::cout << "\n";
   132 
   133   ts.reset();  
   134   leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
   135   //  stGW::EdgeMap<int> pre_flow(stgw);
   136   //Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   137   //  pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
   138   //pre_flow_test.run();
   139   std::cout << "leda matching value: " << ml.size() << std::endl;
   140   std::cout << "elapsed time: " << ts << std::endl;
   141 //   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   142 //     std::cout << e << ": " << pre_flow[e] << "\n"; 
   143 //   }
   144 //   std::cout << "\n";
   145 
   146   return 0;
   147 }