2 //=======================================================================
3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
12 #ifndef BOOST_GRAPH_STRONG_COMPONENTS_HPP
13 #define BOOST_GRAPH_STRONG_COMPONENTS_HPP
16 #include <boost/config.hpp>
17 #include <boost/graph/depth_first_search.hpp>
18 #include <boost/type_traits/conversion_traits.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/graph/overloading.hpp>
21 #include <boost/concept/assert.hpp>
25 //==========================================================================
26 // This is Tarjan's algorithm for strongly connected components
27 // from his paper "Depth first search and linear graph algorithms".
28 // It calculates the components in a single application of DFS.
29 // We implement the algorithm as a dfs-visitor.
33 template <typename ComponentMap, typename RootMap, typename DiscoverTime,
35 class tarjan_scc_visitor : public dfs_visitor<>
37 typedef typename property_traits<ComponentMap>::value_type comp_type;
38 typedef typename property_traits<DiscoverTime>::value_type time_type;
40 tarjan_scc_visitor(ComponentMap comp_map, RootMap r, DiscoverTime d,
41 comp_type& c_, Stack& s_)
42 : c(c_), comp(comp_map), root(r), discover_time(d),
43 dfs_time(time_type()), s(s_) { }
45 template <typename Graph>
46 void discover_vertex(typename graph_traits<Graph>::vertex_descriptor v,
49 put(comp, v, (std::numeric_limits<comp_type>::max)());
50 put(discover_time, v, dfs_time++);
53 template <typename Graph>
54 void finish_vertex(typename graph_traits<Graph>::vertex_descriptor v,
56 typename graph_traits<Graph>::vertex_descriptor w;
57 typename graph_traits<Graph>::out_edge_iterator ei, ei_end;
58 for (boost::tie(ei, ei_end) = out_edges(v, g); ei != ei_end; ++ei) {
60 if (get(comp, w) == (std::numeric_limits<comp_type>::max)())
61 put(root, v, this->min_discover_time(get(root,v), get(root,w)));
63 if (get(root, v) == v) {
73 template <typename Vertex>
74 Vertex min_discover_time(Vertex u, Vertex v) {
75 return get(discover_time, u) < get(discover_time,v) ? u : v;
81 DiscoverTime discover_time;
86 template <class Graph, class ComponentMap, class RootMap,
87 class DiscoverTime, class P, class T, class R>
88 typename property_traits<ComponentMap>::value_type
89 strong_components_impl
90 (const Graph& g, // Input
91 ComponentMap comp, // Output
92 // Internal record keeping
94 DiscoverTime discover_time,
95 const bgl_named_params<P, T, R>& params)
97 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
98 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<ComponentMap, Vertex> ));
99 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<RootMap, Vertex> ));
100 typedef typename property_traits<RootMap>::value_type RootV;
101 BOOST_CONCEPT_ASSERT(( ConvertibleConcept<RootV, Vertex> ));
102 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<DiscoverTime, Vertex> ));
104 typename property_traits<ComponentMap>::value_type total = 0;
106 std::stack<Vertex> s;
107 detail::tarjan_scc_visitor<ComponentMap, RootMap, DiscoverTime,
109 vis(comp, root, discover_time, total, s);
110 depth_first_search(g, params.visitor(vis));
114 //-------------------------------------------------------------------------
115 // The dispatch functions handle the defaults for the rank and discover
116 // time property maps.
117 // dispatch with class specialization to avoid VC++ bug
119 template <class DiscoverTimeMap>
120 struct strong_comp_dispatch2 {
121 template <class Graph, class ComponentMap, class RootMap, class P, class T, class R>
122 inline static typename property_traits<ComponentMap>::value_type
123 apply(const Graph& g,
126 const bgl_named_params<P, T, R>& params,
127 DiscoverTimeMap time_map)
129 return strong_components_impl(g, comp, r_map, time_map, params);
135 struct strong_comp_dispatch2<param_not_found> {
136 template <class Graph, class ComponentMap, class RootMap,
137 class P, class T, class R>
138 inline static typename property_traits<ComponentMap>::value_type
139 apply(const Graph& g,
142 const bgl_named_params<P, T, R>& params,
145 typedef typename graph_traits<Graph>::vertices_size_type size_type;
146 size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1;
147 std::vector<size_type> time_vec(n);
148 return strong_components_impl
150 make_iterator_property_map(time_vec.begin(), choose_const_pmap
151 (get_param(params, vertex_index),
152 g, vertex_index), time_vec[0]),
157 template <class Graph, class ComponentMap, class RootMap,
158 class P, class T, class R, class DiscoverTimeMap>
159 inline typename property_traits<ComponentMap>::value_type
160 scc_helper2(const Graph& g,
163 const bgl_named_params<P, T, R>& params,
164 DiscoverTimeMap time_map)
166 return strong_comp_dispatch2<DiscoverTimeMap>::apply(g, comp, r_map, params, time_map);
169 template <class RootMap>
170 struct strong_comp_dispatch1 {
172 template <class Graph, class ComponentMap, class P, class T, class R>
173 inline static typename property_traits<ComponentMap>::value_type
174 apply(const Graph& g,
176 const bgl_named_params<P, T, R>& params,
179 return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time));
183 struct strong_comp_dispatch1<param_not_found> {
185 template <class Graph, class ComponentMap,
186 class P, class T, class R>
187 inline static typename property_traits<ComponentMap>::value_type
188 apply(const Graph& g,
190 const bgl_named_params<P, T, R>& params,
193 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
194 typename std::vector<Vertex>::size_type
195 n = num_vertices(g) > 0 ? num_vertices(g) : 1;
196 std::vector<Vertex> root_vec(n);
199 make_iterator_property_map(root_vec.begin(), choose_const_pmap
200 (get_param(params, vertex_index),
201 g, vertex_index), root_vec[0]),
203 get_param(params, vertex_discover_time));
207 template <class Graph, class ComponentMap, class RootMap,
208 class P, class T, class R>
209 inline typename property_traits<ComponentMap>::value_type
210 scc_helper1(const Graph& g,
212 const bgl_named_params<P, T, R>& params,
215 return detail::strong_comp_dispatch1<RootMap>::apply(g, comp, params,
219 } // namespace detail
221 template <class Graph, class ComponentMap,
222 class P, class T, class R>
223 inline typename property_traits<ComponentMap>::value_type
224 strong_components(const Graph& g, ComponentMap comp,
225 const bgl_named_params<P, T, R>& params
226 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
228 typedef typename graph_traits<Graph>::directed_category DirCat;
229 BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
230 return detail::scc_helper1(g, comp, params,
231 get_param(params, vertex_root_t()));
234 template <class Graph, class ComponentMap>
235 inline typename property_traits<ComponentMap>::value_type
236 strong_components(const Graph& g, ComponentMap comp
237 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
239 typedef typename graph_traits<Graph>::directed_category DirCat;
240 BOOST_STATIC_ASSERT((is_convertible<DirCat*, directed_tag*>::value == true));
241 bgl_named_params<int, int> params(0);
242 return strong_components(g, comp, params);
245 template <typename Graph, typename ComponentMap, typename ComponentLists>
246 void build_component_lists
248 typename graph_traits<Graph>::vertices_size_type num_scc,
249 ComponentMap component_number,
250 ComponentLists& components)
252 components.resize(num_scc);
253 typename graph_traits<Graph>::vertex_iterator vi, vi_end;
254 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
255 components[component_number[*vi]].push_back(*vi);
263 #include <boost/graph/transpose_graph.hpp>
264 #include <boost/pending/indirect_cmp.hpp>
265 #include <boost/graph/connected_components.hpp> // for components_recorder
269 //==========================================================================
270 // This is the version of strongly connected components from
271 // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was
272 // adapted from "Data Structure and Algorithms" by Aho, Hopcroft,
273 // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir.
274 // The algorithm is based on computing DFS forests the graph
275 // and its transpose.
277 // This algorithm is slower than Tarjan's by a constant factor, uses
278 // more memory, and puts more requirements on the graph type.
280 template <class Graph, class DFSVisitor, class ComponentsMap,
281 class DiscoverTime, class FinishTime,
283 typename property_traits<ComponentsMap>::value_type
284 kosaraju_strong_components(Graph& G, ComponentsMap c,
285 FinishTime finish_time, ColorMap color)
287 BOOST_CONCEPT_ASSERT(( MutableGraphConcept<Graph> ));
290 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
291 typedef typename property_traits<ColorMap>::value_type ColorValue;
292 typedef color_traits<ColorValue> Color;
293 typename property_traits<FinishTime>::value_type time = 0;
295 (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())),
298 Graph G_T(num_vertices(G));
299 transpose_graph(G, G_T);
301 typedef typename property_traits<ComponentsMap>::value_type count_type;
303 count_type c_count(0);
304 detail::components_recorder<ComponentsMap>
308 typename graph_traits<Graph>::vertex_iterator ui, ui_end;
309 for (boost::tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui)
310 put(color, *ui, Color::white());
312 typedef typename property_traits<FinishTime>::value_type D;
313 typedef indirect_cmp< FinishTime, std::less<D> > Compare;
315 Compare fl(finish_time);
316 std::priority_queue<Vertex, std::vector<Vertex>, Compare > Q(fl);
318 typename graph_traits<Graph>::vertex_iterator i, j, iend, jend;
319 boost::tie(i, iend) = vertices(G_T);
320 boost::tie(j, jend) = vertices(G);
321 for ( ; i != iend; ++i, ++j) {
322 put(finish_time, *i, get(finish_time, *j));
326 while ( !Q.empty() ) {
329 if (get(color, u) == Color::white()) {
330 depth_first_visit(G_T, u, vis, color);
339 #ifdef BOOST_GRAPH_USE_MPI
340 # include <boost/graph/distributed/strong_components.hpp>
343 #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP