{"id":616,"date":"2024-10-10T10:52:35","date_gmt":"2024-10-10T05:22:35","guid":{"rendered":"https:\/\/codexplained.in\/?p=616"},"modified":"2025-11-24T16:00:24","modified_gmt":"2025-11-24T10:30:24","slug":"implement-kruskals-algorithm","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=616","title":{"rendered":"Implementation of Kruskal\u2019s Algorithm"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Understanding Kruskal\u2019s Algorithm<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sort All Edges<\/strong>: Begin by sorting all the edges in non-decreasing order of their weights.<\/li>\n\n\n\n<li><strong>Initialize a Forest<\/strong>: Start with an empty forest, which will grow into the minimum spanning tree.<\/li>\n\n\n\n<li><strong>Add Edges<\/strong>: For each edge in the sorted list, add it to the forest if it doesn\u2019t create a cycle. This is typically checked using a Union-Find data structure.<\/li>\n\n\n\n<li><strong>Repeat Until MST is Complete<\/strong>: Continue adding edges until there are V\u22121V &#8211; 1V\u22121 edges in the tree (where VVV is the number of vertices).<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define MAX 100\n\n\/\/ Structure to represent an edge\nstruct Edge {\n    int src, dest, weight;\n};\n\n\/\/ Structure to represent a subset for union-find\nstruct Subset {\n    int parent;\n    int rank;\n};\n\n\/\/ Function to compare two edges (for qsort)\nint compare(const void* a, const void* b) {\n    struct Edge* edge1 = (struct Edge*)a;\n    struct Edge* edge2 = (struct Edge*)b;\n    return edge1-&gt;weight - edge2-&gt;weight;\n}\n\n\/\/ Find function for union-find\nint find(struct Subset subsets&#x5B;], int i) {\n    if (subsets&#x5B;i].parent != i) {\n        subsets&#x5B;i].parent = find(subsets, subsets&#x5B;i].parent);\n    }\n    return subsets&#x5B;i].parent;\n}\n\n\/\/ Union function for union-find\nvoid unionSubsets(struct Subset subsets&#x5B;], int x, int y) {\n    int xroot = find(subsets, x);\n    int yroot = find(subsets, y);\n\n    if (subsets&#x5B;xroot].rank &lt; subsets&#x5B;yroot].rank) {\n        subsets&#x5B;xroot].parent = yroot;\n    } else if (subsets&#x5B;xroot].rank &gt; subsets&#x5B;yroot].rank) {\n        subsets&#x5B;yroot].parent = xroot;\n    } else {\n        subsets&#x5B;yroot].parent = xroot;\n        subsets&#x5B;xroot].rank++;\n    }\n}\n\n\/\/ Function to implement Kruskal&#039;s algorithm\nvoid kruskal(struct Edge edges&#x5B;], int V, int E) {\n    struct Edge result&#x5B;MAX]; \/\/ To store the resulting MST\n    struct Subset subsets&#x5B;MAX];\n\n    \/\/ Step 1: Sort all edges\n    qsort(edges, E, sizeof(edges&#x5B;0]), compare);\n\n    \/\/ Create V subsets with single elements\n    for (int v = 0; v &lt; V; ++v) {\n        subsets&#x5B;v].parent = v;\n        subsets&#x5B;v].rank = 0;\n    }\n\n    int e = 0; \/\/ Index variable for result\n    int i = 0; \/\/ Index variable for sorted edges\n    while (e &lt; V - 1 &amp;&amp; i &lt; E) {\n        \/\/ Step 2: Pick the smallest edge\n        struct Edge nextEdge = edges&#x5B;i++];\n\n        \/\/ Find the subsets of the vertices of the edge\n        int x = find(subsets, nextEdge.src);\n        int y = find(subsets, nextEdge.dest);\n\n        \/\/ If they are in different subsets, include this edge in the result\n        if (x != y) {\n            result&#x5B;e++] = nextEdge;\n            unionSubsets(subsets, x, y);\n        }\n    }\n\n    \/\/ Print the resulting MST\n    printf(&quot;Edges in the Minimum Spanning Tree (Kruskal&#039;s Algorithm):\\n&quot;);\n    for (i = 0; i &lt; e; ++i) {\n        printf(&quot;%d -- %d == %d\\n&quot;, result&#x5B;i].src, result&#x5B;i].dest, result&#x5B;i].weight);\n    }\n}\n\n\/\/ Main function\nint main() {\n    int V, E;\n\n    printf(&quot;Enter number of vertices: &quot;);\n    scanf(&quot;%d&quot;, &amp;V);\n    printf(&quot;Enter number of edges: &quot;);\n    scanf(&quot;%d&quot;, &amp;E);\n\n    struct Edge edges&#x5B;E];\n\n    printf(&quot;Enter edges (src dest weight): \\n&quot;);\n    for (int i = 0; i &lt; E; i++) {\n        scanf(&quot;%d %d %d&quot;, &amp;edges&#x5B;i].src, &amp;edges&#x5B;i].dest, &amp;edges&#x5B;i].weight);\n    }\n\n    kruskal(edges, V, E);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of Kruskal&#8217;s Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Edge Structure<\/strong>: Each edge has a source, destination, and weight.<\/li>\n\n\n\n<li><strong>Union-Find<\/strong>: We use a <code>Subset<\/code> structure to manage disjoint sets. The <code>find<\/code> function retrieves the set of an element, while the <code>unionSubsets<\/code> function merges two sets.<\/li>\n\n\n\n<li><strong>Sorting<\/strong>: We sort the edges by weight using <code>qsort<\/code>.<\/li>\n\n\n\n<li><strong>Building the MST<\/strong>: We iterate through the sorted edges and add them to the MST if they connect disjoint sets.<\/li>\n\n\n\n<li><strong>Output<\/strong>: Finally, we print the edges included in the MST.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Input and Output for Kruskal\u2019s Algorithm<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Enter number of vertices: 4\nEnter number of edges: 5\nEnter edges (src dest weight):\n0 1 10\n0 2 6\n0 3 5\n1 3 15\n2 3 4\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Edges in the Minimum Spanning Tree (Kruskal's Algorithm):\n2 -- 3 == 4\n0 -- 3 == 5\n0 -- 1 == 10\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Explanation of Kruskal&#8217;s Code Sample Input and Output for Kruskal\u2019s Algorithm Input: mathematicaCopy codeEnter number of vertices: 4 Enter number of edges: 5 Enter edges (src dest weight): 0 1 10 [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":621,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Vraj Bhuva\"\/>\n\t<meta name=\"google-site-verification\" content=\"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/codexplained.in\/?p=616\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Code Explained -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Implementation of Kruskal\u2019s Algorithm - Code Explained\" \/>\n\t\t<meta property=\"og:description\" content=\"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/codexplained.in\/?p=616\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2024-10-10T05:22:35+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-11-24T10:30:24+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Implementation of Kruskal\u2019s Algorithm - Code Explained\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#blogposting\",\"name\":\"Implementation of Kruskal\\u2019s Algorithm - Code Explained\",\"headline\":\"Implementation of Kruskal\\u2019s Algorithm\",\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codexplained.in\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/pikaso_text-to-image_Candid-image-photography-natural-textures-highly-r.jpeg\",\"width\":896,\"height\":1152},\"datePublished\":\"2024-10-10T10:52:35+05:30\",\"dateModified\":\"2025-11-24T16:00:24+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#webpage\"},\"articleSection\":\"C\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codexplained.in\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"position\":2,\"name\":\"C\",\"item\":\"https:\\\/\\\/codexplained.in\\\/?cat=75\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#listItem\",\"name\":\"Implementation of Kruskal\\u2019s Algorithm\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#listItem\",\"position\":3,\"name\":\"Implementation of Kruskal\\u2019s Algorithm\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\",\"name\":\"Bhagchandani Niraj\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Bhagchandani Niraj\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?author=39\",\"name\":\"Vraj Bhuva\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5119d0ea156792d338bcbbf255689484c75bc024047277dcf2e25ec9083b0128?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Vraj Bhuva\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#webpage\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?p=616\",\"name\":\"Implementation of Kruskal\\u2019s Algorithm - Code Explained\",\"description\":\"Understanding Kruskal\\u2019s Algorithm Kruskal\\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codexplained.in\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/pikaso_text-to-image_Candid-image-photography-natural-textures-highly-r.jpeg\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616\\\/#mainImage\",\"width\":896,\"height\":1152},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=616#mainImage\"},\"datePublished\":\"2024-10-10T10:52:35+05:30\",\"dateModified\":\"2025-11-24T16:00:24+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\",\"url\":\"https:\\\/\\\/codexplained.in\\\/\",\"name\":\"Code Explained\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Implementation of Kruskal\u2019s Algorithm - Code Explained","description":"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.","canonical_url":"https:\/\/codexplained.in\/?p=616","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/codexplained.in\/?p=616#blogposting","name":"Implementation of Kruskal\u2019s Algorithm - Code Explained","headline":"Implementation of Kruskal\u2019s Algorithm","author":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"publisher":{"@id":"https:\/\/codexplained.in\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/codexplained.in\/wp-content\/uploads\/2024\/10\/pikaso_text-to-image_Candid-image-photography-natural-textures-highly-r.jpeg","width":896,"height":1152},"datePublished":"2024-10-10T10:52:35+05:30","dateModified":"2025-11-24T16:00:24+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/codexplained.in\/?p=616#webpage"},"isPartOf":{"@id":"https:\/\/codexplained.in\/?p=616#webpage"},"articleSection":"C"},{"@type":"BreadcrumbList","@id":"https:\/\/codexplained.in\/?p=616#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","position":1,"name":"Home","item":"https:\/\/codexplained.in","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","position":2,"name":"C","item":"https:\/\/codexplained.in\/?cat=75","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=616#listItem","name":"Implementation of Kruskal\u2019s Algorithm"},"previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=616#listItem","position":3,"name":"Implementation of Kruskal\u2019s Algorithm","previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}}]},{"@type":"Person","@id":"https:\/\/codexplained.in\/#person","name":"Bhagchandani Niraj","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=616#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g","width":96,"height":96,"caption":"Bhagchandani Niraj"}},{"@type":"Person","@id":"https:\/\/codexplained.in\/?author=39#author","url":"https:\/\/codexplained.in\/?author=39","name":"Vraj Bhuva","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=616#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/5119d0ea156792d338bcbbf255689484c75bc024047277dcf2e25ec9083b0128?s=96&d=mm&r=g","width":96,"height":96,"caption":"Vraj Bhuva"}},{"@type":"WebPage","@id":"https:\/\/codexplained.in\/?p=616#webpage","url":"https:\/\/codexplained.in\/?p=616","name":"Implementation of Kruskal\u2019s Algorithm - Code Explained","description":"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/codexplained.in\/#website"},"breadcrumb":{"@id":"https:\/\/codexplained.in\/?p=616#breadcrumblist"},"author":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"creator":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"image":{"@type":"ImageObject","url":"https:\/\/codexplained.in\/wp-content\/uploads\/2024\/10\/pikaso_text-to-image_Candid-image-photography-natural-textures-highly-r.jpeg","@id":"https:\/\/codexplained.in\/?p=616\/#mainImage","width":896,"height":1152},"primaryImageOfPage":{"@id":"https:\/\/codexplained.in\/?p=616#mainImage"},"datePublished":"2024-10-10T10:52:35+05:30","dateModified":"2025-11-24T16:00:24+05:30"},{"@type":"WebSite","@id":"https:\/\/codexplained.in\/#website","url":"https:\/\/codexplained.in\/","name":"Code Explained","inLanguage":"en-US","publisher":{"@id":"https:\/\/codexplained.in\/#person"}}]},"og:locale":"en_US","og:site_name":"Code Explained -","og:type":"article","og:title":"Implementation of Kruskal\u2019s Algorithm - Code Explained","og:description":"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree.","og:url":"https:\/\/codexplained.in\/?p=616","article:published_time":"2024-10-10T05:22:35+00:00","article:modified_time":"2025-11-24T10:30:24+00:00","twitter:card":"summary_large_image","twitter:title":"Implementation of Kruskal\u2019s Algorithm - Code Explained","twitter:description":"Understanding Kruskal\u2019s Algorithm Kruskal\u2019s algorithm is a greedy algorithm that finds the minimum spanning tree for a connected, weighted graph. The steps involved are: Sort All Edges: Begin by sorting all the edges in non-decreasing order of their weights. Initialize a Forest: Start with an empty forest, which will grow into the minimum spanning tree."},"aioseo_meta_data":{"post_id":"616","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-10-11 02:52:01","updated":"2025-11-24 10:31:30","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\/?cat=75\" title=\"C\">C<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tImplementation of Kruskal\u2019s Algorithm\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/codexplained.in"},{"label":"C","link":"https:\/\/codexplained.in\/?cat=75"},{"label":"Implementation of Kruskal\u2019s Algorithm","link":"https:\/\/codexplained.in\/?p=616"}],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=616"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":1474,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/616\/revisions\/1474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/621"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}