prim_minimum_spanning_tree.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: Prim 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:prim"></A>
  16. <img src="figs/python.gif" alt="(Python)"/>
  17. <TT>prim_minimum_spanning_tree</TT>
  18. </H1>
  19. <P>
  20. <PRE>
  21. <i>// named parameter version</i>
  22. template &lt;class Graph, class PredMap, class P, class T, class R&gt;
  23. void prim_minimum_spanning_tree(const Graph&amp; g, PredMap p_map,
  24. const bgl_named_params&lt;P, T, R&gt;&amp; params)
  25. <i>// non-named parameter version</i>
  26. template &lt;class Graph, class DijkstraVisitor,
  27. class PredecessorMap, class DistanceMap,
  28. class WeightMap, class IndexMap&gt;
  29. void prim_minimum_spanning_tree(const Graph&amp; g,
  30. typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
  31. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  32. IndexMap index_map, DijkstraVisitor vis)
  33. </PRE>
  34. <P>
  35. This is Prim's algorithm&nbsp;[<A
  36. HREF="bibliography.html#prim57:_short">25</A>,<A
  37. HREF="bibliography.html#clr90">8</A>,<A
  38. HREF="bibliography.html#tarjan83:_data_struct_network_algo">27</A>,<A
  39. HREF="bibliography.html#graham85">15</A>] for solving the minimum
  40. spanning tree problem for an undirected graph with weighted edges. A
  41. MST is a set of edges that connects all the vertices in the graph
  42. where the total weight of the edges in the tree is minimized. See
  43. Section <A
  44. HREF="graph_theory_review.html#sec:minimum-spanning-tree">Minimum
  45. Spanning Tree Problem</A> for more details. The implementation is
  46. simply a call to <a
  47. href="./dijkstra_shortest_paths.html"><TT>dijkstra_shortest_paths()</TT></a>
  48. with the appropriate choice of comparison and combine functors.
  49. The pseudo-code for Prim's algorithm is listed below.
  50. The algorithm as implemented in Boost.Graph does not produce correct results on
  51. graphs with parallel edges.
  52. </p>
  53. <table>
  54. <tr>
  55. <td valign="top">
  56. <pre>
  57. PRIM-MST(<i>G</i>, <i>s</i>, <i>w</i>)
  58. <b>for</b> each vertex <i>u</i> <i>in</i> <i>V[G]</i>
  59. <i>color[u] :=</i> WHITE
  60. <i>d[u] :=</i> <i>infinity</i>
  61. <b>end for</b>
  62. <i>color[s] :=</i> GRAY
  63. <i>d[s] := 0</i>
  64. ENQUEUE(<i>PQ</i>, <i>s</i>)
  65. <i>p[s] := s</i>
  66. <b>while</b> (<i>PQ != &Oslash;</i>)
  67. <i>u :=</i> DEQUEUE(<i>PQ</i>)
  68. <b>for</b> each <i>v in Adj[u]</i>
  69. <b>if</b> (<i>w(u,v) < d[v]</i>)
  70. <i>d[v] := w(u,v)</i>
  71. <i>p[v] := u</i>
  72. <b>if</b> (<i>color[v] = </i> WHITE)
  73. ENQUEUE(<i>PQ</i>, <i>v</i>)
  74. <i>color[v] :=</i> GRAY
  75. <b>else if</b> (<i>color[v] = </i> GRAY)
  76. UPDATE(<i>PQ</i>, <i>v</i>)
  77. <b>else</b>
  78. do nothing
  79. <b>end for</b>
  80. <i>color[u] :=</i> BLACK
  81. <b>end while</b>
  82. <b>return</b> (<i>p</i>, <i>d</i>)
  83. </pre>
  84. </td>
  85. <td valign="top">
  86. <pre>
  87. initialize vertex <i>u</i>
  88. start vertex <i>s</i>
  89. discover vertex <i>s</i>
  90. examine vertex <i>u</i>
  91. examining edge <i>(u,v)</i>
  92. edge <i>(u,v)</i> relaxed
  93. discover vertex <i>v</i>
  94. edge <i>(u,v)</i> not relaxed
  95. finish <i>u</i>
  96. </pre>
  97. </tr>
  98. </table>
  99. <H3>Where Defined</H3>
  100. <P>
  101. <a href="../../../boost/graph/prim_minimum_spanning_tree.hpp"><TT>boost/graph/prim_minimum_spanning_tree.hpp</TT></a>
  102. <P>
  103. <h3>Parameters</h3>
  104. IN: <tt>const Graph&amp; g</tt>
  105. <blockquote>
  106. An undirected graph. The type <tt>Graph</tt> must be a
  107. model of <a href="./VertexListGraph.html">Vertex List Graph</a>
  108. and <a href="./IncidenceGraph.html">Incidence Graph</a>. It should not
  109. contain parallel edges.<br>
  110. <b>Python</b>: The parameter is named <tt>graph</tt>.
  111. </blockquote>
  112. OUT: <tt>PredecessorMap p_map</tt>
  113. <blockquote>
  114. The predecessor map records the edges in the minimum spanning
  115. tree. Upon completion of the algorithm, the edges
  116. <i>(p[u],u)</i> for all <i>u in V</i> are in the minimum spanning
  117. tree. If <i>p[u] = u</i> then <i>u</i> is either the root of the
  118. tree or is a vertex that is not reachable from the root.
  119. The <tt>PredecessorMap</tt> type must be a <a
  120. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  121. Property Map</a>
  122. with key and vertex types the same as the vertex descriptor type
  123. of the graph.<br>
  124. <b>Python</b>: Must be a <tt>vertex_vertex_map</tt> for the graph.<br>
  125. </blockquote>
  126. <h3>Named Parameters</h3>
  127. IN: <tt>root_vertex(vertex_descriptor r)</tt>
  128. <blockquote>
  129. The vertex that will be the root of the minimum spanning tree.
  130. The choice of the root vertex is arbitrary.<br>
  131. <b>Default:</b> <tt>*vertices(g).first</tt>
  132. </blockquote>
  133. IN: <tt>weight_map(WeightMap w_map)</tt>
  134. <blockquote>
  135. The weight or ``length'' of each edge in the graph.
  136. The type <tt>WeightMap</tt> must be a model of
  137. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  138. the graph needs to be usable as the key type for the weight
  139. map. The value type for the map must be
  140. the same as the value type of the distance map, and that type must be <a
  141. href="http://www.boost.org/sgi/stl/LessThanComparable.html">Less Than
  142. Comparable</a>.<br>
  143. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  144. <b>Python</b>: Must be an <tt>edge_double_map</tt> for the graph.<br>
  145. <b>Python default</b>: <tt>graph.get_edge_double_map("weight")</tt>
  146. </blockquote>
  147. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  148. <blockquote>
  149. This maps each vertex to an integer in the range <tt>[0,
  150. num_vertices(g))</tt>. This is necessary for efficient updates of the
  151. heap data structure when an edge is relaxed. The type
  152. <tt>VertexIndexMap</tt> must be a model of
  153. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  154. integer type. The vertex descriptor type of the graph needs to be
  155. usable as the key type of the map.<br>
  156. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  157. Note: if you use this default, make sure your graph has
  158. an internal <tt>vertex_index</tt> property. For example,
  159. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  160. not have an internal <tt>vertex_index</tt> property.
  161. <br>
  162. <b>Python</b>: Unsupported parameter.
  163. </blockquote>
  164. UTIL/OUT: <tt>distance_map(DistanceMap d_map)</tt>
  165. <blockquote>
  166. The weight of the spanning tree edge into each
  167. vertex in the graph <tt>g</tt> is recorded in this property map, with edges
  168. directed away from the spanning tree root.
  169. The type <tt>DistanceMap</tt> must be a model of <a
  170. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  171. Property Map</a>. The vertex descriptor type of the
  172. graph needs to be usable as the key type of the distance map, and the value
  173. type needs to be the same as the value type of the <tt>weight_map</tt>
  174. argument.<br>
  175. <b>Default:</b> <a href="../../property_map/doc/iterator_property_map.html">
  176. <tt>iterator_property_map</tt></a> created from a
  177. <tt>std::vector</tt> of the <tt>WeightMap</tt>'s value type of size
  178. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  179. map.<br>
  180. <b>Python</b>: Must be a <tt>vertex_double_map</tt> for the graph.<br>
  181. </blockquote>
  182. UTIL/OUT: <tt>color_map(ColorMap c_map)</tt>
  183. <blockquote>
  184. This is used during the execution of the algorithm to mark the
  185. vertices. The vertices start out white and become gray when they are
  186. inserted in the queue. They then turn black when they are removed
  187. from the queue. At the end of the algorithm, vertices reachable from
  188. the source vertex will have been colored black. All other vertices
  189. will still be white. The type <tt>ColorMap</tt> must be a model of
  190. <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  191. Property Map</a>. A vertex descriptor must be usable as the key type
  192. of the map, and the value type of the map must be a model of
  193. <a href="./ColorValue.html">Color Value</a>.<br>
  194. <b>Default:</b> an <a
  195. href="../../property_map/doc/iterator_property_map.html">
  196. <tt>iterator_property_map</tt></a> created from a <tt>std::vector</tt>
  197. of <tt>default_color_type</tt> of size <tt>num_vertices(g)</tt> and
  198. using the <tt>i_map</tt> for the index map.<br>
  199. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  200. the graph.
  201. </blockquote>
  202. OUT: <tt>visitor(DijkstraVisitor v)</tt>
  203. <blockquote>
  204. Use this to specify actions that you would like to happen
  205. during certain event points within the algorithm.
  206. The type <tt>DijkstraVisitor</tt> must be a model of the
  207. <a href="./DijkstraVisitor.html">Dijkstra Visitor</a> concept.
  208. The visitor object is passed by value <a
  209. href="#1">[1]</a>.<br>
  210. <b>Default:</b> <tt>dijkstra_visitor&lt;null_visitor&gt;</tt><br>
  211. <b>Python</b>: The parameter should be an object that derives from
  212. the <a
  213. href="DijkstraVisitor.html#python"><tt>DijkstraVisitor</tt></a> type
  214. of the graph.
  215. </blockquote>
  216. <H3>Complexity</H3>
  217. <P>
  218. The time complexity is <i>O(E log V)</i>.
  219. <P>
  220. <H3>Example</H3>
  221. <P>
  222. The file <a
  223. href="../example/prim-example.cpp"><TT>examples/prim-example.cpp</TT></a>
  224. contains an example of using Prim's algorithm.
  225. <h3>Notes</h3>
  226. <p><a name="1">[1]</a>
  227. Since the visitor parameter is passed by value, if your visitor
  228. contains state then any changes to the state during the algorithm
  229. will be made to a copy of the visitor object, not the visitor object
  230. passed in. Therefore you may want the visitor to hold this state by
  231. pointer or reference.
  232. <br>
  233. <HR>
  234. <TABLE>
  235. <TR valign=top>
  236. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  237. <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>)
  238. </TD></TR></TABLE>
  239. </BODY>
  240. </HTML>