src/work/marci/leda/bipartite_matching_leda_gen.cc
author marci
Wed, 28 Apr 2004 14:17:54 +0000
changeset 459 68e6873f421a
parent 447 9c997ebe4aff
child 482 dce64ce044d6
permissions -rw-r--r--
dijstra const ref, 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@446
    16
#include <time_measure.h>
marci@446
    17
#include <for_each_macros.h>
marci@446
    18
//#include <bfs_iterator.h>
marci@446
    19
#include <graph_wrapper.h>
marci@446
    20
#include <maps.h>
marci@446
    21
#include <edmonds_karp.h>
marci@446
    22
#include <preflow.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
marci@446
    46
using namespace hugo;
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@446
    55
  //for UndirListGraph
marci@446
    56
  //typedef UndirListGraph 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@446
    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@446
    86
//   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
marci@446
    87
//   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
marci@446
    88
marci@446
    89
//   random_init();
marci@446
    90
//   for(int i=0; i<m; ++i) {
marci@446
    91
//     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@446
    92
//   }
marci@446
    93
marci@446
    94
  Graph::NodeMap<int> ref_map(g, -1);
marci@446
    95
marci@446
    96
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@446
    97
//   for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
marci@446
    98
//   for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
marci@446
    99
  leda_node ln;
marci@446
   100
  forall(ln, lS) bipartite_map.insert(ln, false);
marci@446
   101
  forall(ln, lT) bipartite_map.insert(ln, true);
marci@446
   102
marci@446
   103
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@446
   104
  BGW bgw(g, bipartite_map);
marci@446
   105
marci@446
   106
  //  BGW::NodeMap<int> dbyj(bgw);
marci@446
   107
  //  BGW::EdgeMap<int> dbyxcj(bgw);
marci@446
   108
marci@446
   109
  typedef stGraphWrapper<BGW> stGW;
marci@446
   110
  stGW stgw(bgw);
marci@446
   111
  ConstMap<stGW::Edge, int> const1map(1);
marci@446
   112
  stGW::EdgeMap<int> flow(stgw);
marci@446
   113
marci@446
   114
  Timer ts;
marci@446
   115
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@446
   116
  ts.reset();
marci@446
   117
  //  stGW::EdgeMap<int> pre_flow(stgw);
marci@446
   118
  Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@459
   119
    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow/*, true*/);
marci@446
   120
  pre_flow_test.run();
marci@446
   121
  std::cout << "HUGO pre flow value: " << pre_flow_test.flowValue() << std::endl;
marci@446
   122
  std::cout << "elapsed time: " << ts << std::endl;
marci@446
   123
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@446
   124
//     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@446
   125
//   }
marci@446
   126
  std::cout << "\n";
marci@446
   127
marci@446
   128
  ts.reset();  
marci@446
   129
  leda_list<leda_edge> ml=MAX_CARD_BIPARTITE_MATCHING(lg);
marci@446
   130
  //  stGW::EdgeMap<int> pre_flow(stgw);
marci@446
   131
  //Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@446
   132
  //  pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
marci@446
   133
  //pre_flow_test.run();
marci@446
   134
  std::cout << "LEDA matching value: " << ml.size() << std::endl;
marci@446
   135
  std::cout << "elapsed time: " << ts << std::endl;
marci@446
   136
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@446
   137
//     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@446
   138
//   }
marci@446
   139
  std::cout << "\n";
marci@446
   140
marci@446
   141
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) flow.set(e, 0);
marci@446
   142
  ts.reset();
marci@446
   143
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@446
   144
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@446
   145
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@446
   146
  typedef ListGraph MutableGraph;
marci@446
   147
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@446
   148
  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@446
   149
   std::cout << max_flow_test.flowValue() << std::endl;
marci@446
   150
  }
marci@446
   151
  std::cout << "HUGO blocking flow value: " << max_flow_test.flowValue() << std::endl;
marci@446
   152
  std::cout << "elapsed time: " << ts << std::endl;
marci@446
   153
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@446
   154
//     std::cout << e << ": " << max_flow[e] << "\n"; 
marci@446
   155
//   }
marci@446
   156
//   std::cout << "\n";
marci@446
   157
marci@446
   158
  return 0;
marci@446
   159
}