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