sl@0
|
1 |
//=======================================================================
|
sl@0
|
2 |
// Copyright (c) 2005 Aaron Windsor
|
sl@0
|
3 |
//
|
sl@0
|
4 |
// Distributed under the Boost Software License, Version 1.0.
|
sl@0
|
5 |
// (See accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
6 |
// http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
7 |
//
|
sl@0
|
8 |
//=======================================================================
|
sl@0
|
9 |
|
sl@0
|
10 |
/*
|
sl@0
|
11 |
* © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
|
sl@0
|
12 |
*/
|
sl@0
|
13 |
|
sl@0
|
14 |
#include <boost/graph/max_cardinality_matching.hpp>
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <iostream> // for std::cout
|
sl@0
|
17 |
#include <boost/vector_property_map.hpp>
|
sl@0
|
18 |
#include <boost/graph/adjacency_list.hpp>
|
sl@0
|
19 |
#include <boost/graph/adjacency_matrix.hpp>
|
sl@0
|
20 |
#include <boost/graph/random.hpp>
|
sl@0
|
21 |
#include <ctime>
|
sl@0
|
22 |
#include <boost/random.hpp>
|
sl@0
|
23 |
#include <boost/test/minimal.hpp>
|
sl@0
|
24 |
#ifdef __SYMBIAN32__
|
sl@0
|
25 |
#include "std_log_result.h"
|
sl@0
|
26 |
#define LOG_FILENAME_LINE __FILE__, __LINE__
|
sl@0
|
27 |
#endif
|
sl@0
|
28 |
using namespace boost;
|
sl@0
|
29 |
|
sl@0
|
30 |
typedef adjacency_list<vecS,
|
sl@0
|
31 |
vecS,
|
sl@0
|
32 |
undirectedS,
|
sl@0
|
33 |
property<vertex_index_t, int> > undirected_graph;
|
sl@0
|
34 |
|
sl@0
|
35 |
typedef adjacency_list<listS,
|
sl@0
|
36 |
listS,
|
sl@0
|
37 |
undirectedS,
|
sl@0
|
38 |
property<vertex_index_t, int> > undirected_list_graph;
|
sl@0
|
39 |
|
sl@0
|
40 |
typedef adjacency_matrix<undirectedS,
|
sl@0
|
41 |
property<vertex_index_t,int> > undirected_adjacency_matrix_graph;
|
sl@0
|
42 |
|
sl@0
|
43 |
|
sl@0
|
44 |
template <typename Graph>
|
sl@0
|
45 |
struct vertex_index_installer
|
sl@0
|
46 |
{
|
sl@0
|
47 |
static void install(Graph& g) {}
|
sl@0
|
48 |
};
|
sl@0
|
49 |
|
sl@0
|
50 |
|
sl@0
|
51 |
template <>
|
sl@0
|
52 |
struct vertex_index_installer<undirected_list_graph>
|
sl@0
|
53 |
{
|
sl@0
|
54 |
static void install(undirected_list_graph& g)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
typedef graph_traits<undirected_list_graph>::vertex_iterator vertex_iterator_t;
|
sl@0
|
57 |
typedef graph_traits<undirected_list_graph>::vertices_size_type v_size_t;
|
sl@0
|
58 |
|
sl@0
|
59 |
vertex_iterator_t vi, vi_end;
|
sl@0
|
60 |
v_size_t i = 0;
|
sl@0
|
61 |
for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi, ++i)
|
sl@0
|
62 |
put(vertex_index, g, *vi, i);
|
sl@0
|
63 |
}
|
sl@0
|
64 |
};
|
sl@0
|
65 |
|
sl@0
|
66 |
|
sl@0
|
67 |
|
sl@0
|
68 |
template <typename Graph>
|
sl@0
|
69 |
void complete_graph(Graph& g, int n)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
//creates the complete graph on n vertices
|
sl@0
|
72 |
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t;
|
sl@0
|
73 |
|
sl@0
|
74 |
g = Graph(n);
|
sl@0
|
75 |
vertex_iterator_t vi, vi_end, wi;
|
sl@0
|
76 |
tie(vi,vi_end) = vertices(g);
|
sl@0
|
77 |
for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
|
sl@0
|
78 |
{
|
sl@0
|
79 |
wi = vi;
|
sl@0
|
80 |
++wi;
|
sl@0
|
81 |
for(; wi != vi_end; ++wi)
|
sl@0
|
82 |
add_edge(*vi,*wi,g);
|
sl@0
|
83 |
}
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
|
sl@0
|
87 |
|
sl@0
|
88 |
template <typename Graph>
|
sl@0
|
89 |
void gabows_graph(Graph& g, int n)
|
sl@0
|
90 |
{
|
sl@0
|
91 |
//creates a graph with 2n vertices, consisting of the complete graph
|
sl@0
|
92 |
//on n vertices plus n vertices of degree one, each adjacent to one
|
sl@0
|
93 |
//vertex in the complete graph. without any initial matching, this
|
sl@0
|
94 |
//graph forces edmonds' algorithm into worst-case behavior.
|
sl@0
|
95 |
|
sl@0
|
96 |
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t;
|
sl@0
|
97 |
|
sl@0
|
98 |
g = Graph(2* n);
|
sl@0
|
99 |
|
sl@0
|
100 |
vertex_iterator_t vi, vi_end, ui, ui_end, halfway;
|
sl@0
|
101 |
|
sl@0
|
102 |
tie(ui,ui_end) = vertices(g);
|
sl@0
|
103 |
|
sl@0
|
104 |
halfway = ui;
|
sl@0
|
105 |
for(int i = 0; i < n; ++i)
|
sl@0
|
106 |
++halfway;
|
sl@0
|
107 |
|
sl@0
|
108 |
|
sl@0
|
109 |
while(ui != halfway)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
vi = ui;
|
sl@0
|
112 |
++vi;
|
sl@0
|
113 |
while (vi != halfway)
|
sl@0
|
114 |
{
|
sl@0
|
115 |
add_edge(*ui,*vi,g);
|
sl@0
|
116 |
++vi;
|
sl@0
|
117 |
}
|
sl@0
|
118 |
++ui;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
tie(ui,ui_end) = vertices(g);
|
sl@0
|
122 |
|
sl@0
|
123 |
while(halfway != ui_end)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
add_edge(*ui, *halfway, g);
|
sl@0
|
126 |
++ui;
|
sl@0
|
127 |
++halfway;
|
sl@0
|
128 |
}
|
sl@0
|
129 |
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
|
sl@0
|
133 |
|
sl@0
|
134 |
template <typename Graph>
|
sl@0
|
135 |
void matching_test(std::size_t num_v, const std::string& graph_name)
|
sl@0
|
136 |
{
|
sl@0
|
137 |
typedef typename property_map<Graph,vertex_index_t>::type vertex_index_map_t;
|
sl@0
|
138 |
typedef vector_property_map< typename graph_traits<Graph>::vertex_descriptor, vertex_index_map_t > mate_t;
|
sl@0
|
139 |
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t;
|
sl@0
|
140 |
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor_t;
|
sl@0
|
141 |
typedef typename graph_traits<Graph>::vertices_size_type v_size_t;
|
sl@0
|
142 |
|
sl@0
|
143 |
const std::size_t double_num_v = num_v * 2;
|
sl@0
|
144 |
|
sl@0
|
145 |
bool all_tests_passed = true;
|
sl@0
|
146 |
|
sl@0
|
147 |
//form the complete graph on 2*n vertices; the maximum cardinality matching, greedy matching,
|
sl@0
|
148 |
//and extra greedy matching should all be the same - a matching of size n.
|
sl@0
|
149 |
|
sl@0
|
150 |
Graph g(double_num_v);
|
sl@0
|
151 |
complete_graph(g,double_num_v);
|
sl@0
|
152 |
|
sl@0
|
153 |
vertex_index_installer<Graph>::install(g);
|
sl@0
|
154 |
|
sl@0
|
155 |
mate_t edmonds_mate(double_num_v);
|
sl@0
|
156 |
mate_t greedy_mate(double_num_v);
|
sl@0
|
157 |
mate_t extra_greedy_mate(double_num_v);
|
sl@0
|
158 |
|
sl@0
|
159 |
//find a maximum cardinality matching using edmonds' blossom-shrinking algorithm, starting
|
sl@0
|
160 |
//with an empty matching.
|
sl@0
|
161 |
bool edmonds_result =
|
sl@0
|
162 |
matching < Graph, mate_t, vertex_index_map_t,
|
sl@0
|
163 |
edmonds_augmenting_path_finder, empty_matching, maximum_cardinality_matching_verifier>
|
sl@0
|
164 |
(g,edmonds_mate, get(vertex_index,g));
|
sl@0
|
165 |
|
sl@0
|
166 |
BOOST_CHECK (edmonds_result);
|
sl@0
|
167 |
if (!edmonds_result)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
std::cout << "Verifier reporting a problem finding a perfect matching on" << std::endl
|
sl@0
|
170 |
<< "the complete graph using " << graph_name << std::endl;
|
sl@0
|
171 |
all_tests_passed = false;
|
sl@0
|
172 |
}
|
sl@0
|
173 |
|
sl@0
|
174 |
//find a greedy matching
|
sl@0
|
175 |
bool greedy_result =
|
sl@0
|
176 |
matching<Graph, mate_t, vertex_index_map_t,
|
sl@0
|
177 |
no_augmenting_path_finder, greedy_matching, maximum_cardinality_matching_verifier>
|
sl@0
|
178 |
(g,greedy_mate, get(vertex_index,g));
|
sl@0
|
179 |
|
sl@0
|
180 |
BOOST_CHECK (greedy_result);
|
sl@0
|
181 |
if (!greedy_result)
|
sl@0
|
182 |
{
|
sl@0
|
183 |
std::cout << "Verifier reporting a problem finding a greedy matching on" << std::endl
|
sl@0
|
184 |
<< "the complete graph using " << graph_name << std::endl;
|
sl@0
|
185 |
all_tests_passed = false;
|
sl@0
|
186 |
}
|
sl@0
|
187 |
|
sl@0
|
188 |
//find an extra greedy matching
|
sl@0
|
189 |
bool extra_greedy_result =
|
sl@0
|
190 |
matching<Graph, mate_t, vertex_index_map_t,
|
sl@0
|
191 |
no_augmenting_path_finder, extra_greedy_matching, maximum_cardinality_matching_verifier>
|
sl@0
|
192 |
(g,extra_greedy_mate,get(vertex_index,g));
|
sl@0
|
193 |
|
sl@0
|
194 |
BOOST_CHECK (extra_greedy_result);
|
sl@0
|
195 |
if (!extra_greedy_result)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
std::cout << "Verifier reporting a problem finding an extra greedy matching on" << std::endl
|
sl@0
|
198 |
<< "the complete graph using " << graph_name << std::endl;
|
sl@0
|
199 |
all_tests_passed = false;
|
sl@0
|
200 |
}
|
sl@0
|
201 |
|
sl@0
|
202 |
//as a sanity check, make sure that each of the matchings returned is a valid matching and has
|
sl@0
|
203 |
//1000 edges.
|
sl@0
|
204 |
|
sl@0
|
205 |
bool edmonds_sanity_check =
|
sl@0
|
206 |
is_a_matching(g,edmonds_mate) && matching_size(g,edmonds_mate) == num_v;
|
sl@0
|
207 |
|
sl@0
|
208 |
BOOST_CHECK (edmonds_sanity_check);
|
sl@0
|
209 |
if (edmonds_result && !edmonds_sanity_check)
|
sl@0
|
210 |
{
|
sl@0
|
211 |
std::cout << "Verifier okayed edmonds' algorithm on the complete graph, but" << std::endl
|
sl@0
|
212 |
<< "the matching returned either wasn't a valid matching or wasn't" << std::endl
|
sl@0
|
213 |
<< "actually a maximum cardinality matching." << std::endl;
|
sl@0
|
214 |
all_tests_passed = false;
|
sl@0
|
215 |
}
|
sl@0
|
216 |
|
sl@0
|
217 |
bool greedy_sanity_check =
|
sl@0
|
218 |
is_a_matching(g,greedy_mate) && matching_size(g,greedy_mate) == num_v;
|
sl@0
|
219 |
|
sl@0
|
220 |
BOOST_CHECK (greedy_sanity_check);
|
sl@0
|
221 |
if (greedy_result && !greedy_sanity_check)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
std::cout << "Verifier okayed greedy algorithm on the complete graph, but" << std::endl
|
sl@0
|
224 |
<< "the matching returned either wasn't a valid matching or wasn't" << std::endl
|
sl@0
|
225 |
<< "actually a maximum cardinality matching." << std::endl;
|
sl@0
|
226 |
all_tests_passed = false;
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
bool extra_greedy_sanity_check =
|
sl@0
|
230 |
is_a_matching(g,extra_greedy_mate) && matching_size(g,extra_greedy_mate) == num_v;
|
sl@0
|
231 |
|
sl@0
|
232 |
BOOST_CHECK (extra_greedy_sanity_check);
|
sl@0
|
233 |
if (extra_greedy_result && !extra_greedy_sanity_check)
|
sl@0
|
234 |
{
|
sl@0
|
235 |
std::cout << "Verifier okayed extra greedy algorithm on the complete graph, but" << std::endl
|
sl@0
|
236 |
<< "the matching returned either wasn't a valid matching or wasn't" << std::endl
|
sl@0
|
237 |
<< "actually a maximum cardinality matching." << std::endl;
|
sl@0
|
238 |
all_tests_passed = false;
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
//Now remove an edge from the edmonds_mate matching.
|
sl@0
|
242 |
vertex_iterator_t vi,vi_end;
|
sl@0
|
243 |
for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
|
sl@0
|
244 |
if (edmonds_mate[*vi] != graph_traits<Graph>::null_vertex())
|
sl@0
|
245 |
break;
|
sl@0
|
246 |
|
sl@0
|
247 |
edmonds_mate[edmonds_mate[*vi]] = graph_traits<Graph>::null_vertex();
|
sl@0
|
248 |
edmonds_mate[*vi] = graph_traits<Graph>::null_vertex();
|
sl@0
|
249 |
|
sl@0
|
250 |
//...and run the matching verifier - it should tell us that the matching isn't
|
sl@0
|
251 |
//a maximum matching.
|
sl@0
|
252 |
bool modified_edmonds_verification_result =
|
sl@0
|
253 |
maximum_cardinality_matching_verifier<Graph,mate_t,vertex_index_map_t>::verify_matching(g,edmonds_mate,get(vertex_index,g));
|
sl@0
|
254 |
|
sl@0
|
255 |
BOOST_CHECK (!modified_edmonds_verification_result);
|
sl@0
|
256 |
if (modified_edmonds_verification_result)
|
sl@0
|
257 |
{
|
sl@0
|
258 |
std::cout << "Verifier was fooled into thinking that a non-maximum matching was maximum" << std::endl;
|
sl@0
|
259 |
all_tests_passed = false;
|
sl@0
|
260 |
}
|
sl@0
|
261 |
|
sl@0
|
262 |
Graph h(double_num_v);
|
sl@0
|
263 |
gabows_graph(h,num_v);
|
sl@0
|
264 |
|
sl@0
|
265 |
vertex_index_installer<Graph>::install(h);
|
sl@0
|
266 |
|
sl@0
|
267 |
//gabow's graph always has a perfect matching. it's also a good example of why
|
sl@0
|
268 |
//one should initialize with the extra_greedy_matching in most cases.
|
sl@0
|
269 |
|
sl@0
|
270 |
mate_t gabow_mate(double_num_v);
|
sl@0
|
271 |
|
sl@0
|
272 |
bool gabows_graph_result = checked_edmonds_maximum_cardinality_matching(h,gabow_mate);
|
sl@0
|
273 |
|
sl@0
|
274 |
BOOST_CHECK (gabows_graph_result);
|
sl@0
|
275 |
if (!gabows_graph_result)
|
sl@0
|
276 |
{
|
sl@0
|
277 |
std::cout << "Problem on Gabow's Graph with " << graph_name << ":" << std::endl
|
sl@0
|
278 |
<< " Verifier reporting a maximum cardinality matching not found." << std::endl;
|
sl@0
|
279 |
all_tests_passed = false;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
|
sl@0
|
282 |
BOOST_CHECK (matching_size(h,gabow_mate) == num_v);
|
sl@0
|
283 |
if (gabows_graph_result && matching_size(h,gabow_mate) != num_v)
|
sl@0
|
284 |
{
|
sl@0
|
285 |
std::cout << "Problem on Gabow's Graph with " << graph_name << ":" << std::endl
|
sl@0
|
286 |
<< " Verifier reported a maximum cardinality matching found," << std::endl
|
sl@0
|
287 |
<< " but matching size is " << matching_size(h,gabow_mate)
|
sl@0
|
288 |
<< " when it should be " << num_v << std::endl;
|
sl@0
|
289 |
all_tests_passed = false;
|
sl@0
|
290 |
}
|
sl@0
|
291 |
|
sl@0
|
292 |
Graph j(double_num_v);
|
sl@0
|
293 |
vertex_index_installer<Graph>::install(j);
|
sl@0
|
294 |
|
sl@0
|
295 |
typedef boost::mt19937 base_generator_type;
|
sl@0
|
296 |
base_generator_type generator(static_cast<unsigned int>(std::time(0)));
|
sl@0
|
297 |
boost::uniform_int<> distribution(0, double_num_v-1);
|
sl@0
|
298 |
boost::variate_generator<base_generator_type&, boost::uniform_int<> > rand_num(generator, distribution);
|
sl@0
|
299 |
|
sl@0
|
300 |
std::size_t num_edges = 0;
|
sl@0
|
301 |
bool success;
|
sl@0
|
302 |
|
sl@0
|
303 |
while (num_edges < 4*double_num_v)
|
sl@0
|
304 |
{
|
sl@0
|
305 |
vertex_descriptor_t u = random_vertex(j,rand_num);
|
sl@0
|
306 |
vertex_descriptor_t v = random_vertex(j,rand_num);
|
sl@0
|
307 |
if (u != v)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
tie(tuples::ignore, success) = add_edge(u, v, j);
|
sl@0
|
310 |
if (success)
|
sl@0
|
311 |
num_edges++;
|
sl@0
|
312 |
}
|
sl@0
|
313 |
}
|
sl@0
|
314 |
|
sl@0
|
315 |
mate_t random_mate(double_num_v);
|
sl@0
|
316 |
bool random_graph_result = checked_edmonds_maximum_cardinality_matching(j,random_mate);
|
sl@0
|
317 |
|
sl@0
|
318 |
BOOST_CHECK(random_graph_result);
|
sl@0
|
319 |
if (!random_graph_result)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
std::cout << "Matching in random graph not maximum for " << graph_name << std::endl;
|
sl@0
|
322 |
all_tests_passed = false;
|
sl@0
|
323 |
}
|
sl@0
|
324 |
|
sl@0
|
325 |
//Now remove an edge from the random_mate matching.
|
sl@0
|
326 |
for(tie(vi,vi_end) = vertices(j); vi != vi_end; ++vi)
|
sl@0
|
327 |
if (random_mate[*vi] != graph_traits<Graph>::null_vertex())
|
sl@0
|
328 |
break;
|
sl@0
|
329 |
|
sl@0
|
330 |
random_mate[random_mate[*vi]] = graph_traits<Graph>::null_vertex();
|
sl@0
|
331 |
random_mate[*vi] = graph_traits<Graph>::null_vertex();
|
sl@0
|
332 |
|
sl@0
|
333 |
//...and run the matching verifier - it should tell us that the matching isn't
|
sl@0
|
334 |
//a maximum matching.
|
sl@0
|
335 |
bool modified_random_verification_result =
|
sl@0
|
336 |
maximum_cardinality_matching_verifier<Graph,mate_t,vertex_index_map_t>::verify_matching(j,random_mate,get(vertex_index,j));
|
sl@0
|
337 |
|
sl@0
|
338 |
BOOST_CHECK(!modified_random_verification_result);
|
sl@0
|
339 |
if (modified_random_verification_result)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
std::cout << "Verifier was fooled into thinking that a non-maximum matching was maximum" << std::endl;
|
sl@0
|
342 |
all_tests_passed = false;
|
sl@0
|
343 |
}
|
sl@0
|
344 |
|
sl@0
|
345 |
BOOST_CHECK(all_tests_passed);
|
sl@0
|
346 |
if (all_tests_passed)
|
sl@0
|
347 |
{
|
sl@0
|
348 |
std::cout << graph_name << " passed all tests for n = " << num_v << '.' << std::endl;
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
}
|
sl@0
|
352 |
|
sl@0
|
353 |
|
sl@0
|
354 |
|
sl@0
|
355 |
|
sl@0
|
356 |
int test_main(int argc, char* argv[])
|
sl@0
|
357 |
{
|
sl@0
|
358 |
|
sl@0
|
359 |
matching_test<undirected_graph>(10, "adjacency_list (using vectors)");
|
sl@0
|
360 |
matching_test<undirected_list_graph>(10, "adjacency_list (using lists)");
|
sl@0
|
361 |
matching_test<undirected_adjacency_matrix_graph>(10, "adjacency_matrix");
|
sl@0
|
362 |
|
sl@0
|
363 |
matching_test<undirected_graph>(50, "adjacency_list (using vectors)");
|
sl@0
|
364 |
matching_test<undirected_list_graph>(50, "adjacency_list (using lists)");
|
sl@0
|
365 |
matching_test<undirected_adjacency_matrix_graph>(50, "adjacency_matrix");
|
sl@0
|
366 |
|
sl@0
|
367 |
matching_test<undirected_graph>(51, "adjacency_list (using vectors)");
|
sl@0
|
368 |
matching_test<undirected_list_graph>(51, "adjacency_list (using lists)");
|
sl@0
|
369 |
matching_test<undirected_adjacency_matrix_graph>(51, "adjacency_matrix");
|
sl@0
|
370 |
|
sl@0
|
371 |
#ifdef __SYMBIAN32__
|
sl@0
|
372 |
std_log(LOG_FILENAME_LINE,"[End Test Case ]");
|
sl@0
|
373 |
|
sl@0
|
374 |
testResultXml("matching_test");
|
sl@0
|
375 |
close_log_file();
|
sl@0
|
376 |
#endif
|
sl@0
|
377 |
return 0;
|
sl@0
|
378 |
}
|
sl@0
|
379 |
|
sl@0
|
380 |
|