PageRenderTime 33ms CodeModel.GetById 10ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/graph/random.hpp

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