connected_components.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 2000-2001
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Boost Graph Library: Connected Components</Title>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1>
  16. <A NAME="sec:connected-components">
  17. <img src="figs/python.gif" alt="(Python)"/>
  18. <TT>connected_components</TT></A>
  19. </H1>
  20. <PRE>
  21. <i>// named parameter version</i>
  22. template &lt;class VertexListGraph, class ComponentMap, class P, class T, class R&gt;
  23. typename property_traits&lt;ComponentMap&gt;::value_type
  24. connected_components(VertexListGraph&amp; G, ComponentMap comp,
  25. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>);
  26. <i>// there is not a non-named parameter version of this function</i>
  27. </PRE>
  28. <P>
  29. The <TT>connected_components()</TT> functions compute the connected
  30. components of an undirected graph using a DFS-based approach. A
  31. <b><I>connected component</I></b> of an undirected graph is a set of
  32. vertices that are all reachable from each other. If the connected
  33. components need to be maintained while a graph is growing the
  34. disjoint-set based approach of function <a
  35. href="./incremental_components.html">
  36. <TT>incremental_components()</TT></a> is faster. For ``static'' graphs
  37. this DFS-based approach is faster&nbsp;[<A
  38. HREF="bibliography.html#clr90">8</A>].
  39. <P>
  40. The output of the algorithm is recorded in the component property map
  41. <TT>comp</TT>, which will contain numbers giving the component number
  42. assigned to each vertex. The total number of components is the return
  43. value of the function.
  44. <H3>Where Defined</H3>
  45. <P>
  46. <a href="../../../boost/graph/connected_components.hpp"><TT>boost/graph/connected_components.hpp</TT></a>
  47. <h3>Parameters</h3>
  48. IN: <tt>const Graph&amp; g</tt>
  49. <blockquote>
  50. An undirected graph. The graph type must be a model of <a
  51. href="VertexListGraph.html">Vertex List Graph</a> and <a
  52. href="IncidenceGraph.html">Incidence Graph</a>.<br>
  53. <b>Python</b>: The parameter is named <tt>graph</tt>.
  54. </blockquote>
  55. OUT: <tt>ComponentMap c</tt>
  56. <blockquote>
  57. The algorithm computes how many connected components are in the graph,
  58. and assigning each component an integer label. The algorithm then
  59. records which component each vertex in the graph belongs to by
  60. recording the component number in the component property map. The
  61. <tt>ComponentMap</tt> type must be a model of <a
  62. href="../../property_map/doc/WritablePropertyMap.html">Writable Property
  63. Map</a>. The value type should be an integer type, preferably the same
  64. as the <tt>vertices_size_type</tt> of the graph. The key type must be
  65. the graph's vertex descriptor type.<br>
  66. <b>Python</b>: Must be an <tt>vertex_int_map</tt> for the graph.<br>
  67. <b>Python default</b>: <tt>graph.get_vertex_int_map("component")</tt>
  68. </blockquote>
  69. <h3>Named Parameters</h3>
  70. UTIL: <tt>color_map(ColorMap color)</tt>
  71. <blockquote>
  72. This is used by the algorithm to keep track of its progress through
  73. the graph. The type <tt>ColorMap</tt> must be a model of <a
  74. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  75. Property Map</a> and its key type must be the graph's vertex
  76. descriptor type and the value type of the color map must model
  77. <a href="./ColorValue.html">ColorValue</a>.<br>
  78. <b>Default:</b> an <a
  79. href="../../property_map/doc/iterator_property_map.html">
  80. </tt>iterator_property_map</tt></a> created from a
  81. <tt>std::vector</tt> of <tt>default_color_type</tt> of size
  82. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  83. map.<br>
  84. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  85. the graph.
  86. </blockquote>
  87. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  88. <blockquote>
  89. This maps each vertex to an integer in the range <tt>[0,
  90. num_vertices(g))</tt>. This parameter is only necessary when the
  91. default color property map is used. The type <tt>VertexIndexMap</tt>
  92. must be a model of <a
  93. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  94. Map</a>. The value type of the map must be an integer type. The
  95. vertex descriptor type of the graph needs to be usable as the key
  96. type of the map.<br>
  97. <b>Default:</b> <tt>get(vertex_index, g)</tt>.
  98. Note: if you use this default, make sure your graph has
  99. an internal <tt>vertex_index</tt> property. For example,
  100. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  101. not have an internal <tt>vertex_index</tt> property.<br>
  102. <b>Python</b>: Unsupported parameter.
  103. </blockquote>
  104. <H3>Complexity</H3>
  105. <P>
  106. The time complexity for the connected components algorithm is also
  107. <i>O(V + E)</i>.
  108. <P>
  109. <h3>See Also</h3>
  110. <a href="./strong_components.html"><tt>strong_components()</tt></a>
  111. and <a href="./incremental_components.html"><tt>incremental_components()</tt></a>
  112. <H3>Example</H3>
  113. <P>
  114. The file <a
  115. href="../example/connected_components.cpp"><tt>examples/connected_components.cpp</tt></a>
  116. contains an example of calculating the connected components of an
  117. undirected graph.
  118. <br>
  119. <HR>
  120. <TABLE>
  121. <TR valign=top>
  122. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  123. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  124. </TD></TR></TABLE>
  125. </BODY>
  126. </HTML>