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