annotate DEPENDENCIES/generic/include/boost/graph/random.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 //=======================================================================
Chris@16 2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
Chris@16 3 // Copyright (C) Vladimir Prus 2003
Chris@16 4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
Chris@16 5 //
Chris@16 6 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 7 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 8 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 //=======================================================================
Chris@16 10 #ifndef BOOST_GRAPH_RANDOM_HPP
Chris@16 11 #define BOOST_GRAPH_RANDOM_HPP
Chris@16 12
Chris@16 13 #include <boost/graph/graph_traits.hpp>
Chris@16 14 #include <boost/random/uniform_int.hpp>
Chris@16 15 #include <boost/random/uniform_real.hpp>
Chris@16 16 #include <boost/random/variate_generator.hpp>
Chris@16 17
Chris@16 18 #include <boost/pending/property.hpp>
Chris@16 19 #include <boost/graph/properties.hpp>
Chris@16 20 #include <boost/graph/iteration_macros.hpp>
Chris@16 21 #include <boost/next_prior.hpp>
Chris@16 22
Chris@16 23 #include <boost/graph/adjacency_list.hpp>
Chris@16 24 #include <boost/graph/copy.hpp>
Chris@16 25 #include <boost/mpl/if.hpp>
Chris@16 26 #include <boost/type_traits/is_convertible.hpp>
Chris@16 27
Chris@16 28 #include <iostream>
Chris@16 29 #include <boost/assert.hpp>
Chris@16 30
Chris@16 31 namespace boost {
Chris@16 32
Chris@16 33 // grab a random vertex from the graph's vertex set
Chris@16 34 template <class Graph, class RandomNumGen>
Chris@16 35 typename graph_traits<Graph>::vertex_descriptor
Chris@16 36 random_vertex(Graph& g, RandomNumGen& gen)
Chris@16 37 {
Chris@16 38 if (num_vertices(g) > 1) {
Chris@16 39 #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581))
Chris@16 40 std::size_t n = std::random( num_vertices(g) );
Chris@16 41 #else
Chris@16 42 uniform_int<> distrib(0, num_vertices(g)-1);
Chris@16 43 variate_generator<RandomNumGen&, uniform_int<> > rand_gen(gen, distrib);
Chris@16 44 std::size_t n = rand_gen();
Chris@16 45 #endif
Chris@16 46 typename graph_traits<Graph>::vertex_iterator
Chris@16 47 i = vertices(g).first;
Chris@16 48 return *(boost::next(i, n));
Chris@16 49 } else
Chris@16 50 return *vertices(g).first;
Chris@16 51 }
Chris@16 52
Chris@16 53 template <class Graph, class RandomNumGen>
Chris@16 54 typename graph_traits<Graph>::edge_descriptor
Chris@16 55 random_edge(Graph& g, RandomNumGen& gen) {
Chris@16 56 if (num_edges(g) > 1) {
Chris@16 57 #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581))
Chris@16 58 typename graph_traits<Graph>::edges_size_type
Chris@16 59 n = std::random( num_edges(g) );
Chris@16 60 #else
Chris@16 61 uniform_int<> distrib(0, num_edges(g)-1);
Chris@16 62 variate_generator<RandomNumGen&, uniform_int<> > rand_gen(gen, distrib);
Chris@16 63 typename graph_traits<Graph>::edges_size_type
Chris@16 64 n = rand_gen();
Chris@16 65 #endif
Chris@16 66 typename graph_traits<Graph>::edge_iterator
Chris@16 67 i = edges(g).first;
Chris@16 68 return *(boost::next(i, n));
Chris@16 69 } else
Chris@16 70 return *edges(g).first;
Chris@16 71 }
Chris@16 72
Chris@16 73 template <typename Graph, typename RandomNumGen>
Chris@16 74 typename graph_traits<Graph>::edge_descriptor
Chris@16 75 random_out_edge(Graph& g, typename graph_traits<Graph>::vertex_descriptor src, RandomNumGen& gen) {
Chris@16 76 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
Chris@16 77 typedef boost::uniform_int<degree_size_type> ui_type;
Chris@16 78 ui_type ui(0, out_degree(src, g) - 1);
Chris@16 79 boost::variate_generator<RandomNumGen&, ui_type>
Chris@16 80 variate(gen, ui);
Chris@16 81 typename graph_traits<Graph>::out_edge_iterator it = out_edges(src, g).first;
Chris@16 82 std::advance(it, variate());
Chris@16 83 return *it;
Chris@16 84 }
Chris@16 85
Chris@16 86 template <typename Graph, typename WeightMap, typename RandomNumGen>
Chris@16 87 typename graph_traits<Graph>::edge_descriptor
Chris@16 88 weighted_random_out_edge(Graph& g, typename graph_traits<Graph>::vertex_descriptor src, WeightMap weight, RandomNumGen& gen) {
Chris@16 89 typedef typename property_traits<WeightMap>::value_type weight_type;
Chris@16 90 weight_type weight_sum(0);
Chris@16 91 BGL_FORALL_OUTEDGES_T(src, e, g, Graph) {weight_sum += get(weight, e);}
Chris@16 92 typedef boost::uniform_real<> ur_type;
Chris@16 93 ur_type ur(0, weight_sum);
Chris@16 94 boost::variate_generator<RandomNumGen&, ur_type>
Chris@16 95 variate(gen, ur);
Chris@16 96 weight_type chosen_weight = variate();
Chris@16 97 BGL_FORALL_OUTEDGES_T(src, e, g, Graph) {
Chris@16 98 weight_type w = get(weight, e);
Chris@16 99 if (chosen_weight < w) {
Chris@16 100 return e;
Chris@16 101 } else {
Chris@16 102 chosen_weight -= w;
Chris@16 103 }
Chris@16 104 }
Chris@16 105 BOOST_ASSERT (false); // Should not get here
Chris@16 106 }
Chris@16 107
Chris@16 108 namespace detail {
Chris@16 109 class dummy_property_copier {
Chris@16 110 public:
Chris@16 111 template<class V1, class V2>
Chris@16 112 void operator()(const V1&, const V2&) const {}
Chris@16 113 };
Chris@16 114 }
Chris@16 115
Chris@16 116 template <typename MutableGraph, class RandNumGen>
Chris@16 117 void generate_random_graph1
Chris@16 118 (MutableGraph& g,
Chris@16 119 typename graph_traits<MutableGraph>::vertices_size_type V,
Chris@16 120 typename graph_traits<MutableGraph>::vertices_size_type E,
Chris@16 121 RandNumGen& gen,
Chris@16 122 bool allow_parallel = true,
Chris@16 123 bool self_edges = false)
Chris@16 124 {
Chris@16 125 typedef graph_traits<MutableGraph> Traits;
Chris@16 126 typedef typename Traits::edge_descriptor edge_t;
Chris@16 127 typedef typename Traits::vertices_size_type v_size_t;
Chris@16 128 typedef typename Traits::edges_size_type e_size_t;
Chris@16 129 typedef typename Traits::vertex_descriptor vertex_descriptor;
Chris@16 130
Chris@16 131 // When parallel edges are not allowed, we create a new graph which
Chris@16 132 // does not allow parallel edges, construct it and copy back.
Chris@16 133 // This is not efficient if 'g' already disallow parallel edges,
Chris@16 134 // but that's task for later.
Chris@16 135 if (!allow_parallel) {
Chris@16 136
Chris@16 137 typedef typename boost::graph_traits<MutableGraph>::directed_category dir;
Chris@16 138 typedef typename mpl::if_<is_convertible<dir, directed_tag>,
Chris@16 139 directedS, undirectedS>::type select;
Chris@16 140 adjacency_list<setS, vecS, select> g2;
Chris@16 141 generate_random_graph1(g2, V, E, gen, true, self_edges);
Chris@16 142
Chris@16 143 copy_graph(g2, g, vertex_copy(detail::dummy_property_copier()).
Chris@16 144 edge_copy(detail::dummy_property_copier()));
Chris@16 145
Chris@16 146 } else {
Chris@16 147
Chris@16 148 for (v_size_t i = 0; i < V; ++i)
Chris@16 149 add_vertex(g);
Chris@16 150
Chris@16 151 e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */
Chris@16 152 e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g);
Chris@16 153 for (e_size_t j = 0; j < E; /* Increment in body */) {
Chris@16 154 vertex_descriptor a = random_vertex(g, gen), b;
Chris@16 155 do {
Chris@16 156 b = random_vertex(g, gen);
Chris@16 157 } while (self_edges == false && a == b);
Chris@16 158 edge_t e; bool inserted;
Chris@16 159 boost::tie(e, inserted) = add_edge(a, b, g);
Chris@16 160 if (inserted) {
Chris@16 161 ++j;
Chris@16 162 } else {
Chris@16 163 ++not_inserted_counter;
Chris@16 164 }
Chris@16 165 if (not_inserted_counter >= num_vertices_squared) {
Chris@16 166 return; /* Rather than looping forever on complete graph */
Chris@16 167 }
Chris@16 168 }
Chris@16 169 }
Chris@16 170 }
Chris@16 171
Chris@16 172 template <typename MutableGraph, class RandNumGen>
Chris@16 173 void generate_random_graph
Chris@16 174 (MutableGraph& g,
Chris@16 175 typename graph_traits<MutableGraph>::vertices_size_type V,
Chris@16 176 typename graph_traits<MutableGraph>::vertices_size_type E,
Chris@16 177 RandNumGen& gen,
Chris@16 178 bool allow_parallel = true,
Chris@16 179 bool self_edges = false)
Chris@16 180 {
Chris@16 181 generate_random_graph1(g, V, E, gen, allow_parallel, self_edges);
Chris@16 182 }
Chris@16 183
Chris@16 184 template <typename MutableGraph, typename RandNumGen,
Chris@16 185 typename VertexOutputIterator, typename EdgeOutputIterator>
Chris@16 186 void generate_random_graph
Chris@16 187 (MutableGraph& g,
Chris@16 188 typename graph_traits<MutableGraph>::vertices_size_type V,
Chris@16 189 typename graph_traits<MutableGraph>::vertices_size_type E,
Chris@16 190 RandNumGen& gen,
Chris@16 191 VertexOutputIterator vertex_out,
Chris@16 192 EdgeOutputIterator edge_out,
Chris@16 193 bool self_edges = false)
Chris@16 194 {
Chris@16 195 typedef graph_traits<MutableGraph> Traits;
Chris@16 196 typedef typename Traits::vertices_size_type v_size_t;
Chris@16 197 typedef typename Traits::edges_size_type e_size_t;
Chris@16 198 typedef typename Traits::vertex_descriptor vertex_t;
Chris@16 199 typedef typename Traits::edge_descriptor edge_t;
Chris@16 200
Chris@16 201 for (v_size_t i = 0; i < V; ++i)
Chris@16 202 *vertex_out++ = add_vertex(g);
Chris@16 203
Chris@16 204 e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */
Chris@16 205 e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g);
Chris@16 206 for (e_size_t j = 0; j < E; /* Increment in body */) {
Chris@16 207 vertex_t a = random_vertex(g, gen), b;
Chris@16 208 do {
Chris@16 209 b = random_vertex(g, gen);
Chris@16 210 } while (self_edges == false && a == b);
Chris@16 211 edge_t e; bool inserted;
Chris@16 212 boost::tie(e, inserted) = add_edge(a, b, g);
Chris@16 213 if (inserted) {
Chris@16 214 *edge_out++ = std::make_pair(source(e, g), target(e, g));
Chris@16 215 ++j;
Chris@16 216 } else {
Chris@16 217 ++not_inserted_counter;
Chris@16 218 }
Chris@16 219 if (not_inserted_counter >= num_vertices_squared) {
Chris@16 220 return; /* Rather than looping forever on complete graph */
Chris@16 221 }
Chris@16 222 }
Chris@16 223 }
Chris@16 224
Chris@16 225 namespace detail {
Chris@16 226
Chris@16 227 template<class Property, class G, class RandomGenerator>
Chris@16 228 void randomize_property(G& g, RandomGenerator& rg,
Chris@16 229 Property, vertex_property_tag)
Chris@16 230 {
Chris@16 231 typename property_map<G, Property>::type pm = get(Property(), g);
Chris@16 232 typename graph_traits<G>::vertex_iterator vi, ve;
Chris@16 233 for (boost::tie(vi, ve) = vertices(g); vi != ve; ++vi) {
Chris@16 234 pm[*vi] = rg();
Chris@16 235 }
Chris@16 236 }
Chris@16 237
Chris@16 238 template<class Property, class G, class RandomGenerator>
Chris@16 239 void randomize_property(G& g, RandomGenerator& rg,
Chris@16 240 Property, edge_property_tag)
Chris@16 241 {
Chris@16 242 typename property_map<G, Property>::type pm = get(Property(), g);
Chris@16 243 typename graph_traits<G>::edge_iterator ei, ee;
Chris@16 244 for (boost::tie(ei, ee) = edges(g); ei != ee; ++ei) {
Chris@16 245 pm[*ei] = rg();
Chris@16 246 }
Chris@16 247 }
Chris@16 248 }
Chris@16 249
Chris@16 250 template<class Property, class G, class RandomGenerator>
Chris@16 251 void randomize_property(G& g, RandomGenerator& rg)
Chris@16 252 {
Chris@16 253 detail::randomize_property
Chris@16 254 (g, rg, Property(), typename property_kind<Property>::type());
Chris@16 255 }
Chris@16 256
Chris@16 257
Chris@16 258
Chris@16 259
Chris@16 260 }
Chris@16 261
Chris@16 262 #include <boost/graph/iteration_macros_undef.hpp>
Chris@16 263
Chris@16 264 #endif