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