cuthill_mckee_ordering.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_CUTHILL_MCKEE_HPP
  12. #define BOOST_GRAPH_CUTHILL_MCKEE_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/graph/detail/sparse_ordering.hpp>
  15. #include <boost/graph/graph_utility.hpp>
  16. #include <algorithm>
  17. /*
  18. (Reverse) Cuthill-McKee Algorithm for matrix reordering
  19. */
  20. namespace boost {
  21. namespace detail {
  22. template < typename OutputIterator, typename Buffer, typename DegreeMap >
  23. class bfs_rcm_visitor:public default_bfs_visitor
  24. {
  25. public:
  26. bfs_rcm_visitor(OutputIterator *iter, Buffer *b, DegreeMap deg):
  27. permutation(iter), Qptr(b), degree(deg) { }
  28. template <class Vertex, class Graph>
  29. void examine_vertex(Vertex u, Graph&) {
  30. *(*permutation)++ = u;
  31. index_begin = Qptr->size();
  32. }
  33. template <class Vertex, class Graph>
  34. void finish_vertex(Vertex, Graph&) {
  35. using std::sort;
  36. typedef typename property_traits<DegreeMap>::value_type ds_type;
  37. typedef indirect_cmp<DegreeMap, std::less<ds_type> > Compare;
  38. Compare comp(degree);
  39. sort(Qptr->begin()+index_begin, Qptr->end(), comp);
  40. }
  41. protected:
  42. OutputIterator *permutation;
  43. int index_begin;
  44. Buffer *Qptr;
  45. DegreeMap degree;
  46. };
  47. } // namespace detail
  48. // Reverse Cuthill-McKee algorithm with a given starting Vertex.
  49. //
  50. // If user provides a reverse iterator, this will be a reverse-cuthill-mckee
  51. // algorithm, otherwise it will be a standard CM algorithm
  52. template <class Graph, class OutputIterator,
  53. class ColorMap, class DegreeMap>
  54. OutputIterator
  55. cuthill_mckee_ordering(const Graph& g,
  56. std::deque< typename
  57. graph_traits<Graph>::vertex_descriptor > vertex_queue,
  58. OutputIterator permutation,
  59. ColorMap color, DegreeMap degree)
  60. {
  61. //create queue, visitor...don't forget namespaces!
  62. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  63. typedef typename boost::sparse::sparse_ordering_queue<Vertex> queue;
  64. typedef typename detail::bfs_rcm_visitor<OutputIterator, queue, DegreeMap> Visitor;
  65. typedef typename property_traits<ColorMap>::value_type ColorValue;
  66. typedef color_traits<ColorValue> Color;
  67. queue Q;
  68. //create a bfs_rcm_visitor as defined above
  69. Visitor vis(&permutation, &Q, degree);
  70. typename graph_traits<Graph>::vertex_iterator ui, ui_end;
  71. // Copy degree to pseudo_degree
  72. // initialize the color map
  73. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui){
  74. put(color, *ui, Color::white());
  75. }
  76. while( !vertex_queue.empty() ) {
  77. Vertex s = vertex_queue.front();
  78. vertex_queue.pop_front();
  79. //call BFS with visitor
  80. breadth_first_visit(g, s, Q, vis, color);
  81. }
  82. return permutation;
  83. }
  84. // This is the case where only a single starting vertex is supplied.
  85. template <class Graph, class OutputIterator,
  86. class ColorMap, class DegreeMap>
  87. OutputIterator
  88. cuthill_mckee_ordering(const Graph& g,
  89. typename graph_traits<Graph>::vertex_descriptor s,
  90. OutputIterator permutation,
  91. ColorMap color, DegreeMap degree)
  92. {
  93. std::deque< typename graph_traits<Graph>::vertex_descriptor > vertex_queue;
  94. vertex_queue.push_front( s );
  95. return cuthill_mckee_ordering(g, vertex_queue, permutation, color, degree);
  96. }
  97. // This is the version of CM which selects its own starting vertex
  98. template < class Graph, class OutputIterator,
  99. class ColorMap, class DegreeMap>
  100. OutputIterator
  101. cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
  102. ColorMap color, DegreeMap degree)
  103. {
  104. if (boost::graph::has_no_vertices(G))
  105. return permutation;
  106. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  107. typedef typename property_traits<ColorMap>::value_type ColorValue;
  108. typedef color_traits<ColorValue> Color;
  109. std::deque<Vertex> vertex_queue;
  110. // Mark everything white
  111. BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white());
  112. // Find one vertex from each connected component
  113. BGL_FORALL_VERTICES_T(v, G, Graph) {
  114. if (get(color, v) == Color::white()) {
  115. depth_first_visit(G, v, dfs_visitor<>(), color);
  116. vertex_queue.push_back(v);
  117. }
  118. }
  119. // Find starting nodes for all vertices
  120. // TBD: How to do this with a directed graph?
  121. for (typename std::deque<Vertex>::iterator i = vertex_queue.begin();
  122. i != vertex_queue.end(); ++i)
  123. *i = find_starting_node(G, *i, color, degree);
  124. return cuthill_mckee_ordering(G, vertex_queue, permutation,
  125. color, degree);
  126. }
  127. template<typename Graph, typename OutputIterator, typename VertexIndexMap>
  128. OutputIterator
  129. cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
  130. VertexIndexMap index_map)
  131. {
  132. if (boost::graph::has_no_vertices(G))
  133. return permutation;
  134. std::vector<default_color_type> colors(num_vertices(G));
  135. return cuthill_mckee_ordering(G, permutation,
  136. make_iterator_property_map(&colors[0],
  137. index_map,
  138. colors[0]),
  139. make_out_degree_map(G));
  140. }
  141. template<typename Graph, typename OutputIterator>
  142. inline OutputIterator
  143. cuthill_mckee_ordering(const Graph& G, OutputIterator permutation)
  144. { return cuthill_mckee_ordering(G, permutation, get(vertex_index, G)); }
  145. } // namespace boost
  146. #endif // BOOST_GRAPH_CUTHILL_MCKEE_HPP