Chris@16
|
1 //
|
Chris@16
|
2 //=======================================================================
|
Chris@16
|
3 // Copyright 1997-2001 University of Notre Dame.
|
Chris@16
|
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //=======================================================================
|
Chris@16
|
10 //
|
Chris@16
|
11 #ifndef BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
|
Chris@16
|
12 #define BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <boost/graph/depth_first_search.hpp>
|
Chris@16
|
16 #include <boost/graph/properties.hpp>
|
Chris@16
|
17 #include <boost/graph/graph_concepts.hpp>
|
Chris@16
|
18 #include <boost/graph/overloading.hpp>
|
Chris@16
|
19 #include <boost/static_assert.hpp>
|
Chris@16
|
20 #include <boost/concept/assert.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23
|
Chris@16
|
24 namespace detail {
|
Chris@16
|
25
|
Chris@16
|
26 // This visitor is used both in the connected_components algorithm
|
Chris@16
|
27 // and in the kosaraju strong components algorithm during the
|
Chris@16
|
28 // second DFS traversal.
|
Chris@16
|
29 template <class ComponentsMap>
|
Chris@16
|
30 class components_recorder : public dfs_visitor<>
|
Chris@16
|
31 {
|
Chris@16
|
32 typedef typename property_traits<ComponentsMap>::value_type comp_type;
|
Chris@16
|
33 public:
|
Chris@16
|
34 components_recorder(ComponentsMap c,
|
Chris@16
|
35 comp_type& c_count)
|
Chris@16
|
36 : m_component(c), m_count(c_count) {}
|
Chris@16
|
37
|
Chris@16
|
38 template <class Vertex, class Graph>
|
Chris@16
|
39 void start_vertex(Vertex, Graph&) {
|
Chris@16
|
40 if (m_count == (std::numeric_limits<comp_type>::max)())
|
Chris@16
|
41 m_count = 0; // start counting components at zero
|
Chris@16
|
42 else
|
Chris@16
|
43 ++m_count;
|
Chris@16
|
44 }
|
Chris@16
|
45 template <class Vertex, class Graph>
|
Chris@16
|
46 void discover_vertex(Vertex u, Graph&) {
|
Chris@16
|
47 put(m_component, u, m_count);
|
Chris@16
|
48 }
|
Chris@16
|
49 protected:
|
Chris@16
|
50 ComponentsMap m_component;
|
Chris@16
|
51 comp_type& m_count;
|
Chris@16
|
52 };
|
Chris@16
|
53
|
Chris@16
|
54 } // namespace detail
|
Chris@16
|
55
|
Chris@16
|
56 // This function computes the connected components of an undirected
|
Chris@16
|
57 // graph using a single application of depth first search.
|
Chris@16
|
58
|
Chris@16
|
59 template <class Graph, class ComponentMap, class P, class T, class R>
|
Chris@16
|
60 inline typename property_traits<ComponentMap>::value_type
|
Chris@16
|
61 connected_components(const Graph& g, ComponentMap c,
|
Chris@16
|
62 const bgl_named_params<P, T, R>& params
|
Chris@16
|
63 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
|
Chris@16
|
64 {
|
Chris@16
|
65 if (num_vertices(g) == 0) return 0;
|
Chris@16
|
66
|
Chris@16
|
67 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
68 BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept<ComponentMap, Vertex> ));
|
Chris@16
|
69 typedef typename boost::graph_traits<Graph>::directed_category directed;
|
Chris@16
|
70 BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
|
Chris@16
|
71
|
Chris@16
|
72 typedef typename property_traits<ComponentMap>::value_type comp_type;
|
Chris@16
|
73 // c_count initialized to "nil" (with nil represented by (max)())
|
Chris@16
|
74 comp_type c_count((std::numeric_limits<comp_type>::max)());
|
Chris@16
|
75 detail::components_recorder<ComponentMap> vis(c, c_count);
|
Chris@16
|
76 depth_first_search(g, params.visitor(vis));
|
Chris@16
|
77 return c_count + 1;
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 template <class Graph, class ComponentMap>
|
Chris@16
|
81 inline typename property_traits<ComponentMap>::value_type
|
Chris@16
|
82 connected_components(const Graph& g, ComponentMap c
|
Chris@16
|
83 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
|
Chris@16
|
84 {
|
Chris@16
|
85 if (num_vertices(g) == 0) return 0;
|
Chris@16
|
86
|
Chris@16
|
87 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
88 BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept<ComponentMap, Vertex> ));
|
Chris@16
|
89 // typedef typename boost::graph_traits<Graph>::directed_category directed;
|
Chris@16
|
90 // BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
|
Chris@16
|
91
|
Chris@16
|
92 typedef typename property_traits<ComponentMap>::value_type comp_type;
|
Chris@16
|
93 // c_count initialized to "nil" (with nil represented by (max)())
|
Chris@16
|
94 comp_type c_count((std::numeric_limits<comp_type>::max)());
|
Chris@16
|
95 detail::components_recorder<ComponentMap> vis(c, c_count);
|
Chris@16
|
96 depth_first_search(g, visitor(vis));
|
Chris@16
|
97 return c_count + 1;
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100
|
Chris@16
|
101 } // namespace boost
|
Chris@16
|
102
|
Chris@16
|
103 #ifdef BOOST_GRAPH_USE_MPI
|
Chris@16
|
104 # include <boost/graph/distributed/connected_components.hpp>
|
Chris@16
|
105 #endif
|
Chris@16
|
106
|
Chris@16
|
107 #endif // BOOST_GRAPH_CONNECTED_COMPONENTS_HPP
|