// Copyright 2010 The Trustees of Indiana University. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // Authors: Jeremiah Willcock // Andrew Lumsdaine #ifndef BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP #define BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace detail { // Use Wilson's algorithm (based on loop-free random walks) to generate a // random spanning tree. The distribution of edges used is controlled by // the next_edge() function, so this version allows either weighted or // unweighted selection of trees. // Algorithm is from http://en.wikipedia.org/wiki/Uniform_spanning_tree template void random_spanning_tree_internal(const Graph& g, typename graph_traits::vertex_descriptor s, PredMap pred, ColorMap color, NextEdge next_edge) { typedef typename graph_traits::vertex_descriptor vertex_descriptor; typedef typename graph_traits::edge_descriptor edge_descriptor; BOOST_ASSERT (num_vertices(g) >= 1); // g must also be undirected (or symmetric) and connected typedef color_traits::value_type> color_gen; BGL_FORALL_VERTICES_T(v, g, Graph) put(color, v, color_gen::white()); std::vector path; put(color, s, color_gen::black()); put(pred, s, graph_traits::null_vertex()); BGL_FORALL_VERTICES_T(v, g, Graph) { if (get(color, v) != color_gen::white()) continue; loop_erased_random_walk(g, v, next_edge, color, path); for (typename std::vector::const_reverse_iterator i = path.rbegin(); boost::next(i) != (typename std::vector::const_reverse_iterator)path.rend(); ++i) { typename std::vector::const_reverse_iterator j = i; ++j; BOOST_ASSERT (get(color, *j) == color_gen::gray()); put(color, *j, color_gen::black()); put(pred, *j, *i); } } } } // Compute a uniformly-distributed spanning tree on a graph. Use Wilson's algorithm: // @inproceedings{wilson96generating, // author = {Wilson, David Bruce}, // title = {Generating random spanning trees more quickly than the cover time}, // booktitle = {STOC '96: Proceedings of the twenty-eighth annual ACM symposium on Theory of computing}, // year = {1996}, // isbn = {0-89791-785-5}, // pages = {296--303}, // location = {Philadelphia, Pennsylvania, United States}, // doi = {http://doi.acm.org/10.1145/237814.237880}, // publisher = {ACM}, // address = {New York, NY, USA}, // } // template void random_spanning_tree(const Graph& g, Gen& gen, typename graph_traits::vertex_descriptor root, PredMap pred, static_property_map, ColorMap color) { unweighted_random_out_edge_gen random_oe(gen); detail::random_spanning_tree_internal(g, root, pred, color, random_oe); } // Compute a weight-distributed spanning tree on a graph. template void random_spanning_tree(const Graph& g, Gen& gen, typename graph_traits::vertex_descriptor root, PredMap pred, WeightMap weight, ColorMap color) { weighted_random_out_edge_gen random_oe(weight, gen); detail::random_spanning_tree_internal(g, root, pred, color, random_oe); } template void random_spanning_tree(const Graph& g, Gen& gen, const bgl_named_params& params) { using namespace boost::graph::keywords; typedef bgl_named_params params_type; BOOST_GRAPH_DECLARE_CONVERTED_PARAMETERS(params_type, params) random_spanning_tree(g, gen, arg_pack[_root_vertex | *vertices(g).first], arg_pack[_predecessor_map], arg_pack[_weight_map | static_property_map(1.)], boost::detail::make_color_map_from_arg_pack(g, arg_pack)); } } #include #endif // BOOST_GRAPH_RANDOM_SPANNING_TREE_HPP