Chris@16
|
1 //=======================================================================
|
Chris@16
|
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
3 // Copyright 2004, 2005 Trustees of Indiana University
|
Chris@16
|
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek,
|
Chris@16
|
5 // Doug Gregor, D. Kevin McGrath
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //=======================================================================//
|
Chris@16
|
11 #ifndef BOOST_GRAPH_DETAIL_SPARSE_ORDERING_HPP
|
Chris@16
|
12 #define BOOST_GRAPH_DETAIL_SPARSE_ORDERING_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <vector>
|
Chris@16
|
16 #include <queue>
|
Chris@16
|
17 #include <boost/pending/queue.hpp>
|
Chris@16
|
18 #include <boost/pending/mutable_queue.hpp>
|
Chris@16
|
19 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
20 #include <boost/graph/breadth_first_search.hpp>
|
Chris@16
|
21 #include <boost/graph/properties.hpp>
|
Chris@16
|
22 #include <boost/pending/indirect_cmp.hpp>
|
Chris@16
|
23 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
24 #include <boost/bind.hpp>
|
Chris@16
|
25 #include <boost/graph/iteration_macros.hpp>
|
Chris@16
|
26 #include <boost/graph/depth_first_search.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29
|
Chris@16
|
30 namespace sparse {
|
Chris@16
|
31
|
Chris@16
|
32 // rcm_queue
|
Chris@16
|
33 //
|
Chris@16
|
34 // This is a custom queue type used in the
|
Chris@16
|
35 // *_ordering algorithms.
|
Chris@16
|
36 // In addition to the normal queue operations, the
|
Chris@16
|
37 // rcm_queue provides:
|
Chris@16
|
38 //
|
Chris@16
|
39 // int eccentricity() const;
|
Chris@16
|
40 // value_type spouse() const;
|
Chris@16
|
41 //
|
Chris@16
|
42
|
Chris@16
|
43 // yes, it's a bad name...but it works, so use it
|
Chris@16
|
44 template < class Vertex, class DegreeMap,
|
Chris@16
|
45 class Container = std::deque<Vertex> >
|
Chris@16
|
46 class rcm_queue : public std::queue<Vertex, Container> {
|
Chris@16
|
47 typedef std::queue<Vertex> base;
|
Chris@16
|
48 public:
|
Chris@16
|
49 typedef typename base::value_type value_type;
|
Chris@16
|
50 typedef typename base::size_type size_type;
|
Chris@16
|
51
|
Chris@16
|
52 /* SGI queue has not had a contructor queue(const Container&) */
|
Chris@16
|
53 inline rcm_queue(DegreeMap deg)
|
Chris@16
|
54 : _size(0), Qsize(1), eccen(-1), degree(deg) { }
|
Chris@16
|
55
|
Chris@16
|
56 inline void pop() {
|
Chris@16
|
57 if ( !_size )
|
Chris@16
|
58 Qsize = base::size();
|
Chris@16
|
59
|
Chris@16
|
60 base::pop();
|
Chris@16
|
61 if ( _size == Qsize-1 ) {
|
Chris@16
|
62 _size = 0;
|
Chris@16
|
63 ++eccen;
|
Chris@16
|
64 } else
|
Chris@16
|
65 ++_size;
|
Chris@16
|
66
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 inline value_type& front() {
|
Chris@16
|
70 value_type& u = base::front();
|
Chris@16
|
71 if ( _size == 0 )
|
Chris@16
|
72 w = u;
|
Chris@16
|
73 else if (get(degree,u) < get(degree,w) )
|
Chris@16
|
74 w = u;
|
Chris@16
|
75 return u;
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 inline const value_type& front() const {
|
Chris@16
|
79 const value_type& u = base::front();
|
Chris@16
|
80 if ( _size == 0 )
|
Chris@16
|
81 w = u;
|
Chris@16
|
82 else if (get(degree,u) < get(degree,w) )
|
Chris@16
|
83 w = u;
|
Chris@16
|
84 return u;
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 inline value_type& top() { return front(); }
|
Chris@16
|
88 inline const value_type& top() const { return front(); }
|
Chris@16
|
89
|
Chris@16
|
90 inline size_type size() const { return base::size(); }
|
Chris@16
|
91
|
Chris@16
|
92 inline size_type eccentricity() const { return eccen; }
|
Chris@16
|
93 inline value_type spouse() const { return w; }
|
Chris@16
|
94
|
Chris@16
|
95 protected:
|
Chris@16
|
96 size_type _size;
|
Chris@16
|
97 size_type Qsize;
|
Chris@16
|
98 int eccen;
|
Chris@16
|
99 mutable value_type w;
|
Chris@16
|
100 DegreeMap degree;
|
Chris@16
|
101 };
|
Chris@16
|
102
|
Chris@16
|
103
|
Chris@16
|
104 template <typename Tp, typename Sequence = std::deque<Tp> >
|
Chris@16
|
105 class sparse_ordering_queue : public boost::queue<Tp, Sequence>{
|
Chris@16
|
106 public:
|
Chris@16
|
107 typedef typename Sequence::iterator iterator;
|
Chris@16
|
108 typedef typename Sequence::reverse_iterator reverse_iterator;
|
Chris@16
|
109 typedef queue<Tp,Sequence> base;
|
Chris@16
|
110 typedef typename Sequence::size_type size_type;
|
Chris@16
|
111
|
Chris@16
|
112 inline iterator begin() { return this->c.begin(); }
|
Chris@16
|
113 inline reverse_iterator rbegin() { return this->c.rbegin(); }
|
Chris@16
|
114 inline iterator end() { return this->c.end(); }
|
Chris@16
|
115 inline reverse_iterator rend() { return this->c.rend(); }
|
Chris@16
|
116 inline Tp &operator[](int n) { return this->c[n]; }
|
Chris@16
|
117 inline size_type size() {return this->c.size(); }
|
Chris@16
|
118 protected:
|
Chris@16
|
119 //nothing
|
Chris@16
|
120 };
|
Chris@16
|
121
|
Chris@16
|
122 } // namespace sparse
|
Chris@16
|
123
|
Chris@16
|
124 // Compute Pseudo peripheral
|
Chris@16
|
125 //
|
Chris@16
|
126 // To compute an approximated peripheral for a given vertex.
|
Chris@16
|
127 // Used in <tt>king_ordering</tt> algorithm.
|
Chris@16
|
128 //
|
Chris@16
|
129 template <class Graph, class Vertex, class ColorMap, class DegreeMap>
|
Chris@16
|
130 Vertex
|
Chris@16
|
131 pseudo_peripheral_pair(Graph const& G, const Vertex& u, int& ecc,
|
Chris@16
|
132 ColorMap color, DegreeMap degree)
|
Chris@16
|
133 {
|
Chris@16
|
134 typedef typename property_traits<ColorMap>::value_type ColorValue;
|
Chris@16
|
135 typedef color_traits<ColorValue> Color;
|
Chris@16
|
136
|
Chris@16
|
137 sparse::rcm_queue<Vertex, DegreeMap> Q(degree);
|
Chris@16
|
138
|
Chris@16
|
139 typename boost::graph_traits<Graph>::vertex_iterator ui, ui_end;
|
Chris@16
|
140 for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
|
Chris@16
|
141 if (get(color, *ui) != Color::red()) put(color, *ui, Color::white());
|
Chris@16
|
142 breadth_first_visit(G, u, buffer(Q).color_map(color));
|
Chris@16
|
143
|
Chris@16
|
144 ecc = Q.eccentricity();
|
Chris@16
|
145 return Q.spouse();
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 // Find a good starting node
|
Chris@16
|
149 //
|
Chris@16
|
150 // This is to find a good starting node for the
|
Chris@16
|
151 // king_ordering algorithm. "good" is in the sense
|
Chris@16
|
152 // of the ordering generated by RCM.
|
Chris@16
|
153 //
|
Chris@16
|
154 template <class Graph, class Vertex, class Color, class Degree>
|
Chris@16
|
155 Vertex find_starting_node(Graph const& G, Vertex r, Color color, Degree degree)
|
Chris@16
|
156 {
|
Chris@16
|
157 Vertex x, y;
|
Chris@16
|
158 int eccen_r, eccen_x;
|
Chris@16
|
159
|
Chris@16
|
160 x = pseudo_peripheral_pair(G, r, eccen_r, color, degree);
|
Chris@16
|
161 y = pseudo_peripheral_pair(G, x, eccen_x, color, degree);
|
Chris@16
|
162
|
Chris@16
|
163 while (eccen_x > eccen_r) {
|
Chris@16
|
164 r = x;
|
Chris@16
|
165 eccen_r = eccen_x;
|
Chris@16
|
166 x = y;
|
Chris@16
|
167 y = pseudo_peripheral_pair(G, x, eccen_x, color, degree);
|
Chris@16
|
168 }
|
Chris@16
|
169 return x;
|
Chris@16
|
170 }
|
Chris@16
|
171
|
Chris@16
|
172 template <typename Graph>
|
Chris@16
|
173 class out_degree_property_map
|
Chris@16
|
174 : public put_get_helper<typename graph_traits<Graph>::degree_size_type,
|
Chris@16
|
175 out_degree_property_map<Graph> >
|
Chris@16
|
176 {
|
Chris@16
|
177 public:
|
Chris@16
|
178 typedef typename graph_traits<Graph>::vertex_descriptor key_type;
|
Chris@16
|
179 typedef typename graph_traits<Graph>::degree_size_type value_type;
|
Chris@16
|
180 typedef value_type reference;
|
Chris@16
|
181 typedef readable_property_map_tag category;
|
Chris@16
|
182 out_degree_property_map(const Graph& g) : m_g(g) { }
|
Chris@16
|
183 value_type operator[](const key_type& v) const {
|
Chris@16
|
184 return out_degree(v, m_g);
|
Chris@16
|
185 }
|
Chris@16
|
186 private:
|
Chris@16
|
187 const Graph& m_g;
|
Chris@16
|
188 };
|
Chris@16
|
189 template <typename Graph>
|
Chris@16
|
190 inline out_degree_property_map<Graph>
|
Chris@16
|
191 make_out_degree_map(const Graph& g) {
|
Chris@16
|
192 return out_degree_property_map<Graph>(g);
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 } // namespace boost
|
Chris@16
|
196
|
Chris@16
|
197
|
Chris@16
|
198 #endif // BOOST_GRAPH_KING_HPP
|