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