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