{"id":35138,"date":"2024-11-01T09:35:59","date_gmt":"2024-11-01T09:35:59","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35138"},"modified":"2024-11-01T11:44:51","modified_gmt":"2024-11-01T11:44:51","slug":"kotlin-coding-test-course-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35138\/","title":{"rendered":"kotlin coding test course, minimum spanning tree"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>1. What is a Minimum Spanning Tree?<\/h2>\n<p>A Minimum Spanning Tree (MST) is a subgraph in a weighted graph that includes all vertices while minimizing the sum of the weights. In other words, it means a tree where all vertices are connected and the sum of the weights of the given edges is the smallest. Minimum spanning trees are used in various fields such as network design, road construction, and telecommunications.<\/p>\n<\/section>\n<section>\n<h2>2. Problem Statement<\/h2>\n<p>The following is a problem to find the minimum spanning tree:<\/p>\n<blockquote>\n<h3>Problem Description<\/h3>\n<p>Given a graph with a specified number of vertices <code>V<\/code> and edges <code>E<\/code>, write a program in Kotlin to find the sum of weights of the minimum spanning tree. The edges are given with weights.<\/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>.<\/li>\n<li>The next <code>E<\/code> lines provide the information for each edge <code>u, v, w<\/code> (the weight <code>w<\/code> of the edge connecting vertices <code>u<\/code> and <code>v<\/code>).<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Output the sum of the weights of the minimum spanning tree.<\/p>\n<\/blockquote>\n<\/section>\n<section>\n<h2>3. Problem Solving Methodology<\/h2>\n<h3>3.1. Algorithm Selection<\/h3>\n<p>There are methods to find the minimum spanning tree, namely Prim&#8217;s algorithm and Kruskal&#8217;s algorithm. In this tutorial, we will use Prim&#8217;s algorithm to solve the problem. The Prim&#8217;s algorithm proceeds as follows:<\/p>\n<ol>\n<li>Start from an arbitrary vertex and select the edge with the smallest weight among the connected edges to include in the tree.<\/li>\n<li>Select the edge with the smallest weight from all edges connected to the vertices included in the tree.<\/li>\n<li>Repeat this process until all vertices are included in the tree.<\/li>\n<\/ol>\n<h3>3.2. Time Complexity<\/h3>\n<p>The time complexity of Prim&#8217;s algorithm is O(E log V), where E is the number of edges and V is the number of vertices. On the other hand, Kruskal&#8217;s algorithm is O(E log E) and can be more efficient in different scenarios.<\/p>\n<\/section>\n<section>\n<h2>4. Kotlin Code Implementation<\/h2>\n<p>Below is the Kotlin code to find the sum of the weights of the minimum spanning tree using Prim\u2019s algorithm:<\/p>\n<pre><code>\nfun findMinimumSpanningTree(V: Int, edges: List<Triple<Int, Int, Int>>): Int {\n    val graph = Array(V) { mutableListOf<Pair<Int, Int>>() }\n    for (edge in edges) {\n        graph[edge.first].add(Pair(edge.second, edge.third))\n        graph[edge.second].add(Pair(edge.first, edge.third))\n    }\n\n    val visited = BooleanArray(V)\n    val minHeap = PriorityQueue<Pair<Int, Int>>(compareBy { it.second })\n    minHeap.add(Pair(0, 0)) \/\/ (vertex, weight)\n    var totalWeight = 0\n\n    while (minHeap.isNotEmpty()) {\n        val (u, weight) = minHeap.poll()\n        if (visited[u]) continue\n        visited[u] = true\n        totalWeight += weight\n\n        for ((v, w) in graph[u]) {\n            if (!visited[v]) {\n                minHeap.add(Pair(v, w))\n            }\n        }\n    }\n\n    return totalWeight\n}\n            <\/Pair<Int,><\/Pair<Int,><\/Triple<Int,><\/code><\/pre>\n<h3>4.1. Main Function and Input Section<\/h3>\n<p>The main function and input handling part to use the above `findMinimumSpanningTree` function is as follows:<\/p>\n<pre><code>\nfun main() {\n    val (V, E) = readLine()!!.split(\" \").map { it.toInt() }\n    val edges = mutableListOf<Triple<Int, Int, Int>>()\n\n    repeat(E) {\n        val edge = readLine()!!.split(\" \").map { it.toInt() }\n        edges.add(Triple(edge[0], edge[1], edge[2]))\n    }\n\n    val result = findMinimumSpanningTree(V, edges)\n    println(result)\n}\n            <\/Triple<Int,><\/code><\/pre>\n<\/section>\n<section>\n<h2>5. Precautions During Problem Solving Process<\/h2>\n<p>There are several precautions to keep in mind when finding the minimum spanning tree:<\/p>\n<ul>\n<li>Since the vertices of the graph often start from index 0, be careful when entering the data.<\/li>\n<li>Make sure to initialize the visited array as all vertices need to be visited.<\/li>\n<li>Ensure the algorithm is designed to handle cases where all edges have the same weight.<\/li>\n<li>Finally, verify whether the sum of the weights being outputted is correct.<\/li>\n<li>Handle to ensure that edges are not duplicated.<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>6. Example Test Cases<\/h2>\n<p>Below are example cases to test the minimum spanning tree problem:<\/p>\n<blockquote>\n<h3>Example Input<\/h3>\n<pre><code>4 5\n0 1 1\n0 2 3\n1 2 1\n1 3 4\n2 3 1<\/code><\/pre>\n<h3>Example Output<\/h3>\n<pre><code>3<\/code><\/pre>\n<\/blockquote>\n<p>The example input consists of a graph with 4 vertices and 5 edges. The sum of the weights of the edges that form the minimum spanning tree should be 3.<\/p>\n<\/section>\n<section>\n<h2>7. Conclusion<\/h2>\n<p>In this lecture, we implemented Prim&#8217;s algorithm to find the minimum spanning tree using Kotlin. This algorithm is efficient and easy to implement, which can be applied to various problems. It is important to understand the structure of the graph and develop the ability to solve problems optimally during the algorithm problem-solving process. I hope to enhance your skills through various algorithm problems in the future.<\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is a Minimum Spanning Tree? A Minimum Spanning Tree (MST) is a subgraph in a weighted graph that includes all vertices while minimizing the sum of the weights. In other words, it means a tree where all vertices are connected and the sum of the weights of the given edges is the smallest. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35138\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, 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-35138","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, 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\/35138\/\" \/>\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, minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. What is a Minimum Spanning Tree? A Minimum Spanning Tree (MST) is a subgraph in a weighted graph that includes all vertices while minimizing the sum of the weights. In other words, it means a tree where all vertices are connected and the sum of the weights of the given edges is the smallest. &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, minimum spanning tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35138\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:35:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:51+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, minimum spanning tree\",\"datePublished\":\"2024-11-01T09:35:59+00:00\",\"dateModified\":\"2024-11-01T11:44:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/\"},\"wordCount\":553,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35138\/\",\"name\":\"kotlin coding test course, minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:35:59+00:00\",\"dateModified\":\"2024-11-01T11:44:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35138\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35138\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, 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, 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\/35138\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. What is a Minimum Spanning Tree? A Minimum Spanning Tree (MST) is a subgraph in a weighted graph that includes all vertices while minimizing the sum of the weights. In other words, it means a tree where all vertices are connected and the sum of the weights of the given edges is the smallest. &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, minimum spanning tree\"","og_url":"https:\/\/atmokpo.com\/w\/35138\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:35:59+00:00","article_modified_time":"2024-11-01T11:44:51+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35138\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35138\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, minimum spanning tree","datePublished":"2024-11-01T09:35:59+00:00","dateModified":"2024-11-01T11:44:51+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35138\/"},"wordCount":553,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35138\/","url":"https:\/\/atmokpo.com\/w\/35138\/","name":"kotlin coding test course, minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:35:59+00:00","dateModified":"2024-11-01T11:44:51+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35138\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35138\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35138\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, 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\/35138","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=35138"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35138\/revisions"}],"predecessor-version":[{"id":35139,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35138\/revisions\/35139"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}