{"id":548,"date":"2018-07-19T10:06:44","date_gmt":"2018-07-19T10:06:44","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=548"},"modified":"2018-12-12T12:34:25","modified_gmt":"2018-12-12T12:34:25","slug":"minimum-spanning-trees-prims-algorithm","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/minimum-spanning-trees-prims-algorithm\/","title":{"rendered":"Minimum Spanning Trees \u2013Prim\u2019s    algorithm"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/UInYaAU2I3A\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Prim\u2019s algorithm for finding out Minimum Spanning Trees.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022 To understand in detail the Prim\u2019s Algorithm to find Minimum Spanning Trees\r\n\r\n\u2022 To understand Prim\u2019s Algorithm through a walkthrough example\r\n\r\n&nbsp;\r\n\r\n<strong>36.1 Recap Spanning Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When a graph G is connected, a depth-first or breadth first search starting at any vertex will visit all vertices in G. A spanning tree is any tree that consists solely of the edges in G and that includes all the vertices.<\/p>\r\n&nbsp;\r\n\r\nE(G): T(tree edges) + N (non-tree edges)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Where T is the set of edges used during search and N is the set of remaining edges. The Minimum Spanning Tree or MST for a given graph is the spanning tree of minimum cost for that graph.<\/p>\r\n&nbsp;\r\n\r\n<strong>36.2 Prim\u2019s Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Prim\u2019s algorithm was initially discovered in 1930 by Vojt\u011bch Jarn\u00edk, then rediscovered in 1957 by Robert C. Prim. The algorithm starts off by picking any node within the graph and growing from there. Initially we label the starting node A, with a 0 and all others with infinity. Starting from A, we update all the connected nodes\u2019 labels to A with their weighted edges if it is less than the labeled value . Now we find the next smallest label and update the corresponding connecting nodes. We repeat until all the nodes have been visited<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">36.2 Steps of Prim\u2019s Algorithm<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n1. The new graph is constructed - with one node from the old graph.\r\n\r\n2. While new graph has fewer than n nodes,\r\n\r\n&nbsp;\r\n<p style=\"padding-left: 30px\">\u2013 Find the node from the old graph with the smallest connecting edge to the new graph<\/p>\r\n<p style=\"padding-left: 30px\">\u2013\u00a0 Add it to the new graph<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Every step would have joined one additional node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph. This method is quite similar to Kruskal's with one big difference. In Prim\u2019s algorithm the tree that we are \"growing\" always stays connected. However in Kruskal's algorithm we could add an edge to the growing tree that was not connected to the rest of the tree, if that edge had the minimum cost. In other words we could have a forest. Only at the conclusion of the Kruskal\u2019s algorithm would there be the connected spanning tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>36.3 Prim\u2019s algorithm:<\/strong>\r\n\r\n&nbsp;\r\n\r\nSet S = \u00c6.\r\n\r\n2)\u00a0 Add the minimum edge incident to that vertex to S.\r\n\r\n3)\u00a0 Continue to add edges into S (n-2 more times) using the following rule:\r\n\r\nAdd the minimum edge weight to S that is incident to S which doesn't form a cycle when added to S.\r\n\r\nConsider the following graph (Figure 36.1).\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-551 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-361.png\" alt=\"\" width=\"259\" height=\"213\" \/>\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Select any vertex, say A. Select the shortest edge connected to that vertex. The shortest edge connected to A is B. So, AB is one of the edges of the Minimum Spanning Tree (Figure 36.2 (a)).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-552 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-362.png\" alt=\"\" width=\"267\" height=\"225\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now, select the shortest edge connected to any vertex (that is A or B) already connected. This vertex, as can be found from the graph is E and the edge is AE (Figure 36.2 (b))<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-553 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-363.png\" alt=\"\" width=\"259\" height=\"200\" \/>\r\n<p style=\"text-align: justify\">Now, again select the shortest edge connected to any vertex (A,B,E) already connected. The shortest edge is ED (2). This is given in the Figure 36.2 (c))<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-554 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-364.png\" alt=\"\" width=\"249\" height=\"202\" \/>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Now, again select the shortest edge connected to any vertex (A,B,E,D) already connected. The shortest edge connected to any vertex already connected is DC(4) (Figure 36.2(d)).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-555 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365.png\" alt=\"\" width=\"328\" height=\"257\" \/>\r\n<p style=\"text-align: justify\">Now, again select the shortest edge connected to any vertex (A,B,E,D,C) already connected. The shortest edge connected to any vertex already connected is EF (5). (Figure 36.2(e)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-556 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366.png\" alt=\"\" width=\"360\" height=\"269\" \/>\r\n<p style=\"text-align: justify\">Now, all the vertices have been connected. The solution is: AB 3, AE 4, ED 2, DC 4 and EF 5. Thus the total weight of the tree is 18.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">36.2.2. Walk-Through<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Similar to the walk through we carried out for Kruskal algorithm, we will do the same for Prim\u2019s Algorithm. We consider the graph and the initialization of the array as shown in Figure 36.3(a)). We initialize the array with rows corresponding to nodes of the graph. Associated with each node are three attributes. K indicates whether we have visited the node or not. It can take one of the values T or F corresponding to True or False respectively. Initially K is set to False (F) for all nodes. dv is the distance of the node from previous node and is initially set to \u00a5 for all nodes. pv is the previous node from which the edge to this node originated and is not initialized (Figure 36.3 (a).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-557 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367.png\" alt=\"\" width=\"594\" height=\"257\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we start with any vertex, say D. Now the K value of D is set to T and the dv is set to 0, since this is the start node and in the graph the selected node is marked in red (Figure 36.3 (b)).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-558 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368.png\" alt=\"\" width=\"586\" height=\"284\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 36.3 (b)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to update the distances of adjacent, unselected nodes. For the node D, the adjacent nodes are C, F, G and E. The previous vertex <strong><em>p<\/em><\/strong><strong><em>v<\/em><\/strong> of all these nodesare set to D and the distance <strong><em>d<\/em><\/strong><strong><em>v<\/em><\/strong> of each node to D is noted (Figure 36.3 (c )).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-559 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369.png\" alt=\"\" width=\"614\" height=\"318\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, DC, DF, DG and DE, the edge with the smallest weight is that of DG. Hence the K value for G is updated to T as shown in Figure 36.3 (d).<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-560 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370.png\" alt=\"\" width=\"563\" height=\"276\" \/>\r\n<p style=\"text-align: justify\">From G, the adjacent vertices are H and E. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 3 for the edge GH can be directly entered with the pv value as G in the row for H. But when we\u00a0<span style=\"font-size: 1em\">consider node E, we find that there is already an entry for the previous vertex as D. Now we consider both the distances from D to E which is 25 and G to E which is 7. The least of these two is 7 and hence we update the table for row E with pv as G and dv as 7. After the updates, the array is shown in Figure 36.3 (e).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-561 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371.png\" alt=\"\" width=\"573\" height=\"302\" \/>\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is that of edges DC and GH. Here we select node C. Hence the K value for C is updated to T as shown in Figure 36.3 (f).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-562 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372.png\" alt=\"\" width=\"558\" height=\"303\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">From C, the adjacent vertices are B and F. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 4 for the edge BC can be directly entered with the pv value as C in the row for B. But when we\u00a0<span style=\"font-size: 1em;text-align: initial\">consider node F, we find that there is already an entry for the previous vertex as D. Now we consider both the distances from D to F which is 18 and C to F which is 3. The least of these two is 3 and hence we update the table for row F with pv as C and dv as 3. After the updates, the array is shown in Figure 36.3 (g).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-563 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373.png\" alt=\"\" width=\"561\" height=\"306\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is FC. Hence the K value for F is updated to T as shown in Figure 36.3 (h).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-564 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374.png\" alt=\"\" width=\"553\" height=\"302\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">From F, the adjacent vertices that are still unselected are B and A and E. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 10 for the edge AF can be directly entered with the pv value as F in the\u00a0<span style=\"font-size: 1em;text-align: initial\">row for A. But when we consider node E, we find that there is already an entry for the previous vertex as G. Now we consider both the distances from G to E which is 7 and F to E which is 2. The least of these two is 2 and hence we update the table for row E with pv as F and dv as 2. When we consider the node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and F to B which is 7. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. After the updates, the table is shown in Figure 36.3 (i).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-565 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375.png\" alt=\"\" width=\"604\" height=\"296\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is FE. Hence the K value for E is updated to T as shown in Figure 36.3 (j).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-566 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376.png\" alt=\"\" width=\"548\" height=\"285\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 36.3 (j)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">From E, we need to update distances of adjacent, unselected nodes but at this point there are is only one unselected node B. But when we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and E to B which is 10. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. Hence the table remains unchanged as shown in Figure 36.3 (k).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-567 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377.png\" alt=\"\" width=\"560\" height=\"333\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is GH. Hence the K value for H is updated to T as shown in Figure 36.3 (l).<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-568 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378.png\" alt=\"\" width=\"559\" height=\"296\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 36.3 (l)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">From H, we need to update distances of adjacent, unselected nodes and at this point they are A and B. But when we consider node A, we find that there is already an entry for the previous vertex as F. Now we consider both the distances from F to A which is 10 and H to A which is 4. The least of these two is 4 and hence we update the table for row A with pv as H and dv as 4. When we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and H to B which is 9. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. . After the updates, the table is shown in Figure 36.3 (m).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-569 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379.png\" alt=\"\" width=\"519\" height=\"559\" \/>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 36.3 (n)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is AH. Hence the K value for A is updated to T as shown in Figure 36.3 (n).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">From A, we need to update distances of adjacent, unselected nodes but at this point there are is only one unselected node B. But when we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and A to B which is 8. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. Hence the table remains unchanged as shown in Figure 36.3 (o).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-570 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380.png\" alt=\"\" width=\"464\" height=\"258\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to select node with minimum distance . The only edge which is unselected is B , hence we select it and its the K value updated to T as shown in Figure 36.3 (p).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-571 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381.png\" alt=\"\" width=\"474\" height=\"244\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure 36.3 (p)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now all the nodes of the graph has been selected and the spanning tree obtained is shown in Figure 36.3 (p). Now, we can compute the cost of minimum spanning tree\u00a0\u00a0dv= 21<\/p>\r\n&nbsp;\r\n\r\n<strong>36.3. Analysis of the Prim\u2019s Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When there are m edges and n nodes then, the running time for the algorithm is given by O(m + n log n) if a heap is used for sorting the edges. If a heap is not used, the run time will be O(n\u00b2) instead of O(m + n log n). However, using a heap complicates the code since you\u2019re complicating the data structure. A Fibonacci heap is the best kind of heap to use, but again, it complicates the code. Unlike Kruskal\u2019s, it does not need to see all of the graph at once. It can deal with it one piece at a time. It also does not have to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges. For this algorithm the number of nodes needs to be kept to a minimum in addition to the number of edges. For small graphs, the edges matter more, while for large graphs the number of nodes matters more.<\/p>\r\n&nbsp;\r\n\r\n<strong>36.4 Comparing the algorithms<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Both algorithms will always give solutions with the same length. However the two algorithms will usually select edges in a different order. Occasionally they will use different edges \u2013 this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained in detail the Prim\u2019s Algorithm to find Minimum Spanning Trees<\/li>\r\n \t<li>Illustrated Prim\u2019s Algorithm through a walkthrough example<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-572 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382.png\" alt=\"\" width=\"652\" height=\"512\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/UInYaAU2I3A\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we will discuss about Prim\u2019s algorithm for finding out Minimum Spanning Trees.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To understand in detail the Prim\u2019s Algorithm to find Minimum Spanning Trees<\/p>\n<p>\u2022 To understand Prim\u2019s Algorithm through a walkthrough example<\/p>\n<p>&nbsp;<\/p>\n<p><strong>36.1 Recap Spanning Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When a graph G is connected, a depth-first or breadth first search starting at any vertex will visit all vertices in G. A spanning tree is any tree that consists solely of the edges in G and that includes all the vertices.<\/p>\n<p>&nbsp;<\/p>\n<p>E(G): T(tree edges) + N (non-tree edges)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Where T is the set of edges used during search and N is the set of remaining edges. The Minimum Spanning Tree or MST for a given graph is the spanning tree of minimum cost for that graph.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>36.2 Prim\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Prim\u2019s algorithm was initially discovered in 1930 by Vojt\u011bch Jarn\u00edk, then rediscovered in 1957 by Robert C. Prim. The algorithm starts off by picking any node within the graph and growing from there. Initially we label the starting node A, with a 0 and all others with infinity. Starting from A, we update all the connected nodes\u2019 labels to A with their weighted edges if it is less than the labeled value . Now we find the next smallest label and update the corresponding connecting nodes. We repeat until all the nodes have been visited<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">36.2 Steps of Prim\u2019s Algorithm<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>1. The new graph is constructed &#8211; with one node from the old graph.<\/p>\n<p>2. While new graph has fewer than n nodes,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\u2013 Find the node from the old graph with the smallest connecting edge to the new graph<\/p>\n<p style=\"padding-left: 30px\">\u2013\u00a0 Add it to the new graph<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Every step would have joined one additional node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph. This method is quite similar to Kruskal&#8217;s with one big difference. In Prim\u2019s algorithm the tree that we are &#8220;growing&#8221; always stays connected. However in Kruskal&#8217;s algorithm we could add an edge to the growing tree that was not connected to the rest of the tree, if that edge had the minimum cost. In other words we could have a forest. Only at the conclusion of the Kruskal\u2019s algorithm would there be the connected spanning tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>36.3 Prim\u2019s algorithm:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Set S = \u00c6.<\/p>\n<p>2)\u00a0 Add the minimum edge incident to that vertex to S.<\/p>\n<p>3)\u00a0 Continue to add edges into S (n-2 more times) using the following rule:<\/p>\n<p>Add the minimum edge weight to S that is incident to S which doesn&#8217;t form a cycle when added to S.<\/p>\n<p>Consider the following graph (Figure 36.1).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-551 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-361.png\" alt=\"\" width=\"259\" height=\"213\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-361.png 259w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-361-65x53.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-361-225x185.png 225w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Select any vertex, say A. Select the shortest edge connected to that vertex. The shortest edge connected to A is B. So, AB is one of the edges of the Minimum Spanning Tree (Figure 36.2 (a)).<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-552 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-362.png\" alt=\"\" width=\"267\" height=\"225\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-362.png 267w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-362-65x55.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-362-225x190.png 225w\" sizes=\"auto, (max-width: 267px) 100vw, 267px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now, select the shortest edge connected to any vertex (that is A or B) already connected. This vertex, as can be found from the graph is E and the edge is AE (Figure 36.2 (b))<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-553 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-363.png\" alt=\"\" width=\"259\" height=\"200\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-363.png 259w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-363-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-363-225x174.png 225w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/p>\n<p style=\"text-align: justify\">Now, again select the shortest edge connected to any vertex (A,B,E) already connected. The shortest edge is ED (2). This is given in the Figure 36.2 (c))<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-554 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-364.png\" alt=\"\" width=\"249\" height=\"202\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-364.png 249w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-364-65x53.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-364-225x183.png 225w\" sizes=\"auto, (max-width: 249px) 100vw, 249px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Now, again select the shortest edge connected to any vertex (A,B,E,D) already connected. The shortest edge connected to any vertex already connected is DC(4) (Figure 36.2(d)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-555 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365.png\" alt=\"\" width=\"328\" height=\"257\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365.png 328w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365-300x235.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-365-225x176.png 225w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/p>\n<p style=\"text-align: justify\">Now, again select the shortest edge connected to any vertex (A,B,E,D,C) already connected. The shortest edge connected to any vertex already connected is EF (5). (Figure 36.2(e)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-556 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366.png\" alt=\"\" width=\"360\" height=\"269\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366.png 360w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366-300x224.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366-225x168.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-366-350x262.png 350w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/p>\n<p style=\"text-align: justify\">Now, all the vertices have been connected. The solution is: AB 3, AE 4, ED 2, DC 4 and EF 5. Thus the total weight of the tree is 18.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">36.2.2. Walk-Through<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Similar to the walk through we carried out for Kruskal algorithm, we will do the same for Prim\u2019s Algorithm. We consider the graph and the initialization of the array as shown in Figure 36.3(a)). We initialize the array with rows corresponding to nodes of the graph. Associated with each node are three attributes. K indicates whether we have visited the node or not. It can take one of the values T or F corresponding to True or False respectively. Initially K is set to False (F) for all nodes. dv is the distance of the node from previous node and is initially set to \u00a5 for all nodes. pv is the previous node from which the edge to this node originated and is not initialized (Figure 36.3 (a).<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-557 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367.png\" alt=\"\" width=\"594\" height=\"257\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367.png 594w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367-300x130.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367-225x97.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-367-350x151.png 350w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we start with any vertex, say D. Now the K value of D is set to T and the dv is set to 0, since this is the start node and in the graph the selected node is marked in red (Figure 36.3 (b)).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-558 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368.png\" alt=\"\" width=\"586\" height=\"284\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368.png 586w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368-300x145.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368-225x109.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-368-350x170.png 350w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 36.3 (b)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to update the distances of adjacent, unselected nodes. For the node D, the adjacent nodes are C, F, G and E. The previous vertex <strong><em>p<\/em><\/strong><strong><em>v<\/em><\/strong> of all these nodesare set to D and the distance <strong><em>d<\/em><\/strong><strong><em>v<\/em><\/strong> of each node to D is noted (Figure 36.3 (c )).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-559 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369.png\" alt=\"\" width=\"614\" height=\"318\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369.png 614w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-369-350x181.png 350w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, DC, DF, DG and DE, the edge with the smallest weight is that of DG. Hence the K value for G is updated to T as shown in Figure 36.3 (d).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-560 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370.png\" alt=\"\" width=\"563\" height=\"276\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370.png 563w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370-300x147.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-370-350x172.png 350w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/p>\n<p style=\"text-align: justify\">From G, the adjacent vertices are H and E. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 3 for the edge GH can be directly entered with the pv value as G in the row for H. But when we\u00a0<span style=\"font-size: 1em\">consider node E, we find that there is already an entry for the previous vertex as D. Now we consider both the distances from D to E which is 25 and G to E which is 7. The least of these two is 7 and hence we update the table for row E with pv as G and dv as 7. After the updates, the array is shown in Figure 36.3 (e).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-561 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371.png\" alt=\"\" width=\"573\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371.png 573w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371-300x158.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-371-350x184.png 350w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is that of edges DC and GH. Here we select node C. Hence the K value for C is updated to T as shown in Figure 36.3 (f).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-562 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372.png\" alt=\"\" width=\"558\" height=\"303\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372.png 558w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-372-350x190.png 350w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From C, the adjacent vertices are B and F. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 4 for the edge BC can be directly entered with the pv value as C in the row for B. But when we\u00a0<span style=\"font-size: 1em;text-align: initial\">consider node F, we find that there is already an entry for the previous vertex as D. Now we consider both the distances from D to F which is 18 and C to F which is 3. The least of these two is 3 and hence we update the table for row F with pv as C and dv as 3. After the updates, the array is shown in Figure 36.3 (g).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-563 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373.png\" alt=\"\" width=\"561\" height=\"306\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373.png 561w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-373-350x191.png 350w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is FC. Hence the K value for F is updated to T as shown in Figure 36.3 (h).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-564 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374.png\" alt=\"\" width=\"553\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374.png 553w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-374-350x191.png 350w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From F, the adjacent vertices that are still unselected are B and A and E. The distances of these adjacent, unselected nodes are updated in the table. The value of the weight 10 for the edge AF can be directly entered with the pv value as F in the\u00a0<span style=\"font-size: 1em;text-align: initial\">row for A. But when we consider node E, we find that there is already an entry for the previous vertex as G. Now we consider both the distances from G to E which is 7 and F to E which is 2. The least of these two is 2 and hence we update the table for row E with pv as F and dv as 2. When we consider the node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and F to B which is 7. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. After the updates, the table is shown in Figure 36.3 (i).<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-565 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375.png\" alt=\"\" width=\"604\" height=\"296\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375.png 604w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375-300x147.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-375-350x172.png 350w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is FE. Hence the K value for E is updated to T as shown in Figure 36.3 (j).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-566 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376.png\" alt=\"\" width=\"548\" height=\"285\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376.png 548w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-376-350x182.png 350w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 36.3 (j)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From E, we need to update distances of adjacent, unselected nodes but at this point there are is only one unselected node B. But when we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and E to B which is 10. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. Hence the table remains unchanged as shown in Figure 36.3 (k).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-567 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377.png\" alt=\"\" width=\"560\" height=\"333\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377.png 560w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-377-350x208.png 350w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is GH. Hence the K value for H is updated to T as shown in Figure 36.3 (l).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-568 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378.png\" alt=\"\" width=\"559\" height=\"296\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378.png 559w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-378-350x185.png 350w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 36.3 (l)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From H, we need to update distances of adjacent, unselected nodes and at this point they are A and B. But when we consider node A, we find that there is already an entry for the previous vertex as F. Now we consider both the distances from F to A which is 10 and H to A which is 4. The least of these two is 4 and hence we update the table for row A with pv as H and dv as 4. When we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and H to B which is 9. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. . After the updates, the table is shown in Figure 36.3 (m).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-569 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379.png\" alt=\"\" width=\"519\" height=\"559\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379.png 519w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379-279x300.png 279w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379-65x70.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379-225x242.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-379-350x377.png 350w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 36.3 (n)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance. Of all the edges, the edge with the smallest weight is AH. Hence the K value for A is updated to T as shown in Figure 36.3 (n).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From A, we need to update distances of adjacent, unselected nodes but at this point there are is only one unselected node B. But when we consider node B, we find that there is already an entry for the previous vertex as C. Now we consider both the distances from C to B which is 4 and A to B which is 8. The least of these two remains as 4 and hence we do not update the table for row B which still remains as C. Hence the table remains unchanged as shown in Figure 36.3 (o).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-570 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380.png\" alt=\"\" width=\"464\" height=\"258\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380.png 464w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380-300x167.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-380-350x195.png 350w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to select node with minimum distance . The only edge which is unselected is B , hence we select it and its the K value updated to T as shown in Figure 36.3 (p).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-571 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381.png\" alt=\"\" width=\"474\" height=\"244\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381.png 474w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381-300x154.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-381-350x180.png 350w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure 36.3 (p)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now all the nodes of the graph has been selected and the spanning tree obtained is shown in Figure 36.3 (p). Now, we can compute the cost of minimum spanning tree\u00a0\u00a0dv= 21<\/p>\n<p>&nbsp;<\/p>\n<p><strong>36.3. Analysis of the Prim\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When there are m edges and n nodes then, the running time for the algorithm is given by O(m + n log n) if a heap is used for sorting the edges. If a heap is not used, the run time will be O(n\u00b2) instead of O(m + n log n). However, using a heap complicates the code since you\u2019re complicating the data structure. A Fibonacci heap is the best kind of heap to use, but again, it complicates the code. Unlike Kruskal\u2019s, it does not need to see all of the graph at once. It can deal with it one piece at a time. It also does not have to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges. For this algorithm the number of nodes needs to be kept to a minimum in addition to the number of edges. For small graphs, the edges matter more, while for large graphs the number of nodes matters more.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>36.4 Comparing the algorithms<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Both algorithms will always give solutions with the same length. However the two algorithms will usually select edges in a different order. Occasionally they will use different edges \u2013 this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained in detail the Prim\u2019s Algorithm to find Minimum Spanning Trees<\/li>\n<li>Illustrated Prim\u2019s Algorithm through a walkthrough example<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-572 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382.png\" alt=\"\" width=\"652\" height=\"512\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382.png 652w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382-300x236.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382-225x177.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-382-350x275.png 350w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n","protected":false},"author":3,"menu_order":36,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-548","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/548\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/548\/revisions\/986"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/548\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=548"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=548"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}