smallest_last_ordering.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. // Revision History:
  10. // 17 March 2006: Fixed a bug: when updating the degree a vertex
  11. // could be moved to a wrong bucket. (Roman Dementiev)
  12. //
  13. #ifndef BOOST_SMALLEST_LAST_VERTEX_ORDERING_HPP
  14. #define BOOST_SMALLEST_LAST_VERTEX_ORDERING_HPP
  15. /*
  16. The smallest-last ordering is defined for the loopless graph G with
  17. vertices a(j), j = 1,2,...,n where a(j) is the j-th column of A and
  18. with edge (a(i),a(j)) if and only if columns i and j have a
  19. non-zero in the same row position. The smallest-last ordering is
  20. determined recursively by letting list(k), k = n,...,1 be a column
  21. with least degree in the subgraph spanned by the un-ordered
  22. columns.
  23. */
  24. #include <vector>
  25. #include <algorithm>
  26. #include <boost/config.hpp>
  27. #include <boost/graph/graph_traits.hpp>
  28. #include <boost/graph/properties.hpp>
  29. #include <boost/pending/bucket_sorter.hpp>
  30. namespace boost {
  31. template <class VertexListGraph, class Order, class Degree, class Marker>
  32. void
  33. smallest_last_vertex_ordering(const VertexListGraph& G, Order order,
  34. Degree degree, Marker marker) {
  35. typedef typename boost::graph_traits<VertexListGraph> GraphTraits;
  36. typedef typename GraphTraits::vertex_descriptor Vertex;
  37. //typedef typename GraphTraits::size_type size_type;
  38. typedef std::size_t size_type;
  39. const size_type num = num_vertices(G);
  40. typedef typename boost::property_map<VertexListGraph, vertex_index_t>::type ID;
  41. typedef bucket_sorter<size_type, Vertex, Degree, ID> BucketSorter;
  42. BucketSorter degree_bucket_sorter(num, num, degree,
  43. get(vertex_index,G));
  44. smallest_last_vertex_ordering(G, order, degree, marker, degree_bucket_sorter);
  45. }
  46. template <class VertexListGraph, class Order, class Degree,
  47. class Marker, class BucketSorter>
  48. void
  49. smallest_last_vertex_ordering(const VertexListGraph& G, Order order,
  50. Degree degree, Marker marker,
  51. BucketSorter& degree_buckets) {
  52. typedef typename boost::graph_traits<VertexListGraph> GraphTraits;
  53. typedef typename GraphTraits::vertex_descriptor Vertex;
  54. //typedef typename GraphTraits::size_type size_type;
  55. typedef std::size_t size_type;
  56. const size_type num = num_vertices(G);
  57. typename GraphTraits::vertex_iterator v, vend;
  58. for (boost::tie(v, vend) = vertices(G); v != vend; ++v) {
  59. put(marker, *v, num);
  60. put(degree, *v, out_degree(*v, G));
  61. degree_buckets.push(*v);
  62. }
  63. size_type minimum_degree = 0;
  64. size_type current_order = num - 1;
  65. while ( 1 ) {
  66. typedef typename BucketSorter::stack MDStack;
  67. MDStack minimum_degree_stack = degree_buckets[minimum_degree];
  68. while (minimum_degree_stack.empty())
  69. minimum_degree_stack = degree_buckets[++minimum_degree];
  70. Vertex node = minimum_degree_stack.top();
  71. put(order, current_order, node);
  72. if ( current_order == 0 ) //find all vertices
  73. break;
  74. minimum_degree_stack.pop();
  75. put(marker, node, 0); //node has been ordered.
  76. typename GraphTraits::adjacency_iterator v, vend;
  77. for (boost::tie(v,vend) = adjacent_vertices(node, G); v != vend; ++v)
  78. if ( get(marker,*v) > current_order ) { //*v is unordered vertex
  79. put(marker, *v, current_order); //mark the columns adjacent to node
  80. //delete *v from the bucket sorter
  81. degree_buckets.remove(*v);
  82. //It is possible minimum degree goes down
  83. //Here we keep tracking it.
  84. put(degree, *v, get(degree, *v) - 1);
  85. BOOST_USING_STD_MIN();
  86. minimum_degree = min BOOST_PREVENT_MACRO_SUBSTITUTION(minimum_degree, get(degree, *v));
  87. //reinsert *v in the bucket sorter with the new degree
  88. degree_buckets.push(*v);
  89. }
  90. current_order--;
  91. }
  92. //at this point, order[i] = v_i;
  93. }
  94. template <class VertexListGraph, class Order>
  95. void
  96. smallest_last_vertex_ordering(const VertexListGraph& G, Order order) {
  97. typedef typename graph_traits<VertexListGraph>::vertex_descriptor vertex_descriptor;
  98. typedef typename graph_traits<VertexListGraph>::degree_size_type degree_size_type;
  99. smallest_last_vertex_ordering(G, order,
  100. make_shared_array_property_map(num_vertices(G), degree_size_type(0), get(vertex_index, G)),
  101. make_shared_array_property_map(num_vertices(G), (std::size_t)(0), get(vertex_index, G)));
  102. }
  103. template <class VertexListGraph>
  104. std::vector<typename graph_traits<VertexListGraph>::vertex_descriptor>
  105. smallest_last_vertex_ordering(const VertexListGraph& G) {
  106. std::vector<typename graph_traits<VertexListGraph>::vertex_descriptor> o(num_vertices(G));
  107. smallest_last_vertex_ordering(G, make_iterator_property_map(o.begin(), typed_identity_property_map<std::size_t>()));
  108. return o;
  109. }
  110. }
  111. #endif