{"id":2286,"date":"2023-06-15T19:46:34","date_gmt":"2023-06-15T14:16:34","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=2286"},"modified":"2023-06-15T20:01:36","modified_gmt":"2023-06-15T14:31:36","slug":"minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/minimum-spanning-tree\/","title":{"rendered":"Minimum Spanning Tree &#8211; Kruskal Algorithm"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"gutentoc tocactive nostyle\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#problem-statement\">Problem Statement<\/a><\/li><li><a href=\"#approach\">Approach<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#dry-run\">Dry Run<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#step-1\">Step 1:<\/a><\/li><li><a href=\"#step-2\">Step 2:<\/a><\/li><li><a href=\"#step-3\">Step 3:<\/a><\/li><li><a href=\"#step-4\">Step 4:<\/a><\/li><li><a href=\"#step-5\">Step 5:<\/a><\/li><\/ul><li><a href=\"#cc-implementation\">C\/C++ Implementation<\/a><\/li><li><a href=\"#java-implementation\">Java Implementation<\/a><\/li><li><a href=\"#python-implementation\">Python Implementation<\/a><\/li><\/ul><li><a href=\"#practice-question\">Practice Question<\/a><\/li><li><a href=\"#frequently-asked-questions\">Frequently Asked Questions<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#q1-what-is-the-difference-between-kruskals-and-prims-algorithms\">Q.1: What is the difference between Kruskal&#8217;s and Prim&#8217;s algorithms?<\/a><\/li><li><a href=\"#q2-how-efficient-is-the-kruskal-algorithm\">Q.2: How efficient is the Kruskal algorithm?<\/a><\/li><li><a href=\"#q3-is-kruskal-better-than-prim\">Q.3: Is Kruskal better than prim?<\/a><\/li><\/ul><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>In Kruskal Algorithm, initially, all the nodes of the graph are separated from each other, which means they don\u2019t have an edge between them. Then to obtain the minimum spanning tree from that graph we first sort the edges of the graph in a non-decreasing fashion. Then we pick the edges from left to right and connect the graph. <\/p>\n\n\n\n<p>Now there are two possibilities, first one is if the current picked edge is already connected in the tree so in this case, we will just continue our process and if the current picked edge is not connected then we will just connect those two nodes with dsu (disjoint set union). In this manner, we finally conclude with the final MST of the given graph.<\/p>\n\n\n\n<ul><li><strong>Example<\/strong>:<\/li><li><strong>Input:<\/strong><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Example\"  class=\"wp-image-2293 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-4-1.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-4-1.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-4-1-300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-4-1-380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-4-1-550x297.jpg 550w\" ><\/figure><\/div>\n\n\n\n<ul><li><strong>Output:<\/strong> 10<\/li><\/ul>\n\n\n\n<p><em><strong>Explanation:<\/strong> MST Weight = AB + BC + BE + BC+ EF = 10<\/em><\/p>\n\n\n\n<ul><li><strong>Input:<\/strong><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img  loading=\"lazy\"  width=\"824\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Input\"  class=\"wp-image-2294 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 824px) 100vw, 824px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5.jpg 824w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-300x149.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-768x380.jpg 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-380x188.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-550x272.jpg 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-800x396.jpg 800w\" ><\/figure><\/div>\n\n\n\n<ul><li><strong>Output: 14<\/strong><\/li><\/ul>\n\n\n\n<p><em><strong>Explanation: <\/strong>Here the second image is the MST image of the left image.<\/em><\/p>\n\n\n\n<h2 id=\"approach\">Approach<\/h2>\n\n\n\n<ul><li>Sort all the edges of the graph with respect to their weights. Initially sort the graph edges with respect to weights in a non-decreasing manner.<\/li><li>Now traverse left to right means from smallest weight to largest weight and start adding edges to the MST.<\/li><li>Add all those edges which don&#8217;t form a cycle, edges that connect only disconnected components.<\/li><\/ul>\n\n\n\n<p>So now one more question arises here, how will we check if the 2 vertices are connected or not?<\/p>\n\n\n\n<p>To check if 2 vertices are connected or not we can use the dfs approach. In this, we first start our dfs from any 1st vertex and then check whether the 2nd vertex is visited or not. To run it efficiently we can do this by DSU (Disjoint Set Union).<\/p>\n\n\n\n<h3 id=\"dry-run\">Dry Run<\/h3>\n\n\n\n<h4 id=\"step-1\">Step 1:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run\"  class=\"wp-image-2296 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-7-.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-7-.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-7--300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-7--380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-7--550x297.jpg 550w\" ><figcaption>Dry Run<\/figcaption><\/figure>\n\n\n\n<h4 id=\"step-2\">Step 2:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run 2\"  class=\"wp-image-2291 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-3.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-3.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-3-300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-3-380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-3-550x297.jpg 550w\" ><\/figure>\n\n\n\n<h4 id=\"step-3\">Step 3:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run 3\"  class=\"wp-image-2290 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-3.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-3.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-3-300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-3-380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-3-550x297.jpg 550w\" ><\/figure>\n\n\n\n<h4 id=\"step-4\">Step 4:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run 4\"  class=\"wp-image-2295 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-550x297.jpg 550w\" ><\/figure>\n\n\n\n<h4 id=\"step-5\">Step 5:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"755\"  height=\"408\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run 5\"  class=\"wp-image-2292 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 755px) 100vw, 755px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-3.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-3.jpg 755w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-3-300x162.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-3-380x205.jpg 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-3-550x297.jpg 550w\" ><\/figure>\n\n\n\n<h3 id=\"cc-implementation\"><span id=\"c-c-implementation\">C\/C++ Implementation<\/span><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const int MAX = 1e4 + 5;\nint id[MAX], nodes, edges;\npair &lt;long long, pair&lt;int, int> > p[MAX];\n\nvoid initialize()\n{\n    for(int i = 0;i &lt; MAX;++i)\n        id[i] = i;\n}\n\nint root(int x)\n{\n    while(id[x] != x)\n    {\n        id[x] = id[id[x]];\n        x = id[x];\n    }\n    return x;\n}\n\nvoid union1(int x, int y)\n{\n    int p = root(x);\n    int q = root(y);\n    id[p] = id[q];\n}\n\nlong long kruskal(pair&lt;long long, pair&lt;int, int> > p[])\n{\n    int x, y;\n    long long cost, minimumCost = 0;\n    for(int i = 0;i &lt; edges;++i)\n    {\n        \/\/ Selecting edges one by one in increasing order from the beginning\n        x = p[i].second.first;\n        y = p[i].second.second;\n        cost = p[i].first;\n        \/\/ Check if the selected edge is creating a cycle or not\n        if(root(x) != root(y))\n        {\n            minimumCost += cost;\n            union1(x, y);\n        }    \n    }\n    return minimumCost;\n}<\/pre>\n\n\n\n<h3 id=\"java-implementation\">Java Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class UnionFind{\n    final int[] parents;\n    int count;\n    \n    public UnionFind(int n){\n        this.parents = new int[n];\n        reset();\n    }\n    \n    public void reset(){\n        for(int i =0;i&lt;parents.length;i++){\n            parents[i] = i;\n        }\n        count = parents.length;\n    }\n    \n    public int find(int i){\n        int parent = parents[i];\n        if(parent == i){\n            return i;\n        }else{\n            int root = find(parent);\n            parents[i] = root;\n            return root;\n        }\n    }\n    \n    public boolean union(int i, int j){\n        int r1 = find(i);\n        int r2 = find(j);\n        if(r1 != r2){\n            count--;\n            parents[r1] = r2;\n            return true;\n        }else{\n            return false;\n        }\n    }\n    \n}\n\nclass Solution {\n    public List&lt;List&lt;Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {\n       \n        List&lt;Integer>criticals = new ArrayList&lt;>();\n        List&lt;Integer> pseduos = new ArrayList&lt;>();\n        \n        Map&lt;int[], Integer> map = new HashMap&lt;>();\n        for(int i =0;i&lt;edges.length;i++){\n            map.put(edges[i], i);\n        }\n        \n        Arrays.sort(edges, (e1, e2)->Integer.compare(e1[2], e2[2]));\n        int minCost = buildMST(n, edges, null, null);\n        \n        for(int i =0;i&lt;edges.length;i++){\n            int[] edge = edges[i];\n            int index = map.get(edge);\n            int costWithout = buildMST(n, edges, null, edge);\n            if(costWithout > minCost){\n                criticals.add(index);\n            }else{\n                int costWith = buildMST(n, edges, edge, null);\n                if(costWith == minCost){\n                    pseduos.add(index);\n                }\n            }\n            \n        }\n        \n        return Arrays.asList(criticals, pseduos);\n    }\n    \n    private int buildMST(int n, int[][] edges, int[] pick, int[] skip){\n        UnionFind uf = new UnionFind(n);\n        int cost = 0;\n        if(pick != null){\n            uf.union(pick[0], pick[1]);\n            cost += pick[2];\n        }\n        \n        for(int[] edge : edges){\n            if(edge != skip &amp;&amp; uf.union(edge[0], edge[1])){\n                cost += edge[2];\n            }\n        }\n        return uf.count == 1? cost : Integer.MAX_VALUE;\n    }\n}<\/pre>\n\n\n\n<h3 id=\"python-implementation\">Python Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n    \n        def dfs(curr, level, parent):\n            levels[curr] = level\n            for child, i in graph[curr]:\n                if child == parent:\n                    continue\n                elif levels[child] == -1:\n                    levels[curr] = min(levels[curr], dfs(child, level + 1, curr))\n                else:\n                    levels[curr] = min(levels[curr], levels[child])\n                if levels[child] >= level + 1 and i not in p_cri:\n                    cri.add(i)\n            return levels[curr]\n        \n        cri, p_cri = set(), set()\n        \n        dic = collections.defaultdict(list)\n        for i, (u, v, w) in enumerate(edges):\n            dic[w].append([u, v, i])\n        \n        union_set = UnionFindSet(n)\n        \n        for w in sorted(dic):\n            seen = collections.defaultdict(set)\n            for u, v, i in dic[w]:\n                pu, pv = union_set.find(u), union_set.find(v)\n                if pu == pv:\n                    continue\n                seen[min(pu, pv), max(pu, pv)].add(i)            \n            w_edges, graph = [], collections.defaultdict(list)\n            for pu, pv in seen:\n                if len(seen[pu, pv]) > 1:\n                    p_cri |= seen[pu, pv]\n                \n                edge_idx = seen[pu, pv].pop()\n                graph[pu].append((pv, edge_idx))\n                graph[pv].append((pu, edge_idx))\n                w_edges.append((pu, pv, edge_idx))\n               \n                union_set.union(pu, pv)\n            \n           \n            levels = [-1] * n\n            for u, v, i in w_edges:\n                if levels[u] == -1:\n                    dfs(u, 0, -1)\n          \n            for u, v, i in w_edges:\n                if i not in cri:\n                    p_cri.add(i)\n        \n        return [cri, p_cri]<\/pre>\n\n\n\n<ul><li><strong>Time complexity:<\/strong> O(ELogV), Where E is the no. of edges and V is no. of vertices.<\/li><li><strong>Space complexity:<\/strong> O(V) where V is no. of vertices.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"practice-question\">Practice Question<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.interviewbit.com\/problems\/commutable-islands\/\" target=\"_blank\" rel=\"noreferrer noopener\">Commutable Islands<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 id=\"q1-what-is-the-difference-between-kruskals-and-prims-algorithms\"><span id=\"q-1-what-is-the-difference-between-kruskals-and-prims-algorithms\">Q.1: What is the difference between Kruskal&#8217;s and Prim&#8217;s algorithms?<\/span><\/h3>\n\n\n\n<ul><li><strong>Prims Algo: <\/strong>The algorithm obtains the minimum spanning tree by choosing the adjacent vertices from a set of selected vertices<\/li><li><strong>Kruskal Algo: <\/strong>To obtain the minimum spanning tree this algorithm selects the edges from a set of edges.<\/li><\/ul>\n\n\n\n<h3 id=\"q2-how-efficient-is-the-kruskal-algorithm\"><span id=\"q-2-how-efficient-is-the-kruskal-algorithm\">Q.2: How efficient is the Kruskal algorithm?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> Kruskal performs better in typical situations (sparse graphs) because it uses simpler data structures and its time complexity is O(ELogV), Where E is the no. of edges and V is no. of vertices.<\/p>\n\n\n\n<h3 id=\"q3-is-kruskal-better-than-prim\"><span id=\"q-3-is-kruskal-better-than-prim\">Q.3: Is Kruskal better than prim?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> Prim&#8217;s algorithm is significantly faster in the limit when you&#8217;ve got a really dense graph with many more edges than vertices. Kruskal performs better in typical situations (sparse graphs) because it uses simpler data structures.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem Statement In Kruskal Algorithm, initially, all the nodes of the graph are separated from each other, which&hellip;\n","protected":false},"author":5,"featured_media":2289,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[397,396],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2286"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=2286"}],"version-history":[{"count":8,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions"}],"predecessor-version":[{"id":20241,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions\/20241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/2289"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=2286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=2286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=2286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}