sparse_ordering.hpp 6.2 KB

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