king_ordering.hpp 11 KB

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