random.hpp 9.4 KB

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