{"id":524,"date":"2018-07-19T09:41:11","date_gmt":"2018-07-19T09:41:11","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=524"},"modified":"2018-12-12T12:25:14","modified_gmt":"2018-12-12T12:25:14","slug":"minimum-spanning-trees-kruskal-aigorithm","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/minimum-spanning-trees-kruskal-aigorithm\/","title":{"rendered":"Minimum Spanning Trees- Kruskal AIgorithm"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/pDMiWidTnwk\" 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. We have understood the Minimum Spanning Trees. In this module we will discuss about Kruskal\u2019s Algorithm in detail.<\/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\u00a0 To understand in detail the Kruskal\u2019s Algorithm to find Minimum Spanning Trees\r\n\r\n\u2022\u00a0 To discuss implementation issues associated with Kruskal\u2019s Algorithm\r\n\r\n\u2022\u00a0 To understand Kruskal\u2019s Algorithm through a walkthrough example\r\n\r\n&nbsp;\r\n\r\n<strong>35.1\u00a0 Spanning Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This is a recap of Spanning trees and minimum spanning trees. When an undirected 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 edges in G and that includes all the vertices. The edges of the graph E(G) is as below:<\/p>\r\n&nbsp;\r\n\r\nE(G): T (tree edges) + N (nontree edges) where\r\n\r\nT: set of edges used during search\r\n\r\nN: set of remaining edges\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Minimum Spanning Tree (MST) for a given graph is the Spanning Tree of minimum cost for that graph.<\/p>\r\n&nbsp;\r\n\r\n<strong>35.2\u00a0 Greedy Algorithms<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us make a simplifying assumption that all edge costs ce are distinct. With this assumption let us now define two properties namely Cut Property and Cycle Property (Figure 35.1).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Cut property <\/strong>- Let S be any subset of nodes, and let e be the minimum cost edge with exactly one endpoint in S. Then the MST contains e.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Cycle property - <\/strong>Let C be any cycle, and let f be the maximum cost edge belonging to C. Then the MST does not contain f.<\/p>\r\n\r\n<\/div>\r\n<strong><img class=\"alignnone size-full wp-image-527 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341.png\" alt=\"\" width=\"645\" height=\"288\" \/><\/strong>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 35.2 Kruskal\u2019s Algorithm<\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>35.2.1 Generic Algorithm for growing MST<\/strong>\r\n\r\n&nbsp;\r\n\r\nFirst we will examine the generic MST algorithm.\r\n\r\n&nbsp;\r\n\r\nGENERIC_MST(G,w)\r\n\r\n&nbsp;\r\n\r\n1. A:={} \/* (can be a forest or a single tree \u2013 see section 35.2.2 )\r\n\r\n2. while A does not form a spanning tree do\r\n\r\n3. find an edge (u,v) that is safe for A\r\n\r\n4. A:=A\u222a{(u,v)}\r\n\r\n5. return A\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Set A is always a subset of some minimum spanning tree. This property is called the <strong>invariant Property. <\/strong>An edge (u,v) is a<strong> safe edge <\/strong>for A if adding the edge to A does not destroy the invariant property. A <strong>safe edge is<\/strong> just the CORRECT edge to choose to add.<\/p>\r\n&nbsp;\r\n\r\n<strong>35.2.2 Kruskal and Prim\u2019s Algorithms<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Both Kruskal and Prim\u2019s algorithms are based on the generic algorithm. A specific rule to determine a safe edge is what diffrentiates the two algorithms. Set A is always a subset of some minimum spanning tree<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In Kruskal's algorithm, the set A is a forest. In this algorithm the safe edge added is always a least-weight edge in the graph that connects two distinct components.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In Prim's algorithm, the set A forms a single tree <strong>.<\/strong> The safe edge added is always a least-weight edge connecting the tree to a vertex not in the tree.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">35.3 Kruskal Agorithm in detail<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Now we will discuss the Kruskal algorithm in detail. Created in 1957 by Joseph Kruskal, it finds the MST by taking the smallest weight in the graph and connecting the two nodes and repeating until all nodes are connected to just one tree. This is done by creating a priority queue using the weights as keys . It creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If the edge would result in a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">35.3.1 Concept of Kruskal\u2019s Algorithm<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Add the cheapest edge that joins disjoint components as in Fig 35.2. We we select (uv) since that is the cheapest edge, then we select (sb) since that is the next cheapest edge and so on. We do not select edges if they form a cycle a nd stop when all nodes are covered.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-528 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342.png\" alt=\"\" width=\"499\" height=\"346\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.The forest is constructed with each node forming an independent tree. The edges are placed in a priority queue using the weights as keys.<\/p>\r\n<p style=\"text-align: justify\">2.Until we have added n-1 edges,<\/p>\r\n&nbsp;\r\n<p style=\"padding-left: 60px\">(a)\u00a0 Extract the cheapest edge from the queue,<\/p>\r\n<p style=\"padding-left: 60px\">(b)\u00a0 If it forms a cycle, reject it,<\/p>\r\n<p style=\"padding-left: 60px\"><span style=\"font-size: 1em;text-align: initial\">(c)\u00a0 Else add it to the forest. Adding it to the forest will join two trees together.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nEvery step Joins - two trees in the forest together, so that at the end, there will only be one tree in T.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The reason the algorithm works is that each added edge is a connection between two sets of vertices, and since we select the edges in order of weight, we are always selecting the minimum edge weight that connects the two sets of vertices.<\/p>\r\n&nbsp;\r\n\r\n<strong>35.3.2 Cycle detection<\/strong>\r\n\r\n&nbsp;\r\n\r\nOne important part of Kruskal\u2019s algorithm is the cycle detection. Here the steps are as follows:\r\n\r\n&nbsp;\r\n\r\nKeep track of disjoint sets.\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>Initially, each vertex is in its own disjoint set.<\/li>\r\n \t<li>When you add an edge you are unionizing two sets.<\/li>\r\n \t<li>A union cannot happen if the two vertices are already in the same set.<\/li>\r\n<\/ul>\r\n<p style=\"padding-left: 60px\">\u2022\u00a0 This would create a cycle.<\/p>\r\n&nbsp;\r\n\r\n<strong>35.3.3 Basics of Kruskal\u2019s Algorithm<\/strong>\r\n<ol>\r\n \t<li>Sort the edges E in an non-decreasing order (please note two edges can have the same weight)<\/li>\r\n \t<li>A:={}<\/li>\r\n \t<li><strong style=\"text-align: initial;font-size: 1em\">while <\/strong><span style=\"text-align: initial;font-size: 1em\">E is not empty<\/span><strong style=\"text-align: initial;font-size: 1em\"> do {<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"padding-left: 30px\">take an edge (u, v) that is shortest in E and delete it from E<\/p>\r\n<p style=\"padding-left: 30px\"><strong>if <\/strong>u and v are in different components<strong> then<\/strong><\/p>\r\n<p style=\"padding-left: 30px\">add (u, v) to A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here each time a shortest edge in E is considered. We work with edges, rather than nodes. In other words there are two fundamental steps. We need to sort edges by increasing edge weight and then select the first |V| \u2013 1 edges that do not generate a cycle. Each step of the Kruskal Algorithm is explained below with an illustartive example..<\/p>\r\n&nbsp;\r\n\r\n<strong>Step1<\/strong>\r\n\r\n&nbsp;\r\n\r\nList the edges of graph where the edges are sorted by increasing weight as shown in Fig 35.3a as below.\r\n\r\n(ED-2, AB-3, AE-4,CD-4, BC-5, EF-5, CF-6,AF-7,BF-8,DF-8)\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-529 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-343.png\" alt=\"\" width=\"286\" height=\"273\" \/>\r\n\r\n<strong>Step 2<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the shortest edge in the list (ie ED-2) as in Fig 35.3b.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-530 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344.png\" alt=\"\" width=\"332\" height=\"219\" \/>\r\n\r\n<strong>Step 3<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next shortest edge (AB-3) which does not create a cycle as in Fig 35.3c\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-531 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345.png\" alt=\"\" width=\"338\" height=\"247\" \/>\r\n\r\n<strong>Step 4<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Select the next shortest edge CD-4 (either CD or AE could have been selected first since both have same weight) which does not create a cycle (Fig 35.3d).<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-532 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346.png\" alt=\"\" width=\"400\" height=\"274\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 5<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next shortest edge (AE-4) which does not create a cycle as in Fig 35.3e.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-533 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347.png\" alt=\"\" width=\"343\" height=\"252\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 6<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Select the next shortest edge which does not create a cycle (EF-5). BC-5 is not selected because it forms a cycle (Fig35.3f).<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-534 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348.png\" alt=\"\" width=\"437\" height=\"290\" \/>\r\n<div><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Step 7<\/strong><\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Finally all vertices have been connected. The solution is (ED-2, AB-3, CD-4,AE-4,EF-5). Total weight if tree is 18.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.3<\/strong>\u00a0<strong>Walk Through<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe steps of \u2018walk through\u2019 operation is explained below in Figures 35.4a to 35.4k.\r\n<p style=\"text-align: justify\">The list consists of a sorted list of edges along with the cost of each edge.<\/p>\r\n&nbsp;\r\n\r\n<strong>Step 1<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider an undirected, weighted graph and sort the edges by non-decreasing edge weight as in Fig 35.4 a.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-535 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349.png\" alt=\"\" width=\"450\" height=\"288\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Step 2<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Select the first edge in the sorted list which does not generate a cycle (DE). This task of finding a cycle is carried out by using sets and is explained in section 35.3.2. Here this is not relevant since this is the first edge selected (Figure 35.4 (<strong>b<\/strong>))<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-536 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350.png\" alt=\"\" width=\"414\" height=\"235\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Step 3<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (DG) that has does not generate a cycle (Figure 35.4 (c).\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-537 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351.png\" alt=\"\" width=\"450\" height=\"268\" \/>\r\n\r\n<strong>Step 4<\/strong>\r\n<p style=\"text-align: justify\">Select the next cheapest edge in the list which is EG but that results in a cycle and hence is not considered (Figure 35 d).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-538 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352.png\" alt=\"\" width=\"470\" height=\"230\" \/>\r\n\r\n<strong>Step 5<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (CD) that has does not generate a cycle (Figure 35.4 (e).\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-539 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353.png\" alt=\"\" width=\"447\" height=\"243\" \/>\r\n\r\n<strong>Step 6<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (GH) that has does not generate a cycle (Figure 35.4 (f).\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-540 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354.png\" alt=\"\" width=\"459\" height=\"248\" \/><\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 6<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (CF) that has does not generate a cycle (Figure 35.4 (g).\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-541 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355.png\" alt=\"\" width=\"448\" height=\"262\" \/>\r\n\r\n<strong>Step 7<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (BC) that has does not generate a cycle (Figure 35.4 (h).\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-542 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356.png\" alt=\"\" width=\"464\" height=\"235\" \/>\r\n\r\n<strong>Step 8, Step9 &amp; Step 10<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelection of the next three edges, BE,BF,BH but that result in a cycle and hence they are not considered (Figure 35 i.).\r\n\r\n<\/div>\r\n<strong><img class=\"size-full wp-image-543 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357.png\" alt=\"\" width=\"498\" height=\"245\" \/><\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Step 11<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect the next cheapest edge (AH) that has does not generate a cycle (Figure 35.4 (j).\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-544 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358.png\" alt=\"\" width=\"426\" height=\"261\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now the remaining edges are not considered since all the vertices have been accounted for. The final MST is shown in Figure 35.4 (k). Total Cost of the MST= S <em>d<\/em><em>v<\/em> <em>= 21<\/em><\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-545 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359.png\" alt=\"\" width=\"449\" height=\"241\" \/>\r\n\r\n<strong>35.4<\/strong>\u00a0<strong>Kruskal's algorithm ( Details)<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe details of the algorithm is shown below.\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">MST_KRUSKAL(G,w)<\/span>\r\n<div>\r\n\r\n1 A:={}\r\n2 for each vertex v in V[G]\r\n3 do MAKE_SET(v)\r\n4 sort the edges of E by nondecreasing weight w\r\n5 for each edge (u,v) in E, in order by nondecreasing weight\r\n6 do if FIND_SET(u) != FIND_SET(v)\r\n7 then A:=A\u222a{(u,v)}\r\n8 UNION(u,v)\r\n9 return A\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">To implement this algorithm we must find a way to represent sets. We need to find which set a vertex is in . This is because when an edge is being considered for the MST, we must first find which sets the edge vertices belong to. Hence a <\/span><strong style=\"text-align: justify;font-size: 1em\"><em>findSet()<\/em><\/strong><span style=\"text-align: justify;font-size: 1em\"> operation is required.<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">We need to get the union of two sets since if the vertex sets are disjoint, the edge is added to the MST and the union of the two sets is obtained. So a <\/span><strong style=\"text-align: justify;font-size: 1em\"><em>union()<\/em><\/strong><span style=\"text-align: justify;font-size: 1em\"> operation is also needed.<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Testing if an edge creates a cycle can be slow, and hence we need a data structure which supports these operations- Union-Find data structure. There are two standard implementations : <\/span><strong style=\"text-align: justify;font-size: 1em\">Disjoint-set linked lists &amp; Disjoint-set trees.<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>35.4.1 Disjoint Sets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss this disjoint set. Here we keep a collection of sets S1, S2, .., Sk where each Si is a set of vertices, e,g, S1={v1, v2, v8}. We associate three operations with this Disjoint set. They are<\/p>\r\n\r\n<ul>\r\n \t<li>Make-Set(x)- This operation creates a new set whose only member is x.<\/li>\r\n \t<li style=\"text-align: justify\">Union(x, y) \u2013This operation unites the sets that contain x and y, say, Sx and Sy, into a new set that is the union of the two sets.<\/li>\r\n \t<li>Find-Set(x)-This operation returns a pointer to the representative of the set containing x.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nEach of the operations discussed above takes O(log n) time.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The union-find data structure is used to check if adding an edge to a set would create a cycle. Here we maintain a set for each connected component. ! If vertices x and y are in same component, then adding edge x-y creates a cycle. To add x-y to a set we create a new set by merging the sets containing x and y.<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong>35.4.2 Analysis of Kruskal Algorithm<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe Running Time of the Kruskal\u2019s algorithm =\u00a0 <strong>O(E log V)<\/strong>\r\n\r\n&nbsp;\r\n\r\nwhere\u00a0 (E = edges, V= vertices) of the graph.\r\n<p style=\"text-align: justify\">It usually only has to check a small fraction of the edges, but in some cases (like if there was a vertex connected to the graph by only one edge and it was the longest edge) it would have to check all the edges. This algorithm works best, of course, if the number of edges is kept to a minimum.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained in detail the Kruskal\u2019s Algorithm to find Minimum Spanning Trees<\/li>\r\n \t<li>Discussed the implementation issues associated with Kruskal\u2019s Algorithm<\/li>\r\n \t<li>Illustrated Kruskal\u2019s Algorithm through a walkthrough example<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Minimum Spanning Trees- Kruskal AIgorithm<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/pDMiWidTnwk\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<img class=\"size-full wp-image-546 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360.png\" alt=\"\" width=\"719\" height=\"351\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/pDMiWidTnwk\" 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. We have understood the Minimum Spanning Trees. In this module we will discuss about Kruskal\u2019s Algorithm in detail.<\/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\u00a0 To understand in detail the Kruskal\u2019s Algorithm to find Minimum Spanning Trees<\/p>\n<p>\u2022\u00a0 To discuss implementation issues associated with Kruskal\u2019s Algorithm<\/p>\n<p>\u2022\u00a0 To understand Kruskal\u2019s Algorithm through a walkthrough example<\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.1\u00a0 Spanning Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This is a recap of Spanning trees and minimum spanning trees. When an undirected 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 edges in G and that includes all the vertices. The edges of the graph E(G) is as below:<\/p>\n<p>&nbsp;<\/p>\n<p>E(G): T (tree edges) + N (nontree edges) where<\/p>\n<p>T: set of edges used during search<\/p>\n<p>N: set of remaining edges<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Minimum Spanning Tree (MST) for a given graph is the Spanning Tree of minimum cost for that graph.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.2\u00a0 Greedy Algorithms<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us make a simplifying assumption that all edge costs ce are distinct. With this assumption let us now define two properties namely Cut Property and Cycle Property (Figure 35.1).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Cut property <\/strong>&#8211; Let S be any subset of nodes, and let e be the minimum cost edge with exactly one endpoint in S. Then the MST contains e.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Cycle property &#8211; <\/strong>Let C be any cycle, and let f be the maximum cost edge belonging to C. Then the MST does not contain f.<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-527 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341.png\" alt=\"\" width=\"645\" height=\"288\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341.png 645w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341-300x134.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341-65x29.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341-225x100.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-341-350x156.png 350w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/strong><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 35.2 Kruskal\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.2.1 Generic Algorithm for growing MST<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>First we will examine the generic MST algorithm.<\/p>\n<p>&nbsp;<\/p>\n<p>GENERIC_MST(G,w)<\/p>\n<p>&nbsp;<\/p>\n<p>1. A:={} \/* (can be a forest or a single tree \u2013 see section 35.2.2 )<\/p>\n<p>2. while A does not form a spanning tree do<\/p>\n<p>3. find an edge (u,v) that is safe for A<\/p>\n<p>4. A:=A\u222a{(u,v)}<\/p>\n<p>5. return A<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Set A is always a subset of some minimum spanning tree. This property is called the <strong>invariant Property. <\/strong>An edge (u,v) is a<strong> safe edge <\/strong>for A if adding the edge to A does not destroy the invariant property. A <strong>safe edge is<\/strong> just the CORRECT edge to choose to add.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.2.2 Kruskal and Prim\u2019s Algorithms<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Both Kruskal and Prim\u2019s algorithms are based on the generic algorithm. A specific rule to determine a safe edge is what diffrentiates the two algorithms. Set A is always a subset of some minimum spanning tree<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In Kruskal&#8217;s algorithm, the set A is a forest. In this algorithm the safe edge added is always a least-weight edge in the graph that connects two distinct components.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In Prim&#8217;s algorithm, the set A forms a single tree <strong>.<\/strong> The safe edge added is always a least-weight edge connecting the tree to a vertex not in the tree.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">35.3 Kruskal Agorithm in detail<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Now we will discuss the Kruskal algorithm in detail. Created in 1957 by Joseph Kruskal, it finds the MST by taking the smallest weight in the graph and connecting the two nodes and repeating until all nodes are connected to just one tree. This is done by creating a priority queue using the weights as keys . It creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If the edge would result in a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">35.3.1 Concept of Kruskal\u2019s Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Add the cheapest edge that joins disjoint components as in Fig 35.2. We we select (uv) since that is the cheapest edge, then we select (sb) since that is the next cheapest edge and so on. We do not select edges if they form a cycle a nd stop when all nodes are covered.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-528 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342.png\" alt=\"\" width=\"499\" height=\"346\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342.png 499w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342-300x208.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342-225x156.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-342-350x243.png 350w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.The forest is constructed with each node forming an independent tree. The edges are placed in a priority queue using the weights as keys.<\/p>\n<p style=\"text-align: justify\">2.Until we have added n-1 edges,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 60px\">(a)\u00a0 Extract the cheapest edge from the queue,<\/p>\n<p style=\"padding-left: 60px\">(b)\u00a0 If it forms a cycle, reject it,<\/p>\n<p style=\"padding-left: 60px\"><span style=\"font-size: 1em;text-align: initial\">(c)\u00a0 Else add it to the forest. Adding it to the forest will join two trees together.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Every step Joins &#8211; two trees in the forest together, so that at the end, there will only be one tree in T.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The reason the algorithm works is that each added edge is a connection between two sets of vertices, and since we select the edges in order of weight, we are always selecting the minimum edge weight that connects the two sets of vertices.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.3.2 Cycle detection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>One important part of Kruskal\u2019s algorithm is the cycle detection. Here the steps are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>Keep track of disjoint sets.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Initially, each vertex is in its own disjoint set.<\/li>\n<li>When you add an edge you are unionizing two sets.<\/li>\n<li>A union cannot happen if the two vertices are already in the same set.<\/li>\n<\/ul>\n<p style=\"padding-left: 60px\">\u2022\u00a0 This would create a cycle.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>35.3.3 Basics of Kruskal\u2019s Algorithm<\/strong><\/p>\n<ol>\n<li>Sort the edges E in an non-decreasing order (please note two edges can have the same weight)<\/li>\n<li>A:={}<\/li>\n<li><strong style=\"text-align: initial;font-size: 1em\">while <\/strong><span style=\"text-align: initial;font-size: 1em\">E is not empty<\/span><strong style=\"text-align: initial;font-size: 1em\"> do {<\/strong><\/li>\n<\/ol>\n<p style=\"padding-left: 30px\">take an edge (u, v) that is shortest in E and delete it from E<\/p>\n<p style=\"padding-left: 30px\"><strong>if <\/strong>u and v are in different components<strong> then<\/strong><\/p>\n<p style=\"padding-left: 30px\">add (u, v) to A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here each time a shortest edge in E is considered. We work with edges, rather than nodes. In other words there are two fundamental steps. We need to sort edges by increasing edge weight and then select the first |V| \u2013 1 edges that do not generate a cycle. Each step of the Kruskal Algorithm is explained below with an illustartive example..<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>List the edges of graph where the edges are sorted by increasing weight as shown in Fig 35.3a as below.<\/p>\n<p>(ED-2, AB-3, AE-4,CD-4, BC-5, EF-5, CF-6,AF-7,BF-8,DF-8)<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-529 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-343.png\" alt=\"\" width=\"286\" height=\"273\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-343.png 286w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-343-65x62.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-343-225x215.png 225w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/p>\n<p><strong>Step 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the shortest edge in the list (ie ED-2) as in Fig 35.3b.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-530 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344.png\" alt=\"\" width=\"332\" height=\"219\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344.png 332w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344-300x198.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-344-225x148.png 225w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/p>\n<p><strong>Step 3<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next shortest edge (AB-3) which does not create a cycle as in Fig 35.3c<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-531 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345.png\" alt=\"\" width=\"338\" height=\"247\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345.png 338w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345-300x219.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-345-225x164.png 225w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><\/p>\n<p><strong>Step 4<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Select the next shortest edge CD-4 (either CD or AE could have been selected first since both have same weight) which does not create a cycle (Fig 35.3d).<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-532 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346.png\" alt=\"\" width=\"400\" height=\"274\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346.png 400w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346-300x206.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-346-350x240.png 350w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next shortest edge (AE-4) which does not create a cycle as in Fig 35.3e.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-533 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347.png\" alt=\"\" width=\"343\" height=\"252\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347.png 343w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347-300x220.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-347-225x165.png 225w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Select the next shortest edge which does not create a cycle (EF-5). BC-5 is not selected because it forms a cycle (Fig35.3f).<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-534 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348.png\" alt=\"\" width=\"437\" height=\"290\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348.png 437w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348-300x199.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348-225x149.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-348-350x232.png 350w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/p>\n<div><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Step 7<\/strong><\/div>\n<div>\n<p style=\"text-align: justify\">Finally all vertices have been connected. The solution is (ED-2, AB-3, CD-4,AE-4,EF-5). Total weight if tree is 18.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3<\/strong>\u00a0<strong>Walk Through<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The steps of \u2018walk through\u2019 operation is explained below in Figures 35.4a to 35.4k.<\/p>\n<p style=\"text-align: justify\">The list consists of a sorted list of edges along with the cost of each edge.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider an undirected, weighted graph and sort the edges by non-decreasing edge weight as in Fig 35.4 a.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-535 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349.png\" alt=\"\" width=\"450\" height=\"288\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349.png 450w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349-300x192.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349-225x144.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-349-350x224.png 350w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Select the first edge in the sorted list which does not generate a cycle (DE). This task of finding a cycle is carried out by using sets and is explained in section 35.3.2. Here this is not relevant since this is the first edge selected (Figure 35.4 (<strong>b<\/strong>))<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-536 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350.png\" alt=\"\" width=\"414\" height=\"235\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350.png 414w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-350-350x199.png 350w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (DG) that has does not generate a cycle (Figure 35.4 (c).<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-537 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351.png\" alt=\"\" width=\"450\" height=\"268\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351.png 450w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-351-350x208.png 350w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p><strong>Step 4<\/strong><\/p>\n<p style=\"text-align: justify\">Select the next cheapest edge in the list which is EG but that results in a cycle and hence is not considered (Figure 35 d).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-538 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352.png\" alt=\"\" width=\"470\" height=\"230\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352.png 470w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352-300x147.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-352-350x171.png 350w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/p>\n<p><strong>Step 5<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (CD) that has does not generate a cycle (Figure 35.4 (e).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-539 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353.png\" alt=\"\" width=\"447\" height=\"243\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353.png 447w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-353-350x190.png 350w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/p>\n<p><strong>Step 6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (GH) that has does not generate a cycle (Figure 35.4 (f).<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-540 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354.png\" alt=\"\" width=\"459\" height=\"248\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354.png 459w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354-300x162.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-354-350x189.png 350w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (CF) that has does not generate a cycle (Figure 35.4 (g).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-541 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355.png\" alt=\"\" width=\"448\" height=\"262\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355.png 448w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355-300x175.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355-225x132.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-355-350x205.png 350w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/p>\n<p><strong>Step 7<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (BC) that has does not generate a cycle (Figure 35.4 (h).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-542 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356.png\" alt=\"\" width=\"464\" height=\"235\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356.png 464w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356-300x152.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356-225x114.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-356-350x177.png 350w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><\/p>\n<p><strong>Step 8, Step9 &amp; Step 10<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Selection of the next three edges, BE,BF,BH but that result in a cycle and hence they are not considered (Figure 35 i.).<\/p>\n<\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-543 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357.png\" alt=\"\" width=\"498\" height=\"245\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357.png 498w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-357-350x172.png 350w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Step 11<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select the next cheapest edge (AH) that has does not generate a cycle (Figure 35.4 (j).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-544 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358.png\" alt=\"\" width=\"426\" height=\"261\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358.png 426w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358-300x184.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-358-350x214.png 350w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now the remaining edges are not considered since all the vertices have been accounted for. The final MST is shown in Figure 35.4 (k). Total Cost of the MST= S <em>d<\/em><em>v<\/em> <em>= 21<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-545 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359.png\" alt=\"\" width=\"449\" height=\"241\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359.png 449w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359-300x161.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-359-350x188.png 350w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/p>\n<p><strong>35.4<\/strong>\u00a0<strong>Kruskal&#8217;s algorithm ( Details)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The details of the algorithm is shown below.<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">MST_KRUSKAL(G,w)<\/span><\/p>\n<div>\n<p>1 A:={}<br \/>\n2 for each vertex v in V[G]<br \/>\n3 do MAKE_SET(v)<br \/>\n4 sort the edges of E by nondecreasing weight w<br \/>\n5 for each edge (u,v) in E, in order by nondecreasing weight<br \/>\n6 do if FIND_SET(u) != FIND_SET(v)<br \/>\n7 then A:=A\u222a{(u,v)}<br \/>\n8 UNION(u,v)<br \/>\n9 return A<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">To implement this algorithm we must find a way to represent sets. We need to find which set a vertex is in . This is because when an edge is being considered for the MST, we must first find which sets the edge vertices belong to. Hence a <\/span><strong style=\"text-align: justify;font-size: 1em\"><em>findSet()<\/em><\/strong><span style=\"text-align: justify;font-size: 1em\"> operation is required.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">We need to get the union of two sets since if the vertex sets are disjoint, the edge is added to the MST and the union of the two sets is obtained. So a <\/span><strong style=\"text-align: justify;font-size: 1em\"><em>union()<\/em><\/strong><span style=\"text-align: justify;font-size: 1em\"> operation is also needed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Testing if an edge creates a cycle can be slow, and hence we need a data structure which supports these operations- Union-Find data structure. There are two standard implementations : <\/span><strong style=\"text-align: justify;font-size: 1em\">Disjoint-set linked lists &amp; Disjoint-set trees.<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>35.4.1 Disjoint Sets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss this disjoint set. Here we keep a collection of sets S1, S2, .., Sk where each Si is a set of vertices, e,g, S1={v1, v2, v8}. We associate three operations with this Disjoint set. They are<\/p>\n<ul>\n<li>Make-Set(x)- This operation creates a new set whose only member is x.<\/li>\n<li style=\"text-align: justify\">Union(x, y) \u2013This operation unites the sets that contain x and y, say, Sx and Sy, into a new set that is the union of the two sets.<\/li>\n<li>Find-Set(x)-This operation returns a pointer to the representative of the set containing x.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Each of the operations discussed above takes O(log n) time.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The union-find data structure is used to check if adding an edge to a set would create a cycle. Here we maintain a set for each connected component. ! If vertices x and y are in same component, then adding edge x-y creates a cycle. To add x-y to a set we create a new set by merging the sets containing x and y.<\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>35.4.2 Analysis of Kruskal Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The Running Time of the Kruskal\u2019s algorithm =\u00a0 <strong>O(E log V)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>where\u00a0 (E = edges, V= vertices) of the graph.<\/p>\n<p style=\"text-align: justify\">It usually only has to check a small fraction of the edges, but in some cases (like if there was a vertex connected to the graph by only one edge and it was the longest edge) it would have to check all the edges. This algorithm works best, of course, if the number of edges is kept to a minimum.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained in detail the Kruskal\u2019s Algorithm to find Minimum Spanning Trees<\/li>\n<li>Discussed the implementation issues associated with Kruskal\u2019s Algorithm<\/li>\n<li>Illustrated Kruskal\u2019s Algorithm through a walkthrough example<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Minimum Spanning Trees- Kruskal AIgorithm<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/pDMiWidTnwk\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-546 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360.png\" alt=\"\" width=\"719\" height=\"351\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360.png 719w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360-300x146.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-360-350x171.png 350w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/p>\n","protected":false},"author":3,"menu_order":35,"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-524","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\/524","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":9,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/524\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/524\/revisions\/983"}],"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\/524\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=524"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=524"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}