1 | //======================================================================= |
---|
2 | // Copyright (c) 2005 Aaron Windsor |
---|
3 | // |
---|
4 | // Distributed under the Boost Software License, Version 1.0. |
---|
5 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | // |
---|
8 | //======================================================================= |
---|
9 | #include <cstdlib> |
---|
10 | #include <iostream> |
---|
11 | #include <boost/vector_property_map.hpp> |
---|
12 | #include <boost/graph/adjacency_list.hpp> |
---|
13 | #include <boost/graph/random.hpp> |
---|
14 | #include <ctime> |
---|
15 | #include <boost/random.hpp> |
---|
16 | |
---|
17 | #include <boost/graph/max_cardinality_matching.hpp> |
---|
18 | |
---|
19 | using namespace boost; |
---|
20 | |
---|
21 | typedef adjacency_list<vecS, |
---|
22 | vecS, |
---|
23 | undirectedS, |
---|
24 | property<vertex_index_t, int> > undirected_graph; |
---|
25 | |
---|
26 | typedef property_map<undirected_graph,vertex_index_t>::type vertex_index_map_t; |
---|
27 | typedef vector_property_map<graph_traits<undirected_graph>::vertex_descriptor, vertex_index_map_t > mate_t; |
---|
28 | typedef graph_traits<undirected_graph>::vertex_iterator vertex_iterator_t; |
---|
29 | typedef graph_traits<undirected_graph>::vertex_descriptor vertex_descriptor_t; |
---|
30 | typedef graph_traits<undirected_graph>::vertices_size_type v_size_t; |
---|
31 | |
---|
32 | |
---|
33 | int main(int argc, char** argv) |
---|
34 | { |
---|
35 | if (argc < 3) |
---|
36 | { |
---|
37 | std::cout << "Usage: " << argv[0] << " n m" << std::endl |
---|
38 | << "Tests the checked matching on a random graph w/ n vertices and m edges" << std::endl; |
---|
39 | exit(-1); |
---|
40 | } |
---|
41 | |
---|
42 | int n = atoi(argv[1]); |
---|
43 | int m = atoi(argv[2]); |
---|
44 | |
---|
45 | undirected_graph g(n); |
---|
46 | |
---|
47 | |
---|
48 | typedef boost::mt19937 base_generator_type; |
---|
49 | base_generator_type generator(static_cast<unsigned int>(std::time(0))); |
---|
50 | boost::uniform_int<> distribution(0, n-1); |
---|
51 | boost::variate_generator<base_generator_type&, boost::uniform_int<> > rand_num(generator, distribution); |
---|
52 | |
---|
53 | int num_edges = 0; |
---|
54 | bool success; |
---|
55 | |
---|
56 | while (num_edges < m) |
---|
57 | { |
---|
58 | vertex_descriptor_t u = random_vertex(g,rand_num); |
---|
59 | vertex_descriptor_t v = random_vertex(g,rand_num); |
---|
60 | if (u != v) |
---|
61 | { |
---|
62 | if (!edge(u,v,g).second) |
---|
63 | tie(tuples::ignore, success) = add_edge(u, v, g); |
---|
64 | else |
---|
65 | success = false; |
---|
66 | |
---|
67 | if (success) |
---|
68 | num_edges++; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | mate_t mate(n); |
---|
73 | bool random_graph_result = checked_edmonds_maximum_cardinality_matching(g,mate); |
---|
74 | |
---|
75 | if (!random_graph_result) |
---|
76 | { |
---|
77 | std::cout << "TEST 1 FAILED!!!" << std::endl << std::endl; |
---|
78 | |
---|
79 | std::cout << "Graph has edges: "; |
---|
80 | typedef graph_traits<undirected_graph>::edge_iterator edge_iterator_t; |
---|
81 | edge_iterator_t ei,ei_end; |
---|
82 | for(tie(ei,ei_end) = edges(g); ei != ei_end; ++ei) |
---|
83 | std:: cout << *ei << ", "; |
---|
84 | std::cout << std::endl; |
---|
85 | |
---|
86 | std::cout << "Matching is: "; |
---|
87 | vertex_iterator_t vi, vi_end; |
---|
88 | for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) |
---|
89 | if (mate[*vi] != graph_traits<undirected_graph>::null_vertex() && |
---|
90 | *vi < mate[*vi]) |
---|
91 | std::cout << "{" << *vi << "," << mate[*vi] << "}, "; |
---|
92 | std::cout << std::endl; |
---|
93 | } |
---|
94 | |
---|
95 | //Now remove an edge from the random_mate matching. |
---|
96 | vertex_iterator_t vi, vi_end; |
---|
97 | for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) |
---|
98 | if (mate[*vi] != graph_traits<undirected_graph>::null_vertex()) |
---|
99 | break; |
---|
100 | |
---|
101 | mate[mate[*vi]] = graph_traits<undirected_graph>::null_vertex(); |
---|
102 | mate[*vi] = graph_traits<undirected_graph>::null_vertex(); |
---|
103 | |
---|
104 | //...and run the matching verifier - it should tell us that the matching isn't |
---|
105 | //a maximum matching. |
---|
106 | bool modified_random_verification_result = |
---|
107 | maximum_cardinality_matching_verifier<undirected_graph,mate_t,vertex_index_map_t>::verify_matching(g,mate,get(vertex_index,g)); |
---|
108 | |
---|
109 | if (modified_random_verification_result) |
---|
110 | { |
---|
111 | std::cout << "TEST 2 FAILED!!!" << std::endl; |
---|
112 | } |
---|
113 | |
---|
114 | //find a greedy matching on the graph |
---|
115 | mate_t greedy_mate(n); |
---|
116 | greedy_matching<undirected_graph, mate_t>::find_matching(g,greedy_mate); |
---|
117 | |
---|
118 | if (matching_size(g,mate) > matching_size(g,greedy_mate) && |
---|
119 | maximum_cardinality_matching_verifier<undirected_graph,mate_t,vertex_index_map_t>::verify_matching(g,greedy_mate,get(vertex_index,g))) |
---|
120 | std::cout << "TEST 3 FAILED!!!" << std::endl; |
---|
121 | |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|