sl@0: // sl@0: //======================================================================= sl@0: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. sl@0: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: //======================================================================= sl@0: // sl@0: sl@0: #ifndef BOOST_GRAPH_STRONG_COMPONENTS_HPP sl@0: #define BOOST_GRAPH_STRONG_COMPONENTS_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: //========================================================================== sl@0: // This is Tarjan's algorithm for strongly connected components sl@0: // from his paper "Depth first search and linear graph algorithms". sl@0: // It calculates the components in a single application of DFS. sl@0: // We implement the algorithm as a dfs-visitor. sl@0: sl@0: namespace detail { sl@0: sl@0: template sl@0: class tarjan_scc_visitor : public dfs_visitor<> sl@0: { sl@0: typedef typename property_traits::value_type comp_type; sl@0: typedef typename property_traits::value_type time_type; sl@0: public: sl@0: tarjan_scc_visitor(ComponentMap comp_map, RootMap r, DiscoverTime d, sl@0: comp_type& c_, Stack& s_) sl@0: : c(c_), comp(comp_map), root(r), discover_time(d), sl@0: dfs_time(time_type()), s(s_) { } sl@0: sl@0: template sl@0: void discover_vertex(typename graph_traits::vertex_descriptor v, sl@0: const Graph&) { sl@0: put(root, v, v); sl@0: put(comp, v, (std::numeric_limits::max)()); sl@0: put(discover_time, v, dfs_time++); sl@0: s.push(v); sl@0: } sl@0: template sl@0: void finish_vertex(typename graph_traits::vertex_descriptor v, sl@0: const Graph& g) { sl@0: typename graph_traits::vertex_descriptor w; sl@0: typename graph_traits::out_edge_iterator ei, ei_end; sl@0: for (tie(ei, ei_end) = out_edges(v, g); ei != ei_end; ++ei) { sl@0: w = target(*ei, g); sl@0: if (get(comp, w) == (std::numeric_limits::max)()) sl@0: put(root, v, this->min_discover_time(get(root,v), get(root,w))); sl@0: } sl@0: if (get(root, v) == v) { sl@0: do { sl@0: w = s.top(); s.pop(); sl@0: put(comp, w, c); sl@0: } while (w != v); sl@0: ++c; sl@0: } sl@0: } sl@0: private: sl@0: template sl@0: Vertex min_discover_time(Vertex u, Vertex v) { sl@0: return get(discover_time, u) < get(discover_time,v) ? u : v; sl@0: } sl@0: sl@0: comp_type& c; sl@0: ComponentMap comp; sl@0: RootMap root; sl@0: DiscoverTime discover_time; sl@0: time_type dfs_time; sl@0: Stack& s; sl@0: }; sl@0: sl@0: template sl@0: typename property_traits::value_type sl@0: strong_components_impl sl@0: (const Graph& g, // Input sl@0: ComponentMap comp, // Output sl@0: // Internal record keeping sl@0: RootMap root, sl@0: DiscoverTime discover_time, sl@0: const bgl_named_params& params) sl@0: { sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: function_requires< ReadWritePropertyMapConcept >(); sl@0: function_requires< ReadWritePropertyMapConcept >(); sl@0: typedef typename property_traits::value_type RootV; sl@0: function_requires< ConvertibleConcept >(); sl@0: function_requires< ReadWritePropertyMapConcept >(); sl@0: sl@0: typename property_traits::value_type total = 0; sl@0: sl@0: std::stack s; sl@0: detail::tarjan_scc_visitor > sl@0: vis(comp, root, discover_time, total, s); sl@0: depth_first_search(g, params.visitor(vis)); sl@0: return total; sl@0: } sl@0: sl@0: //------------------------------------------------------------------------- sl@0: // The dispatch functions handle the defaults for the rank and discover sl@0: // time property maps. sl@0: // dispatch with class specialization to avoid VC++ bug sl@0: sl@0: template sl@0: struct strong_comp_dispatch2 { sl@0: template sl@0: inline static typename property_traits::value_type sl@0: apply(const Graph& g, sl@0: ComponentMap comp, sl@0: RootMap r_map, sl@0: const bgl_named_params& params, sl@0: DiscoverTimeMap time_map) sl@0: { sl@0: return strong_components_impl(g, comp, r_map, time_map, params); sl@0: } sl@0: }; sl@0: sl@0: sl@0: template <> sl@0: struct strong_comp_dispatch2 { sl@0: template sl@0: inline static typename property_traits::value_type sl@0: apply(const Graph& g, sl@0: ComponentMap comp, sl@0: RootMap r_map, sl@0: const bgl_named_params& params, sl@0: detail::error_property_not_found) sl@0: { sl@0: typedef typename graph_traits::vertices_size_type size_type; sl@0: size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; sl@0: std::vector time_vec(n); sl@0: return strong_components_impl sl@0: (g, comp, r_map, sl@0: make_iterator_property_map(time_vec.begin(), choose_const_pmap sl@0: (get_param(params, vertex_index), sl@0: g, vertex_index), time_vec[0]), sl@0: params); sl@0: } sl@0: }; sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: scc_helper2(const Graph& g, sl@0: ComponentMap comp, sl@0: RootMap r_map, sl@0: const bgl_named_params& params, sl@0: DiscoverTimeMap time_map) sl@0: { sl@0: return strong_comp_dispatch2::apply(g, comp, r_map, params, time_map); sl@0: } sl@0: sl@0: template sl@0: struct strong_comp_dispatch1 { sl@0: sl@0: template sl@0: inline static typename property_traits::value_type sl@0: apply(const Graph& g, sl@0: ComponentMap comp, sl@0: const bgl_named_params& params, sl@0: RootMap r_map) sl@0: { sl@0: return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); sl@0: } sl@0: }; sl@0: template <> sl@0: struct strong_comp_dispatch1 { sl@0: sl@0: template sl@0: inline static typename property_traits::value_type sl@0: apply(const Graph& g, sl@0: ComponentMap comp, sl@0: const bgl_named_params& params, sl@0: detail::error_property_not_found) sl@0: { sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: typename std::vector::size_type sl@0: n = num_vertices(g) > 0 ? num_vertices(g) : 1; sl@0: std::vector root_vec(n); sl@0: return scc_helper2 sl@0: (g, comp, sl@0: make_iterator_property_map(root_vec.begin(), choose_const_pmap sl@0: (get_param(params, vertex_index), sl@0: g, vertex_index), root_vec[0]), sl@0: params, sl@0: get_param(params, vertex_discover_time)); sl@0: } sl@0: }; sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: scc_helper1(const Graph& g, sl@0: ComponentMap comp, sl@0: const bgl_named_params& params, sl@0: RootMap r_map) sl@0: { sl@0: return detail::strong_comp_dispatch1::apply(g, comp, params, sl@0: r_map); sl@0: } sl@0: sl@0: } // namespace detail sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: strong_components(const Graph& g, ComponentMap comp, sl@0: const bgl_named_params& params) sl@0: { sl@0: typedef typename graph_traits::directed_category DirCat; sl@0: BOOST_STATIC_ASSERT((is_convertible::value == true)); sl@0: return detail::scc_helper1(g, comp, params, sl@0: get_param(params, vertex_root_t())); sl@0: } sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: strong_components(const Graph& g, ComponentMap comp) sl@0: { sl@0: typedef typename graph_traits::directed_category DirCat; sl@0: BOOST_STATIC_ASSERT((is_convertible::value == true)); sl@0: bgl_named_params params(0); sl@0: return strong_components(g, comp, params); sl@0: } sl@0: sl@0: template sl@0: void build_component_lists sl@0: (const Graph& g, sl@0: typename graph_traits::vertices_size_type num_scc, sl@0: ComponentMap component_number, sl@0: ComponentLists& components) sl@0: { sl@0: components.resize(num_scc); sl@0: typename graph_traits::vertex_iterator vi, vi_end; sl@0: for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) sl@0: components[component_number[*vi]].push_back(*vi); sl@0: } sl@0: sl@0: sl@0: } // namespace boost sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include // for components_recorder sl@0: sl@0: namespace boost { sl@0: sl@0: //========================================================================== sl@0: // This is the version of strongly connected components from sl@0: // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was sl@0: // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, sl@0: // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. sl@0: // The algorithm is based on computing DFS forests the graph sl@0: // and its transpose. sl@0: sl@0: // This algorithm is slower than Tarjan's by a constant factor, uses sl@0: // more memory, and puts more requirements on the graph type. sl@0: sl@0: template sl@0: typename property_traits::value_type sl@0: kosaraju_strong_components(Graph& G, ComponentsMap c, sl@0: FinishTime finish_time, ColorMap color) sl@0: { sl@0: function_requires< MutableGraphConcept >(); sl@0: // ... sl@0: sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: typedef typename property_traits::value_type ColorValue; sl@0: typedef color_traits Color; sl@0: typename property_traits::value_type time = 0; sl@0: depth_first_search sl@0: (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), sl@0: color); sl@0: sl@0: Graph G_T(num_vertices(G)); sl@0: transpose_graph(G, G_T); sl@0: sl@0: typedef typename property_traits::value_type count_type; sl@0: sl@0: count_type c_count(0); sl@0: detail::components_recorder sl@0: vis(c, c_count); sl@0: sl@0: // initialize G_T sl@0: typename graph_traits::vertex_iterator ui, ui_end; sl@0: for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) sl@0: put(color, *ui, Color::white()); sl@0: sl@0: typedef typename property_traits::value_type D; sl@0: typedef indirect_cmp< FinishTime, std::less > Compare; sl@0: sl@0: Compare fl(finish_time); sl@0: std::priority_queue, Compare > Q(fl); sl@0: sl@0: typename graph_traits::vertex_iterator i, j, iend, jend; sl@0: tie(i, iend) = vertices(G_T); sl@0: tie(j, jend) = vertices(G); sl@0: for ( ; i != iend; ++i, ++j) { sl@0: put(finish_time, *i, get(finish_time, *j)); sl@0: Q.push(*i); sl@0: } sl@0: sl@0: while ( !Q.empty() ) { sl@0: Vertex u = Q.top(); sl@0: Q.pop(); sl@0: if (get(color, u) == Color::white()) { sl@0: depth_first_visit(G_T, u, vis, color); sl@0: ++c_count; sl@0: } sl@0: } sl@0: return c_count; sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP