ssca_graph_generator.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2004, 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Nick Edmonds
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_SSCA_GENERATOR_HPP
  8. #define BOOST_GRAPH_SSCA_GENERATOR_HPP
  9. #include <iterator>
  10. #include <utility>
  11. #include <vector>
  12. #include <queue>
  13. #include <boost/config.hpp>
  14. #include <boost/random/uniform_int.hpp>
  15. #include <boost/random/uniform_01.hpp>
  16. #include <boost/graph/graph_traits.hpp>
  17. #include <boost/type_traits/is_base_and_derived.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. enum Direction
  20. {
  21. FORWARD = 1,
  22. BACKWARD = 2,
  23. BOTH = FORWARD | BACKWARD
  24. };
  25. namespace boost
  26. {
  27. // This generator generates graphs according to the method specified
  28. // in SSCA 1.1. Current versions of SSCA use R-MAT graphs
  29. template < typename RandomGenerator, typename Graph > class ssca_iterator
  30. {
  31. typedef typename graph_traits< Graph >::directed_category directed_category;
  32. typedef
  33. typename graph_traits< Graph >::vertices_size_type vertices_size_type;
  34. public:
  35. typedef std::input_iterator_tag iterator_category;
  36. typedef std::pair< vertices_size_type, vertices_size_type > value_type;
  37. typedef const value_type& reference;
  38. typedef const value_type* pointer;
  39. typedef void difference_type;
  40. // No argument constructor, set to terminating condition
  41. ssca_iterator() : gen(), verticesRemaining(0) {}
  42. // Initialize for edge generation
  43. ssca_iterator(RandomGenerator& gen, vertices_size_type totVertices,
  44. vertices_size_type maxCliqueSize, double probUnidirectional,
  45. int maxParallelEdges, double probIntercliqueEdges)
  46. : gen(&gen)
  47. , totVertices(totVertices)
  48. , maxCliqueSize(maxCliqueSize)
  49. , probUnidirectional(probUnidirectional)
  50. , maxParallelEdges(maxParallelEdges)
  51. , probIntercliqueEdges(probIntercliqueEdges)
  52. , currentClique(0)
  53. , verticesRemaining(totVertices)
  54. {
  55. cliqueNum = std::vector< int >(totVertices, -1);
  56. current = std::make_pair(0, 0);
  57. }
  58. reference operator*() const { return current; }
  59. pointer operator->() const { return &current; }
  60. ssca_iterator& operator++()
  61. {
  62. BOOST_USING_STD_MIN();
  63. while (values.empty() && verticesRemaining > 0)
  64. { // If there are no values left, generate a new clique
  65. uniform_int< vertices_size_type > clique_size(1, maxCliqueSize);
  66. uniform_int< vertices_size_type > rand_vertex(0, totVertices - 1);
  67. uniform_int< int > num_parallel_edges(1, maxParallelEdges);
  68. uniform_int< short > direction(0, 1);
  69. uniform_01< RandomGenerator > prob(*gen);
  70. std::vector< vertices_size_type > cliqueVertices;
  71. cliqueVertices.clear();
  72. vertices_size_type size = min BOOST_PREVENT_MACRO_SUBSTITUTION(
  73. clique_size(*gen), verticesRemaining);
  74. while (cliqueVertices.size() < size)
  75. {
  76. vertices_size_type v = rand_vertex(*gen);
  77. if (cliqueNum[v] == -1)
  78. {
  79. cliqueNum[v] = currentClique;
  80. cliqueVertices.push_back(v);
  81. verticesRemaining--;
  82. }
  83. } // Nick: This is inefficient when only a few vertices remain...
  84. // I should probably just select the remaining vertices
  85. // in order when only a certain fraction remain.
  86. typename std::vector< vertices_size_type >::iterator first, second;
  87. for (first = cliqueVertices.begin(); first != cliqueVertices.end();
  88. ++first)
  89. for (second = first + 1; second != cliqueVertices.end();
  90. ++second)
  91. {
  92. Direction d;
  93. int edges;
  94. d = prob() < probUnidirectional
  95. ? (direction(*gen) == 0 ? FORWARD : BACKWARD)
  96. : BOTH;
  97. if (d & FORWARD)
  98. {
  99. edges = num_parallel_edges(*gen);
  100. for (int i = 0; i < edges; ++i)
  101. values.push(std::make_pair(*first, *second));
  102. }
  103. if (d & BACKWARD)
  104. {
  105. edges = num_parallel_edges(*gen);
  106. for (int i = 0; i < edges; ++i)
  107. values.push(std::make_pair(*second, *first));
  108. }
  109. }
  110. if (verticesRemaining == 0)
  111. {
  112. // Generate interclique edges
  113. for (vertices_size_type i = 0; i < totVertices; ++i)
  114. {
  115. double p = probIntercliqueEdges;
  116. for (vertices_size_type d = 2; d < totVertices / 2;
  117. d *= 2, p /= 2)
  118. {
  119. vertices_size_type j = (i + d) % totVertices;
  120. if (cliqueNum[j] != cliqueNum[i] && prob() < p)
  121. {
  122. int edges = num_parallel_edges(*gen);
  123. for (int i = 0; i < edges; ++i)
  124. values.push(std::make_pair(i, j));
  125. }
  126. }
  127. }
  128. }
  129. currentClique++;
  130. }
  131. if (!values.empty())
  132. { // If we're not done return a value
  133. current = values.front();
  134. values.pop();
  135. }
  136. return *this;
  137. }
  138. ssca_iterator operator++(int)
  139. {
  140. ssca_iterator temp(*this);
  141. ++(*this);
  142. return temp;
  143. }
  144. bool operator==(const ssca_iterator& other) const
  145. {
  146. return verticesRemaining == other.verticesRemaining && values.empty()
  147. && other.values.empty();
  148. }
  149. bool operator!=(const ssca_iterator& other) const
  150. {
  151. return !(*this == other);
  152. }
  153. private:
  154. // Parameters
  155. RandomGenerator* gen;
  156. vertices_size_type totVertices;
  157. vertices_size_type maxCliqueSize;
  158. double probUnidirectional;
  159. int maxParallelEdges;
  160. double probIntercliqueEdges;
  161. // Internal data structures
  162. std::vector< int > cliqueNum;
  163. std::queue< value_type > values;
  164. int currentClique;
  165. vertices_size_type verticesRemaining;
  166. value_type current;
  167. };
  168. } // end namespace boost
  169. #endif // BOOST_GRAPH_SSCA_GENERATOR_HPP