random_spanning_tree.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2010 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP
  8. #define BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP
  9. #include <vector>
  10. #include <boost/assert.hpp>
  11. #include <boost/graph/loop_erased_random_walk.hpp>
  12. #include <boost/graph/random.hpp>
  13. #include <boost/graph/iteration_macros.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/graph/graph_traits.hpp>
  17. #include <boost/graph/graph_concepts.hpp>
  18. #include <boost/graph/properties.hpp>
  19. #include <boost/graph/named_function_params.hpp>
  20. namespace boost {
  21. namespace detail {
  22. // Use Wilson's algorithm (based on loop-free random walks) to generate a
  23. // random spanning tree. The distribution of edges used is controlled by
  24. // the next_edge() function, so this version allows either weighted or
  25. // unweighted selection of trees.
  26. // Algorithm is from http://en.wikipedia.org/wiki/Uniform_spanning_tree
  27. template <typename Graph, typename PredMap, typename ColorMap, typename NextEdge>
  28. void random_spanning_tree_internal(const Graph& g, typename graph_traits<Graph>::vertex_descriptor s, PredMap pred, ColorMap color, NextEdge next_edge) {
  29. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  30. BOOST_ASSERT (num_vertices(g) >= 1); // g must also be undirected (or symmetric) and connected
  31. typedef color_traits<typename property_traits<ColorMap>::value_type> color_gen;
  32. BGL_FORALL_VERTICES_T(v, g, Graph) put(color, v, color_gen::white());
  33. std::vector<vertex_descriptor> path;
  34. put(color, s, color_gen::black());
  35. put(pred, s, graph_traits<Graph>::null_vertex());
  36. BGL_FORALL_VERTICES_T(v, g, Graph) {
  37. if (get(color, v) != color_gen::white()) continue;
  38. loop_erased_random_walk(g, v, next_edge, color, path);
  39. for (typename std::vector<vertex_descriptor>::const_reverse_iterator i = path.rbegin();
  40. boost::next(i) !=
  41. (typename std::vector<vertex_descriptor>::const_reverse_iterator)path.rend();
  42. ++i) {
  43. typename std::vector<vertex_descriptor>::const_reverse_iterator j = i;
  44. ++j;
  45. BOOST_ASSERT (get(color, *j) == color_gen::gray());
  46. put(color, *j, color_gen::black());
  47. put(pred, *j, *i);
  48. }
  49. }
  50. }
  51. }
  52. // Compute a uniformly-distributed spanning tree on a graph. Use Wilson's algorithm:
  53. // @inproceedings{wilson96generating,
  54. // author = {Wilson, David Bruce},
  55. // title = {Generating random spanning trees more quickly than the cover time},
  56. // booktitle = {STOC '96: Proceedings of the twenty-eighth annual ACM symposium on Theory of computing},
  57. // year = {1996},
  58. // isbn = {0-89791-785-5},
  59. // pages = {296--303},
  60. // location = {Philadelphia, Pennsylvania, United States},
  61. // doi = {http://doi.acm.org/10.1145/237814.237880},
  62. // publisher = {ACM},
  63. // address = {New York, NY, USA},
  64. // }
  65. //
  66. template <typename Graph, typename Gen, typename PredMap, typename ColorMap>
  67. void random_spanning_tree(const Graph& g, Gen& gen, typename graph_traits<Graph>::vertex_descriptor root,
  68. PredMap pred, static_property_map<double>, ColorMap color) {
  69. unweighted_random_out_edge_gen<Graph, Gen> random_oe(gen);
  70. detail::random_spanning_tree_internal(g, root, pred, color, random_oe);
  71. }
  72. // Compute a weight-distributed spanning tree on a graph.
  73. template <typename Graph, typename Gen, typename PredMap, typename WeightMap, typename ColorMap>
  74. void random_spanning_tree(const Graph& g, Gen& gen, typename graph_traits<Graph>::vertex_descriptor root,
  75. PredMap pred, WeightMap weight, ColorMap color) {
  76. weighted_random_out_edge_gen<Graph, WeightMap, Gen> random_oe(weight, gen);
  77. detail::random_spanning_tree_internal(g, root, pred, color, random_oe);
  78. }
  79. template <typename Graph, typename Gen, typename P, typename T, typename R>
  80. void random_spanning_tree(const Graph& g, Gen& gen, const bgl_named_params<P, T, R>& params) {
  81. using namespace boost::graph::keywords;
  82. typedef bgl_named_params<P, T, R> params_type;
  83. BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params)
  84. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  85. vertex_descriptor default_vertex = *vertices(g).first;
  86. vertex_descriptor start_vertex = arg_pack[_root_vertex | default_vertex];
  87. typename boost::parameter::binding<
  88. arg_pack_type,
  89. boost::graph::keywords::tag::predecessor_map
  90. >::type pred_map = arg_pack[_predecessor_map];
  91. static_property_map<double> default_weight_map(1.);
  92. typename boost::parameter::value_type<
  93. arg_pack_type,
  94. boost::graph::keywords::tag::weight_map,
  95. static_property_map<double>
  96. >::type e_w_map = arg_pack[_weight_map | default_weight_map];
  97. typename boost::detail::map_maker<
  98. Graph,
  99. arg_pack_type,
  100. boost::graph::keywords::tag::color_map,
  101. boost::default_color_type
  102. >::map_type c_map = boost::detail::make_color_map_from_arg_pack(g, arg_pack);
  103. random_spanning_tree(g, gen, start_vertex, pred_map, e_w_map, c_map);
  104. }
  105. }
  106. #include <boost/graph/iteration_macros_undef.hpp>
  107. #endif // BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP