kruskal_min_spanning_tree.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 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: Kruskal Minimum Spanning Tree</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><A NAME="sec:kruskal">
  16. <img src="figs/python.gif" alt="(Python)"/>
  17. <TT>kruskal_minimum_spanning_tree</TT>
  18. </H1>
  19. <PRE>
  20. template &lt;class Graph, class OutputIterator, class P, class T, class R&gt;
  21. OutputIterator
  22. kruskal_minimum_spanning_tree(Graph&amp; g, OutputIterator tree_edges,
  23. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>);
  24. </PRE>
  25. <P>
  26. The <tt>kruskal_minimum_spanning_tree()</tt> function find a minimum
  27. spanning tree (MST) in an undirected graph with weighted edges. A MST is a
  28. set of edges that connects all the vertices in the graph where the
  29. total weight of the edges in the tree is minimized. For more details,
  30. see section <a
  31. href="graph_theory_review.html#sec:minimum-spanning-tree">Minimum
  32. Spanning Tree Problem</a>. The edges in the MST are output to the
  33. <tt>tree_edges</tt> output iterator. This function uses Kruskal's
  34. algorithm to compute the MST&nbsp;[<A
  35. HREF="bibliography.html#kruskal56">18</A>,<A
  36. HREF="bibliography.html#clr90">8</A>,<A
  37. HREF="bibliography.html#tarjan83:_data_struct_network_algo">27</A>,<A
  38. HREF="bibliography.html#graham85">15</A>].
  39. </p>
  40. <p>
  41. Kruskal's algorithm starts with each vertex in a tree by itself, and
  42. with no edges in the minimum spanning tree <i>T</i>. The algorithm
  43. then examines each edge in the graph in order of increasing edge
  44. weight. If an edge connects two vertices in different trees the
  45. algorithm merges the two trees into a single tree and adds the edge to
  46. <i>T</i>. We use the ``union by rank'' and ``path compression''
  47. heuristics to provide fast implementations of the disjoint set
  48. operations (<tt>MAKE-SET</tt>, <tt>FIND-SET</tt>, and
  49. <tt>UNION-SET</tt>). The algorithm is as follows:
  50. </p>
  51. <pre>
  52. KRUSKAL-MST(<i>G</i>, <i>w</i>)
  53. <i>T := &Oslash;</i>
  54. <b>for</b> each vertex <i>u in V</i>
  55. MAKE-SET(<i>DS</i>, <i>u</i>)
  56. <b>end for</b>
  57. <b>for</b> each edge <i>(u,v) in E</i> in order of nondecreasing weight
  58. <b>if</b> FIND-SET(<i>DS</i>, <i>u</i>) != FIND-SET(<i>DS</i>, <i>v</i>)
  59. UNION-SET(<i>DS</i>, <i>u</i>, <i>v</i>)
  60. <i>T := T U {(u,v)}</i>
  61. <b>end for</b>
  62. <b>return</b> <i>T</i>
  63. </pre>
  64. <H3>Where Defined</H3>
  65. <P>
  66. <a href="../../../boost/graph/kruskal_min_spanning_tree.hpp"><TT>boost/graph/kruskal_min_spanning_tree.hpp</TT></a>
  67. <P>
  68. <h3>Parameters</h3>
  69. IN: <tt>const Graph&amp; g</tt>
  70. <blockquote>
  71. An undirected graph. The graph type must be a model of
  72. <a href="./VertexListGraph.html">Vertex List Graph</a>
  73. and <a href="./EdgeListGraph.html">Edge List Graph</a>.<br>
  74. <b>Python</b>: The parameter is named <tt>graph</tt>.
  75. </blockquote>
  76. IN: <tt>OutputIterator spanning_tree_edges</tt>
  77. <blockquote>
  78. The edges of the minimum spanning tree are output to this <a
  79. href="http://www.boost.org/sgi/stl/OutputIterator.html">Output
  80. Iterator</a>.<br>
  81. <b>Python</b>: This parameter is not used in Python. Instead, a
  82. Python <tt>list</tt> containing all of the spanning tree edges is
  83. returned.
  84. </blockquote>
  85. <h3>Named Parameters</h3>
  86. IN: <tt>weight_map(WeightMap w_map)</tt>
  87. <blockquote>
  88. The weight or ``length'' of
  89. each edge in the graph. The <tt>WeightMap</tt> type must be a model
  90. of <a href="../../property_map/doc/ReadablePropertyMap.html">Readable
  91. Property Map</a> and its value type must be <a
  92. href="http://www.boost.org/sgi/stl/LessThanComparable.html">Less Than
  93. Comparable</a>. The key type of this map needs to be the graph's
  94. edge descriptor type.<br>
  95. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  96. <b>Python</b>: Must be an <tt>edge_double_map</tt> for the graph.<br>
  97. <b>Python default</b>: <tt>graph.get_edge_double_map("weight")</tt>
  98. </blockquote>
  99. UTIL: <tt>rank_map(RankMap r_map)</tt>
  100. <blockquote>
  101. This is used by the disjoint sets data structure.
  102. The type <tt>RankMap</tt> must be a model of <a
  103. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  104. Property Map</a>. The vertex descriptor type of the graph needs to
  105. be usable as the key type of the rank map. The value type of the
  106. rank map must be an integer type.<br>
  107. <b>Default:</b> an <a
  108. href="../../property_map/doc/iterator_property_map.html">
  109. <tt>iterator_property_map</tt></a> created from a
  110. <tt>std::vector</tt> of the integers of size
  111. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  112. map.<br>
  113. <b>Python</b>: Unsupported parameter.
  114. </blockquote>
  115. UTIL: <tt>predecessor_map(PredecessorMap p_map)</tt>
  116. <blockquote>
  117. This is used by the disjoint sets data structure, and is <b>not</b>
  118. used for storing predecessors in the spanning tree. The predecessors
  119. of the spanning tree can be obtained from the spanning tree edges
  120. output. The type <tt>PredecessorMap</tt> must be a model of <a
  121. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  122. Property Map</a>. The key type value types of the predecessor map
  123. must be the vertex descriptor type of the graph. <br>
  124. <b>Default:</b> an <a
  125. href="../../property_map/doc/iterator_property_map.html">
  126. <tt>iterator_property_map</tt></a> created from a
  127. <tt>std::vector</tt> of vertex descriptors of size
  128. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  129. map.<br>
  130. <b>Python</b>: Unsupported parameter.
  131. </blockquote>
  132. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  133. <blockquote>
  134. This maps each vertex to an integer in the range <tt>[0,
  135. num_vertices(g))</tt>. This is only necessary if the default is used
  136. for the rank or predecessor maps. The type <tt>VertexIndexMap</tt>
  137. must be a model of <a
  138. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  139. Map</a>. The value type of the map must be an integer type. The
  140. vertex descriptor type of the graph needs to be usable as the key
  141. type of the map.<br>
  142. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  143. Note: if you use this default, make sure your graph has
  144. an internal <tt>vertex_index</tt> property. For example,
  145. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  146. not have an internal <tt>vertex_index</tt> property.
  147. <br>
  148. <b>Python</b>: Unsupported parameter.
  149. </blockquote>
  150. <H3>Complexity</H3>
  151. <P>
  152. The time complexity is <i>O(E log E)</i>
  153. <H3>Example</H3>
  154. <P>
  155. The file <a
  156. href="../example/kruskal-example.cpp"><TT>examples/kruskal-example.cpp</TT></a>
  157. contains an example of using Kruskal's algorithm.
  158. <br>
  159. <HR>
  160. <TABLE>
  161. <TR valign=top>
  162. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  163. <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>)
  164. </TD></TR></TABLE>
  165. </BODY>
  166. </HTML>