sparse_ordering.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2004, 2005 Trustees of Indiana University
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek,
  5. // Doug Gregor, D. Kevin McGrath
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //=======================================================================//
  11. #ifndef BOOST_GRAPH_DETAIL_SPARSE_ORDERING_HPP
  12. #define BOOST_GRAPH_DETAIL_SPARSE_ORDERING_HPP
  13. #include <boost/config.hpp>
  14. #include <vector>
  15. #include <queue>
  16. #include <boost/pending/queue.hpp>
  17. #include <boost/pending/mutable_queue.hpp>
  18. #include <boost/graph/graph_traits.hpp>
  19. #include <boost/graph/breadth_first_search.hpp>
  20. #include <boost/graph/properties.hpp>
  21. #include <boost/pending/indirect_cmp.hpp>
  22. #include <boost/property_map/property_map.hpp>
  23. #include <boost/graph/iteration_macros.hpp>
  24. #include <boost/graph/depth_first_search.hpp>
  25. namespace boost
  26. {
  27. namespace sparse
  28. {
  29. // rcm_queue
  30. //
  31. // This is a custom queue type used in the
  32. // *_ordering algorithms.
  33. // In addition to the normal queue operations, the
  34. // rcm_queue provides:
  35. //
  36. // int eccentricity() const;
  37. // value_type spouse() const;
  38. //
  39. // yes, it's a bad name...but it works, so use it
  40. template < class Vertex, class DegreeMap,
  41. class Container = std::deque< Vertex > >
  42. class rcm_queue : public std::queue< Vertex, Container >
  43. {
  44. typedef std::queue< Vertex > base;
  45. public:
  46. typedef typename base::value_type value_type;
  47. typedef typename base::size_type size_type;
  48. /* SGI queue has not had a contructor queue(const Container&) */
  49. inline rcm_queue(DegreeMap deg)
  50. : _size(0), Qsize(1), eccen(-1), degree(deg)
  51. {
  52. }
  53. inline void pop()
  54. {
  55. if (!_size)
  56. Qsize = base::size();
  57. base::pop();
  58. if (_size == Qsize - 1)
  59. {
  60. _size = 0;
  61. ++eccen;
  62. }
  63. else
  64. ++_size;
  65. }
  66. inline value_type& front()
  67. {
  68. value_type& u = base::front();
  69. if (_size == 0)
  70. w = u;
  71. else if (get(degree, u) < get(degree, w))
  72. w = u;
  73. return u;
  74. }
  75. inline const value_type& front() const
  76. {
  77. const value_type& u = base::front();
  78. if (_size == 0)
  79. w = u;
  80. else if (get(degree, u) < get(degree, w))
  81. w = u;
  82. return u;
  83. }
  84. inline value_type& top() { return front(); }
  85. inline const value_type& top() const { return front(); }
  86. inline size_type size() const { return base::size(); }
  87. inline size_type eccentricity() const { return eccen; }
  88. inline value_type spouse() const { return w; }
  89. protected:
  90. size_type _size;
  91. size_type Qsize;
  92. int eccen;
  93. mutable value_type w;
  94. DegreeMap degree;
  95. };
  96. template < typename Tp, typename Sequence = std::deque< Tp > >
  97. class sparse_ordering_queue : public boost::queue< Tp, Sequence >
  98. {
  99. public:
  100. typedef typename Sequence::iterator iterator;
  101. typedef typename Sequence::reverse_iterator reverse_iterator;
  102. typedef queue< Tp, Sequence > base;
  103. typedef typename Sequence::size_type size_type;
  104. inline iterator begin() { return this->c.begin(); }
  105. inline reverse_iterator rbegin() { return this->c.rbegin(); }
  106. inline iterator end() { return this->c.end(); }
  107. inline reverse_iterator rend() { return this->c.rend(); }
  108. inline Tp& operator[](int n) { return this->c[n]; }
  109. inline size_type size() { return this->c.size(); }
  110. protected:
  111. // nothing
  112. };
  113. } // namespace sparse
  114. // Compute Pseudo peripheral
  115. //
  116. // To compute an approximated peripheral for a given vertex.
  117. // Used in <tt>king_ordering</tt> algorithm.
  118. //
  119. template < class Graph, class Vertex, class ColorMap, class DegreeMap >
  120. Vertex pseudo_peripheral_pair(
  121. Graph const& G, const Vertex& u, int& ecc, ColorMap color, DegreeMap degree)
  122. {
  123. typedef typename property_traits< ColorMap >::value_type ColorValue;
  124. typedef color_traits< ColorValue > Color;
  125. sparse::rcm_queue< Vertex, DegreeMap > Q(degree);
  126. typename boost::graph_traits< Graph >::vertex_iterator ui, ui_end;
  127. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  128. if (get(color, *ui) != Color::red())
  129. put(color, *ui, Color::white());
  130. breadth_first_visit(G, u, buffer(Q).color_map(color));
  131. ecc = Q.eccentricity();
  132. return Q.spouse();
  133. }
  134. // Find a good starting node
  135. //
  136. // This is to find a good starting node for the
  137. // king_ordering algorithm. "good" is in the sense
  138. // of the ordering generated by RCM.
  139. //
  140. template < class Graph, class Vertex, class Color, class Degree >
  141. Vertex find_starting_node(Graph const& G, Vertex r, Color color, Degree degree)
  142. {
  143. Vertex x, y;
  144. int eccen_r, eccen_x;
  145. x = pseudo_peripheral_pair(G, r, eccen_r, color, degree);
  146. y = pseudo_peripheral_pair(G, x, eccen_x, color, degree);
  147. while (eccen_x > eccen_r)
  148. {
  149. r = x;
  150. eccen_r = eccen_x;
  151. x = y;
  152. y = pseudo_peripheral_pair(G, x, eccen_x, color, degree);
  153. }
  154. return x;
  155. }
  156. template < typename Graph >
  157. class out_degree_property_map
  158. : public put_get_helper< typename graph_traits< Graph >::degree_size_type,
  159. out_degree_property_map< Graph > >
  160. {
  161. public:
  162. typedef typename graph_traits< Graph >::vertex_descriptor key_type;
  163. typedef typename graph_traits< Graph >::degree_size_type value_type;
  164. typedef value_type reference;
  165. typedef readable_property_map_tag category;
  166. out_degree_property_map(const Graph& g) : m_g(g) {}
  167. value_type operator[](const key_type& v) const
  168. {
  169. return out_degree(v, m_g);
  170. }
  171. private:
  172. const Graph& m_g;
  173. };
  174. template < typename Graph >
  175. inline out_degree_property_map< Graph > make_out_degree_map(const Graph& g)
  176. {
  177. return out_degree_property_map< Graph >(g);
  178. }
  179. } // namespace boost
  180. #endif // BOOST_GRAPH_KING_HPP