COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/max_bipartite_matching_demo.cc @ 498:eb8bfa683d92

Last change on this file since 498:eb8bfa683d92 was 198:5cec393baade, checked in by marci, 21 years ago

max cardinality bipartite matching demo, something to play with it

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