{"id":26347,"date":"2017-10-26T21:06:45","date_gmt":"2017-10-26T15:36:45","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26347"},"modified":"2017-10-26T21:06:45","modified_gmt":"2017-10-26T15:36:45","slug":"greedy-algorithms-set-2-kruskals-minimum-spanning-tree-algorithm","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-algorithm\/","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>Java programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program for Kruskal&#039;s algorithm to find Minimum Spanning Tree<br\/>\/\/ of a given connected, undirected and weighted graph<br\/>import java.util.*;<br\/>import java.lang.*;<br\/>import java.io.*;<br\/> <br\/>class Graph<br\/>{<br\/>    \/\/ A class to represent a graph edge<br\/>    class Edge implements Comparable&lt;Edge&gt;<br\/>    {<br\/>        int src, dest, weight;<br\/> <br\/>        \/\/ Comparator function used for sorting edges based on<br\/>        \/\/ their weight<br\/>        public int compareTo(Edge compareEdge)<br\/>        {<br\/>            return this.weight-compareEdge.weight;<br\/>        }<br\/>    };<br\/> <br\/>    \/\/ A class to represent a subset for union-find<br\/>    class subset<br\/>    {<br\/>        int parent, rank;<br\/>    };<br\/> <br\/>    int V, E;    \/\/ V-&gt; no. of vertices &amp; E-&gt;no.of edges<br\/>    Edge edge[]; \/\/ collection of all edges<br\/> <br\/>    \/\/ Creates a graph with V vertices and E edges<br\/>    Graph(int v, int e)<br\/>    {<br\/>        V = v;<br\/>        E = e;<br\/>        edge = new Edge[E];<br\/>        for (int i=0; i&lt;e; ++i)<br\/>            edge[i] = new Edge();<br\/>    }<br\/> <br\/>    \/\/ A utility function to find set of an element i<br\/>    \/\/ (uses path compression technique)<br\/>    int find(subset subsets[], int i)<br\/>    {<br\/>        \/\/ find root and make root as parent of i (path compression)<br\/>        if (subsets[i].parent != i)<br\/>            subsets[i].parent = find(subsets, subsets[i].parent);<br\/> <br\/>        return subsets[i].parent;<br\/>    }<br\/> <br\/>    \/\/ A function that does union of two sets of x and y<br\/>    \/\/ (uses union by rank)<br\/>    void Union(subset subsets[], int x, int y)<br\/>    {<br\/>        int xroot = find(subsets, x);<br\/>        int yroot = find(subsets, y);<br\/> <br\/>        \/\/ Attach smaller rank tree under root of high rank tree<br\/>        \/\/ (Union by Rank)<br\/>        if (subsets[xroot].rank &lt; subsets[yroot].rank)<br\/>            subsets[xroot].parent = yroot;<br\/>        else if (subsets[xroot].rank &gt; subsets[yroot].rank)<br\/>            subsets[yroot].parent = xroot;<br\/> <br\/>        \/\/ If ranks are same, then make one as root and increment<br\/>        \/\/ its rank by one<br\/>        else<br\/>        {<br\/>            subsets[yroot].parent = xroot;<br\/>            subsets[xroot].rank++;<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ The main function to construct MST using Kruskal&#039;s algorithm<br\/>    void KruskalMST()<br\/>    {<br\/>        Edge result[] = new Edge[V];  \/\/ Tnis will store the resultant MST<br\/>        int e = 0;  \/\/ An index variable, used for result[]<br\/>        int i = 0;  \/\/ An index variable, used for sorted edges<br\/>        for (i=0; i&lt;V; ++i)<br\/>            result[i] = new Edge();<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 array of edges<br\/>        Arrays.sort(edge);<br\/> <br\/>        \/\/ Allocate memory for creating V ssubsets<br\/>        subset subsets[] = new subset[V];<br\/>        for(i=0; i&lt;V; ++i)<br\/>            subsets[i]=new subset();<br\/> <br\/>        \/\/ Create V subsets with single elements<br\/>        for (int v = 0; v &lt; V; ++v)<br\/>        {<br\/>            subsets[v].parent = v;<br\/>            subsets[v].rank = 0;<br\/>        }<br\/> <br\/>        i = 0;  \/\/ Index used to pick next edge<br\/> <br\/>        \/\/ Number of edges to be taken is equal to V-1<br\/>        while (e &lt; V - 1)<br\/>        {<br\/>            \/\/ Step 2: Pick the smallest edge. And increment the index<br\/>            \/\/ for next iteration<br\/>            Edge next_edge = new Edge();<br\/>            next_edge = edge[i++];<br\/> <br\/>            int x = find(subsets, next_edge.src);<br\/>            int y = find(subsets, next_edge.dest);<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\/>            {<br\/>                result[e++] = next_edge;<br\/>                Union(subsets, x, y);<br\/>            }<br\/>            \/\/ Else discard the next_edge<br\/>        }<br\/> <br\/>        \/\/ print the contents of result[] to display the built MST<br\/>        System.out.println(&quot;Following are the edges in the constructed MST&quot;);<br\/>        for (i = 0; i &lt; e; ++i)<br\/>            System.out.println(result[i].src+&quot; -- &quot;+result[i].dest+&quot; == &quot;+<br\/>                               result[i].weight);<br\/>    }<br\/> <br\/>    \/\/ Driver Program<br\/>    public static void main (String[] args)<br\/>    {<br\/> <br\/>        \/* Let us create following weighted graph<br\/>                 10<br\/>            0--------1<br\/>            |  \\     |<br\/>           6|   5\\   |15<br\/>            |      \\ |<br\/>            2--------3<br\/>                4       *\/<br\/>        int V = 4;  \/\/ Number of vertices in graph<br\/>        int E = 5;  \/\/ Number of edges in graph<br\/>        Graph graph = new Graph(V, E);<br\/> <br\/>        \/\/ add edge 0-1<br\/>        graph.edge[0].src = 0;<br\/>        graph.edge[0].dest = 1;<br\/>        graph.edge[0].weight = 10;<br\/> <br\/>        \/\/ add edge 0-2<br\/>        graph.edge[1].src = 0;<br\/>        graph.edge[1].dest = 2;<br\/>        graph.edge[1].weight = 6;<br\/> <br\/>        \/\/ add edge 0-3<br\/>        graph.edge[2].src = 0;<br\/>        graph.edge[2].dest = 3;<br\/>        graph.edge[2].weight = 5;<br\/> <br\/>        \/\/ add edge 1-3<br\/>        graph.edge[3].src = 1;<br\/>        graph.edge[3].dest = 3;<br\/>        graph.edge[3].weight = 15;<br\/> <br\/>        \/\/ add edge 2-3<br\/>        graph.edge[4].src = 2;<br\/>        graph.edge[4].dest = 3;<br\/>        graph.edge[4].weight = 4;<br\/> <br\/>        graph.KruskalMST();<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,76379],"tags":[70774,78372,70781,78371,78374,78373,70770,70804],"class_list":["post-26347","post","type-post","status-publish","format-standard","hentry","category-algorithm","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\/26347","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=26347"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26347\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}