sparse_ordering.hpp 6.0 KB

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