random.hpp 9.0 KB

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