{"id":26302,"date":"2017-10-26T21:02:24","date_gmt":"2017-10-26T15:32:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26302"},"modified":"2017-10-26T21:02:24","modified_gmt":"2017-10-26T15:32:24","slug":"minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/minimum-spanning-tree\/","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 this for applications of MST.<\/p>\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 Union-Find algorithm to detect cycle. So we recommend to read following post as a prerequisite.<br \/>\nUnion-Find Algorithm | Set 1 (Detect Cycle in a Graph)<br \/>\nUnion-Find Algorithm | Set 2 (Union By Rank and Path Compression)<\/p>\n[ad type=&#8221;banner&#8221;]\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>&nbsp;<\/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>&nbsp;<\/p>\n<p>&nbsp;<\/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>C++ Programming<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program for Kruskal&#039;s algorithm to find Minimum Spanning Tree<br\/>\/\/ of a given connected, undirected and weighted graph<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>\/\/ a structure to represent a weighted edge in graph<br\/>struct Edge<br\/>{<br\/>    int src, dest, weight;<br\/>};<br\/> <br\/>\/\/ a structure to represent a connected, undirected and weighted graph<br\/>struct Graph<br\/>{<br\/>    \/\/ V-&gt; Number of vertices, E-&gt; Number of edges<br\/>    int V, E;<br\/> <br\/>    \/\/ graph is represented as an array of edges. Since the graph is<br\/>    \/\/ undirected, the edge from src to dest is also edge from dest<br\/>    \/\/ to src. Both are counted as 1 edge here.<br\/>    struct Edge* edge;<br\/>};<br\/> <br\/>\/\/ Creates a graph with V vertices and E edges<br\/>struct Graph* createGraph(int V, int E)<br\/>{<br\/>    struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) );<br\/>    graph-&gt;V = V;<br\/>    graph-&gt;E = E;<br\/> <br\/>    graph-&gt;edge = (struct Edge*) malloc( graph-&gt;E * sizeof( struct Edge ) );<br\/> <br\/>    return graph;<br\/>}<br\/> <br\/>\/\/ A structure to represent a subset for union-find<br\/>struct subset<br\/>{<br\/>    int parent;<br\/>    int rank;<br\/>};<br\/> <br\/>\/\/ A utility function to find set of an element i<br\/>\/\/ (uses path compression technique)<br\/>int find(struct 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(struct 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\/>\/\/ Compare two edges according to their weights.<br\/>\/\/ Used in qsort() for sorting an array of edges<br\/>int myComp(const void* a, const void* b)<br\/>{<br\/>    struct Edge* a1 = (struct Edge*)a;<br\/>    struct Edge* b1 = (struct Edge*)b;<br\/>    return a1-&gt;weight &gt; b1-&gt;weight;<br\/>}<br\/> <br\/>\/\/ The main function to construct MST using Kruskal&#039;s algorithm<br\/>void KruskalMST(struct Graph* graph)<br\/>{<br\/>    int V = graph-&gt;V;<br\/>    struct Edge result[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\/> <br\/>    \/\/ Step 1:  Sort all the edges in non-decreasing order of their weight<br\/>    \/\/ If we are not allowed to change the given graph, we can create a copy of<br\/>    \/\/ array of edges<br\/>    qsort(graph-&gt;edge, graph-&gt;E, sizeof(graph-&gt;edge[0]), myComp);<br\/> <br\/>    \/\/ Allocate memory for creating V ssubsets<br\/>    struct subset *subsets =<br\/>        (struct subset*) malloc( V * sizeof(struct 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\/>    \/\/ 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\/>        struct Edge next_edge = graph-&gt;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\/>    printf(&quot;Following are the edges in the constructed MST\\n&quot;);<br\/>    for (i = 0; i &lt; e; ++i)<br\/>        printf(&quot;%d -- %d == %d\\n&quot;, result[i].src, result[i].dest,<br\/>                                                   result[i].weight);<br\/>    return;<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<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\/>    struct Graph* graph = createGraph(V, E);<br\/> <br\/> <br\/>    \/\/ add edge 0-1<br\/>    graph-&gt;edge[0].src = 0;<br\/>    graph-&gt;edge[0].dest = 1;<br\/>    graph-&gt;edge[0].weight = 10;<br\/> <br\/>    \/\/ add edge 0-2<br\/>    graph-&gt;edge[1].src = 0;<br\/>    graph-&gt;edge[1].dest = 2;<br\/>    graph-&gt;edge[1].weight = 6;<br\/> <br\/>    \/\/ add edge 0-3<br\/>    graph-&gt;edge[2].src = 0;<br\/>    graph-&gt;edge[2].dest = 3;<br\/>    graph-&gt;edge[2].weight = 5;<br\/> <br\/>    \/\/ add edge 1-3<br\/>    graph-&gt;edge[3].src = 1;<br\/>    graph-&gt;edge[3].dest = 3;<br\/>    graph-&gt;edge[3].weight = 15;<br\/> <br\/>    \/\/ add edge 2-3<br\/>    graph-&gt;edge[4].src = 2;<br\/>    graph-&gt;edge[4].dest = 3;<br\/>    graph-&gt;edge[4].weight = 4;<br\/> <br\/>    KruskalMST(graph);<br\/> <br\/>    return 0;<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 (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,83620,76379],"tags":[70774,78372,70781,78371,78374,78373,70770,70804],"class_list":["post-26302","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-kruskals-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\/26302","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=26302"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26302\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}