& params) { typedef typename graph_traits::vertex_descriptor Vertex; function_requires< ReadWritePropertyMapConcept >(); function_requires< ReadWritePropertyMapConcept >(); typedef typename property_traits::value_type RootV; function_requires< ConvertibleConcept >(); function_requires< ReadWritePropertyMapConcept >(); typename property_traits::value_type total = 0; std::stack s; detail::tarjan_scc_visitor > vis(comp, root, discover_time, total, s); depth_first_search(g, params.visitor(vis)); return total; } //------------------------------------------------------------------------- // The dispatch functions handle the defaults for the rank and discover // time property maps. // dispatch with class specialization to avoid VC++ bug template struct strong_comp_dispatch2 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, DiscoverTimeMap time_map) { return strong_components_impl(g, comp, r_map, time_map, params); } }; template <> struct strong_comp_dispatch2 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertices_size_type size_type; size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector time_vec(n); return strong_components_impl (g, comp, r_map, make_iterator_property_map(time_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), time_vec[0]), params); } }; template inline typename property_traits::value_type scc_helper2(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, DiscoverTimeMap time_map) { return strong_comp_dispatch2::apply(g, comp, r_map, params, time_map); } template struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); } }; template <> struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, DiscoverTimeMap time_map) { return strong_components_impl(g, comp, r_map, time_map, params); } }; template <> struct strong_comp_dispatch2 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertices_size_type size_type; size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector time_vec(n); return strong_components_impl (g, comp, r_map, make_iterator_property_map(time_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), time_vec[0]), params); } }; template inline typename property_traits::value_type scc_helper2(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, DiscoverTimeMap time_map) { return strong_comp_dispatch2::apply(g, comp, r_map, params, time_map); } template struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); } }; template <> struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, detail::error_property_not_found) { typedef typename graph_traits::vertices_size_type size_type; size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector time_vec(n); return strong_components_impl (g, comp, r_map, make_iterator_property_map(time_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), time_vec[0]), params); } }; template inline typename property_traits::value_type scc_helper2(const Graph& g, ComponentMap comp, RootMap r_map, const bgl_named_params& params, DiscoverTimeMap time_map) { return strong_comp_dispatch2::apply(g, comp, r_map, params, time_map); } template struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); } }; template <> struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, DiscoverTimeMap time_map) { return strong_comp_dispatch2::apply(g, comp, r_map, params, time_map); } template struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); } }; template <> struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, RootMap r_map) { return scc_helper2(g, comp, r_map, params, get_param(params, vertex_discover_time)); } }; template <> struct strong_comp_dispatch1 { template inline static typename property_traits::value_type apply(const Graph& g, ComponentMap comp, const bgl_named_params& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, detail::error_property_not_found) { typedef typename graph_traits::vertex_descriptor Vertex; typename std::vector::size_type n = num_vertices(g) > 0 ? num_vertices(g) : 1; std::vector root_vec(n); return scc_helper2 (g, comp, make_iterator_property_map(root_vec.begin(), choose_const_pmap (get_param(params, vertex_index), g, vertex_index), root_vec[0]), params, get_param(params, vertex_discover_time)); } }; template inline typename property_traits::value_type scc_helper1(const Graph& g, ComponentMap comp, const bgl_named_params& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params, RootMap r_map) { return detail::strong_comp_dispatch1::apply(g, comp, params, r_map); } } // namespace detail template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp, const bgl_named_params& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP
& params) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); return detail::scc_helper1(g, comp, params, get_param(params, vertex_root_t())); } template inline typename property_traits::value_type strong_components(const Graph& g, ComponentMap comp) { typedef typename graph_traits::directed_category DirCat; BOOST_STATIC_ASSERT((is_convertible::value == true)); bgl_named_params params(0); return strong_components(g, comp, params); } template void build_component_lists (const Graph& g, typename graph_traits::vertices_size_type num_scc, ComponentMap component_number, ComponentLists& components) { components.resize(num_scc); typename graph_traits::vertex_iterator vi, vi_end; for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) components[component_number[*vi]].push_back(*vi); } } // namespace boost #include #include #include #include #include // for components_recorder namespace boost { //========================================================================== // This is the version of strongly connected components from // "Intro. to Algorithms" by Cormen, Leiserson, Rivest, which was // adapted from "Data Structure and Algorithms" by Aho, Hopcroft, // and Ullman, who credit the algorithm to S.R. Kosaraju and M. Sharir. // The algorithm is based on computing DFS forests the graph // and its transpose. // This algorithm is slower than Tarjan's by a constant factor, uses // more memory, and puts more requirements on the graph type. template typename property_traits::value_type kosaraju_strong_components(Graph& G, ComponentsMap c, FinishTime finish_time, ColorMap color) { function_requires< MutableGraphConcept >(); // ... typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_traits::value_type ColorValue; typedef color_traits Color; typename property_traits::value_type time = 0; depth_first_search (G, make_dfs_visitor(stamp_times(finish_time, time, on_finish_vertex())), color); Graph G_T(num_vertices(G)); transpose_graph(G, G_T); typedef typename property_traits::value_type count_type; count_type c_count(0); detail::components_recorder vis(c, c_count); // initialize G_T typename graph_traits::vertex_iterator ui, ui_end; for (tie(ui, ui_end) = vertices(G_T); ui != ui_end; ++ui) put(color, *ui, Color::white()); typedef typename property_traits::value_type D; typedef indirect_cmp< FinishTime, std::less > Compare; Compare fl(finish_time); std::priority_queue, Compare > Q(fl); typename graph_traits::vertex_iterator i, j, iend, jend; tie(i, iend) = vertices(G_T); tie(j, jend) = vertices(G); for ( ; i != iend; ++i, ++j) { put(finish_time, *i, get(finish_time, *j)); Q.push(*i); } while ( !Q.empty() ) { Vertex u = Q.top(); Q.pop(); if (get(color, u) == Color::white()) { depth_first_visit(G_T, u, vis, color); ++c_count; } } return c_count; } } // namespace boost #endif // BOOST_GRAPH_STRONG_COMPONENTS_HPP