src/work/marci/leda/comparison.cc
author marci
Tue, 11 May 2004 21:26:29 +0000
changeset 617 dc17013b0e52
child 648 8c13444bccf6
permissions -rw-r--r--
bip matching comparison
marci@617
     1
// -*- c++ -*-
marci@617
     2
#include <iostream>
marci@617
     3
#include <fstream>
marci@617
     4
#include <vector>
marci@617
     5
#include <cstdlib>
marci@617
     6
marci@617
     7
#include <LEDA/graph.h>
marci@617
     8
#include <LEDA/mcb_matching.h>
marci@617
     9
#include <LEDA/list.h>
marci@617
    10
#include <LEDA/graph_gen.h>
marci@617
    11
marci@617
    12
#include <leda_graph_wrapper.h>
marci@617
    13
#include <list_graph.h>
marci@617
    14
//#include <smart_graph.h>
marci@617
    15
//#include <dimacs.h>
marci@617
    16
#include <hugo/time_measure.h>
marci@617
    17
#include <for_each_macros.h>
marci@617
    18
#include <hugo/graph_wrapper.h>
marci@617
    19
#include <bipartite_graph_wrapper.h>
marci@617
    20
#include <hugo/maps.h>
marci@617
    21
#include <max_flow.h>
marci@617
    22
marci@617
    23
/**
marci@617
    24
 * Inicializalja a veletlenszamgeneratort.
marci@617
    25
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@617
    26
 * erre ne bizzad a titkaidat!
marci@617
    27
 */
marci@617
    28
void random_init()
marci@617
    29
{
marci@617
    30
	unsigned int seed = getpid();
marci@617
    31
	seed |= seed << 15;
marci@617
    32
	seed ^= time(0);
marci@617
    33
marci@617
    34
	srand(seed);
marci@617
    35
}
marci@617
    36
marci@617
    37
/**
marci@617
    38
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@617
    39
 */
marci@617
    40
int random(int m)
marci@617
    41
{
marci@617
    42
  return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@617
    43
}
marci@617
    44
marci@617
    45
using namespace hugo;
marci@617
    46
marci@617
    47
int main() {
marci@617
    48
  //for leda graph
marci@617
    49
  leda::graph lg;
marci@617
    50
  //lg.make_undirected();
marci@617
    51
  typedef LedaGraphWrapper<leda::graph> Graph;
marci@617
    52
  Graph g(lg);
marci@617
    53
marci@617
    54
  //for UndirListGraph
marci@617
    55
  //typedef UndirListGraph Graph; 
marci@617
    56
  //Graph g;
marci@617
    57
marci@617
    58
  typedef Graph::Node Node;
marci@617
    59
  typedef Graph::NodeIt NodeIt;
marci@617
    60
  typedef Graph::Edge Edge;
marci@617
    61
  typedef Graph::EdgeIt EdgeIt;
marci@617
    62
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@617
    63
marci@617
    64
  std::vector<Graph::Node> s_nodes;
marci@617
    65
  std::vector<Graph::Node> t_nodes;
marci@617
    66
marci@617
    67
  int a;
marci@617
    68
  std::cout << "number of nodes in the first color class=";
marci@617
    69
  std::cin >> a; 
marci@617
    70
  int b;
marci@617
    71
  std::cout << "number of nodes in the second color class=";
marci@617
    72
  std::cin >> b; 
marci@617
    73
  int m;
marci@617
    74
  std::cout << "number of edges=";
marci@617
    75
  std::cin >> m; 
marci@617
    76
  int k;
marci@617
    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";
marci@617
    78
  std::cout << "number of groups in LEDA random group graph=";
marci@617
    79
  std::cin >> k; 
marci@617
    80
  std::cout << std::endl;
marci@617
    81
  
marci@617
    82
  leda_list<leda_node> lS;
marci@617
    83
  leda_list<leda_node> lT;
marci@617
    84
  random_bigraph(lg, a, b, m, lS, lT, k);
marci@617
    85
marci@617
    86
  Graph::NodeMap<int> ref_map(g, -1);
marci@617
    87
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@617
    88
marci@617
    89
  //generating leda random group graph
marci@617
    90
  leda_node ln;
marci@617
    91
  forall(ln, lS) bipartite_map.insert(ln, false);
marci@617
    92
  forall(ln, lT) bipartite_map.insert(ln, true);
marci@617
    93
marci@617
    94
  //making bipartite graph
marci@617
    95
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@617
    96
  BGW bgw(g, bipartite_map);
marci@617
    97
marci@617
    98
marci@617
    99
  //st-wrapper
marci@617
   100
  typedef stGraphWrapper<BGW> stGW;
marci@617
   101
  stGW stgw(bgw);
marci@617
   102
  ConstMap<stGW::Edge, int> const1map(1);
marci@617
   103
  stGW::EdgeMap<int> flow(stgw);
marci@617
   104
marci@617
   105
  Timer ts;
marci@617
   106
marci@617
   107
  ts.reset();
marci@617
   108
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@617
   109
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@617
   110
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
marci@617
   111
  max_flow_test.run();
marci@617
   112
  std::cout << "HUGO max matching algorithm based on preflow." << std::endl 
marci@617
   113
	    << "Size of matching: " 
marci@617
   114
	    << max_flow_test.flowValue() << std::endl;
marci@617
   115
  std::cout << "elapsed time: " << ts << std::endl << std::endl;
marci@617
   116
marci@617
   117
  ts.reset();  
marci@617
   118
  leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@617
   119
  std::cout << "LEDA max matching algorithm." << std::endl 
marci@617
   120
	    << "Size of matching: " 
marci@617
   121
	    << ml.size() << std::endl;
marci@617
   122
  std::cout << "elapsed time: " << ts << std::endl << std::endl;
marci@617
   123
marci@617
   124
//   ts.reset();
marci@617
   125
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@617
   126
//   typedef ListGraph MutableGraph;
marci@617
   127
//   while (max_flow_test.augmentOnBlockingFlow<MutableGraph>()) { }
marci@617
   128
//   std::cout << "HUGO max matching algorithm based on blocking flow augmentation." 
marci@617
   129
// 	    << std::endl << "Matching size: " 
marci@617
   130
// 	    << max_flow_test.flowValue() << std::endl;
marci@617
   131
//   std::cout << "elapsed time: " << ts << std::endl << std::endl;
marci@617
   132
marci@617
   133
  {
marci@617
   134
  ListGraph hg;
marci@617
   135
  ListGraph::Node s=hg.addNode();  
marci@617
   136
  ListGraph::Node t=hg.addNode();
marci@617
   137
  BGW::NodeMap<ListGraph::Node> b_s_nodes(bgw);  
marci@617
   138
  BGW::NodeMap<ListGraph::Node> b_t_nodes(bgw);
marci@617
   139
  
marci@617
   140
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::S_CLASS) {
marci@617
   141
    b_s_nodes.set(n, hg.addNode());
marci@617
   142
    hg.addEdge(s, b_s_nodes[n]);
marci@617
   143
  }
marci@617
   144
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, BGW::T_CLASS) {
marci@617
   145
    b_t_nodes.set(n, hg.addNode());
marci@617
   146
    hg.addEdge(b_t_nodes[n], t);
marci@617
   147
  }
marci@617
   148
marci@617
   149
  FOR_EACH_LOC(BGW::EdgeIt, e, bgw) 
marci@617
   150
    hg.addEdge(b_s_nodes[bgw.tail(e)], b_t_nodes[bgw.head(e)]);
marci@617
   151
marci@617
   152
  ConstMap<ListGraph::Edge, int> cm(1);
marci@617
   153
  ListGraph::EdgeMap<int> flow(hg); //0
marci@617
   154
  
marci@617
   155
  Timer ts;
marci@617
   156
marci@617
   157
  ts.reset();
marci@617
   158
  MaxFlow<ListGraph, int, ConstMap<ListGraph::Edge, int>, 
marci@617
   159
    ListGraph::EdgeMap<int> > 
marci@617
   160
    max_flow_test(hg, s, t, cm, flow);
marci@617
   161
  max_flow_test.run();
marci@617
   162
  std::cout << "HUGO max matching algorithm on ListGraph by copying the graph, based on preflow." 
marci@617
   163
	    << std::endl 
marci@617
   164
	    << "Size of matching: " 
marci@617
   165
	    << max_flow_test.flowValue() << std::endl;
marci@617
   166
  std::cout << "elapsed time: " << ts << std::endl << std::endl;
marci@617
   167
  }
marci@617
   168
marci@617
   169
  return 0;
marci@617
   170
}