Chris@16
|
1 //=======================================================================
|
Chris@16
|
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
3 // Copyright 2004, 2005 Trustees of Indiana University
|
Chris@16
|
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek,
|
Chris@16
|
5 // Doug Gregor, D. Kevin McGrath
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //=======================================================================
|
Chris@16
|
11 #ifndef BOOST_GRAPH_CUTHILL_MCKEE_HPP
|
Chris@16
|
12 #define BOOST_GRAPH_CUTHILL_MCKEE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <boost/graph/detail/sparse_ordering.hpp>
|
Chris@16
|
16 #include <boost/graph/graph_utility.hpp>
|
Chris@16
|
17 #include <algorithm>
|
Chris@16
|
18
|
Chris@16
|
19
|
Chris@16
|
20 /*
|
Chris@16
|
21 (Reverse) Cuthill-McKee Algorithm for matrix reordering
|
Chris@16
|
22 */
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27
|
Chris@16
|
28
|
Chris@16
|
29
|
Chris@16
|
30 template < typename OutputIterator, typename Buffer, typename DegreeMap >
|
Chris@16
|
31 class bfs_rcm_visitor:public default_bfs_visitor
|
Chris@16
|
32 {
|
Chris@16
|
33 public:
|
Chris@16
|
34 bfs_rcm_visitor(OutputIterator *iter, Buffer *b, DegreeMap deg):
|
Chris@16
|
35 permutation(iter), Qptr(b), degree(deg) { }
|
Chris@16
|
36 template <class Vertex, class Graph>
|
Chris@16
|
37 void examine_vertex(Vertex u, Graph&) {
|
Chris@16
|
38 *(*permutation)++ = u;
|
Chris@16
|
39 index_begin = Qptr->size();
|
Chris@16
|
40 }
|
Chris@16
|
41 template <class Vertex, class Graph>
|
Chris@16
|
42 void finish_vertex(Vertex, Graph&) {
|
Chris@16
|
43 using std::sort;
|
Chris@16
|
44
|
Chris@16
|
45 typedef typename property_traits<DegreeMap>::value_type ds_type;
|
Chris@16
|
46
|
Chris@16
|
47 typedef indirect_cmp<DegreeMap, std::less<ds_type> > Compare;
|
Chris@16
|
48 Compare comp(degree);
|
Chris@16
|
49
|
Chris@16
|
50 sort(Qptr->begin()+index_begin, Qptr->end(), comp);
|
Chris@16
|
51 }
|
Chris@16
|
52 protected:
|
Chris@16
|
53 OutputIterator *permutation;
|
Chris@16
|
54 int index_begin;
|
Chris@16
|
55 Buffer *Qptr;
|
Chris@16
|
56 DegreeMap degree;
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 } // namespace detail
|
Chris@16
|
60
|
Chris@16
|
61
|
Chris@16
|
62 // Reverse Cuthill-McKee algorithm with a given starting Vertex.
|
Chris@16
|
63 //
|
Chris@16
|
64 // If user provides a reverse iterator, this will be a reverse-cuthill-mckee
|
Chris@16
|
65 // algorithm, otherwise it will be a standard CM algorithm
|
Chris@16
|
66
|
Chris@16
|
67 template <class Graph, class OutputIterator,
|
Chris@16
|
68 class ColorMap, class DegreeMap>
|
Chris@16
|
69 OutputIterator
|
Chris@16
|
70 cuthill_mckee_ordering(const Graph& g,
|
Chris@16
|
71 std::deque< typename
|
Chris@16
|
72 graph_traits<Graph>::vertex_descriptor > vertex_queue,
|
Chris@16
|
73 OutputIterator permutation,
|
Chris@16
|
74 ColorMap color, DegreeMap degree)
|
Chris@16
|
75 {
|
Chris@16
|
76
|
Chris@16
|
77 //create queue, visitor...don't forget namespaces!
|
Chris@16
|
78 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
79 typedef typename boost::sparse::sparse_ordering_queue<Vertex> queue;
|
Chris@16
|
80 typedef typename detail::bfs_rcm_visitor<OutputIterator, queue, DegreeMap> Visitor;
|
Chris@16
|
81 typedef typename property_traits<ColorMap>::value_type ColorValue;
|
Chris@16
|
82 typedef color_traits<ColorValue> Color;
|
Chris@16
|
83
|
Chris@16
|
84
|
Chris@16
|
85 queue Q;
|
Chris@16
|
86
|
Chris@16
|
87 //create a bfs_rcm_visitor as defined above
|
Chris@16
|
88 Visitor vis(&permutation, &Q, degree);
|
Chris@16
|
89
|
Chris@16
|
90 typename graph_traits<Graph>::vertex_iterator ui, ui_end;
|
Chris@16
|
91
|
Chris@16
|
92 // Copy degree to pseudo_degree
|
Chris@16
|
93 // initialize the color map
|
Chris@16
|
94 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui){
|
Chris@16
|
95 put(color, *ui, Color::white());
|
Chris@16
|
96 }
|
Chris@16
|
97
|
Chris@16
|
98
|
Chris@16
|
99 while( !vertex_queue.empty() ) {
|
Chris@16
|
100 Vertex s = vertex_queue.front();
|
Chris@16
|
101 vertex_queue.pop_front();
|
Chris@16
|
102
|
Chris@16
|
103 //call BFS with visitor
|
Chris@16
|
104 breadth_first_visit(g, s, Q, vis, color);
|
Chris@16
|
105 }
|
Chris@16
|
106 return permutation;
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109
|
Chris@16
|
110 // This is the case where only a single starting vertex is supplied.
|
Chris@16
|
111 template <class Graph, class OutputIterator,
|
Chris@16
|
112 class ColorMap, class DegreeMap>
|
Chris@16
|
113 OutputIterator
|
Chris@16
|
114 cuthill_mckee_ordering(const Graph& g,
|
Chris@16
|
115 typename graph_traits<Graph>::vertex_descriptor s,
|
Chris@16
|
116 OutputIterator permutation,
|
Chris@16
|
117 ColorMap color, DegreeMap degree)
|
Chris@16
|
118 {
|
Chris@16
|
119
|
Chris@16
|
120 std::deque< typename graph_traits<Graph>::vertex_descriptor > vertex_queue;
|
Chris@16
|
121 vertex_queue.push_front( s );
|
Chris@16
|
122
|
Chris@16
|
123 return cuthill_mckee_ordering(g, vertex_queue, permutation, color, degree);
|
Chris@16
|
124
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127
|
Chris@16
|
128 // This is the version of CM which selects its own starting vertex
|
Chris@16
|
129 template < class Graph, class OutputIterator,
|
Chris@16
|
130 class ColorMap, class DegreeMap>
|
Chris@16
|
131 OutputIterator
|
Chris@16
|
132 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
|
Chris@16
|
133 ColorMap color, DegreeMap degree)
|
Chris@16
|
134 {
|
Chris@16
|
135 if (boost::graph::has_no_vertices(G))
|
Chris@16
|
136 return permutation;
|
Chris@16
|
137
|
Chris@16
|
138 typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
139 typedef typename property_traits<ColorMap>::value_type ColorValue;
|
Chris@16
|
140 typedef color_traits<ColorValue> Color;
|
Chris@16
|
141
|
Chris@16
|
142 std::deque<Vertex> vertex_queue;
|
Chris@16
|
143
|
Chris@16
|
144 // Mark everything white
|
Chris@16
|
145 BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white());
|
Chris@16
|
146
|
Chris@16
|
147 // Find one vertex from each connected component
|
Chris@16
|
148 BGL_FORALL_VERTICES_T(v, G, Graph) {
|
Chris@16
|
149 if (get(color, v) == Color::white()) {
|
Chris@16
|
150 depth_first_visit(G, v, dfs_visitor<>(), color);
|
Chris@16
|
151 vertex_queue.push_back(v);
|
Chris@16
|
152 }
|
Chris@16
|
153 }
|
Chris@16
|
154
|
Chris@16
|
155 // Find starting nodes for all vertices
|
Chris@16
|
156 // TBD: How to do this with a directed graph?
|
Chris@16
|
157 for (typename std::deque<Vertex>::iterator i = vertex_queue.begin();
|
Chris@16
|
158 i != vertex_queue.end(); ++i)
|
Chris@16
|
159 *i = find_starting_node(G, *i, color, degree);
|
Chris@16
|
160
|
Chris@16
|
161 return cuthill_mckee_ordering(G, vertex_queue, permutation,
|
Chris@16
|
162 color, degree);
|
Chris@16
|
163 }
|
Chris@16
|
164
|
Chris@16
|
165 template<typename Graph, typename OutputIterator, typename VertexIndexMap>
|
Chris@16
|
166 OutputIterator
|
Chris@16
|
167 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation,
|
Chris@16
|
168 VertexIndexMap index_map)
|
Chris@16
|
169 {
|
Chris@16
|
170 if (boost::graph::has_no_vertices(G))
|
Chris@16
|
171 return permutation;
|
Chris@16
|
172
|
Chris@16
|
173 std::vector<default_color_type> colors(num_vertices(G));
|
Chris@16
|
174 return cuthill_mckee_ordering(G, permutation,
|
Chris@16
|
175 make_iterator_property_map(&colors[0],
|
Chris@16
|
176 index_map,
|
Chris@16
|
177 colors[0]),
|
Chris@16
|
178 make_out_degree_map(G));
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 template<typename Graph, typename OutputIterator>
|
Chris@16
|
182 inline OutputIterator
|
Chris@16
|
183 cuthill_mckee_ordering(const Graph& G, OutputIterator permutation)
|
Chris@16
|
184 { return cuthill_mckee_ordering(G, permutation, get(vertex_index, G)); }
|
Chris@16
|
185 } // namespace boost
|
Chris@16
|
186
|
Chris@16
|
187
|
Chris@16
|
188 #endif // BOOST_GRAPH_CUTHILL_MCKEE_HPP
|