incremental_components.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  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: Incremental Connected Components</Title>
  10. <style type="text/css">
  11. <!--
  12. .code
  13. {
  14. border-left-style: groove;
  15. border-left-width: 1px;
  16. padding-left: 2em;
  17. }
  18. -->
  19. </style>
  20. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  21. ALINK="#ff0000">
  22. <IMG SRC="../../../boost.png"
  23. ALT="C++ Boost" width="277" height="86">
  24. <BR Clear>
  25. <H1>Incremental Connected Components</H1>
  26. <P>
  27. This section describes a family of functions and classes that work
  28. together to calculate the connected components of an undirected graph.
  29. The algorithm used here is based on the disjoint-sets (fast
  30. union-find) data structure&nbsp;[<A
  31. HREF="bibliography.html#clr90">8</A>,<A
  32. HREF="bibliography.html#tarjan83:_data_struct_network_algo">27</A>]
  33. which is a good method to use for situations where the graph is
  34. growing (edges are being added) and the connected components
  35. information needs to be updated repeatedly. This method does not cover
  36. the situation where edges are both added and removed from the graph,
  37. hence it is called <b><i>incremental</i></b><a
  38. href="bibliography.html#eppstein97:dynamic_graph">[42]</a> (and not
  39. fully dynamic). The disjoint-sets class is described in Section <A
  40. HREF="../../disjoint_sets/disjoint_sets.html">Disjoint Sets</A>.
  41. <P>
  42. The following five operations are the primary functions that you will
  43. use to calculate and maintain the connected components. The objects
  44. used here are a graph <TT>g</TT>, a disjoint-sets structure <TT>ds</TT>,
  45. and vertices <TT>u</TT> and <TT>v</TT>.
  46. <P>
  47. <UL>
  48. <LI><TT>initialize_incremental_components(g, ds)</TT>
  49. <BR>
  50. Basic initialization of the disjoint-sets structure. Each
  51. vertex in the graph <TT>g</TT> is in its own set.
  52. </LI>
  53. <LI><TT>incremental_components(g, ds)</TT>
  54. <BR>
  55. The connected components are calculated based on the edges in the graph
  56. <TT>g</TT> and the information is embedded in <TT>ds</TT>.
  57. </LI>
  58. <LI><TT>ds.find_set(v)</TT>
  59. <BR>
  60. Extracts the component information for vertex <TT>v</TT> from the
  61. disjoint-sets.
  62. </LI>
  63. <LI><TT>ds.union_set(u, v)</TT>
  64. <BR>
  65. Update the disjoint-sets structure when edge <i>(u,v)</i> is added to the graph.
  66. </LI>
  67. </UL>
  68. <P>
  69. <H3>Complexity</H3>
  70. <P>
  71. The time complexity for the whole process is <i>O(V + E
  72. alpha(E,V))</i> where <i>E</i> is the total number of edges in the
  73. graph (by the end of the process) and <i>V</i> is the number of
  74. vertices. <i>alpha</i> is the inverse of Ackermann's function which
  75. has explosive recursively exponential growth. Therefore its inverse
  76. function grows <I>very</I> slowly. For all practical purposes
  77. <i>alpha(m,n) <= 4</i> which means the time complexity is only
  78. slightly larger than <i>O(V + E)</i>.
  79. <P>
  80. <H3>Example</H3>
  81. <P>
  82. Maintain the connected components of a graph while adding edges using
  83. the disjoint-sets data structure. The full source code for this
  84. example can be found in <a
  85. href="../example/incremental_components.cpp"><TT>examples/incremental_components.cpp</TT></a>.
  86. <P>
  87. <PRE class="code">
  88. using namespace boost;
  89. int main(int argc, char* argv[])
  90. {
  91. typedef adjacency_list <vecS, vecS, undirectedS> Graph;
  92. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  93. typedef graph_traits<Graph>::vertices_size_type VertexIndex;
  94. const int VERTEX_COUNT = 6;
  95. Graph graph(VERTEX_COUNT);
  96. std::vector<VertexIndex> rank(num_vertices(graph));
  97. std::vector<Vertex> parent(num_vertices(graph));
  98. typedef VertexIndex* Rank;
  99. typedef Vertex* Parent;
  100. disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]);
  101. initialize_incremental_components(graph, ds);
  102. incremental_components(graph, ds);
  103. graph_traits<Graph>::edge_descriptor edge;
  104. bool flag;
  105. boost::tie(edge, flag) = add_edge(0, 1, graph);
  106. ds.union_set(0,1);
  107. boost::tie(edge, flag) = add_edge(1, 4, graph);
  108. ds.union_set(1,4);
  109. boost::tie(edge, flag) = add_edge(4, 0, graph);
  110. ds.union_set(4,0);
  111. boost::tie(edge, flag) = add_edge(2, 5, graph);
  112. ds.union_set(2,5);
  113. std::cout << "An undirected graph:" << std::endl;
  114. print_graph(graph, get(boost::vertex_index, graph));
  115. std::cout << std::endl;
  116. BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
  117. std::cout << "representative[" << current_vertex << "] = " <<
  118. ds.find_set(current_vertex) << std::endl;
  119. }
  120. std::cout << std::endl;
  121. typedef component_index<VertexIndex> Components;
  122. // NOTE: Because we're using vecS for the graph type, we're
  123. // effectively using identity_property_map for a vertex index map.
  124. // If we were to use listS instead, the index map would need to be
  125. // explicity passed to the component_index constructor.
  126. Components components(parent.begin(), parent.end());
  127. // Iterate through the component indices
  128. BOOST_FOREACH(VertexIndex current_index, components) {
  129. std::cout << "component " << current_index << " contains: ";
  130. // Iterate through the child vertex indices for [current_index]
  131. BOOST_FOREACH(VertexIndex child_index,
  132. components[current_index]) {
  133. std::cout << child_index << " ";
  134. }
  135. std::cout << std::endl;
  136. }
  137. return (0);
  138. }
  139. </PRE>
  140. <P>
  141. <hr>
  142. <p>
  143. <H2><A NAME="sec:initialize-incremental-components"></A>
  144. <TT>initialize_incremental_components</TT>
  145. </H2>
  146. <P>
  147. <DIV ALIGN="left">
  148. <TABLE CELLPADDING=3 border>
  149. <TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
  150. <TD ALIGN="LEFT">undirected</TD>
  151. </TR>
  152. <TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
  153. <TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
  154. </TR>
  155. <TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
  156. <TD></TD>
  157. </TR>
  158. </TABLE>
  159. </DIV>
  160. <P>
  161. <PRE>
  162. template &lt;class VertexListGraph, class DisjointSets&gt;
  163. void initialize_incremental_components(VertexListGraph&amp; G, DisjointSets&amp; ds)
  164. </PRE>
  165. <P>
  166. This prepares the disjoint-sets data structure for the incremental
  167. connected components algorithm by making each vertex in the graph a
  168. member of its own component (or set).
  169. <P>
  170. <H3>Where Defined</H3>
  171. <P>
  172. <a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>
  173. <p>
  174. <hr>
  175. <P>
  176. <H2><A NAME="sec:incremental-components"></A>
  177. <TT>incremental_components</TT>
  178. </H2>
  179. <P>
  180. <DIV ALIGN="left">
  181. <TABLE CELLPADDING=3 border>
  182. <TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
  183. <TD ALIGN="LEFT">undirected</TD>
  184. </TR>
  185. <TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
  186. <TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
  187. </TR>
  188. <TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
  189. <TD ALIGN="LEFT"><i>O(E)</i></TD>
  190. </TR>
  191. </TABLE>
  192. </DIV>
  193. <p>
  194. <PRE>
  195. template &lt;class EdgeListGraph, class DisjointSets&gt;
  196. void incremental_components(EdgeListGraph&amp; g, DisjointSets&amp; ds)
  197. </PRE>
  198. <P>
  199. This function calculates the connected components of the graph,
  200. embedding the results in the disjoint-sets data structure.
  201. <P>
  202. <H3>Where Defined</H3>
  203. <P>
  204. <a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>
  205. <P>
  206. <H3>Requirements on Types</H3>
  207. <P>
  208. <UL>
  209. <LI>The graph type must be a model of <a href="./EdgeListGraph.html">EdgeListGraph</a>.
  210. </LI>
  211. </UL>
  212. <P>
  213. <hr>
  214. <p>
  215. <H2><A NAME="sec:same-component">
  216. <TT>same_component</TT></A>
  217. </H2>
  218. <P>
  219. <DIV ALIGN="left">
  220. <TABLE CELLPADDING=3 border>
  221. <TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
  222. <TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
  223. </TR>
  224. <TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
  225. <TD ALIGN="LEFT"><i>O(alpha(E,V))</i></TD>
  226. </TR>
  227. </TABLE>
  228. </DIV>
  229. <P>
  230. <PRE>
  231. template &lt;class Vertex, class DisjointSet&gt;
  232. bool same_component(Vertex u, Vertex v, DisjointSet&amp; ds)
  233. </PRE>
  234. <P>
  235. This function determines whether <TT>u</TT> and <TT>v</TT> are in the same
  236. component.
  237. <P>
  238. <H3>Where Defined</H3>
  239. <P>
  240. <a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>
  241. <P>
  242. <H3>Requirements on Types</H3>
  243. <P>
  244. <UL>
  245. <LI><TT>Vertex</TT> must be compatible with the rank and parent
  246. property maps of the <TT>DisjointSets</TT> data structure.
  247. </LI>
  248. </UL>
  249. <P>
  250. <hr>
  251. <p>
  252. <H2><A NAME="sec:component-index"></A>
  253. <TT>component_index</TT>
  254. </H2>
  255. <p>
  256. <PRE>
  257. component_index&lt;Index&gt;
  258. </PRE>
  259. <P>
  260. The <tt>component_index</tt> class provides an STL
  261. container-like view for the components of the graph. Each component is
  262. a container-like object, and access is provided via
  263. the <TT>operator[]</TT>. A <TT>component_index</TT> object is
  264. initialized with the parents property in the disjoint-sets calculated
  265. from the <TT>incremental_components()</TT> function. Optionally, a
  266. vertex -&gt; index property map is passed in
  267. (<tt>identity_property_map</tt> is used by default).
  268. <P>
  269. <H3>Where Defined</H3>
  270. <P>
  271. <a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>
  272. <P>
  273. <H3>Members</H3>
  274. <P>
  275. <table border>
  276. <tr>
  277. <th>Member</th> <th>Description</th>
  278. </tr>
  279. <tr>
  280. <td><tt>value_type/size_type</tt></td>
  281. <td>
  282. The type for a component index (same as <tt>Index</tt>).
  283. </td>
  284. </tr>
  285. <tr>
  286. <td><tt>size_type size()</tt></td>
  287. <td>
  288. Returns the number of components in the graph.
  289. </td>
  290. </tr>
  291. <tr>
  292. <td><tt>iterator/const_iterator</tt></td>
  293. <td>
  294. Iterators used to traverse the available component indices [0 to <tt>size()</tt>).
  295. </td>
  296. </tr>
  297. <tr>
  298. <td><tt>iterator begin() const</tt></td>
  299. <td>
  300. Returns an iterator at the start of the component indices (0).
  301. </td>
  302. </tr>
  303. <tr>
  304. <td><tt>iterator end() const</tt></td>
  305. <td>
  306. Returns an iterator past the end of the component indices (<tt>size()</tt>).
  307. </td>
  308. </tr>
  309. <tr>
  310. <td><tt>std::pair&lt;component_iterator, component_iterator&gt; operator[size_type index] const</tt></td>
  311. <td>
  312. Returns a pair of iterators for the component at <tt>index</tt> where <tt>index</tt> is in [0, <tt>size()</tt>).
  313. </td>
  314. </tr>
  315. </table>
  316. <br>
  317. <HR>
  318. <TABLE>
  319. <TR valign=top>
  320. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  321. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  322. Indiana University (<A
  323. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  324. <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
  325. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  326. Indiana University (<A
  327. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  328. </TD></TR></TABLE>
  329. </BODY>
  330. </HTML>