Chris@16: //======================================================================= Chris@16: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. Chris@16: // Copyright (C) Vladimir Prus 2003 Chris@16: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: #ifndef BOOST_GRAPH_RANDOM_HPP Chris@16: #define BOOST_GRAPH_RANDOM_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: // grab a random vertex from the graph's vertex set Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: random_vertex(Graph& g, RandomNumGen& gen) Chris@16: { Chris@16: if (num_vertices(g) > 1) { Chris@16: #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581)) Chris@16: std::size_t n = std::random( num_vertices(g) ); Chris@16: #else Chris@16: uniform_int<> distrib(0, num_vertices(g)-1); Chris@16: variate_generator > rand_gen(gen, distrib); Chris@16: std::size_t n = rand_gen(); Chris@16: #endif Chris@16: typename graph_traits::vertex_iterator Chris@16: i = vertices(g).first; Chris@16: return *(boost::next(i, n)); Chris@16: } else Chris@16: return *vertices(g).first; Chris@16: } Chris@16: Chris@16: template Chris@16: typename graph_traits::edge_descriptor Chris@16: random_edge(Graph& g, RandomNumGen& gen) { Chris@16: if (num_edges(g) > 1) { Chris@16: #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581)) Chris@16: typename graph_traits::edges_size_type Chris@16: n = std::random( num_edges(g) ); Chris@16: #else Chris@16: uniform_int<> distrib(0, num_edges(g)-1); Chris@16: variate_generator > rand_gen(gen, distrib); Chris@16: typename graph_traits::edges_size_type Chris@16: n = rand_gen(); Chris@16: #endif Chris@16: typename graph_traits::edge_iterator Chris@16: i = edges(g).first; Chris@16: return *(boost::next(i, n)); Chris@16: } else Chris@16: return *edges(g).first; Chris@16: } Chris@16: Chris@16: template Chris@16: typename graph_traits::edge_descriptor Chris@16: random_out_edge(Graph& g, typename graph_traits::vertex_descriptor src, RandomNumGen& gen) { Chris@16: typedef typename graph_traits::degree_size_type degree_size_type; Chris@16: typedef boost::uniform_int ui_type; Chris@16: ui_type ui(0, out_degree(src, g) - 1); Chris@16: boost::variate_generator Chris@16: variate(gen, ui); Chris@16: typename graph_traits::out_edge_iterator it = out_edges(src, g).first; Chris@16: std::advance(it, variate()); Chris@16: return *it; Chris@16: } Chris@16: Chris@16: template Chris@16: typename graph_traits::edge_descriptor Chris@16: weighted_random_out_edge(Graph& g, typename graph_traits::vertex_descriptor src, WeightMap weight, RandomNumGen& gen) { Chris@16: typedef typename property_traits::value_type weight_type; Chris@16: weight_type weight_sum(0); Chris@16: BGL_FORALL_OUTEDGES_T(src, e, g, Graph) {weight_sum += get(weight, e);} Chris@16: typedef boost::uniform_real<> ur_type; Chris@16: ur_type ur(0, weight_sum); Chris@16: boost::variate_generator Chris@16: variate(gen, ur); Chris@16: weight_type chosen_weight = variate(); Chris@16: BGL_FORALL_OUTEDGES_T(src, e, g, Graph) { Chris@16: weight_type w = get(weight, e); Chris@16: if (chosen_weight < w) { Chris@16: return e; Chris@16: } else { Chris@16: chosen_weight -= w; Chris@16: } Chris@16: } Chris@16: BOOST_ASSERT (false); // Should not get here Chris@16: } Chris@16: Chris@16: namespace detail { Chris@16: class dummy_property_copier { Chris@16: public: Chris@16: template Chris@16: void operator()(const V1&, const V2&) const {} Chris@16: }; Chris@16: } Chris@16: Chris@16: template Chris@16: void generate_random_graph1 Chris@16: (MutableGraph& g, Chris@16: typename graph_traits::vertices_size_type V, Chris@16: typename graph_traits::vertices_size_type E, Chris@16: RandNumGen& gen, Chris@16: bool allow_parallel = true, Chris@16: bool self_edges = false) Chris@16: { Chris@16: typedef graph_traits Traits; Chris@16: typedef typename Traits::edge_descriptor edge_t; Chris@16: typedef typename Traits::vertices_size_type v_size_t; Chris@16: typedef typename Traits::edges_size_type e_size_t; Chris@16: typedef typename Traits::vertex_descriptor vertex_descriptor; Chris@16: Chris@16: // When parallel edges are not allowed, we create a new graph which Chris@16: // does not allow parallel edges, construct it and copy back. Chris@16: // This is not efficient if 'g' already disallow parallel edges, Chris@16: // but that's task for later. Chris@16: if (!allow_parallel) { Chris@16: Chris@16: typedef typename boost::graph_traits::directed_category dir; Chris@16: typedef typename mpl::if_, Chris@16: directedS, undirectedS>::type select; Chris@16: adjacency_list g2; Chris@16: generate_random_graph1(g2, V, E, gen, true, self_edges); Chris@16: Chris@16: copy_graph(g2, g, vertex_copy(detail::dummy_property_copier()). Chris@16: edge_copy(detail::dummy_property_copier())); Chris@16: Chris@16: } else { Chris@16: Chris@16: for (v_size_t i = 0; i < V; ++i) Chris@16: add_vertex(g); Chris@16: Chris@16: e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */ Chris@16: e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g); Chris@16: for (e_size_t j = 0; j < E; /* Increment in body */) { Chris@16: vertex_descriptor a = random_vertex(g, gen), b; Chris@16: do { Chris@16: b = random_vertex(g, gen); Chris@16: } while (self_edges == false && a == b); Chris@16: edge_t e; bool inserted; Chris@16: boost::tie(e, inserted) = add_edge(a, b, g); Chris@16: if (inserted) { Chris@16: ++j; Chris@16: } else { Chris@16: ++not_inserted_counter; Chris@16: } Chris@16: if (not_inserted_counter >= num_vertices_squared) { Chris@16: return; /* Rather than looping forever on complete graph */ Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void generate_random_graph Chris@16: (MutableGraph& g, Chris@16: typename graph_traits::vertices_size_type V, Chris@16: typename graph_traits::vertices_size_type E, Chris@16: RandNumGen& gen, Chris@16: bool allow_parallel = true, Chris@16: bool self_edges = false) Chris@16: { Chris@16: generate_random_graph1(g, V, E, gen, allow_parallel, self_edges); Chris@16: } Chris@16: Chris@16: template Chris@16: void generate_random_graph Chris@16: (MutableGraph& g, Chris@16: typename graph_traits::vertices_size_type V, Chris@16: typename graph_traits::vertices_size_type E, Chris@16: RandNumGen& gen, Chris@16: VertexOutputIterator vertex_out, Chris@16: EdgeOutputIterator edge_out, Chris@16: bool self_edges = false) Chris@16: { Chris@16: typedef graph_traits Traits; Chris@16: typedef typename Traits::vertices_size_type v_size_t; Chris@16: typedef typename Traits::edges_size_type e_size_t; Chris@16: typedef typename Traits::vertex_descriptor vertex_t; Chris@16: typedef typename Traits::edge_descriptor edge_t; Chris@16: Chris@16: for (v_size_t i = 0; i < V; ++i) Chris@16: *vertex_out++ = add_vertex(g); Chris@16: Chris@16: e_size_t not_inserted_counter = 0; /* Number of edge insertion failures */ Chris@16: e_size_t num_vertices_squared = num_vertices(g) * num_vertices(g); Chris@16: for (e_size_t j = 0; j < E; /* Increment in body */) { Chris@16: vertex_t a = random_vertex(g, gen), b; Chris@16: do { Chris@16: b = random_vertex(g, gen); Chris@16: } while (self_edges == false && a == b); Chris@16: edge_t e; bool inserted; Chris@16: boost::tie(e, inserted) = add_edge(a, b, g); Chris@16: if (inserted) { Chris@16: *edge_out++ = std::make_pair(source(e, g), target(e, g)); Chris@16: ++j; Chris@16: } else { Chris@16: ++not_inserted_counter; Chris@16: } Chris@16: if (not_inserted_counter >= num_vertices_squared) { Chris@16: return; /* Rather than looping forever on complete graph */ Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: void randomize_property(G& g, RandomGenerator& rg, Chris@16: Property, vertex_property_tag) Chris@16: { Chris@16: typename property_map::type pm = get(Property(), g); Chris@16: typename graph_traits::vertex_iterator vi, ve; Chris@16: for (boost::tie(vi, ve) = vertices(g); vi != ve; ++vi) { Chris@16: pm[*vi] = rg(); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void randomize_property(G& g, RandomGenerator& rg, Chris@16: Property, edge_property_tag) Chris@16: { Chris@16: typename property_map::type pm = get(Property(), g); Chris@16: typename graph_traits::edge_iterator ei, ee; Chris@16: for (boost::tie(ei, ee) = edges(g); ei != ee; ++ei) { Chris@16: pm[*ei] = rg(); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void randomize_property(G& g, RandomGenerator& rg) Chris@16: { Chris@16: detail::randomize_property Chris@16: (g, rg, Property(), typename property_kind::type()); Chris@16: } Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: } Chris@16: Chris@16: #include Chris@16: Chris@16: #endif