random_matching_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/property_map/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. #include <boost/graph/max_cardinality_matching.hpp>
  17. using namespace boost;
  18. typedef adjacency_list<vecS,
  19. vecS,
  20. undirectedS,
  21. property<vertex_index_t, int> > undirected_graph;
  22. typedef property_map<undirected_graph,vertex_index_t>::type vertex_index_map_t;
  23. typedef vector_property_map<graph_traits<undirected_graph>::vertex_descriptor, vertex_index_map_t > mate_t;
  24. typedef graph_traits<undirected_graph>::vertex_iterator vertex_iterator_t;
  25. typedef graph_traits<undirected_graph>::vertex_descriptor vertex_descriptor_t;
  26. typedef graph_traits<undirected_graph>::vertices_size_type v_size_t;
  27. int main(int argc, char** argv)
  28. {
  29. if (argc < 3)
  30. {
  31. std::cout << "Usage: " << argv[0] << " n m" << std::endl
  32. << "Tests the checked matching on a random graph w/ n vertices and m edges" << std::endl;
  33. exit(-1);
  34. }
  35. int n = atoi(argv[1]);
  36. int m = atoi(argv[2]);
  37. undirected_graph g(n);
  38. typedef boost::mt19937 base_generator_type;
  39. base_generator_type generator(static_cast<unsigned int>(std::time(0)));
  40. boost::uniform_int<> distribution(0, n-1);
  41. boost::variate_generator<base_generator_type&, boost::uniform_int<> > rand_num(generator, distribution);
  42. int num_edges = 0;
  43. bool success;
  44. while (num_edges < m)
  45. {
  46. vertex_descriptor_t u = random_vertex(g,rand_num);
  47. vertex_descriptor_t v = random_vertex(g,rand_num);
  48. if (u != v)
  49. {
  50. if (!edge(u,v,g).second)
  51. boost::tie(tuples::ignore, success) = add_edge(u, v, g);
  52. else
  53. success = false;
  54. if (success)
  55. num_edges++;
  56. }
  57. }
  58. mate_t mate(n);
  59. bool random_graph_result = checked_edmonds_maximum_cardinality_matching(g,mate);
  60. if (!random_graph_result)
  61. {
  62. std::cout << "TEST 1 FAILED!!!" << std::endl << std::endl;
  63. std::cout << "Graph has edges: ";
  64. typedef graph_traits<undirected_graph>::edge_iterator edge_iterator_t;
  65. edge_iterator_t ei,ei_end;
  66. for(boost::tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
  67. std:: cout << *ei << ", ";
  68. std::cout << std::endl;
  69. std::cout << "Matching is: ";
  70. vertex_iterator_t vi, vi_end;
  71. for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
  72. if (mate[*vi] != graph_traits<undirected_graph>::null_vertex() &&
  73. *vi < mate[*vi])
  74. std::cout << "{" << *vi << "," << mate[*vi] << "}, ";
  75. std::cout << std::endl;
  76. }
  77. //Now remove an edge from the random_mate matching.
  78. vertex_iterator_t vi, vi_end;
  79. for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
  80. if (mate[*vi] != graph_traits<undirected_graph>::null_vertex())
  81. break;
  82. mate[mate[*vi]] = graph_traits<undirected_graph>::null_vertex();
  83. mate[*vi] = graph_traits<undirected_graph>::null_vertex();
  84. //...and run the matching verifier - it should tell us that the matching isn't
  85. //a maximum matching.
  86. bool modified_random_verification_result =
  87. maximum_cardinality_matching_verifier<undirected_graph,mate_t,vertex_index_map_t>::verify_matching(g,mate,get(vertex_index,g));
  88. if (modified_random_verification_result)
  89. {
  90. std::cout << "TEST 2 FAILED!!!" << std::endl;
  91. }
  92. //find a greedy matching on the graph
  93. mate_t greedy_mate(n);
  94. greedy_matching<undirected_graph, mate_t>::find_matching(g,greedy_mate);
  95. if (matching_size(g,mate) > matching_size(g,greedy_mate) &&
  96. maximum_cardinality_matching_verifier<undirected_graph,mate_t,vertex_index_map_t>::verify_matching(g,greedy_mate,get(vertex_index,g)))
  97. std::cout << "TEST 3 FAILED!!!" << std::endl;
  98. return 0;
  99. }