{"id":26357,"date":"2017-10-26T21:11:23","date_gmt":"2017-10-26T15:41:23","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26357"},"modified":"2017-10-26T21:11:23","modified_gmt":"2017-10-26T15:41:23","slug":"greedy-algorithms-set-2-kruskals-minimum-spanning-tree-algorithm-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-algorithm-2\/","title":{"rendered":"Greedy Algorithms | Set 2 (Kruskal\u2019s Minimum Spanning Tree Algorithm)"},"content":{"rendered":"<p><em>What is Minimum Spanning Tree?<\/em><br \/>\nGiven a connected and undirected graph, a <em>spanning tree<\/em> of that graph is a subgraph that is a tree and connects all the vertices together. <span id=\"more-26604\"><\/span>A single graph can have many different spanning trees. A <em>minimum spanning tree (MST)<\/em> or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree.<\/p>\n<p><em>How many edges does a minimum spanning tree has?<\/em><br \/>\nA minimum spanning tree has (V \u2013 1) edges where V is the number of vertices in the given graph.<\/p>\n<p><em>What are the applications of Minimum Spanning Tree?<\/em><br \/>\nSee <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/11110\" target=\"_blank\" rel=\"noopener\">this <\/a>for applications of MST.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Below are the steps for finding MST using Kruskal\u2019s algorithm:<\/p>\n<pre><strong>1.<\/strong> Sort all the edges in non-decreasing order of their weight.\r\n\r\n<strong>2.<\/strong> Pick the smallest edge. Check if it forms a cycle with the spanning tree \r\nformed so far. If cycle is not formed, include this edge. Else, discard it.  \r\n\r\n<strong>3.<\/strong> Repeat step#2 until there are (V-1) edges in the spanning tree.<\/pre>\n<p>The step#2 uses <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/26350\" target=\"_blank\" rel=\"noopener\">Union-Find algorithm<\/a> to detect cycle. So we recommend to read following post as a prerequisite.<br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/union-find\/\" target=\"_blank\" rel=\"noopener\">Union-Find Algorithm | Set 1 (Detect Cycle in a Graph)<\/a><br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/union-find-algorithm-set-2-union-by-rank\/\" target=\"_blank\" rel=\"noopener\">Union-Find Algorithm | Set 2 (Union By Rank and Path Compression)<\/a><\/p>\n<p>The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight edge that does not cause a cycle in the MST constructed so far. Let us understand it with an example: Consider the below input graph.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26263\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Dijkstra\u2019s-Algorithm-for-Adjacency-List-Representation.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm\" width=\"717\" height=\"334\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Dijkstra\u2019s-Algorithm-for-Adjacency-List-Representation.png 717w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Dijkstra\u2019s-Algorithm-for-Adjacency-List-Representation-300x140.png 300w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/p>\n<p>The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be having (9 \u2013 1) = 8 edges.<\/p>\n<pre>After sorting:\r\nWeight   Src    Dest\r\n1         7      6\r\n2         8      2\r\n2         6      5\r\n4         0      1\r\n4         2      5\r\n6         8      6\r\n7         2      3\r\n7         7      8\r\n8         0      7\r\n8         1      2\r\n9         3      4\r\n10        5      4\r\n11        1      7\r\n14        3      5<\/pre>\n<p><strong>1.<\/strong> <em>Pick edge 7-6:<\/em> No cycle is formed, include it.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26318\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-11-2.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm)\" width=\"264\" height=\"72\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-11-2.png 264w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-11-2-260x72.png 260w\" sizes=\"(max-width: 264px) 100vw, 264px\" \/><\/p>\n<p><strong>2.<\/strong> <em>Pick edge 8-2:<\/em> No cycle is formed, include it.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26317\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-2-241x300.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm)\" width=\"241\" height=\"300\" \/><\/p>\n<p><strong>3.<\/strong> <em>Pick edge 6-5:<\/em> No cycle is formed, include it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26326\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-3-300x232.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm\" width=\"300\" height=\"232\" \/><\/p>\n<p><strong>4.<\/strong> <em>Pick edge 0-1:<\/em> No cycle is formed, include it<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26328\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-4-300x175-1.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm\" width=\"300\" height=\"175\" \/><\/p>\n<p><strong>5.<\/strong> <em>Pick edge 2-5:<\/em> No cycle is formed, include it.<\/p>\n<p><strong>6.<\/strong><em> Pick edge 8-6: <\/em>Since including this edge results in cycle, discard it.<\/p>\n<p><strong>7.<\/strong> <em>Pick edge 2-3:<\/em> No cycle is formed, include it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26331\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-6-300x175.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm)\" width=\"300\" height=\"175\" \/><\/p>\n<p><strong>8.<\/strong> <em>Pick edge 7-8:<\/em> Since including this edge results in cycle, discard it.<\/p>\n<p><strong>9.<\/strong> <em>Pick edge 0-7:<\/em> No cycle is formed, include it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26336\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Fig-7-300x175.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm)\" width=\"300\" height=\"175\" \/><\/p>\n<p><strong>10.<\/strong> <em>Pick edge 1-2: <\/em>Since including this edge results in cycle, discard it.<\/p>\n<p><strong>11.<\/strong> <em>Pick edge 3-4:<\/em> No cycle is formed, include it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26340\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/fig8new.png\" alt=\"Kruskal\u2019s Minimum Spanning Tree Algorithm)\" width=\"712\" height=\"328\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/fig8new.png 712w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/fig8new-300x138.png 300w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Python programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for Kruskal&#039;s algorithm to find Minimum Spanning Tree<br\/># of a given connected, undirected and weighted graph<br\/> <br\/>from collections import defaultdict<br\/> <br\/>#Class to represent a graph<br\/>class Graph:<br\/> <br\/>    def __init__(self,vertices):<br\/>        self.V= vertices #No. of vertices<br\/>        self.graph = [] # default dictionary to store graph<br\/>         <br\/>  <br\/>    # function to add an edge to graph<br\/>    def addEdge(self,u,v,w):<br\/>        self.graph.append([u,v,w])<br\/> <br\/>    # A utility function to find set of an element i<br\/>    # (uses path compression technique)<br\/>    def find(self, parent, i):<br\/>        if parent[i] == i:<br\/>            return i<br\/>        return self.find(parent, parent[i])<br\/> <br\/>    # A function that does union of two sets of x and y<br\/>    # (uses union by rank)<br\/>    def union(self, parent, rank, x, y):<br\/>        xroot = self.find(parent, x)<br\/>        yroot = self.find(parent, y)<br\/> <br\/>        # Attach smaller rank tree under root of high rank tree<br\/>        # (Union by Rank)<br\/>        if rank[xroot] &lt; rank[yroot]:<br\/>            parent[xroot] = yroot<br\/>        elif rank[xroot] &gt; rank[yroot]:<br\/>            parent[yroot] = xroot<br\/>        #If ranks are same, then make one as root and increment<br\/>        # its rank by one<br\/>        else :<br\/>            parent[yroot] = xroot<br\/>            rank[xroot] += 1<br\/> <br\/>    # The main function to construct MST using Kruskal&#039;s algorithm<br\/>    def KruskalMST(self):<br\/> <br\/>        result =[] #This will store the resultant MST<br\/> <br\/>        i = 0 # An index variable, used for sorted edges<br\/>        e = 0 # An index variable, used for result[]<br\/> <br\/>        #Step 1:  Sort all the edges in non-decreasing order of their<br\/>        # weight.  If we are not allowed to change the given graph, we<br\/>        # can create a copy of graph<br\/>        self.graph =  sorted(self.graph,key=lambda item: item[2])<br\/>        #print self.graph<br\/> <br\/>        parent = [] ; rank = []<br\/> <br\/>        # Create V subsets with single elements<br\/>        for node in range(self.V):<br\/>            parent.append(node)<br\/>            rank.append(0)<br\/>     <br\/>        # Number of edges to be taken is equal to V-1<br\/>        while e &lt; self.V -1 :<br\/> <br\/>            # Step 2: Pick the smallest edge and increment the index<br\/>            # for next iteration<br\/>            u,v,w =  self.graph[i]<br\/>            i = i + 1<br\/>            x = self.find(parent, u)<br\/>            y = self.find(parent ,v)<br\/> <br\/>            # If including this edge does&#039;t cause cycle, include it<br\/>            # in result and increment the index of result for next edge<br\/>            if x != y:<br\/>                e = e + 1  <br\/>                result.append([u,v,w])<br\/>                self.union(parent, rank, x, y)          <br\/>            # Else discard the edge<br\/> <br\/>        # print the contents of result[] to display the built MST<br\/>        print &quot;Following are the edges in the constructed MST&quot;<br\/>        for u,v,weight  in result:<br\/>            #print str(u) + &quot; -- &quot; + str(v) + &quot; == &quot; + str(weight)<br\/>            print (&quot;%d -- %d == %d&quot; % (u,v,weight))<br\/> <br\/> <br\/>g = Graph(4)<br\/>g.addEdge(0, 1, 10)<br\/>g.addEdge(0, 2, 6)<br\/>g.addEdge(0, 3, 5)<br\/>g.addEdge(1, 3, 15)<br\/>g.addEdge(2, 3, 4)<br\/> <br\/>g.KruskalMST()<\/code><\/pre> <\/div>\n<div class=\"line number1 index0 alt2\"><code class=\"python comments\"><\/code><code class=\"python plain\"><\/code><\/div>\n<pre>Following are the edges in the constructed MST\r\n2 -- 3 == 4\r\n0 -- 3 == 5\r\n0 -- 1 == 10<\/pre>\n<p><strong>Time Complexity:<\/strong> O(ElogE) or O(ElogV). Sorting of edges takes O(ELogE) time. After sorting, we iterate through all edges and apply find-union algorithm. The find and union operations can take atmost O(LogV) time. So overall complexity is O(ELogE + ELogV) time. The value of E can be atmost O(V<sup>2<\/sup>), so O(LogV) are O(LogE) same. Therefore, overall time complexity is O(ElogE) or O(ElogV)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Kruskal\u2019s Minimum Spanning Tree Algorithm) &#8211; Minimum Spanning Tree A single graph can have many different spanning trees. A minimum spanning tree (MST).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,76379],"tags":[70774,78372,70781,78371,78374,78373,70770,70804],"class_list":["post-26357","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-minimum-spanning-tree","tag-kruskals-algorithm-c","tag-kruskals-algorithm-geeksforgeeks","tag-kruskals-algorithm-in-c","tag-kruskals-algorithm-java","tag-kruskals-algorithm-pseudocode","tag-kruskals-algorithm-time-complexity","tag-prims-algorithm-example","tag-prims-algorithm-minimum-spanning-tree"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26357"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26357\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}