ssca_graph_generator.hpp 6.4 KB

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