1 // Copyright 2004, 2005 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Nick Edmonds
9 #ifndef BOOST_GRAPH_RMAT_GENERATOR_HPP
10 #define BOOST_GRAPH_RMAT_GENERATOR_HPP
18 #include <boost/shared_ptr.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/random/uniform_int.hpp>
21 #include <boost/random/uniform_01.hpp>
22 #include <boost/graph/graph_traits.hpp>
23 #include <boost/type_traits/is_base_and_derived.hpp>
24 #include <boost/type_traits/is_same.hpp>
25 // #include <boost/test/floating_point_comparison.hpp>
27 using boost::shared_ptr;
28 using boost::uniform_01;
30 // Returns floor(log_2(n)), and -1 when n is 0
31 template <typename IntegerType>
32 inline int int_log2(IntegerType n) {
34 while (n > 0) {++l; n >>= 1;}
38 struct keep_all_edges {
40 bool operator()(const T&, const T&) { return true; }
43 template <typename Distribution, typename ProcessId>
44 struct keep_local_edges {
46 keep_local_edges(const Distribution& distrib, const ProcessId& id)
47 : distrib(distrib), id(id)
51 bool operator()(const T& x, const T& y)
52 { return distrib(x) == id || distrib(y) == id; }
55 const Distribution& distrib;
59 template <typename RandomGenerator, typename T>
61 generate_permutation_vector(RandomGenerator& gen, std::vector<T>& vertexPermutation, T n)
63 using boost::uniform_int;
65 vertexPermutation.resize(n);
67 // Generate permutation map of vertex numbers
68 uniform_int<T> rand_vertex(0, n-1);
69 for (T i = 0; i < n; ++i)
70 vertexPermutation[i] = i;
72 // Can't use std::random_shuffle unless we create another (synchronized) PRNG
73 for (T i = 0; i < n; ++i)
74 std::swap(vertexPermutation[i], vertexPermutation[rand_vertex(gen)]);
77 template <typename RandomGenerator, typename T>
79 generate_edge(shared_ptr<uniform_01<RandomGenerator> > prob, T n,
80 unsigned int SCALE, double a, double b, double c, double d)
84 for (unsigned int j = 0; j < SCALE; ++j) {
89 else if (p >= a && p < a + b)
91 else if (p >= a + b && p < a + b + c)
93 else { // p > a + b + c && p < a + b + c + d
100 // 0.2 and 0.9 are hardcoded in the reference SSCA implementation.
101 // The maximum change in any given value should be less than 10%
102 a *= 0.9 + 0.2 * (*prob)();
103 b *= 0.9 + 0.2 * (*prob)();
104 c *= 0.9 + 0.2 * (*prob)();
105 d *= 0.9 + 0.2 * (*prob)();
107 double S = a + b + c + d;
109 a /= S; b /= S; c /= S;
111 // Ensure all values add up to 1, regardless of floating point errors
115 return std::make_pair(u, v);
121 Chakrabarti's R-MAT scale free generator.
123 For all flavors of the R-MAT iterator a+b+c+d must equal 1 and for the
124 unique_rmat_iterator 'm' << 'n^2'. If 'm' is too close to 'n^2' the
125 generator may be unable to generate sufficient unique edges
127 To get a true scale free distribution {a, b, c, d : a > b, a > c, a > d}
130 template<typename RandomGenerator, typename Graph>
133 typedef typename graph_traits<Graph>::directed_category directed_category;
134 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
135 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
138 typedef std::input_iterator_tag iterator_category;
139 typedef std::pair<vertices_size_type, vertices_size_type> value_type;
140 typedef const value_type& reference;
141 typedef const value_type* pointer;
142 typedef std::ptrdiff_t difference_type; // Not used
144 // No argument constructor, set to terminating condition
148 // Initialize for edge generation
149 rmat_iterator(RandomGenerator& gen, vertices_size_type n,
150 edges_size_type m, double a, double b, double c,
151 double d, bool permute_vertices = true)
152 : gen(), n(n), a(a), b(b), c(c), d(d), edge(m),
153 permute_vertices(permute_vertices),
157 this->gen.reset(new uniform_01<RandomGenerator>(gen));
159 // BOOST_ASSERT(boost::test_tools::check_is_close(a + b + c + d, 1., 1.e-5));
161 if (permute_vertices)
162 generate_permutation_vector(gen, vertexPermutation, n);
164 // TODO: Generate the entire adjacency matrix then "Clip and flip" if undirected graph
166 // Generate the first edge
167 vertices_size_type u, v;
168 boost::tie(u, v) = generate_edge(this->gen, n, SCALE, a, b, c, d);
170 if (permute_vertices)
171 current = std::make_pair(vertexPermutation[u],
172 vertexPermutation[v]);
174 current = std::make_pair(u, v);
179 reference operator*() const { return current; }
180 pointer operator->() const { return ¤t; }
182 rmat_iterator& operator++()
184 vertices_size_type u, v;
185 boost::tie(u, v) = generate_edge(this->gen, n, SCALE, a, b, c, d);
187 if (permute_vertices)
188 current = std::make_pair(vertexPermutation[u],
189 vertexPermutation[v]);
191 current = std::make_pair(u, v);
198 rmat_iterator operator++(int)
200 rmat_iterator temp(*this);
205 bool operator==(const rmat_iterator& other) const
207 return edge == other.edge;
210 bool operator!=(const rmat_iterator& other) const
211 { return !(*this == other); }
216 shared_ptr<uniform_01<RandomGenerator> > gen;
217 vertices_size_type n;
220 bool permute_vertices;
223 // Internal data structures
224 std::vector<vertices_size_type> vertexPermutation;
228 // Sorted version for CSR
229 template <typename T>
231 bool operator() (const std::pair<T,T>& x, const std::pair<T,T>& y)
233 if (x.first == y.first)
234 return x.second > y.second;
236 return x.first > y.first;
240 template<typename RandomGenerator, typename Graph,
241 typename EdgePredicate = keep_all_edges>
242 class sorted_rmat_iterator
244 typedef typename graph_traits<Graph>::directed_category directed_category;
245 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
246 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
249 typedef std::input_iterator_tag iterator_category;
250 typedef std::pair<vertices_size_type, vertices_size_type> value_type;
251 typedef const value_type& reference;
252 typedef const value_type* pointer;
253 typedef std::ptrdiff_t difference_type; // Not used
255 // No argument constructor, set to terminating condition
256 sorted_rmat_iterator()
257 : gen(), values(sort_pair<vertices_size_type>()), done(true)
260 // Initialize for edge generation
261 sorted_rmat_iterator(RandomGenerator& gen, vertices_size_type n,
262 edges_size_type m, double a, double b, double c,
263 double d, bool permute_vertices = true,
264 EdgePredicate ep = keep_all_edges())
265 : gen(), permute_vertices(permute_vertices),
266 values(sort_pair<vertices_size_type>()), done(false)
269 // BOOST_ASSERT(boost::test_tools::check_is_close(a + b + c + d, 1., 1.e-5));
271 this->gen.reset(new uniform_01<RandomGenerator>(gen));
273 std::vector<vertices_size_type> vertexPermutation;
274 if (permute_vertices)
275 generate_permutation_vector(gen, vertexPermutation, n);
277 // TODO: "Clip and flip" if undirected graph
278 int SCALE = int_log2(n);
280 for (edges_size_type i = 0; i < m; ++i) {
282 vertices_size_type u, v;
283 boost::tie(u, v) = generate_edge(this->gen, n, SCALE, a, b, c, d);
285 if (permute_vertices) {
286 if (ep(vertexPermutation[u], vertexPermutation[v]))
287 values.push(std::make_pair(vertexPermutation[u], vertexPermutation[v]));
290 values.push(std::make_pair(u, v));
295 current = values.top();
299 reference operator*() const { return current; }
300 pointer operator->() const { return ¤t; }
302 sorted_rmat_iterator& operator++()
304 if (!values.empty()) {
305 current = values.top();
313 sorted_rmat_iterator operator++(int)
315 sorted_rmat_iterator temp(*this);
320 bool operator==(const sorted_rmat_iterator& other) const
322 return values.empty() && other.values.empty() && done && other.done;
325 bool operator!=(const sorted_rmat_iterator& other) const
326 { return !(*this == other); }
331 shared_ptr<uniform_01<RandomGenerator> > gen;
332 bool permute_vertices;
334 // Internal data structures
335 std::priority_queue<value_type, std::vector<value_type>, sort_pair<vertices_size_type> > values;
341 // This version is slow but guarantees unique edges
342 template<typename RandomGenerator, typename Graph,
343 typename EdgePredicate = keep_all_edges>
344 class unique_rmat_iterator
346 typedef typename graph_traits<Graph>::directed_category directed_category;
347 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
348 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
351 typedef std::input_iterator_tag iterator_category;
352 typedef std::pair<vertices_size_type, vertices_size_type> value_type;
353 typedef const value_type& reference;
354 typedef const value_type* pointer;
355 typedef std::ptrdiff_t difference_type; // Not used
357 // No argument constructor, set to terminating condition
358 unique_rmat_iterator()
362 // Initialize for edge generation
363 unique_rmat_iterator(RandomGenerator& gen, vertices_size_type n,
364 edges_size_type m, double a, double b, double c,
365 double d, bool permute_vertices = true,
366 EdgePredicate ep = keep_all_edges())
370 // BOOST_ASSERT(boost::test_tools::check_is_close(a + b + c + d, 1., 1.e-5));
372 this->gen.reset(new uniform_01<RandomGenerator>(gen));
374 std::vector<vertices_size_type> vertexPermutation;
375 if (permute_vertices)
376 generate_permutation_vector(gen, vertexPermutation, n);
378 int SCALE = int_log2(n);
380 std::map<value_type, bool> edge_map;
382 edges_size_type edges = 0;
384 vertices_size_type u, v;
385 boost::tie(u, v) = generate_edge(this->gen, n, SCALE, a, b, c, d);
387 // Lowest vertex number always comes first
388 // (this means we don't have to worry about i->j and j->i being in the edge list)
389 if (u > v && is_same<directed_category, undirected_tag>::value)
392 if (edge_map.find(std::make_pair(u, v)) == edge_map.end()) {
393 edge_map[std::make_pair(u, v)] = true;
395 if (permute_vertices) {
396 if (ep(vertexPermutation[u], vertexPermutation[v]))
397 values.push_back(std::make_pair(vertexPermutation[u], vertexPermutation[v]));
400 values.push_back(std::make_pair(u, v));
406 // NGE - Asking for more than n^2 edges will result in an infinite loop here
407 // Asking for a value too close to n^2 edges may as well
409 current = values.back();
413 reference operator*() const { return current; }
414 pointer operator->() const { return ¤t; }
416 unique_rmat_iterator& operator++()
418 if (!values.empty()) {
419 current = values.back();
427 unique_rmat_iterator operator++(int)
429 unique_rmat_iterator temp(*this);
434 bool operator==(const unique_rmat_iterator& other) const
436 return values.empty() && other.values.empty() && done && other.done;
439 bool operator!=(const unique_rmat_iterator& other) const
440 { return !(*this == other); }
445 shared_ptr<uniform_01<RandomGenerator> > gen;
447 // Internal data structures
448 std::vector<value_type> values;
453 // This version is slow but guarantees unique edges
454 template<typename RandomGenerator, typename Graph,
455 typename EdgePredicate = keep_all_edges>
456 class sorted_unique_rmat_iterator
458 typedef typename graph_traits<Graph>::directed_category directed_category;
459 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
460 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
463 typedef std::input_iterator_tag iterator_category;
464 typedef std::pair<vertices_size_type, vertices_size_type> value_type;
465 typedef const value_type& reference;
466 typedef const value_type* pointer;
467 typedef std::ptrdiff_t difference_type; // Not used
469 // No argument constructor, set to terminating condition
470 sorted_unique_rmat_iterator()
471 : gen(), values(sort_pair<vertices_size_type>()), done(true) { }
473 // Initialize for edge generation
474 sorted_unique_rmat_iterator(RandomGenerator& gen, vertices_size_type n,
475 edges_size_type m, double a, double b, double c,
476 double d, bool bidirectional = false,
477 bool permute_vertices = true,
478 EdgePredicate ep = keep_all_edges())
479 : gen(), bidirectional(bidirectional),
480 values(sort_pair<vertices_size_type>()), done(false)
483 // BOOST_ASSERT(boost::test_tools::check_is_close(a + b + c + d, 1., 1.e-5));
485 this->gen.reset(new uniform_01<RandomGenerator>(gen));
487 std::vector<vertices_size_type> vertexPermutation;
488 if (permute_vertices)
489 generate_permutation_vector(gen, vertexPermutation, n);
491 int SCALE = int_log2(n);
493 std::map<value_type, bool> edge_map;
495 edges_size_type edges = 0;
498 vertices_size_type u, v;
499 boost::tie(u, v) = generate_edge(this->gen, n, SCALE, a, b, c, d);
502 if (edge_map.find(std::make_pair(u, v)) == edge_map.end()) {
503 edge_map[std::make_pair(u, v)] = true;
504 edge_map[std::make_pair(v, u)] = true;
507 if (permute_vertices) {
508 values.push(std::make_pair(vertexPermutation[u], vertexPermutation[v]));
509 values.push(std::make_pair(vertexPermutation[v], vertexPermutation[u]));
511 values.push(std::make_pair(u, v));
512 values.push(std::make_pair(v, u));
519 // Lowest vertex number always comes first
520 // (this means we don't have to worry about i->j and j->i being in the edge list)
521 if (u > v && is_same<directed_category, undirected_tag>::value)
524 if (edge_map.find(std::make_pair(u, v)) == edge_map.end()) {
525 edge_map[std::make_pair(u, v)] = true;
527 if (permute_vertices) {
528 if (ep(vertexPermutation[u], vertexPermutation[v]))
529 values.push(std::make_pair(vertexPermutation[u], vertexPermutation[v]));
532 values.push(std::make_pair(u, v));
540 // NGE - Asking for more than n^2 edges will result in an infinite loop here
541 // Asking for a value too close to n^2 edges may as well
543 current = values.top();
547 reference operator*() const { return current; }
548 pointer operator->() const { return ¤t; }
550 sorted_unique_rmat_iterator& operator++()
552 if (!values.empty()) {
553 current = values.top();
561 sorted_unique_rmat_iterator operator++(int)
563 sorted_unique_rmat_iterator temp(*this);
568 bool operator==(const sorted_unique_rmat_iterator& other) const
570 return values.empty() && other.values.empty() && done && other.done;
573 bool operator!=(const sorted_unique_rmat_iterator& other) const
574 { return !(*this == other); }
579 shared_ptr<uniform_01<RandomGenerator> > gen;
582 // Internal data structures
583 std::priority_queue<value_type, std::vector<value_type>,
584 sort_pair<vertices_size_type> > values;
589 } // end namespace boost
591 #ifdef BOOST_GRAPH_USE_MPI
592 #include <boost/graph/distributed/rmat_graph_generator.hpp>
593 #endif // BOOST_GRAPH_USE_MPI
595 #endif // BOOST_GRAPH_RMAT_GENERATOR_HPP