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 #ifndef BOOST_GRAPH_DETAIL_CONNECTED_COMPONENTS_HPP
|
Chris@16
|
10 #define BOOST_GRAPH_DETAIL_CONNECTED_COMPONENTS_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #if defined(__sgi) && !defined(__GNUC__)
|
Chris@16
|
13 #pragma set woff 1234
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/operators.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost {
|
Chris@16
|
19
|
Chris@16
|
20 namespace detail {
|
Chris@16
|
21
|
Chris@16
|
22 //=========================================================================
|
Chris@16
|
23 // Implementation details of connected_components
|
Chris@16
|
24
|
Chris@16
|
25 // This is used both in the connected_components algorithm and in
|
Chris@16
|
26 // the kosaraju strong components algorithm during the second DFS
|
Chris@16
|
27 // traversal.
|
Chris@16
|
28 template <class ComponentsPA, class DFSVisitor>
|
Chris@16
|
29 class components_recorder : public DFSVisitor
|
Chris@16
|
30 {
|
Chris@16
|
31 typedef typename property_traits<ComponentsPA>::value_type comp_type;
|
Chris@16
|
32 public:
|
Chris@16
|
33 components_recorder(ComponentsPA c,
|
Chris@16
|
34 comp_type& c_count,
|
Chris@16
|
35 DFSVisitor v)
|
Chris@16
|
36 : DFSVisitor(v), m_component(c), m_count(c_count) {}
|
Chris@16
|
37
|
Chris@16
|
38 template <class Vertex, class Graph>
|
Chris@16
|
39 void start_vertex(Vertex u, Graph& g) {
|
Chris@16
|
40 ++m_count;
|
Chris@16
|
41 DFSVisitor::start_vertex(u, g);
|
Chris@16
|
42 }
|
Chris@16
|
43 template <class Vertex, class Graph>
|
Chris@16
|
44 void discover_vertex(Vertex u, Graph& g) {
|
Chris@16
|
45 put(m_component, u, m_count);
|
Chris@16
|
46 DFSVisitor::discover_vertex(u, g);
|
Chris@16
|
47 }
|
Chris@16
|
48 protected:
|
Chris@16
|
49 ComponentsPA m_component;
|
Chris@16
|
50 comp_type& m_count;
|
Chris@16
|
51 };
|
Chris@16
|
52
|
Chris@16
|
53 template <class DiscoverTimeMap, class FinishTimeMap, class TimeT,
|
Chris@16
|
54 class DFSVisitor>
|
Chris@16
|
55 class time_recorder : public DFSVisitor
|
Chris@16
|
56 {
|
Chris@16
|
57 public:
|
Chris@16
|
58 time_recorder(DiscoverTimeMap d, FinishTimeMap f, TimeT& t, DFSVisitor v)
|
Chris@16
|
59 : DFSVisitor(v), m_discover_time(d), m_finish_time(f), m_t(t) {}
|
Chris@16
|
60
|
Chris@16
|
61 template <class Vertex, class Graph>
|
Chris@16
|
62 void discover_vertex(Vertex u, Graph& g) {
|
Chris@16
|
63 put(m_discover_time, u, ++m_t);
|
Chris@16
|
64 DFSVisitor::discover_vertex(u, g);
|
Chris@16
|
65 }
|
Chris@16
|
66 template <class Vertex, class Graph>
|
Chris@16
|
67 void finish_vertex(Vertex u, Graph& g) {
|
Chris@16
|
68 put(m_finish_time, u, ++m_t);
|
Chris@16
|
69 DFSVisitor::discover_vertex(u, g);
|
Chris@16
|
70 }
|
Chris@16
|
71 protected:
|
Chris@16
|
72 DiscoverTimeMap m_discover_time;
|
Chris@16
|
73 FinishTimeMap m_finish_time;
|
Chris@16
|
74 TimeT m_t;
|
Chris@16
|
75 };
|
Chris@16
|
76 template <class DiscoverTimeMap, class FinishTimeMap, class TimeT,
|
Chris@16
|
77 class DFSVisitor>
|
Chris@16
|
78 time_recorder<DiscoverTimeMap, FinishTimeMap, TimeT, DFSVisitor>
|
Chris@16
|
79 record_times(DiscoverTimeMap d, FinishTimeMap f, TimeT& t, DFSVisitor vis)
|
Chris@16
|
80 {
|
Chris@16
|
81 return time_recorder<DiscoverTimeMap, FinishTimeMap, TimeT, DFSVisitor>
|
Chris@16
|
82 (d, f, t, vis);
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 //=========================================================================
|
Chris@16
|
86 // Implementation detail of dynamic_components
|
Chris@16
|
87
|
Chris@16
|
88
|
Chris@16
|
89 //-------------------------------------------------------------------------
|
Chris@16
|
90 // Helper functions for the component_index class
|
Chris@16
|
91
|
Chris@16
|
92 // Record the representative vertices in the header array.
|
Chris@16
|
93 // Representative vertices now point to the component number.
|
Chris@16
|
94
|
Chris@16
|
95 template <class Parent, class OutputIterator, class Integer>
|
Chris@16
|
96 inline void
|
Chris@16
|
97 build_components_header(Parent p,
|
Chris@16
|
98 OutputIterator header,
|
Chris@16
|
99 Integer num_nodes)
|
Chris@16
|
100 {
|
Chris@16
|
101 Parent component = p;
|
Chris@16
|
102 Integer component_num = 0;
|
Chris@16
|
103 for (Integer v = 0; v != num_nodes; ++v)
|
Chris@16
|
104 if (p[v] == v) {
|
Chris@16
|
105 *header++ = v;
|
Chris@16
|
106 component[v] = component_num++;
|
Chris@16
|
107 }
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110
|
Chris@16
|
111 // Pushes x onto the front of the list. The list is represented in
|
Chris@16
|
112 // an array.
|
Chris@16
|
113 template <class Next, class T, class V>
|
Chris@16
|
114 inline void push_front(Next next, T& head, V x)
|
Chris@16
|
115 {
|
Chris@16
|
116 T tmp = head;
|
Chris@16
|
117 head = x;
|
Chris@16
|
118 next[x] = tmp;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121
|
Chris@16
|
122 // Create a linked list of the vertices in each component
|
Chris@16
|
123 // by reusing the representative array.
|
Chris@16
|
124 template <class Parent1, class Parent2,
|
Chris@16
|
125 class Integer>
|
Chris@16
|
126 void
|
Chris@16
|
127 link_components(Parent1 component, Parent2 header,
|
Chris@16
|
128 Integer num_nodes, Integer num_components)
|
Chris@16
|
129 {
|
Chris@16
|
130 // Make the non-representative vertices point to their component
|
Chris@16
|
131 Parent1 representative = component;
|
Chris@16
|
132 for (Integer v = 0; v != num_nodes; ++v)
|
Chris@16
|
133 if (component[v] >= num_components || header[component[v]] != v)
|
Chris@16
|
134 component[v] = component[representative[v]];
|
Chris@16
|
135
|
Chris@16
|
136 // initialize the "head" of the lists to "NULL"
|
Chris@16
|
137 std::fill_n(header, num_components, num_nodes);
|
Chris@16
|
138
|
Chris@16
|
139 // Add each vertex to the linked list for its component
|
Chris@16
|
140 Parent1 next = component;
|
Chris@16
|
141 for (Integer k = 0; k != num_nodes; ++k)
|
Chris@16
|
142 push_front(next, header[component[k]], k);
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145
|
Chris@16
|
146
|
Chris@16
|
147 template <class IndexContainer, class HeaderContainer>
|
Chris@16
|
148 void
|
Chris@16
|
149 construct_component_index(IndexContainer& index, HeaderContainer& header)
|
Chris@16
|
150 {
|
Chris@16
|
151 build_components_header(index.begin(),
|
Chris@16
|
152 std::back_inserter(header),
|
Chris@16
|
153 index.end() - index.begin());
|
Chris@16
|
154
|
Chris@16
|
155 link_components(index.begin(), header.begin(),
|
Chris@16
|
156 index.end() - index.begin(),
|
Chris@16
|
157 header.end() - header.begin());
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160
|
Chris@16
|
161
|
Chris@16
|
162 template <class IndexIterator, class Integer, class Distance>
|
Chris@16
|
163 class component_iterator
|
Chris@16
|
164 : boost::forward_iterator_helper<
|
Chris@16
|
165 component_iterator<IndexIterator,Integer,Distance>,
|
Chris@16
|
166 Integer, Distance,Integer*, Integer&>
|
Chris@16
|
167 {
|
Chris@16
|
168 public:
|
Chris@16
|
169 typedef component_iterator self;
|
Chris@16
|
170
|
Chris@16
|
171 IndexIterator next;
|
Chris@16
|
172 Integer node;
|
Chris@16
|
173
|
Chris@16
|
174 typedef std::forward_iterator_tag iterator_category;
|
Chris@16
|
175 typedef Integer value_type;
|
Chris@16
|
176 typedef Integer& reference;
|
Chris@16
|
177 typedef Integer* pointer;
|
Chris@16
|
178 typedef Distance difference_type;
|
Chris@16
|
179
|
Chris@16
|
180 component_iterator() {}
|
Chris@16
|
181 component_iterator(IndexIterator x, Integer i)
|
Chris@16
|
182 : next(x), node(i) {}
|
Chris@16
|
183 Integer operator*() const {
|
Chris@16
|
184 return node;
|
Chris@16
|
185 }
|
Chris@16
|
186 self& operator++() {
|
Chris@16
|
187 node = next[node];
|
Chris@16
|
188 return *this;
|
Chris@16
|
189 }
|
Chris@16
|
190 };
|
Chris@16
|
191
|
Chris@16
|
192 template <class IndexIterator, class Integer, class Distance>
|
Chris@16
|
193 inline bool
|
Chris@16
|
194 operator==(const component_iterator<IndexIterator, Integer, Distance>& x,
|
Chris@16
|
195 const component_iterator<IndexIterator, Integer, Distance>& y)
|
Chris@16
|
196 {
|
Chris@16
|
197 return x.node == y.node;
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 } // namespace detail
|
Chris@16
|
201
|
Chris@16
|
202 } // namespace detail
|
Chris@16
|
203
|
Chris@16
|
204 #if defined(__sgi) && !defined(__GNUC__)
|
Chris@16
|
205 #pragma reset woff 1234
|
Chris@16
|
206 #endif
|
Chris@16
|
207
|
Chris@16
|
208 #endif
|