annotate DEPENDENCIES/generic/include/boost/graph/random_spanning_tree.hpp @ 118:770eb830ec19 emscripten

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