connected_components.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. {
  23. namespace detail
  24. {
  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<>
  30. {
  31. typedef typename property_traits< ComponentsMap >::value_type comp_type;
  32. public:
  33. components_recorder(ComponentsMap c, comp_type& c_count)
  34. : m_component(c), m_count(c_count)
  35. {
  36. }
  37. template < class Vertex, class Graph > void start_vertex(Vertex, Graph&)
  38. {
  39. if (m_count == (std::numeric_limits< comp_type >::max)())
  40. m_count = 0; // start counting components at zero
  41. else
  42. ++m_count;
  43. }
  44. template < class Vertex, class Graph >
  45. void discover_vertex(Vertex u, Graph&)
  46. {
  47. put(m_component, u, m_count);
  48. }
  49. protected:
  50. ComponentsMap m_component;
  51. comp_type& m_count;
  52. };
  53. } // namespace detail
  54. // This function computes the connected components of an undirected
  55. // graph using a single application of depth first search.
  56. template < class Graph, class ComponentMap, class P, class T, class R >
  57. inline typename property_traits< ComponentMap >::value_type
  58. connected_components(const Graph& g, ComponentMap c,
  59. const bgl_named_params< P, T, R >& params BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  60. Graph, vertex_list_graph_tag))
  61. {
  62. if (num_vertices(g) == 0)
  63. return 0;
  64. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  65. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< ComponentMap, Vertex >));
  66. typedef typename boost::graph_traits< Graph >::directed_category directed;
  67. BOOST_STATIC_ASSERT((boost::is_same< directed, undirected_tag >::value));
  68. typedef typename property_traits< ComponentMap >::value_type comp_type;
  69. // c_count initialized to "nil" (with nil represented by (max)())
  70. comp_type c_count((std::numeric_limits< comp_type >::max)());
  71. detail::components_recorder< ComponentMap > vis(c, c_count);
  72. depth_first_search(g, params.visitor(vis));
  73. return c_count + 1;
  74. }
  75. template < class Graph, class ComponentMap >
  76. inline typename property_traits< ComponentMap >::value_type
  77. connected_components(const Graph& g,
  78. ComponentMap c BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  79. Graph, vertex_list_graph_tag))
  80. {
  81. if (num_vertices(g) == 0)
  82. return 0;
  83. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  84. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept< ComponentMap, Vertex >));
  85. // typedef typename boost::graph_traits<Graph>::directed_category directed;
  86. // BOOST_STATIC_ASSERT((boost::is_same<directed, undirected_tag>::value));
  87. typedef typename property_traits< ComponentMap >::value_type comp_type;
  88. // c_count initialized to "nil" (with nil represented by (max)())
  89. comp_type c_count((std::numeric_limits< comp_type >::max)());
  90. detail::components_recorder< ComponentMap > vis(c, c_count);
  91. depth_first_search(g, visitor(vis));
  92. return c_count + 1;
  93. }
  94. } // namespace boost
  95. #include BOOST_GRAPH_MPI_INCLUDE(< boost / graph / distributed / connected_components.hpp >)
  96. #endif // BOOST_GRAPH_CONNECTED_COMPONENTS_HPP