src/work/marci/leda/max_bipartite_matching_demo.cc
author alpar
Thu, 03 Feb 2005 16:08:56 +0000
changeset 1119 d3504fc075dc
parent 921 818510fa3d99
permissions -rw-r--r--
Two incomplete additions:
- Exceptions
- bool map indication reached nodes (NullMap by default)
marci@771
     1
// -*- c++ -*-
marci@771
     2
#include <iostream>
marci@771
     3
#include <fstream>
marci@771
     4
#include <vector>
marci@771
     5
#include <cstdlib>
marci@771
     6
marci@771
     7
#include <LEDA/graph.h>
marci@771
     8
#include <LEDA/mcb_matching.h>
marci@771
     9
#include <LEDA/list.h>
marci@771
    10
marci@771
    11
#include <leda_graph_wrapper.h>
marci@771
    12
#include <sage_graph.h>
marci@771
    13
#include <dimacs.h>
marci@771
    14
#include <time_measure.h>
marci@771
    15
#include <edmonds_karp.h>
marci@771
    16
marci@771
    17
/**
marci@771
    18
 * Inicializalja a veletlenszamgeneratort.
marci@771
    19
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@771
    20
 * erre ne bizzad a titkaidat!
marci@771
    21
 */
marci@771
    22
void random_init()
marci@771
    23
{
marci@771
    24
	unsigned int seed = getpid();
marci@771
    25
	seed |= seed << 15;
marci@771
    26
	seed ^= time(0);
marci@771
    27
marci@771
    28
	srand(seed);
marci@771
    29
}
marci@771
    30
marci@771
    31
/**
marci@771
    32
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@771
    33
 */
marci@771
    34
int random(int m)
marci@771
    35
{
marci@771
    36
	return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@771
    37
}
marci@771
    38
alpar@921
    39
using namespace lemon;
marci@771
    40
marci@771
    41
using std::cout; 
marci@771
    42
using std::cin; 
marci@771
    43
using std::endl;
marci@771
    44
marci@771
    45
int main() {
marci@771
    46
   leda::graph g;
marci@771
    47
   typedef LedaGraphWrapper<leda::graph> Graph;
marci@771
    48
   Graph G(g);
marci@771
    49
//  typedef ListGraph Graph;
marci@771
    50
//  Graph G;
marci@771
    51
marci@771
    52
  typedef Graph::Node Node;
marci@771
    53
  typedef Graph::NodeIt NodeIt;  
marci@771
    54
  typedef Graph::Edge Edge;
marci@771
    55
  typedef Graph::EdgeIt EdgeIt;
marci@771
    56
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@771
    57
  typedef Graph::InEdgeIt InEdgeIt;
marci@771
    58
marci@771
    59
  //Node s, t;
marci@771
    60
  //Graph::EdgeMap<int> cap(G);
marci@771
    61
  //readDimacsMaxFlow(std::cin, G, s, t, cap);
marci@771
    62
  std::vector<Node> s_nodes;
marci@771
    63
  std::vector<Node> t_nodes;
marci@771
    64
marci@771
    65
  int a;
marci@771
    66
  cout << "number of nodes in the first color class=";
marci@771
    67
  cin >> a; 
marci@771
    68
  int b;
marci@771
    69
  cout << "number of nodes in the second color class=";
marci@771
    70
  cin >> b; 
marci@771
    71
  int m;
marci@771
    72
  cout << "number of edges=";
marci@771
    73
  cin >> m;   
marci@771
    74
marci@771
    75
  for(int i=0; i<a; ++i) {
marci@771
    76
    s_nodes.push_back(G.addNode());
marci@771
    77
  }
marci@771
    78
  for(int i=0; i<a; ++i) {
marci@771
    79
    t_nodes.push_back(G.addNode());
marci@771
    80
  }
marci@771
    81
  random_init();
marci@771
    82
  for(int i=0; i<m; ++i) {
marci@771
    83
    G.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@771
    84
  }
marci@771
    85
  
marci@771
    86
//   G.addEdge(s_nodes[1], t_nodes[5-4]);
marci@771
    87
//   G.addEdge(s_nodes[1], t_nodes[5-4]);
marci@771
    88
//   G.addEdge(s_nodes[1], t_nodes[4-4]);
marci@771
    89
//   G.addEdge(s_nodes[1], t_nodes[4-4]);
marci@771
    90
//   G.addEdge(s_nodes[2], t_nodes[4-4]);
marci@771
    91
//   G.addEdge(s_nodes[3], t_nodes[4-4]);
marci@771
    92
marci@771
    93
  leda_list<leda_node> A;
marci@771
    94
  leda_list<leda_node> B;
marci@771
    95
  Graph::NodeMap<bool> s_map(G); //false
marci@771
    96
  Graph::NodeMap<bool> t_map(G); //false
marci@771
    97
  
marci@771
    98
  for(int i=0; i<a; ++i) { s_map.set(s_nodes[i], true); A+=s_nodes[i]; }
marci@771
    99
  for(int i=0; i<b; ++i) { t_map.set(t_nodes[i], true); B+=t_nodes[i]; }
marci@771
   100
marci@771
   101
//   cout << "bfs and dfs iterator demo on the directed graph" << endl;
marci@771
   102
//   for(NodeIt n=G.first<NodeIt>(); G.valid(n); G.next(n)) { 
marci@771
   103
//     cout << G.id(n) << ": ";
marci@771
   104
//     cout << "out edges: ";
marci@771
   105
//     for(OutEdgeIt e=G.first<OutEdgeIt>(n); G.valid(e); G.next(e)) 
alpar@986
   106
//       cout << G.id(G.source(e)) << "->" << G.id(G.target(e)) << " ";
marci@771
   107
//     cout << "in edges: ";
marci@771
   108
//     for(InEdgeIt e=G.first<InEdgeIt>(n); G.valid(e); G.next(e)) 
alpar@986
   109
//       cout << G.id(G.source(e)) << "->" << G.id(G.target(e)) << " ";
marci@771
   110
//     cout << endl;
marci@771
   111
//   }
marci@771
   112
marci@771
   113
marci@771
   114
  {
marci@771
   115
    std::cout << "on-the-fly max bipartite matching (Edmonds-Karp) demo on wrapped leda graph..." << std::endl;
marci@771
   116
    Graph::EdgeMap<int> flow(G); //0 flow
marci@771
   117
    Graph::EdgeMap<int> cap(G, 1);
marci@771
   118
marci@771
   119
    Timer ts;
marci@771
   120
    ts.reset();
marci@771
   121
marci@771
   122
    MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap);
marci@771
   123
    int i=0;
marci@771
   124
    while (max_flow_test.augmentOnShortestPath()) { 
marci@771
   125
//       for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
alpar@986
   126
// 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   127
//       std::cout<<std::endl;
marci@771
   128
      ++i; 
marci@771
   129
    }
marci@771
   130
marci@771
   131
//     std::cout << "maximum matching: "<< std::endl;
marci@771
   132
//     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   133
//       if (flow.get(e))
alpar@986
   134
// 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   135
//     std::cout<<std::endl;
marci@771
   136
//     std::cout << "edges which are not in this maximum matching: "<< std::endl;
marci@771
   137
//     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   138
//       if (!flow.get(e))
alpar@986
   139
// 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   140
//     std::cout<<std::endl;
marci@771
   141
    
marci@771
   142
    std::cout << "elapsed time: " << ts << std::endl;
marci@771
   143
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@771
   144
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@771
   145
  }
marci@771
   146
marci@771
   147
//   {
marci@771
   148
//     std::cout << "on-the-fly max bipartite matching demo (Hopcroft-Karp) on wrapped leda graph..." << std::endl;
marci@771
   149
//     Graph::EdgeMap<int> flow(G); //0 flow
marci@771
   150
//     Graph::EdgeMap<int> cap(G, 1);
marci@771
   151
marci@771
   152
//     Timer ts;
marci@771
   153
//     ts.reset();
marci@771
   154
marci@771
   155
//     MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap);
marci@771
   156
//     int i=0;
marci@771
   157
//     while (max_flow_test.augmentOnBlockingFlow2()) { 
marci@771
   158
// //       for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
alpar@986
   159
// // 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   160
// //       std::cout<<std::endl;
marci@771
   161
//       ++i; 
marci@771
   162
//     }
marci@771
   163
marci@771
   164
// //     std::cout << "maximum matching: "<< std::endl;
marci@771
   165
// //     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   166
// //       if (flow.get(e))
alpar@986
   167
// // 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   168
// //     std::cout<<std::endl;
marci@771
   169
// //     std::cout << "edges which are not in this maximum matching: "<< std::endl;
marci@771
   170
// //     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   171
// //       if (!flow.get(e))
alpar@986
   172
// // 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   173
// //     std::cout<<std::endl;
marci@771
   174
    
marci@771
   175
//     std::cout << "elapsed time: " << ts << std::endl;
marci@771
   176
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@771
   177
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@771
   178
//   }
marci@771
   179
marci@771
   180
  {
marci@771
   181
    std::cout << "max bipartite matching (LEDA)..." << std::endl;
marci@771
   182
    //Graph::EdgeMap<int> flow(G); //0 flow
marci@771
   183
    //Graph::EdgeMap<int> cap(G, 1);
marci@771
   184
marci@771
   185
    leda_node_array<bool> NC(g);
marci@771
   186
marci@771
   187
    Timer ts;
marci@771
   188
    ts.reset();
marci@771
   189
marci@771
   190
    //MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap);
marci@771
   191
    //int i=0;
marci@771
   192
    //while (max_flow_test.augmentOnShortestPath()) { ++i; }
marci@771
   193
    
marci@771
   194
    //leda_list<leda_edge> l=MAX_CARD_BIPARTITE_MATCHING_HK(g, A, B, NC, false);
marci@771
   195
    leda_list<leda_edge> l=MAX_CARD_BIPARTITE_MATCHING(g);    
marci@771
   196
    
marci@771
   197
marci@771
   198
//     std::cout << "maximum matching: "<< std::endl;
marci@771
   199
//     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   200
//       if (flow.get(e))
alpar@986
   201
// 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   202
//     std::cout<<std::endl;
marci@771
   203
//     std::cout << "edges which are not in this maximum matching: "<< std::endl;
marci@771
   204
//     for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e))  
marci@771
   205
//       if (!flow.get(e))
alpar@986
   206
// 	std::cout << G.id(G.source(e)) << "-" << flow.get(e) << "->" << G.id(G.target(e)) << " ";
marci@771
   207
//     std::cout<<std::endl;
marci@771
   208
    
marci@771
   209
    
marci@771
   210
    std::cout << "elapsed time: " << ts << std::endl;
marci@771
   211
    //std::cout << "number of augmentation phases: " << i << std::endl; 
marci@771
   212
    std::cout << "flow value: "<< l.size() << std::endl;
marci@771
   213
  }
marci@771
   214
  
marci@771
   215
  
marci@771
   216
marci@771
   217
  return 0;
marci@771
   218
}