src/work/marci/leda/bipartite_matching_leda.cc
author marci
Thu, 18 Nov 2004 14:37:22 +0000
changeset 1007 a7d5fe18d8f9
parent 769 eb61fbc64c16
permissions -rw-r--r--
MergeNodeGraphWrapper
marci@411
     1
// -*- c++ -*-
marci@411
     2
#include <iostream>
marci@411
     3
#include <fstream>
marci@411
     4
#include <vector>
marci@411
     5
#include <cstdlib>
marci@411
     6
marci@411
     7
#include <LEDA/graph.h>
marci@411
     8
#include <LEDA/mcb_matching.h>
marci@411
     9
#include <LEDA/list.h>
marci@446
    10
#include <LEDA/graph_gen.h>
marci@411
    11
marci@411
    12
#include <leda_graph_wrapper.h>
marci@648
    13
#include <sage_graph.h>
marci@411
    14
//#include <smart_graph.h>
marci@411
    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@411
    22
marci@411
    23
/**
marci@411
    24
 * Inicializalja a veletlenszamgeneratort.
marci@411
    25
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@411
    26
 * erre ne bizzad a titkaidat!
marci@411
    27
 */
marci@411
    28
void random_init()
marci@411
    29
{
marci@411
    30
	unsigned int seed = getpid();
marci@411
    31
	seed |= seed << 15;
marci@411
    32
	seed ^= time(0);
marci@411
    33
marci@411
    34
	srand(seed);
marci@411
    35
}
marci@411
    36
marci@411
    37
/**
marci@411
    38
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@411
    39
 */
marci@411
    40
int random(int m)
marci@411
    41
{
marci@411
    42
  return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@411
    43
}
marci@411
    44
alpar@921
    45
using namespace lemon;
marci@411
    46
marci@411
    47
int main() {
marci@411
    48
  //for leda graph
marci@411
    49
  leda::graph lg;
marci@413
    50
  //lg.make_undirected();
marci@411
    51
  typedef LedaGraphWrapper<leda::graph> Graph;
marci@411
    52
  Graph g(lg);
marci@411
    53
marci@648
    54
  //for UndirSageGraph
marci@648
    55
  //typedef UndirSageGraph Graph; 
marci@411
    56
  //Graph g;
marci@411
    57
marci@411
    58
  typedef Graph::Node Node;
marci@411
    59
  typedef Graph::NodeIt NodeIt;
marci@411
    60
  typedef Graph::Edge Edge;
marci@411
    61
  typedef Graph::EdgeIt EdgeIt;
marci@411
    62
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@411
    63
marci@411
    64
  std::vector<Graph::Node> s_nodes;
marci@411
    65
  std::vector<Graph::Node> t_nodes;
marci@411
    66
marci@411
    67
  int a;
marci@411
    68
  std::cout << "number of nodes in the first color class=";
marci@411
    69
  std::cin >> a; 
marci@411
    70
  int b;
marci@411
    71
  std::cout << "number of nodes in the second color class=";
marci@411
    72
  std::cin >> b; 
marci@411
    73
  int m;
marci@411
    74
  std::cout << "number of edges=";
marci@411
    75
  std::cin >> m; 
marci@411
    76
  
marci@411
    77
marci@411
    78
  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
marci@411
    79
  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
marci@411
    80
marci@411
    81
  random_init();
marci@411
    82
  for(int i=0; i<m; ++i) {
marci@411
    83
    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@411
    84
  }
marci@411
    85
marci@411
    86
  Graph::NodeMap<int> ref_map(g, -1);
marci@411
    87
marci@411
    88
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@411
    89
  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
marci@411
    90
  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
marci@411
    91
marci@411
    92
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@411
    93
  BGW bgw(g, bipartite_map);
marci@411
    94
marci@411
    95
  BGW::NodeMap<int> dbyj(bgw);
marci@411
    96
  BGW::EdgeMap<int> dbyxcj(bgw);
marci@411
    97
marci@769
    98
  typedef stBipartiteGraphWrapper<BGW> stGW;
marci@411
    99
  stGW stgw(bgw);
marci@411
   100
  ConstMap<stGW::Edge, int> const1map(1);
marci@411
   101
marci@411
   102
  Timer ts;
marci@482
   103
  stGW::EdgeMap<int> flow(stgw);
marci@482
   104
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@482
   105
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@482
   106
marci@411
   107
  ts.reset();
marci@482
   108
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@411
   109
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@648
   110
  typedef SageGraph MutableGraph;
marci@411
   111
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@769
   112
//   while (max_flow_test.augmentOnBlockingFlow2()) {
marci@769
   113
//    std::cout << max_flow_test.flowValue() << std::endl;
marci@769
   114
//   }
marci@769
   115
  max_flow_test.run();
marci@411
   116
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@411
   117
  std::cout << "elapsed time: " << ts << std::endl;
marci@411
   118
marci@411
   119
  ts.reset();
marci@482
   120
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@482
   121
  max_flow_test.run();
marci@411
   122
  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
marci@411
   123
  std::cout << "elapsed time: " << ts << std::endl;
marci@411
   124
marci@411
   125
  ts.reset();  
marci@413
   126
  leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@413
   127
  std::cout << "leda matching value: " << ml.size() << std::endl;
marci@411
   128
  std::cout << "elapsed time: " << ts << std::endl;
marci@411
   129
marci@411
   130
  return 0;
marci@411
   131
}