File indexing completed on 2025-01-30 09:42:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
0012 #define BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
0013
0014 #include <boost/config.hpp>
0015 #include <boost/graph/depth_first_search.hpp>
0016 #include <boost/graph/properties.hpp>
0017 #include <boost/graph/graph_concepts.hpp>
0018 #include <boost/graph/overloading.hpp>
0019 #include <boost/graph/detail/mpi_include.hpp>
0020 #include <boost/static_assert.hpp>
0021 #include <boost/concept/assert.hpp>
0022
0023 namespace boost
0024 {
0025
0026 namespace detail
0027 {
0028
0029
0030
0031
0032 template < class ComponentsMap >
0033 class components_recorder : public dfs_visitor<>
0034 {
0035 typedef typename property_traits< ComponentsMap >::value_type comp_type;
0036
0037 public:
0038 components_recorder(ComponentsMap c, comp_type& c_count)
0039 : m_component(c), m_count(c_count)
0040 {
0041 }
0042
0043 template < class Vertex, class Graph > void start_vertex(Vertex, Graph&)
0044 {
0045 if (m_count == (std::numeric_limits< comp_type >::max)())
0046 m_count = 0;
0047 else
0048 ++m_count;
0049 }
0050 template < class Vertex, class Graph >
0051 void discover_vertex(Vertex u, Graph&)
0052 {
0053 put(m_component, u, m_count);
0054 }
0055
0056 protected:
0057 ComponentsMap m_component;
0058 comp_type& m_count;
0059 };
0060
0061 }
0062
0063
0064
0065
0066 template < class Graph, class ComponentMap, class P, class T, class R >
0067 inline typename property_traits< ComponentMap >::value_type
0068 connected_components(const Graph& g, ComponentMap c,
0069 const bgl_named_params< P, T, R >& params BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
0070 Graph, vertex_list_graph_tag))
0071 {
0072 if (num_vertices(g) == 0)
0073 return 0;
0074
0075 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
0076 BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< ComponentMap, Vertex >));
0077 typedef typename boost::graph_traits< Graph >::directed_category directed;
0078 BOOST_STATIC_ASSERT((boost::is_same< directed, undirected_tag >::value));
0079
0080 typedef typename property_traits< ComponentMap >::value_type comp_type;
0081
0082 comp_type c_count((std::numeric_limits< comp_type >::max)());
0083 detail::components_recorder< ComponentMap > vis(c, c_count);
0084 depth_first_search(g, params.visitor(vis));
0085 return c_count + 1;
0086 }
0087
0088 template < class Graph, class ComponentMap >
0089 inline typename property_traits< ComponentMap >::value_type
0090 connected_components(const Graph& g,
0091 ComponentMap c BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
0092 Graph, vertex_list_graph_tag))
0093 {
0094 if (num_vertices(g) == 0)
0095 return 0;
0096
0097 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
0098 BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< ComponentMap, Vertex >));
0099
0100
0101
0102 typedef typename property_traits< ComponentMap >::value_type comp_type;
0103
0104 comp_type c_count((std::numeric_limits< comp_type >::max)());
0105 detail::components_recorder< ComponentMap > vis(c, c_count);
0106 depth_first_search(g, visitor(vis));
0107 return c_count + 1;
0108 }
0109
0110 }
0111
0112 #include BOOST_GRAPH_MPI_INCLUDE( <boost/graph/distributed/connected_components.hpp >)
0113
0114 #endif