disjoint_sets.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  6. <title>Boost Disjoint Sets</title>
  7. </head>
  8. <body bgcolor="#FFFFFF" link="#0000EE" text="#000000" vlink="#551A8B" alink=
  9. "#FF0000">
  10. <img src="../../boost.png" alt="C++ Boost" width="277" height=
  11. "86"><br clear="none">
  12. <h1><a name="sec:disjoint-sets" id="sec:disjoint-sets"></a> Disjoint
  13. Sets</h1>
  14. <pre>
  15. disjoint_sets&lt;Rank, Parent, FindCompress&gt;
  16. </pre>
  17. <p>This is class that provides disjoint sets operations with <i>union by
  18. rank</i> and <i>path compression</i>. A disjoint-sets data structure
  19. maintains a collection <i>S = {S<sub>1</sub>, S<sub>2</sub>, ...,
  20. S<sub>k</sub>}</i> of disjoint sets. Each set is identified by a
  21. <i>representative</i> which is some member of of the set. Sets are
  22. represented by rooted trees which are encoded in the <tt>Parent</tt>
  23. property map. Two heuristics: "union by rank" and "path compression" are
  24. used to speed up the operations&nbsp;[<a href=
  25. "./bibliography.html#tarjan83:_data_struct_network_algo">1</a>, <a href=
  26. "./bibliography.html#clr90">2</a>].</p>
  27. <h3>Where Defined</h3><a href=
  28. "../../boost/pending/disjoint_sets.hpp"><tt>boost/pending/disjoint_sets.hpp</tt></a>
  29. <h3>Template Parameters</h3>
  30. <table border summary="">
  31. <tr>
  32. <td><tt>Rank</tt></td>
  33. <td>must be a model of <a href=
  34. "../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
  35. with an integer value type and a key type equal to the set's element
  36. type.</td>
  37. </tr>
  38. <tr>
  39. <td><tt>Parent</tt></td>
  40. <td>must be a model of <a href=
  41. "../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
  42. and the key and value type the same as the set's element type.</td>
  43. </tr>
  44. <tr>
  45. <td><tt>FindCompress</tt></td>
  46. <td>should be one of the find representative and path compress function
  47. objects.</td>
  48. </tr>
  49. </table>
  50. <h3>Example</h3>
  51. <p>A typical usage pattern for <tt>disjoint_sets</tt> can be seen in the
  52. <a href=
  53. "../graph/doc/kruskal_min_spanning_tree.html"><tt>kruskal_minimum_spanning_tree()</tt></a>
  54. algorithm. In this example, we call <tt>link()</tt> instead of
  55. <tt>union_set()</tt> because <tt>u</tt> and <tt>v</tt> were obtained from
  56. <tt>find_set()</tt> and therefore are already the representatives for their
  57. sets.</p>
  58. <pre>
  59. ...
  60. disjoint_sets&lt;Rank, Parent, FindCompress&gt; dsets(rank, p);
  61. for (ui = vertices(G).first; ui != vertices(G).second; ++ui)
  62. dsets.make_set(*ui);
  63. ...
  64. while ( !Q.empty() ) {
  65. e = Q.front();
  66. Q.pop();
  67. u = dsets.find_set(source(e));
  68. v = dsets.find_set(target(e));
  69. if ( u != v ) {
  70. *out++ = e;
  71. dsets.link(u, v);
  72. }
  73. }
  74. </pre>
  75. <h3>Members</h3>
  76. <table border summary="">
  77. <tr>
  78. <th>Member</th>
  79. <th>Description</th>
  80. </tr>
  81. <tr>
  82. <td><tt>disjoint_sets(Rank r, Parent p)</tt></td>
  83. <td>Constructor.</td>
  84. </tr>
  85. <tr>
  86. <td><tt>disjoint_sets(const disjoint_sets&amp; x)</tt></td>
  87. <td>Copy constructor.</td>
  88. </tr>
  89. <tr>
  90. <td><tt>template &lt;class Element&gt;<br>
  91. void make_set(Element x)</tt></td>
  92. <td>Creates a singleton set containing Element <tt>x</tt>.</td>
  93. </tr>
  94. <tr>
  95. <td><tt>template &lt;class Element&gt;<br>
  96. void link(Element x, Element y)</tt></td>
  97. <td>Union the two sets <i>represented</i> by element <tt>x</tt> and
  98. <tt>y</tt>.</td>
  99. </tr>
  100. <tr>
  101. <td><tt>template &lt;class Element&gt;<br>
  102. void union_set(Element x, Element y)</tt></td>
  103. <td>Union the two sets that <i>contain</i> elements <tt>x</tt> and
  104. <tt>y</tt>. This is equivalent to
  105. <tt>link(find_set(x),find_set(y))</tt>.</td>
  106. </tr>
  107. <tr>
  108. <td><tt>template &lt;class Element&gt;<br>
  109. Element find_set(Element x)</tt></td>
  110. <td>Return the representative for the set containing element
  111. <tt>x</tt>.</td>
  112. </tr>
  113. <tr>
  114. <td><tt>template &lt;class ElementIterator&gt;<br>
  115. std::size_t count_sets(ElementIterator first, ElementIterator
  116. last)</tt></td>
  117. <td>Returns the number of disjoint sets.</td>
  118. </tr>
  119. <tr>
  120. <td><tt>template &lt;class ElementIterator&gt;<br>
  121. void compress_sets(ElementIterator first, ElementIterator
  122. last)</tt></td>
  123. <td>Flatten the parents tree so that the parent of every element is its
  124. representative.</td>
  125. </tr>
  126. </table>
  127. <h3>Complexity</h3>
  128. <p>The time complexity is <i>O(m alpha(m,n))</i>, where <i>alpha</i> is the
  129. inverse Ackermann's function, <i>m</i> is the number of disjoint-set
  130. operations (<tt>make_set()</tt>, <tt>find_set()</tt>, and <tt>link()</tt>
  131. and <i>n</i> is the number of elements. The <i>alpha</i> function grows
  132. very slowly, much more slowly than the <i>log</i> function.</p>
  133. <h3>See Also</h3><a href=
  134. "../graph/doc/incremental_components.html"><tt>incremental_connected_components()</tt></a>
  135. <hr>
  136. <pre>
  137. disjoint_sets_with_storage&lt;ID,InverseID,FindCompress&gt;
  138. </pre>
  139. <p>This class manages the storage for the rank and parent properties
  140. internally. The storage is in arrays, which are indexed by element ID,
  141. hence the requirement for the <tt>ID</tt> and <tt>InverseID</tt> functors.
  142. The rank and parent properties are initialized during construction so the
  143. each element is in a set by itself (so it is not necessary to initialize
  144. objects of this class with the <a href=
  145. "../graph/doc/incremental_components.html#sec:initialize-incremental-components">
  146. <tt>initialize_incremental_components()</tt></a> function). This class is
  147. especially useful when computing the (dynamic) connected components of an
  148. <tt>edge_list</tt> graph which does not provide a place to store vertex
  149. properties.</p>
  150. <h3>Template Parameters</h3>
  151. <table border summary="">
  152. <tr>
  153. <th>Parameter</th>
  154. <th>Description</th>
  155. <th>Default</th>
  156. </tr>
  157. <tr>
  158. <td><tt>ID</tt></td>
  159. <td>must be a model of <a href=
  160. "../property_map/doc/ReadablePropertyMap.html">ReadablePropertyMap</a> that
  161. maps elements to integers between zero 0 and N, the total number of
  162. elements in the sets.</td>
  163. <td><tt>boost::identity_property_map</tt></td>
  164. </tr>
  165. <tr>
  166. <td><tt>InverseID</tt></td>
  167. <td>must be a model of <a href=
  168. "../property_map/doc/ReadablePropertyMap.html">ReadablePropertyMap</a> that
  169. maps integers to elements.</td>
  170. <td><tt>boost::identity_property_map</tt></td>
  171. </tr>
  172. <tr>
  173. <td><tt>FindCompress</tt></td>
  174. <td>should be one of the find representative and path compress function
  175. objects.</td>
  176. <td><tt>representative_with_full_path_compression</tt></td>
  177. </tr>
  178. </table>
  179. <h3>Members</h3>
  180. <p>This class has all of the members in <tt>disjoint_sets</tt> as well as
  181. the following members.</p>
  182. <pre>
  183. disjoint_sets_with_storage(size_type n = 0,
  184. ID id = ID(),
  185. InverseID inv = InverseID())
  186. </pre>Constructor.
  187. <pre>
  188. template &lt;class ElementIterator&gt;
  189. void disjoint_sets_with_storage::
  190. normalize_sets(ElementIterator first, ElementIterator last)
  191. </pre>This rearranges the representatives such that the representative of
  192. each set is the element with the smallest ID.<br>
  193. Postcondition: <tt>v &gt;= parent[v]</tt><br>
  194. Precondition: the disjoint sets structure must be compressed.<br>
  195. <hr>
  196. <h2><a name="sec:representative-with-path-halving" id=
  197. "sec:representative-with-path-halving"></a></h2>
  198. <pre>
  199. representative_with_path_halving&lt;Parent&gt;
  200. </pre>
  201. <p>This is a functor which finds the representative vertex for the same
  202. component as the element <tt>x</tt>. While traversing up the representative
  203. tree, the functor also applies the path halving technique to shorten the
  204. height of the tree.</p>
  205. <pre>
  206. Element operator()(Parent p, Element x)
  207. </pre>
  208. <hr>
  209. <h2><a name="sec:representative-with-full-path-compression" id=
  210. "sec:representative-with-full-path-compression"></a><br></h2>
  211. <pre>
  212. representative_with_full_path_compression&lt;Parent&gt;
  213. </pre>
  214. <p>This is a functor which finds the representative element for the set
  215. that element <tt>x</tt> belongs to.</p>
  216. <pre>
  217. Element operator()(Parent p, Element x)
  218. </pre>
  219. <p><br></p>
  220. <hr>
  221. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  222. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  223. height="31" width="88"></a></p>
  224. <p>Revised
  225. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->01
  226. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38508" --></p>
  227. <table summary="">
  228. <tr valign="top">
  229. <td nowrap><i>Copyright &copy; 2000</i></td>
  230. <td><i><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>, Univ.of
  231. Notre Dame (<a href="mailto:jsiek@lsc.nd.edu">jsiek@lsc.nd.edu</a>)<br>
  232. <a href="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</a>,
  233. Univ.of Notre Dame (<a href=
  234. "mailto:llee1@lsc.nd.edu">llee1@lsc.nd.edu</a>)<br>
  235. <a href="http://www.lsc.nd.edu/~lums">Andrew Lumsdaine</a>, Univ.of
  236. Notre Dame (<a href=
  237. "mailto:lums@lsc.nd.edu">lums@lsc.nd.edu</a>)</i></td>
  238. </tr>
  239. </table>
  240. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  241. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  242. copy at <a href=
  243. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  244. </body>
  245. </html>