2 //=======================================================================
3 // Copyright 1997-2001 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 //=======================================================================
11 #ifndef BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
12 #define BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
14 #include <boost/config.hpp>
15 #include <boost/graph/depth_first_search.hpp>
16 #include <boost/graph/properties.hpp>
17 #include <boost/graph/graph_concepts.hpp>
19 #include <boost/static_assert.hpp>
25 // This visitor is used both in the connected_components algorithm
26 // and in the kosaraju strong components algorithm during the
27 // second DFS traversal.
28 template <class ComponentsMap>
29 class components_recorder : public dfs_visitor<>
31 typedef typename property_traits<ComponentsMap>::value_type comp_type;
33 components_recorder(ComponentsMap c,
35 : m_component(c), m_count(c_count) {}
37 template <class Vertex, class Graph>
38 void start_vertex(Vertex, Graph&) {
39 if (m_count == (std::numeric_limits<comp_type>::max)())
40 m_count = 0; // start counting components at zero
44 template <class Vertex, class Graph>
45 void discover_vertex(Vertex u, Graph&) {
46 put(m_component, u, m_count);
49 ComponentsMap m_component;
55 // This function computes the connected components of an undirected
56 // graph using a single application of depth first search.
58 template <class Graph, class ComponentMap, class P, class T, class R>
59 inline typename property_traits<ComponentMap>::value_type
60 connected_components(const Graph& g, ComponentMap c,
61 const bgl_named_params<P, T, R>& params)
63 if (num_vertices(g) == 0) return 0;
65 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
66 function_requires< WritablePropertyMapConcept<ComponentMap, Vertex> >();
67 typedef typename boost::graph_traits<Graph>::directed_category directed;
68 BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
70 typedef typename property_traits<ComponentMap>::value_type comp_type;
71 // c_count initialized to "nil" (with nil represented by (max)())
72 comp_type c_count((std::numeric_limits<comp_type>::max)());
73 detail::components_recorder<ComponentMap> vis(c, c_count);
74 depth_first_search(g, params.visitor(vis));
78 template <class Graph, class ComponentMap>
79 inline typename property_traits<ComponentMap>::value_type
80 connected_components(const Graph& g, ComponentMap c)
82 if (num_vertices(g) == 0) return 0;
84 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
85 function_requires< WritablePropertyMapConcept<ComponentMap, Vertex> >();
86 typedef typename boost::graph_traits<Graph>::directed_category directed;
87 BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
89 typedef typename property_traits<ComponentMap>::value_type comp_type;
90 // c_count initialized to "nil" (with nil represented by (max)())
91 comp_type c_count((std::numeric_limits<comp_type>::max)());
92 detail::components_recorder<ComponentMap> vis(c, c_count);
93 depth_first_search(g, visitor(vis));
101 #endif // BOOST_GRAPH_CONNECTED_COMPONENTS_HPP