{"id":35140,"date":"2024-11-01T09:36:00","date_gmt":"2024-11-01T09:36:00","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35140"},"modified":"2024-11-01T12:40:34","modified_gmt":"2024-11-01T12:40:34","slug":"%ec%bd%94%ed%8b%80%eb%a6%b0-%ec%bd%94%eb%94%a9%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-%ec%b5%9c%ec%86%8c-%ec%8b%a0%ec%9e%a5-%ed%8a%b8%eb%a6%ac-%ea%b5%ac%ed%95%98%ea%b8%b0-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35140\/","title":{"rendered":"Kotlin coding test course, finding the minimum spanning tree"},"content":{"rendered":"<article>\n<header>\n<p>Written on: October 21, 2023<\/p>\n<\/header>\n<section>\n<h2>Introduction<\/h2>\n<p>\n            The Minimum Spanning Tree (MST) problem, which frequently appears in programming tests, is one of the important concepts in graph theory. A minimum spanning tree refers to a tree structure that includes all vertices of a graph while minimizing the sum of weights.<br \/>\n            In this article, we will explore an algorithm for finding the minimum spanning tree using the Kotlin language and learn how to apply the theory through related problem-solving examples.\n        <\/p>\n<\/section>\n<section>\n<h2>Basic Concepts<\/h2>\n<h3>Graph<\/h3>\n<p>\n            A graph is a data structure composed of vertices and edges. Vertices represent nodes, and edges represent the connections between those nodes.<br \/>\n            Graphs can be classified into directed and undirected graphs, and if edges have weights, they are called weighted graphs.\n        <\/p>\n<h3>Minimum Spanning Tree (MST)<\/h3>\n<p>\n            A minimum spanning tree is a tree with the minimum sum of weights among the subgraphs that connect all vertices in a weighted graph.<br \/>\n            Prim&#8217;s algorithm and Kruskal&#8217;s algorithm are commonly used algorithms for finding the MST.\n        <\/p>\n<\/section>\n<section>\n<h2>Problem Description<\/h2>\n<h3>Problem Definition<\/h3>\n<p>\n            You are given an undirected graph and the weights of its edges. Write a program to find the minimum spanning tree that includes all vertices of this graph and outputs the sum of its weights.\n        <\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains the number of vertices <code>V<\/code> and the number of edges <code>E<\/code>. (<code>1 \u2264 V \u2264 1000<\/code>, <code>1 \u2264 E \u2264 10000<\/code>)<\/li>\n<li>The next <code>E<\/code> lines contain the two vertices of each edge and the weight.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>\n            Output the sum of the weights of the minimum spanning tree.\n        <\/p>\n<\/section>\n<section>\n<h2>Problem Solving Process<\/h2>\n<h3>Approach Using Kruskal&#8217;s Algorithm<\/h3>\n<p>\n            Kruskal&#8217;s algorithm involves sorting the edges based on their weights and then selecting edges that do not form cycles to construct the minimum spanning tree.<br \/>\n            This algorithm sorts the edges of the given graph and uses a union-find data structure to check for cycles.<br \/>\n            The process proceeds through the following steps.\n        <\/p>\n<ol>\n<li><strong>Sort edges:<\/strong> Sort all edges in ascending order based on their weights.<\/li>\n<li><strong>Initialize union-find:<\/strong> Initialize each vertex as an independent set.<\/li>\n<li><strong>Construct the minimum spanning tree:<\/strong> Traverse the sorted edges and add the edge to the MST if it does not form a cycle.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Kotlin Implementation<\/h2>\n<h3>Code Example<\/h3>\n<pre><code>class Edge(val u: Int, val v: Int, val weight: Int): Comparable<Edge> {\n    override fun compareTo(other: Edge) = this.weight - other.weight\n}\n\nclass DisjointSet(val size: Int) {\n    private val parent = IntArray(size) { it }\n    private val rank = IntArray(size) { 0 }\n\n    fun find(x: Int): Int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x])\n        }\n        return parent[x]\n    }\n\n    fun union(x: Int, y: Int): Boolean {\n        val rootX = find(x)\n        val rootY = find(y)\n\n        if (rootX == rootY) return false\n\n        if (rank[rootX] > rank[rootY]) {\n            parent[rootY] = rootX\n        } else if (rank[rootX] < rank[rootY]) {\n            parent[rootX] = rootY\n        } else {\n            parent[rootY] = rootX\n            rank[rootX]++\n        }\n        return true\n    }\n}\n\nfun kruskal(vertices: Int, edges: List<Edge>): Int {\n    val disjointSet = DisjointSet(vertices)\n    val sortedEdges = edges.sorted()\n    \n    var totalWeight = 0\n    \n    for (edge in sortedEdges) {\n        if (disjointSet.union(edge.u, edge.v)) {\n            totalWeight += edge.weight\n        }\n    }\n\n    return totalWeight\n}\n\nfun main() {\n    val reader = System.`in`.bufferedReader()\n    val (V, E) = reader.readLine().split(\" \").map { it.toInt() }\n    val edges = mutableListOf<Edge>()\n\n    for (i in 0 until E) {\n        val (u, v, weight) = reader.readLine().split(\" \").map { it.toInt() }\n        edges.add(Edge(u - 1, v - 1, weight))\n    }\n\n    val result = kruskal(V, edges)\n    println(result)\n}\n<\/code><\/pre>\n<\/section>\n<section>\n<h2>Code Explanation<\/h2>\n<p>\n            The above code finds the minimum spanning tree based on Kruskal&#8217;s algorithm.<br \/>\n            The <strong>Edge<\/strong> class stores edge information, and the <strong>DisjointSet<\/strong> class provides functionality to find the representative of sets and merge two sets based on the union-find algorithm.\n        <\/p>\n<p>\n<strong>kruskal<\/strong> function uses the input number of vertices and the edge list to sort all edges based on their weights.<br \/>\n            It then traverses the sorted edges, adding an edge to the MST only if no cycle occurs.<br \/>\n            Finally, it returns the total weight of the MST.\n        <\/p>\n<\/section>\n<section>\n<h2>Test Cases<\/h2>\n<h3>Example Input<\/h3>\n<pre><code>5 6\n1 2 3\n1 3 4\n2 3 1\n2 4 6\n3 4 5\n4 5 2<\/code><\/pre>\n<h3>Example Output<\/h3>\n<pre><code>10<\/code><\/pre>\n<h3>Explanation<\/h3>\n<p>\n            The given graph consists of 5 vertices and 6 edges, and the minimum spanning tree consists of edges with weights 1, 2, 3, and 4, totaling a weight of 10.\n        <\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n            In this article, we implemented an algorithm to find the minimum spanning tree using Kotlin and solved a problem to enhance our understanding of the process.<br \/>\n            The minimum spanning tree is an important concept that can be applied to various real-world problems, and there are other approaches like Prim&#8217;s algorithm in addition to Kruskal&#8217;s algorithm.<br \/>\n            Utilizing these algorithms enables efficient solutions to graph problems.<br \/>\n            We will continue to introduce various algorithms to help you achieve successful results in coding tests.\n        <\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Written on: October 21, 2023 Introduction The Minimum Spanning Tree (MST) problem, which frequently appears in programming tests, is one of the important concepts in graph theory. A minimum spanning tree refers to a tree structure that includes all vertices of a graph while minimizing the sum of weights. In this article, we will explore &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35140\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Kotlin coding test course, finding the 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":[106],"tags":[],"class_list":["post-35140","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Kotlin coding test course, finding the 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\/35140\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Written on: October 21, 2023 Introduction The Minimum Spanning Tree (MST) problem, which frequently appears in programming tests, is one of the important concepts in graph theory. A minimum spanning tree refers to a tree structure that includes all vertices of a graph while minimizing the sum of weights. In this article, we will explore &hellip; \ub354 \ubcf4\uae30 &quot;Kotlin coding test course, finding the minimum spanning tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35140\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:40:34+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Kotlin coding test course, finding the minimum spanning tree\",\"datePublished\":\"2024-11-01T09:36:00+00:00\",\"dateModified\":\"2024-11-01T12:40:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/\"},\"wordCount\":561,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35140\/\",\"name\":\"Kotlin coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:00+00:00\",\"dateModified\":\"2024-11-01T12:40:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35140\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35140\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kotlin coding test course, finding the 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":"Kotlin coding test course, finding the 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\/35140\/","og_locale":"ko_KR","og_type":"article","og_title":"Kotlin coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Written on: October 21, 2023 Introduction The Minimum Spanning Tree (MST) problem, which frequently appears in programming tests, is one of the important concepts in graph theory. A minimum spanning tree refers to a tree structure that includes all vertices of a graph while minimizing the sum of weights. In this article, we will explore &hellip; \ub354 \ubcf4\uae30 \"Kotlin coding test course, finding the minimum spanning tree\"","og_url":"https:\/\/atmokpo.com\/w\/35140\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:00+00:00","article_modified_time":"2024-11-01T12:40:34+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35140\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35140\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Kotlin coding test course, finding the minimum spanning tree","datePublished":"2024-11-01T09:36:00+00:00","dateModified":"2024-11-01T12:40:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35140\/"},"wordCount":561,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35140\/","url":"https:\/\/atmokpo.com\/w\/35140\/","name":"Kotlin coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:00+00:00","dateModified":"2024-11-01T12:40:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35140\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35140\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35140\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Kotlin coding test course, finding the 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\/35140","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=35140"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35140\/revisions"}],"predecessor-version":[{"id":38073,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35140\/revisions\/38073"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}