king_ordering.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_KING_HPP
  12. #define BOOST_GRAPH_KING_HPP
  13. #include <deque>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <boost/config.hpp>
  17. #include <boost/bind/bind.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <boost/graph/detail/sparse_ordering.hpp>
  20. #include <boost/graph/graph_utility.hpp>
  21. /*
  22. King Algorithm for matrix reordering
  23. */
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. template < typename OutputIterator, typename Buffer, typename Compare,
  29. typename PseudoDegreeMap, typename VecMap, typename VertexIndexMap >
  30. class bfs_king_visitor : public default_bfs_visitor
  31. {
  32. public:
  33. bfs_king_visitor(OutputIterator* iter, Buffer* b, Compare compare,
  34. PseudoDegreeMap deg, std::vector< int > loc, VecMap color,
  35. VertexIndexMap vertices)
  36. : permutation(iter)
  37. , Qptr(b)
  38. , degree(deg)
  39. , comp(compare)
  40. , Qlocation(loc)
  41. , colors(color)
  42. , vertex_map(vertices)
  43. {
  44. }
  45. template < typename Vertex, typename Graph >
  46. void finish_vertex(Vertex, Graph& g)
  47. {
  48. using namespace boost::placeholders;
  49. typename graph_traits< Graph >::out_edge_iterator ei, ei_end;
  50. Vertex v, w;
  51. typedef typename std::deque< Vertex >::reverse_iterator
  52. reverse_iterator;
  53. reverse_iterator rend = Qptr->rend() - index_begin;
  54. reverse_iterator rbegin = Qptr->rbegin();
  55. // heap the vertices already there
  56. std::make_heap(rbegin, rend, boost::bind< bool >(comp, _2, _1));
  57. unsigned i = 0;
  58. for (i = index_begin; i != Qptr->size(); ++i)
  59. {
  60. colors[get(vertex_map, (*Qptr)[i])] = 1;
  61. Qlocation[get(vertex_map, (*Qptr)[i])] = i;
  62. }
  63. i = 0;
  64. for (; rbegin != rend; rend--)
  65. {
  66. percolate_down< Vertex >(i);
  67. w = (*Qptr)[index_begin + i];
  68. for (boost::tie(ei, ei_end) = out_edges(w, g); ei != ei_end;
  69. ++ei)
  70. {
  71. v = target(*ei, g);
  72. put(degree, v, get(degree, v) - 1);
  73. if (colors[get(vertex_map, v)] == 1)
  74. {
  75. percolate_up< Vertex >(get(vertex_map, v), i);
  76. }
  77. }
  78. colors[get(vertex_map, w)] = 0;
  79. i++;
  80. }
  81. }
  82. template < typename Vertex, typename Graph >
  83. void examine_vertex(Vertex u, const Graph&)
  84. {
  85. *(*permutation)++ = u;
  86. index_begin = Qptr->size();
  87. }
  88. protected:
  89. // this function replaces pop_heap, and tracks state information
  90. template < typename Vertex > void percolate_down(int offset)
  91. {
  92. int heap_last = index_begin + offset;
  93. int heap_first = Qptr->size() - 1;
  94. // pop_heap functionality:
  95. // swap first, last
  96. std::swap((*Qptr)[heap_last], (*Qptr)[heap_first]);
  97. // swap in the location queue
  98. std::swap(Qlocation[heap_first], Qlocation[heap_last]);
  99. // set drifter, children
  100. int drifter = heap_first;
  101. int drifter_heap = Qptr->size() - drifter;
  102. int right_child_heap = drifter_heap * 2 + 1;
  103. int right_child = Qptr->size() - right_child_heap;
  104. int left_child_heap = drifter_heap * 2;
  105. int left_child = Qptr->size() - left_child_heap;
  106. // check that we are staying in the heap
  107. bool valid = (right_child < heap_last) ? false : true;
  108. // pick smallest child of drifter, and keep in mind there might only
  109. // be left child
  110. int smallest_child = (valid
  111. && get(degree, (*Qptr)[left_child])
  112. > get(degree, (*Qptr)[right_child]))
  113. ? right_child
  114. : left_child;
  115. while (valid && smallest_child < heap_last
  116. && comp((*Qptr)[drifter], (*Qptr)[smallest_child]))
  117. {
  118. // if smallest child smaller than drifter, swap them
  119. std::swap((*Qptr)[smallest_child], (*Qptr)[drifter]);
  120. std::swap(Qlocation[drifter], Qlocation[smallest_child]);
  121. // update the values, run again, as necessary
  122. drifter = smallest_child;
  123. drifter_heap = Qptr->size() - drifter;
  124. right_child_heap = drifter_heap * 2 + 1;
  125. right_child = Qptr->size() - right_child_heap;
  126. left_child_heap = drifter_heap * 2;
  127. left_child = Qptr->size() - left_child_heap;
  128. valid = (right_child < heap_last) ? false : true;
  129. smallest_child = (valid
  130. && get(degree, (*Qptr)[left_child])
  131. > get(degree, (*Qptr)[right_child]))
  132. ? right_child
  133. : left_child;
  134. }
  135. }
  136. // this is like percolate down, but we always compare against the
  137. // parent, as there is only a single choice
  138. template < typename Vertex > void percolate_up(int vertex, int offset)
  139. {
  140. int child_location = Qlocation[vertex];
  141. int heap_child_location = Qptr->size() - child_location;
  142. int heap_parent_location = (int)(heap_child_location / 2);
  143. unsigned parent_location = Qptr->size() - heap_parent_location;
  144. bool valid = (heap_parent_location != 0
  145. && child_location > index_begin + offset
  146. && parent_location < Qptr->size());
  147. while (valid
  148. && comp((*Qptr)[child_location], (*Qptr)[parent_location]))
  149. {
  150. // swap in the heap
  151. std::swap((*Qptr)[child_location], (*Qptr)[parent_location]);
  152. // swap in the location queue
  153. std::swap(
  154. Qlocation[child_location], Qlocation[parent_location]);
  155. child_location = parent_location;
  156. heap_child_location = heap_parent_location;
  157. heap_parent_location = (int)(heap_child_location / 2);
  158. parent_location = Qptr->size() - heap_parent_location;
  159. valid = (heap_parent_location != 0
  160. && child_location > index_begin + offset);
  161. }
  162. }
  163. OutputIterator* permutation;
  164. int index_begin;
  165. Buffer* Qptr;
  166. PseudoDegreeMap degree;
  167. Compare comp;
  168. std::vector< int > Qlocation;
  169. VecMap colors;
  170. VertexIndexMap vertex_map;
  171. };
  172. } // namespace detail
  173. template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
  174. typename VertexIndexMap >
  175. OutputIterator king_ordering(const Graph& g,
  176. std::deque< typename graph_traits< Graph >::vertex_descriptor >
  177. vertex_queue,
  178. OutputIterator permutation, ColorMap color, DegreeMap degree,
  179. VertexIndexMap index_map)
  180. {
  181. typedef typename property_traits< DegreeMap >::value_type ds_type;
  182. typedef typename property_traits< ColorMap >::value_type ColorValue;
  183. typedef color_traits< ColorValue > Color;
  184. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  185. typedef iterator_property_map< typename std::vector< ds_type >::iterator,
  186. VertexIndexMap, ds_type, ds_type& >
  187. PseudoDegreeMap;
  188. typedef indirect_cmp< PseudoDegreeMap, std::less< ds_type > > Compare;
  189. typedef typename boost::sparse::sparse_ordering_queue< Vertex > queue;
  190. typedef typename detail::bfs_king_visitor< OutputIterator, queue, Compare,
  191. PseudoDegreeMap, std::vector< int >, VertexIndexMap >
  192. Visitor;
  193. typedef
  194. typename graph_traits< Graph >::vertices_size_type vertices_size_type;
  195. std::vector< ds_type > pseudo_degree_vec(num_vertices(g));
  196. PseudoDegreeMap pseudo_degree(pseudo_degree_vec.begin(), index_map);
  197. typename graph_traits< Graph >::vertex_iterator ui, ui_end;
  198. queue Q;
  199. // Copy degree to pseudo_degree
  200. // initialize the color map
  201. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
  202. {
  203. put(pseudo_degree, *ui, get(degree, *ui));
  204. put(color, *ui, Color::white());
  205. }
  206. Compare comp(pseudo_degree);
  207. std::vector< int > colors(num_vertices(g));
  208. for (vertices_size_type i = 0; i < num_vertices(g); i++)
  209. colors[i] = 0;
  210. std::vector< int > loc(num_vertices(g));
  211. // create the visitor
  212. Visitor vis(&permutation, &Q, comp, pseudo_degree, loc, colors, index_map);
  213. while (!vertex_queue.empty())
  214. {
  215. Vertex s = vertex_queue.front();
  216. vertex_queue.pop_front();
  217. // call BFS with visitor
  218. breadth_first_visit(g, s, Q, vis, color);
  219. }
  220. return permutation;
  221. }
  222. // This is the case where only a single starting vertex is supplied.
  223. template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
  224. typename VertexIndexMap >
  225. OutputIterator king_ordering(const Graph& g,
  226. typename graph_traits< Graph >::vertex_descriptor s,
  227. OutputIterator permutation, ColorMap color, DegreeMap degree,
  228. VertexIndexMap index_map)
  229. {
  230. std::deque< typename graph_traits< Graph >::vertex_descriptor >
  231. vertex_queue;
  232. vertex_queue.push_front(s);
  233. return king_ordering(
  234. g, vertex_queue, permutation, color, degree, index_map);
  235. }
  236. template < class Graph, class OutputIterator, class ColorMap, class DegreeMap,
  237. class VertexIndexMap >
  238. OutputIterator king_ordering(const Graph& G, OutputIterator permutation,
  239. ColorMap color, DegreeMap degree, VertexIndexMap index_map)
  240. {
  241. if (has_no_vertices(G))
  242. return permutation;
  243. typedef typename boost::graph_traits< Graph >::vertex_descriptor Vertex;
  244. typedef typename property_traits< ColorMap >::value_type ColorValue;
  245. typedef color_traits< ColorValue > Color;
  246. std::deque< Vertex > vertex_queue;
  247. // Mark everything white
  248. BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white());
  249. // Find one vertex from each connected component
  250. BGL_FORALL_VERTICES_T(v, G, Graph)
  251. {
  252. if (get(color, v) == Color::white())
  253. {
  254. depth_first_visit(G, v, dfs_visitor<>(), color);
  255. vertex_queue.push_back(v);
  256. }
  257. }
  258. // Find starting nodes for all vertices
  259. // TBD: How to do this with a directed graph?
  260. for (typename std::deque< Vertex >::iterator i = vertex_queue.begin();
  261. i != vertex_queue.end(); ++i)
  262. *i = find_starting_node(G, *i, color, degree);
  263. return king_ordering(
  264. G, vertex_queue, permutation, color, degree, index_map);
  265. }
  266. template < typename Graph, typename OutputIterator, typename VertexIndexMap >
  267. OutputIterator king_ordering(
  268. const Graph& G, OutputIterator permutation, VertexIndexMap index_map)
  269. {
  270. if (has_no_vertices(G))
  271. return permutation;
  272. std::vector< default_color_type > colors(num_vertices(G));
  273. return king_ordering(G, permutation,
  274. make_iterator_property_map(&colors[0], index_map, colors[0]),
  275. make_out_degree_map(G), index_map);
  276. }
  277. template < typename Graph, typename OutputIterator >
  278. inline OutputIterator king_ordering(const Graph& G, OutputIterator permutation)
  279. {
  280. return king_ordering(G, permutation, get(vertex_index, G));
  281. }
  282. } // namespace boost
  283. #endif // BOOST_GRAPH_KING_HPP