sl@0: // sl@0: //======================================================================= sl@0: // Copyright 1997-2001 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: #ifndef BOOST_GRAPH_CONNECTED_COMPONENTS_HPP sl@0: #define BOOST_GRAPH_CONNECTED_COMPONENTS_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: namespace detail { sl@0: sl@0: // This visitor is used both in the connected_components algorithm sl@0: // and in the kosaraju strong components algorithm during the sl@0: // second DFS traversal. sl@0: template sl@0: class components_recorder : public dfs_visitor<> sl@0: { sl@0: typedef typename property_traits::value_type comp_type; sl@0: public: sl@0: components_recorder(ComponentsMap c, sl@0: comp_type& c_count) sl@0: : m_component(c), m_count(c_count) {} sl@0: sl@0: template sl@0: void start_vertex(Vertex, Graph&) { sl@0: if (m_count == (std::numeric_limits::max)()) sl@0: m_count = 0; // start counting components at zero sl@0: else sl@0: ++m_count; sl@0: } sl@0: template sl@0: void discover_vertex(Vertex u, Graph&) { sl@0: put(m_component, u, m_count); sl@0: } sl@0: protected: sl@0: ComponentsMap m_component; sl@0: comp_type& m_count; sl@0: }; sl@0: sl@0: } // namespace detail sl@0: sl@0: // This function computes the connected components of an undirected sl@0: // graph using a single application of depth first search. sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: connected_components(const Graph& g, ComponentMap c, sl@0: const bgl_named_params& params) sl@0: { sl@0: if (num_vertices(g) == 0) return 0; sl@0: sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: function_requires< WritablePropertyMapConcept >(); sl@0: typedef typename boost::graph_traits::directed_category directed; sl@0: BOOST_STATIC_ASSERT((boost::is_same::value)); sl@0: sl@0: typedef typename property_traits::value_type comp_type; sl@0: // c_count initialized to "nil" (with nil represented by (max)()) sl@0: comp_type c_count((std::numeric_limits::max)()); sl@0: detail::components_recorder vis(c, c_count); sl@0: depth_first_search(g, params.visitor(vis)); sl@0: return c_count + 1; sl@0: } sl@0: sl@0: template sl@0: inline typename property_traits::value_type sl@0: connected_components(const Graph& g, ComponentMap c) sl@0: { sl@0: if (num_vertices(g) == 0) return 0; sl@0: sl@0: typedef typename graph_traits::vertex_descriptor Vertex; sl@0: function_requires< WritablePropertyMapConcept >(); sl@0: typedef typename boost::graph_traits::directed_category directed; sl@0: BOOST_STATIC_ASSERT((boost::is_same::value)); sl@0: sl@0: typedef typename property_traits::value_type comp_type; sl@0: // c_count initialized to "nil" (with nil represented by (max)()) sl@0: comp_type c_count((std::numeric_limits::max)()); sl@0: detail::components_recorder vis(c, c_count); sl@0: depth_first_search(g, visitor(vis)); sl@0: return c_count + 1; sl@0: } sl@0: sl@0: sl@0: } // namespace boost sl@0: sl@0: sl@0: #endif // BOOST_GRAPH_CONNECTED_COMPONENTS_HPP