{"id":21,"date":"2011-08-24T20:49:37","date_gmt":"2011-08-24T20:49:37","guid":{"rendered":"http:\/\/learninglover.com\/blog\/?p=21"},"modified":"2013-11-06T20:19:29","modified_gmt":"2013-11-07T01:19:29","slug":"prims-algorithm","status":"publish","type":"post","link":"https:\/\/learninglover.com\/blog\/index.php\/2011\/08\/24\/prims-algorithm\/","title":{"rendered":"Prim&#8217;s Algorithm"},"content":{"rendered":"<p>I have just written <a href=\"http:\/\/www.learninglover.com\/examples.php?id=4\">a script that executes Prim&#8217;s Algorithm<\/a> that finds the minimum spanning tree on a randomly generated graph.<\/p>\n<p>Given a weighted graph, many times we are interested in finding a minimum spanning tree (MST) for that graph. This has many applications including the very important network simplex method. Prim&#8217;s algorithm is a greedy method which does finds this MST. A spanning tree is a subset of the edges of a graph that connects every vertex, but contains no cycles. This spanning tree is called a minimum spanning tree if in addition the sum of the weights of the edges included in this tree is less than or equal to the sum of the weights of the edges of any other spanning tree for this graph.<\/p>\n<p>Prim&#8217;s algorithm works by the following procedure.<br \/>\n1. Let <em>Tree<sub>v<\/sub><\/em> be the set of vertices included in the tree, and <em>Tree<sub>E<\/sub><\/em> be the set of edges included in the tree. Initially <em>Tree<sub>v<\/sub><\/em> and <em>Tree<sub>E<\/sub><\/em> are empty.<br \/>\n2. Add an arbitrary vertex to <em>Tree<sub>v<\/sub><\/em> (<em>Tree<sub>E<\/sub><\/em> is still empty).<br \/>\n3. Find the edge <em>e<\/em> of minimum weight such that one vertex is in <em>Tree<sub>v<\/sub><\/em> and vertex is not in <em>Tree<sub>v<\/sub><\/em>. Add the associated vertex to <em>Tree<sub>v<\/sub><\/em>, and add <em>e<\/em> to <em>Tree<sub>E<\/sub><\/em>.<br \/>\n4. If edge was found in step 3, goto 5, else go to 6.<br \/>\n5. If the number of vertices in <em>Tree<sub>v<\/sub><\/em> is less than the number of vertices in the original graph, then the graph is not connected and thus does not contain a minimum spanning tree. Goto 8.<br \/>\n6 If the number of vertices in <em>Tree<sub>v<\/sub><\/em> is less than the number of vertices in the original graph, go to 2, else go to 7.<br \/>\n7. Output &#8220;The Minimum Spanning Tree is &#8220;, <em>Tree<sub>E<\/sub><\/em>.<br \/>\n8. Output &#8220;This graph does not have a minimum spanning tree because it is not connected. &#8221;<\/p>\n<p>For example, consider the graph represented by the following adjacency matrix:<\/p>\n<table>\n<tr>\n<td><\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>13<\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>16<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>24<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>13<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>13<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>12<\/td>\n<td>16<\/td>\n<td>24<\/td>\n<td>13<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/table>\n<p><img decoding=\"async\" src=\"http:\/\/learninglover.com\/blog\/wp-content\/gallery\/examples\/prim.jpg\" alt=\"Graph with 5 nodes\" \/><\/p>\n<p>Initially our tree (T<sub>v<\/sub> is empty). The first step says to choose a random vertex and add it to the tree, so lets choose vertex 2. <\/p>\n<p>Iteration 1: Now our tree contains the vertex 2 (i.e. T<sub>v<\/sub> = {2}) and likewise T<sub>E<\/sub> contains the edges coming from T<sub>v<\/sub>. Thus T<sub>E<\/sub> = {(2, 4)}.<br \/>\nWe want to choose the cheapest edge that has one endpoint in T<sub>v<\/sub> and one endpoint not in T<sub>v<\/sub>. These edges are represented by T<sub>E<\/sub>. Notice that T<sub>E<\/sub> only contains one edge, so we select this<br \/>\nedge, which has a cost of 24. <\/p>\n<p>Iteration 2: Our tree thus contains the vertices 2 and 4 (i.e T<sub>v<\/sub> = {2, 4}) and likewise T<sub>E<\/sub> contains the edges coming from T<sub>v<\/sub>. Thus T<sub>E<\/sub> = {(0, 4), (1, 4), (3, 4)}.<br \/>\nAgain, we want to choose the cheapest edge that has one endpoint in T<sub>v<\/sub> and one endpoint not in T<sub>v<\/sub>. This will be the edge (0, 4) which has a cost of 12. <\/p>\n<p>Iteration 3: Now T<sub>v<\/sub> = {0, 2, 4} and T<sub>E<\/sub> = {(1, 4), (3, 4), (0, 3)}. The cheapest of these three edges is the edge (0, 3) with a cost of 13, which means we will add it to our tree. <\/p>\n<p>Iteration 4: Now T<sub>v<\/sub> = {0, 2, 3, 4} and T<sub>E<\/sub> = {(1, 4)}. Since (1, 4) is the only edge connected to our tree we add it and it has a cost of 16. <\/p>\n<p>Iteration 5: Now T<sub>v<\/sub> = {0, 1, 2, 3, 4} and T<sub>E<\/sub> = {}. Because our tree contains all the vertices of the graph it is now spanning tree. The cost of this spanning tree is 24 + 12 + 13 + 16 = 65. <\/p>\n<p>To learn more and see more examples, view <a href=\"http:\/\/www.learninglover.com\/examples.php?id=4\">Prim&#8217;s Algorithm at LEARNINGlover.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have just written a script that executes Prim&#8217;s Algorithm that finds the minimum spanning tree on a randomly generated graph. Given a weighted graph, many times we are interested in finding a minimum spanning tree (MST) for that graph. This has many applications including the very important network simplex method. Prim&#8217;s algorithm is a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learninglover.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}