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_KING_HPP
12 #define BOOST_GRAPH_KING_HPP
14 #include <boost/config.hpp>
15 #include <boost/graph/detail/sparse_ordering.hpp>
16 #include <boost/graph/graph_utility.hpp>
19 King Algorithm for matrix reordering
26 template < typename OutputIterator, typename Buffer, typename Compare,
27 typename PseudoDegreeMap, typename VecMap, typename VertexIndexMap >
28 class bfs_king_visitor : public default_bfs_visitor
31 bfs_king_visitor(OutputIterator* iter, Buffer* b, Compare compare,
32 PseudoDegreeMap deg, std::vector< int > loc, VecMap color,
33 VertexIndexMap vertices)
40 , vertex_map(vertices)
44 template < typename Vertex, typename Graph >
45 void finish_vertex(Vertex, Graph& g)
47 typename graph_traits< Graph >::out_edge_iterator ei, ei_end;
50 typedef typename std::deque< Vertex >::reverse_iterator
53 reverse_iterator rend = Qptr->rend() - index_begin;
54 reverse_iterator rbegin = Qptr->rbegin();
56 // heap the vertices already there
57 std::make_heap(rbegin, rend, boost::bind< bool >(comp, _2, _1));
61 for (i = index_begin; i != Qptr->size(); ++i)
63 colors[get(vertex_map, (*Qptr)[i])] = 1;
64 Qlocation[get(vertex_map, (*Qptr)[i])] = i;
69 for (; rbegin != rend; rend--)
71 percolate_down< Vertex >(i);
72 w = (*Qptr)[index_begin + i];
73 for (boost::tie(ei, ei_end) = out_edges(w, g); ei != ei_end;
77 put(degree, v, get(degree, v) - 1);
79 if (colors[get(vertex_map, v)] == 1)
81 percolate_up< Vertex >(get(vertex_map, v), i);
85 colors[get(vertex_map, w)] = 0;
90 template < typename Vertex, typename Graph >
91 void examine_vertex(Vertex u, const Graph&)
94 *(*permutation)++ = u;
95 index_begin = Qptr->size();
99 // this function replaces pop_heap, and tracks state information
100 template < typename Vertex > void percolate_down(int offset)
102 int heap_last = index_begin + offset;
103 int heap_first = Qptr->size() - 1;
105 // pop_heap functionality:
107 std::swap((*Qptr)[heap_last], (*Qptr)[heap_first]);
109 // swap in the location queue
110 std::swap(Qlocation[heap_first], Qlocation[heap_last]);
112 // set drifter, children
113 int drifter = heap_first;
114 int drifter_heap = Qptr->size() - drifter;
116 int right_child_heap = drifter_heap * 2 + 1;
117 int right_child = Qptr->size() - right_child_heap;
119 int left_child_heap = drifter_heap * 2;
120 int left_child = Qptr->size() - left_child_heap;
122 // check that we are staying in the heap
123 bool valid = (right_child < heap_last) ? false : true;
125 // pick smallest child of drifter, and keep in mind there might only
127 int smallest_child = (valid
128 && get(degree, (*Qptr)[left_child])
129 > get(degree, (*Qptr)[right_child]))
133 while (valid && smallest_child < heap_last
134 && comp((*Qptr)[drifter], (*Qptr)[smallest_child]))
137 // if smallest child smaller than drifter, swap them
138 std::swap((*Qptr)[smallest_child], (*Qptr)[drifter]);
139 std::swap(Qlocation[drifter], Qlocation[smallest_child]);
141 // update the values, run again, as necessary
142 drifter = smallest_child;
143 drifter_heap = Qptr->size() - drifter;
145 right_child_heap = drifter_heap * 2 + 1;
146 right_child = Qptr->size() - right_child_heap;
148 left_child_heap = drifter_heap * 2;
149 left_child = Qptr->size() - left_child_heap;
151 valid = (right_child < heap_last) ? false : true;
153 smallest_child = (valid
154 && get(degree, (*Qptr)[left_child])
155 > get(degree, (*Qptr)[right_child]))
161 // this is like percolate down, but we always compare against the
162 // parent, as there is only a single choice
163 template < typename Vertex > void percolate_up(int vertex, int offset)
166 int child_location = Qlocation[vertex];
167 int heap_child_location = Qptr->size() - child_location;
168 int heap_parent_location = (int)(heap_child_location / 2);
169 unsigned parent_location = Qptr->size() - heap_parent_location;
171 bool valid = (heap_parent_location != 0
172 && child_location > index_begin + offset
173 && parent_location < Qptr->size());
176 && comp((*Qptr)[child_location], (*Qptr)[parent_location]))
180 std::swap((*Qptr)[child_location], (*Qptr)[parent_location]);
182 // swap in the location queue
184 Qlocation[child_location], Qlocation[parent_location]);
186 child_location = parent_location;
187 heap_child_location = heap_parent_location;
188 heap_parent_location = (int)(heap_child_location / 2);
189 parent_location = Qptr->size() - heap_parent_location;
190 valid = (heap_parent_location != 0
191 && child_location > index_begin + offset);
195 OutputIterator* permutation;
198 PseudoDegreeMap degree;
200 std::vector< int > Qlocation;
202 VertexIndexMap vertex_map;
205 } // namespace detail
207 template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
208 typename VertexIndexMap >
209 OutputIterator king_ordering(const Graph& g,
210 std::deque< typename graph_traits< Graph >::vertex_descriptor >
212 OutputIterator permutation, ColorMap color, DegreeMap degree,
213 VertexIndexMap index_map)
215 typedef typename property_traits< DegreeMap >::value_type ds_type;
216 typedef typename property_traits< ColorMap >::value_type ColorValue;
217 typedef color_traits< ColorValue > Color;
218 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
219 typedef iterator_property_map< typename std::vector< ds_type >::iterator,
220 VertexIndexMap, ds_type, ds_type& >
222 typedef indirect_cmp< PseudoDegreeMap, std::less< ds_type > > Compare;
223 typedef typename boost::sparse::sparse_ordering_queue< Vertex > queue;
224 typedef typename detail::bfs_king_visitor< OutputIterator, queue, Compare,
225 PseudoDegreeMap, std::vector< int >, VertexIndexMap >
228 typename graph_traits< Graph >::vertices_size_type vertices_size_type;
229 std::vector< ds_type > pseudo_degree_vec(num_vertices(g));
230 PseudoDegreeMap pseudo_degree(pseudo_degree_vec.begin(), index_map);
232 typename graph_traits< Graph >::vertex_iterator ui, ui_end;
234 // Copy degree to pseudo_degree
235 // initialize the color map
236 for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
238 put(pseudo_degree, *ui, get(degree, *ui));
239 put(color, *ui, Color::white());
242 Compare comp(pseudo_degree);
243 std::vector< int > colors(num_vertices(g));
245 for (vertices_size_type i = 0; i < num_vertices(g); i++)
248 std::vector< int > loc(num_vertices(g));
250 // create the visitor
251 Visitor vis(&permutation, &Q, comp, pseudo_degree, loc, colors, index_map);
253 while (!vertex_queue.empty())
255 Vertex s = vertex_queue.front();
256 vertex_queue.pop_front();
258 // call BFS with visitor
259 breadth_first_visit(g, s, Q, vis, color);
265 // This is the case where only a single starting vertex is supplied.
266 template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
267 typename VertexIndexMap >
268 OutputIterator king_ordering(const Graph& g,
269 typename graph_traits< Graph >::vertex_descriptor s,
270 OutputIterator permutation, ColorMap color, DegreeMap degree,
271 VertexIndexMap index_map)
274 std::deque< typename graph_traits< Graph >::vertex_descriptor >
276 vertex_queue.push_front(s);
277 return king_ordering(
278 g, vertex_queue, permutation, color, degree, index_map);
281 template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
282 class VertexIndexMap >
283 OutputIterator king_ordering(const Graph& G, OutputIterator permutation,
284 ColorMap color, DegreeMap degree, VertexIndexMap index_map)
286 if (has_no_vertices(G))
289 typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
290 typedef typename property_traits< ColorMap >::value_type ColorValue;
291 typedef color_traits< ColorValue > Color;
293 std::deque< Vertex > vertex_queue;
295 // Mark everything white
296 BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white());
298 // Find one vertex from each connected component
299 BGL_FORALL_VERTICES_T(v, G, Graph)
301 if (get(color, v) == Color::white())
303 depth_first_visit(G, v, dfs_visitor<>(), color);
304 vertex_queue.push_back(v);
308 // Find starting nodes for all vertices
309 // TBD: How to do this with a directed graph?
310 for (typename std::deque< Vertex >::iterator i = vertex_queue.begin();
311 i != vertex_queue.end(); ++i)
312 *i = find_starting_node(G, *i, color, degree);
314 return king_ordering(
315 G, vertex_queue, permutation, color, degree, index_map);
318 template < typename Graph, typename OutputIterator, typename VertexIndexMap >
319 OutputIterator king_ordering(
320 const Graph& G, OutputIterator permutation, VertexIndexMap index_map)
322 if (has_no_vertices(G))
325 std::vector< default_color_type > colors(num_vertices(G));
326 return king_ordering(G, permutation,
327 make_iterator_property_map(&colors[0], index_map, colors[0]),
328 make_out_degree_map(G), index_map);
331 template < typename Graph, typename OutputIterator >
332 inline OutputIterator king_ordering(const Graph& G, OutputIterator permutation)
334 return king_ordering(G, permutation, get(vertex_index, G));
339 #endif // BOOST_GRAPH_KING_HPP