king_ordering.hpp 12 KB

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