{"id":34324,"date":"2024-11-01T09:26:50","date_gmt":"2024-11-01T09:26:50","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34324"},"modified":"2024-11-01T10:57:42","modified_gmt":"2024-11-01T10:57:42","slug":"c-coding-test-course-finding-minimum-spanning-tree-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34324\/","title":{"rendered":"C++ Coding Test Course, Finding Minimum Spanning Tree"},"content":{"rendered":"<p>In this course, we will learn about the Minimum Spanning Tree (MST). A minimum spanning tree is a tree that includes all vertices in a weighted graph while ensuring that the total weight is minimized. Such problems can be applied in various fields and play an important role in network design, road construction, and communication network establishment.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given a weighted undirected graph as follows, find the minimum spanning tree.<\/p>\n<pre><code>Input:\nThe first line contains the number of vertices <em>V<\/em> and the number of edges <em>E<\/em>. (1 \u2264 <em>V<\/em> \u2264 1000, 1 \u2264 <em>E<\/em> \u2264 10000)\nThe next <em>E<\/em> lines each contain the edge information <em>u, v, w<\/em>. This means that the weight <em>w<\/em> connects vertex <em>u<\/em> and vertex <em>v<\/em>.\n\nOutput:\nPrint the total weight of the minimum spanning tree.<\/code><\/pre>\n<h2>Example Input<\/h2>\n<pre><code>3 3\n1 2 1\n2 3 2\n1 3 3\n<\/code><\/pre>\n<h2>Example Output<\/h2>\n<pre><code>3\n<\/code><\/pre>\n<h2>Solution Method<\/h2>\n<p>To solve this problem, we can use Prim&#8217;s algorithm or Kruskal&#8217;s algorithm. Here, we will use Kruskal&#8217;s algorithm for the solution.<\/p>\n<h3>Kruskal&#8217;s Algorithm Explanation<\/h3>\n<p>Kruskal&#8217;s algorithm is an edge-based algorithm that proceeds in the following steps:<\/p>\n<ol>\n<li>Sort all edges in ascending order of weight.<\/li>\n<li>Initialize the vertex set. (Using Union-Find data structure)<\/li>\n<li>Select edges one by one, and if the two vertices are not yet connected, choose the edge. Repeat this process to create a minimum spanning tree that connects all vertices.<\/li>\n<\/ol>\n<h3>Code Implementation<\/h3>\n<p>Now, let&#8217;s implement Kruskal&#8217;s algorithm in C++.<\/p>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\nstruct Edge {\n    int u, v, weight;\n};\n\nbool compare(Edge a, Edge b) {\n    return a.weight &lt; b.weight;\n}\n\nint find(int parent[], int x) {\n    if (parent[x] != x) {\n        parent[x] = find(parent, parent[x]);\n    }\n    return parent[x];\n}\n\nvoid unionSet(int parent[], int rank[], int x, int y) {\n    int rootX = find(parent, x);\n    int rootY = find(parent, y);\n\n    if (rootX != rootY) {\n        if (rank[rootX] &lt; rank[rootY]) {\n            parent[rootX] = rootY;\n        } else if (rank[rootX] &gt; rank[rootY]) {\n            parent[rootY] = rootX;\n        } else {\n            parent[rootY] = rootX;\n            rank[rootX]++;\n        }\n    }\n}\n\nint kruskal(int V, vector<edge>&amp; edges) {\n    sort(edges.begin(), edges.end(), compare);\n    int parent[V + 1];\n    int rank[V + 1];\n    for (int i = 0; i &lt;= V; i++) {\n        parent[i] = i;\n        rank[i] = 0;\n    }\n\n    int totalWeight = 0;\n    for (Edge edge : edges) {\n        if (find(parent, edge.u) != find(parent, edge.v)) {\n            totalWeight += edge.weight;\n            unionSet(parent, rank, edge.u, edge.v);\n        }\n    }\n    return totalWeight;\n}\n\nint main() {\n    int V, E;\n    cin &gt;&gt; V &gt;&gt; E;\n    vector<edge> edges(E);\n    for (int i = 0; i &lt; E; i++) {\n        cin &gt;&gt; edges[i].u &gt;&gt; edges[i].v &gt;&gt; edges[i].weight;\n    }\n    cout &lt;&lt; kruskal(V, edges) &lt;&lt; endl;\n    return 0;\n}\n<\/edge><\/edge><\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>First, we define the Edge structure and sort based on weights using a comparison function.<\/p>\n<p><b>find<\/b> function uses path compression to efficiently find the parent node. <b>unionSet<\/b> function merges the sets of two vertices while using rank to prevent the tree from becoming too large.<\/p>\n<p>The main function receives input and calculates the total weight of the minimum spanning tree using Kruskal&#8217;s algorithm and then outputs it.<\/p>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of Kruskal&#8217;s algorithm is O(E log E). This is the time required for edge sorting. When optimizing Union-Find operations, it operates as a very efficient algorithm.<\/p>\n<h2>Conclusion<\/h2>\n<p>The minimum spanning tree is used in many real-world problems, so systematically understanding it is important. Through this example, we have learned how to apply Kruskal&#8217;s algorithm to find MST and have acquired foundational theories applicable to various modified problems. Additionally, we recommend practicing Prim&#8217;s algorithm as well.<\/p>\n<p>The next lecture will cover other algorithms related to the minimum spanning tree or various problem-solving, so stay tuned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this course, we will learn about the Minimum Spanning Tree (MST). A minimum spanning tree is a tree that includes all vertices in a weighted graph while ensuring that the total weight is minimized. Such problems can be applied in various fields and play an important role in network design, road construction, and communication &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34324\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding Minimum Spanning Tree&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34324","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34324\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this course, we will learn about the Minimum Spanning Tree (MST). A minimum spanning tree is a tree that includes all vertices in a weighted graph while ensuring that the total weight is minimized. Such problems can be applied in various fields and play an important role in network design, road construction, and communication &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding Minimum Spanning Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34324\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:42+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding Minimum Spanning Tree\",\"datePublished\":\"2024-11-01T09:26:50+00:00\",\"dateModified\":\"2024-11-01T10:57:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/\"},\"wordCount\":356,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34324\/\",\"name\":\"C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:50+00:00\",\"dateModified\":\"2024-11-01T10:57:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34324\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34324\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding Minimum Spanning Tree\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34324\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this course, we will learn about the Minimum Spanning Tree (MST). A minimum spanning tree is a tree that includes all vertices in a weighted graph while ensuring that the total weight is minimized. Such problems can be applied in various fields and play an important role in network design, road construction, and communication &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding Minimum Spanning Tree\"","og_url":"https:\/\/atmokpo.com\/w\/34324\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:50+00:00","article_modified_time":"2024-11-01T10:57:42+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34324\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34324\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding Minimum Spanning Tree","datePublished":"2024-11-01T09:26:50+00:00","dateModified":"2024-11-01T10:57:42+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34324\/"},"wordCount":356,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34324\/","url":"https:\/\/atmokpo.com\/w\/34324\/","name":"C++ Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:50+00:00","dateModified":"2024-11-01T10:57:42+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34324\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34324\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34324\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding Minimum Spanning Tree"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34324"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34324\/revisions"}],"predecessor-version":[{"id":34325,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34324\/revisions\/34325"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}