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
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
14 #include <boost/config.hpp>
15 #include <boost/graph/detail/sparse_ordering.hpp>
16 #include <boost/graph/graph_utility.hpp>
21 (Reverse) Cuthill-McKee Algorithm for matrix reordering
30 template < typename OutputIterator, typename Buffer, typename DegreeMap >
31 class bfs_rcm_visitor:public default_bfs_visitor
34 bfs_rcm_visitor(OutputIterator *iter, Buffer *b, DegreeMap deg):
35 permutation(iter), Qptr(b), degree(deg) { }
36 template <class Vertex, class Graph>
37 void examine_vertex(Vertex u, Graph&) {
38 *(*permutation)++ = u;
39 index_begin = Qptr->size();
41 template <class Vertex, class Graph>
42 void finish_vertex(Vertex, Graph&) {
45 typedef typename property_traits<DegreeMap>::value_type ds_type;
47 typedef indirect_cmp<DegreeMap, std::less<ds_type> > Compare;
50 sort(Qptr->begin()+index_begin, Qptr->end(), comp);
53 OutputIterator *permutation;
62 // Reverse Cuthill-McKee algorithm with a given starting Vertex.
64 // If user provides a reverse iterator, this will be a reverse-cuthill-mckee
65 // algorithm, otherwise it will be a standard CM algorithm
67 template <class Graph, class OutputIterator,
68 class ColorMap, class DegreeMap>
70 cuthill_mckee_ordering(const Graph& g,
72 graph_traits<Graph>::vertex_descriptor > vertex_queue,
73 OutputIterator permutation,
74 ColorMap color, DegreeMap degree)
77 //create queue, visitor...don't forget namespaces!
78 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
79 typedef typename boost::sparse::sparse_ordering_queue<Vertex> queue;
80 typedef typename detail::bfs_rcm_visitor<OutputIterator, queue, DegreeMap> Visitor;
81 typedef typename property_traits<ColorMap>::value_type ColorValue;
82 typedef color_traits<ColorValue> Color;
87 //create a bfs_rcm_visitor as defined above
88 Visitor vis(&permutation, &Q, degree);
90 typename graph_traits<Graph>::vertex_iterator ui, ui_end;
92 // Copy degree to pseudo_degree
93 // initialize the color map
94 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui){
95 put(color, *ui, Color::white());
99 while( !vertex_queue.empty() ) {
100 Vertex s = vertex_queue.front();
101 vertex_queue.pop_front();
103 //call BFS with visitor
104 breadth_first_visit(g, s, Q, vis, color);
110 // This is the case where only a single starting vertex is supplied.
111 template <class Graph, class OutputIterator,
112 class ColorMap, class DegreeMap>
114 cuthill_mckee_ordering(const Graph& g,
115 typename graph_traits<Graph>::vertex_descriptor s,
116 OutputIterator permutation,
117 ColorMap color, DegreeMap degree)
120 std::deque< typename graph_traits<Graph>::vertex_descriptor > vertex_queue;
121 vertex_queue.push_front( s );
123 return cuthill_mckee_ordering(g, vertex_queue, permutation, color, degree);
128 // This is the version of CM which selects its own starting vertex
129 template < class Graph, class OutputIterator,
130 class ColorMap, class DegreeMap>
132 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
133 ColorMap color, DegreeMap degree)
135 if (boost::graph::has_no_vertices(G))
138 typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
139 typedef typename property_traits<ColorMap>::value_type ColorValue;
140 typedef color_traits<ColorValue> Color;
142 std::deque<Vertex> vertex_queue;
144 // Mark everything white
145 BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white());
147 // Find one vertex from each connected component
148 BGL_FORALL_VERTICES_T(v, G, Graph) {
149 if (get(color, v) == Color::white()) {
150 depth_first_visit(G, v, dfs_visitor<>(), color);
151 vertex_queue.push_back(v);
155 // Find starting nodes for all vertices
156 // TBD: How to do this with a directed graph?
157 for (typename std::deque<Vertex>::iterator i = vertex_queue.begin();
158 i != vertex_queue.end(); ++i)
159 *i = find_starting_node(G, *i, color, degree);
161 return cuthill_mckee_ordering(G, vertex_queue, permutation,
165 template<typename Graph, typename OutputIterator, typename VertexIndexMap>
167 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
168 VertexIndexMap index_map)
170 if (boost::graph::has_no_vertices(G))
173 std::vector<default_color_type> colors(num_vertices(G));
174 return cuthill_mckee_ordering(G, permutation,
175 make_iterator_property_map(&colors[0],
178 make_out_degree_map(G));
181 template<typename Graph, typename OutputIterator>
182 inline OutputIterator
183 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation)
184 { return cuthill_mckee_ordering(G, permutation, get(vertex_index, G)); }
188 #endif // BOOST_GRAPH_CUTHILL_MCKEE_HPP