cuthill_mckee_ordering.hpp 5.9 KB

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