1 // Copyright 2004 The Trustees of Indiana University.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_GRAPH_SMALL_WORLD_GENERATOR_HPP
10 #define BOOST_GRAPH_SMALL_WORLD_GENERATOR_HPP
14 #include <boost/random/uniform_01.hpp>
15 #include <boost/random/uniform_int.hpp>
21 template < typename RandomGenerator, typename Graph > class small_world_iterator
24 typename graph_traits< Graph >::vertices_size_type vertices_size_type;
27 typedef std::input_iterator_tag iterator_category;
28 typedef std::pair< vertices_size_type, vertices_size_type > value_type;
29 typedef const value_type& reference;
30 typedef const value_type* pointer;
31 typedef void difference_type;
33 small_world_iterator() : gen(0) {}
34 small_world_iterator(RandomGenerator& gen, vertices_size_type n,
35 vertices_size_type k, double prob = 0.0, bool allow_self_loops = false)
41 , target(allow_self_loops ? 0 : 1)
42 , allow_self_loops(allow_self_loops)
43 , current(0, allow_self_loops ? 0 : 1)
47 reference operator*() const { return current; }
48 pointer operator->() const { return ¤t; }
50 small_world_iterator& operator++()
52 target = (target + 1) % n;
53 if (target == (source + k / 2 + 1) % n)
59 target = (source + 1) % n;
61 current.first = source;
63 uniform_01< RandomGenerator, double > rand01(*gen);
64 uniform_int< vertices_size_type > rand_vertex_gen(0, n - 1);
66 *gen = rand01.base(); // GRRRR
69 vertices_size_type lower = (source + n - k / 2) % n;
70 vertices_size_type upper = (source + k / 2) % n;
73 current.second = rand_vertex_gen(*gen);
74 } while ((current.second >= lower && current.second <= upper)
76 && (current.second >= lower || current.second <= upper)));
80 current.second = target;
85 small_world_iterator operator++(int)
87 small_world_iterator temp(*this);
92 bool operator==(const small_world_iterator& other) const
94 if (!gen && other.gen)
95 return other == *this;
96 else if (gen && !other.gen)
98 else if (!gen && !other.gen)
100 return source == other.source && target == other.target;
103 bool operator!=(const small_world_iterator& other) const
105 return !(*this == other);
111 uniform_int< vertices_size_type > rand_vertex(0, n - 1);
112 current.first = rand_vertex(*gen);
115 current.second = rand_vertex(*gen);
116 } while (current.first == current.second && !allow_self_loops);
119 RandomGenerator* gen;
120 vertices_size_type n;
121 vertices_size_type k;
123 vertices_size_type source;
124 vertices_size_type target;
125 bool allow_self_loops;
129 } // end namespace boost
131 #endif // BOOST_GRAPH_SMALL_WORLD_GENERATOR_HPP