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