connected_components.hpp 4.0 KB

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