random.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. return typename graph_traits<Graph>::edge_descriptor();
  98. }
  99. namespace detail {
  100. class dummy_property_copier {
  101. public:
  102. template<class V1, class V2>
  103. void operator()(const V1&, const V2&) const {}
  104. };
  105. }
  106. template <typename MutableGraph, class RandNumGen>
  107. void generate_random_graph1
  108. (MutableGraph& g,
  109. typename graph_traits<MutableGraph>::vertices_size_type V,
  110. typename graph_traits<MutableGraph>::vertices_size_type E,
  111. RandNumGen& gen,
  112. bool allow_parallel = true,
  113. bool self_edges = false)
  114. {
  115. typedef graph_traits<MutableGraph> Traits;
  116. typedef typename Traits::edge_descriptor edge_t;
  117. typedef typename Traits::vertices_size_type v_size_t;
  118. typedef typename Traits::edges_size_type e_size_t;
  119. typedef typename Traits::vertex_descriptor vertex_descriptor;
  120. // When parallel edges are not allowed, we create a new graph which
  121. // does not allow parallel edges, construct it and copy back.
  122. // This is not efficient if 'g' already disallow parallel edges,
  123. // but that's task for later.
  124. if (!allow_parallel) {
  125. typedef typename boost::graph_traits<MutableGraph>::directed_category dir;
  126. typedef typename mpl::if_<is_convertible<dir, directed_tag>,
  127. directedS, undirectedS>::type select;
  128. adjacency_list<setS, vecS, select> g2;
  129. generate_random_graph1(g2, V, E, gen, true, self_edges);
  130. copy_graph(g2, g, vertex_copy(detail::dummy_property_copier()).
  131. edge_copy(detail::dummy_property_copier()));
  132. } else {
  133. for (v_size_t i = 0; i < V; ++i)
  134. add_vertex(g);
  135. e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */
  136. e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g);
  137. for (e_size_t j = 0; j < E; /* Increment in body */) {
  138. vertex_descriptor a = random_vertex(g, gen), b;
  139. do {
  140. b = random_vertex(g, gen);
  141. } while (self_edges == false && a == b);
  142. edge_t e; bool inserted;
  143. boost::tie(e, inserted) = add_edge(a, b, g);
  144. if (inserted) {
  145. ++j;
  146. } else {
  147. ++not_inserted_counter;
  148. }
  149. if (not_inserted_counter >= num_vertices_squared) {
  150. return; /* Rather than looping forever on complete graph */
  151. }
  152. }
  153. }
  154. }
  155. template <typename MutableGraph, class RandNumGen>
  156. void generate_random_graph
  157. (MutableGraph& g,
  158. typename graph_traits<MutableGraph>::vertices_size_type V,
  159. typename graph_traits<MutableGraph>::vertices_size_type E,
  160. RandNumGen& gen,
  161. bool allow_parallel = true,
  162. bool self_edges = false)
  163. {
  164. generate_random_graph1(g, V, E, gen, allow_parallel, self_edges);
  165. }
  166. template <typename MutableGraph, typename RandNumGen,
  167. typename VertexOutputIterator, typename EdgeOutputIterator>
  168. void generate_random_graph
  169. (MutableGraph& g,
  170. typename graph_traits<MutableGraph>::vertices_size_type V,
  171. typename graph_traits<MutableGraph>::vertices_size_type E,
  172. RandNumGen& gen,
  173. VertexOutputIterator vertex_out,
  174. EdgeOutputIterator edge_out,
  175. bool self_edges = false)
  176. {
  177. typedef graph_traits<MutableGraph> Traits;
  178. typedef typename Traits::vertices_size_type v_size_t;
  179. typedef typename Traits::edges_size_type e_size_t;
  180. typedef typename Traits::vertex_descriptor vertex_t;
  181. typedef typename Traits::edge_descriptor edge_t;
  182. for (v_size_t i = 0; i < V; ++i)
  183. *vertex_out++ = add_vertex(g);
  184. e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */
  185. e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g);
  186. for (e_size_t j = 0; j < E; /* Increment in body */) {
  187. vertex_t a = random_vertex(g, gen), b;
  188. do {
  189. b = random_vertex(g, gen);
  190. } while (self_edges == false && a == b);
  191. edge_t e; bool inserted;
  192. boost::tie(e, inserted) = add_edge(a, b, g);
  193. if (inserted) {
  194. *edge_out++ = std::make_pair(source(e, g), target(e, g));
  195. ++j;
  196. } else {
  197. ++not_inserted_counter;
  198. }
  199. if (not_inserted_counter >= num_vertices_squared) {
  200. return; /* Rather than looping forever on complete graph */
  201. }
  202. }
  203. }
  204. namespace detail {
  205. template<class Property, class G, class RandomGenerator>
  206. void randomize_property(G& g, RandomGenerator& rg,
  207. Property, vertex_property_tag)
  208. {
  209. typename property_map<G, Property>::type pm = get(Property(), g);
  210. typename graph_traits<G>::vertex_iterator vi, ve;
  211. for (boost::tie(vi, ve) = vertices(g); vi != ve; ++vi) {
  212. pm[*vi] = rg();
  213. }
  214. }
  215. template<class Property, class G, class RandomGenerator>
  216. void randomize_property(G& g, RandomGenerator& rg,
  217. Property, edge_property_tag)
  218. {
  219. typename property_map<G, Property>::type pm = get(Property(), g);
  220. typename graph_traits<G>::edge_iterator ei, ee;
  221. for (boost::tie(ei, ee) = edges(g); ei != ee; ++ei) {
  222. pm[*ei] = rg();
  223. }
  224. }
  225. }
  226. template<class Property, class G, class RandomGenerator>
  227. void randomize_property(G& g, RandomGenerator& rg)
  228. {
  229. detail::randomize_property
  230. (g, rg, Property(), typename property_kind<Property>::type());
  231. }
  232. }
  233. #include <boost/graph/iteration_macros_undef.hpp>
  234. #endif