{"id":33502,"date":"2024-11-01T09:17:11","date_gmt":"2024-11-01T09:17:11","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33502"},"modified":"2024-11-01T11:38:16","modified_gmt":"2024-11-01T11:38:16","slug":"java-coding-test-course-finding-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33502\/","title":{"rendered":"Java Coding Test Course, Finding Minimum Spanning Tree"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today, we will learn about the <strong>Minimum Spanning Tree<\/strong> problem that frequently appears in algorithm exams. In particular, we will delve deeply into how to solve this problem using Java.<\/p>\n<h2>Problem Description<\/h2>\n<p>Let&#8217;s assume there is a weighted connected graph as follows. All vertices of this graph are connected, and the weights are different from each other. Your goal is to find the minimum spanning tree of this graph.<\/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 contain the two vertices and the weight of each edge <code>u<\/code>, <code>v<\/code>, <code>w<\/code>.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Print the list of edges that make up the minimum spanning tree and the total weight.<\/p>\n<h2>Example Input\/Output<\/h2>\n<div class=\"example\">\n<h4>Example Input<\/h4>\n<pre>\n        3 3\n        1 2 1\n        2 3 2\n        1 3 3\n        <\/pre>\n<h4>Example Output<\/h4>\n<pre>\n        1 - 2\n        2 - 3\n        Total Weight: 3\n        <\/pre>\n<\/div>\n<h2>Approach to Problem Solving<\/h2>\n<p>The representative algorithms used to find the minimum spanning tree are <strong>Kruskal&#8217;s Algorithm<\/strong> and <strong>Prim&#8217;s Algorithm<\/strong>. Here, we will use Kruskal&#8217;s Algorithm to solve this problem.<\/p>\n<h3>Overview of Kruskal&#8217;s Algorithm<\/h3>\n<ol>\n<li>Sort the edges in ascending order based on weight.<\/li>\n<li>Select the edge with the smallest weight, and if it doesn&#8217;t form a cycle, include this edge in the result.<\/li>\n<li>Repeat the above process until all vertices are connected.<\/li>\n<\/ol>\n<h2>Java Code Implementation<\/h2>\n<p>Now, let&#8217;s implement Kruskal&#8217;s Algorithm through Java code.<\/p>\n<pre><code>\nimport java.util.Arrays;\n\npublic class Kruskal {\n    static class Edge implements Comparable<Edge> {\n        int u, v, weight;\n\n        Edge(int u, int v, int weight) {\n            this.u = u;\n            this.v = v;\n            this.weight = weight;\n        }\n\n        @Override\n        public int compareTo(Edge other) {\n            return this.weight - other.weight;\n        }\n    }\n\n    static int[] parent;\n    static int[] rank;\n\n    public static void main(String[] args) {\n        int V = 3; \/\/ Number of vertices\n        int E = 3; \/\/ Number of edges\n        Edge[] edges = new Edge[E];\n\n        edges[0] = new Edge(1, 2, 1);\n        edges[1] = new Edge(2, 3, 2);\n        edges[2] = new Edge(1, 3, 3);\n\n        Arrays.sort(edges);\n\n        parent = new int[V + 1];\n        rank = new int[V + 1];\n\n        for (int i = 1; i <= V; i++) {\n            parent[i] = i;\n            rank[i] = 0;\n        }\n\n        int totalWeight = 0;\n        System.out.println(\"Edges of the minimum spanning tree:\");\n        for (Edge edge : edges) {\n            if (union(edge.u, edge.v)) {\n                System.out.println(edge.u + \" - \" + edge.v);\n                totalWeight += edge.weight;\n            }\n        }\n        System.out.println(\"Total Weight: \" + totalWeight);\n    }\n\n    static int find(int u) {\n        if (parent[u] != u) {\n            parent[u] = find(parent[u]);\n        }\n        return parent[u];\n    }\n\n    static boolean union(int u, int v) {\n        int rootU = find(u);\n        int rootV = find(v);\n        if (rootU != rootV) {\n            if (rank[rootU] < rank[rootV]) {\n                parent[rootU] = rootV;\n            } else if (rank[rootU] > rank[rootV]) {\n                parent[rootV] = rootU;\n            } else {\n                parent[rootV] = rootU;\n                rank[rootU]++;\n            }\n            return true;\n        }\n        return false;\n    }\n}\n    <\/Edge><\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>The above code implements the following key functionalities:<\/p>\n<ol>\n<li><strong>Edge Class<\/strong>: A class representing edges, set up to be comparable based on weight.<\/li>\n<li><strong>find Method<\/strong>: Finds and returns the root node through the Union-Find algorithm. It improves performance using Path Compression.<\/li>\n<li><strong>union Method<\/strong>: Combines the root nodes of two vertices if they are different and returns whether the union was successful. This helps to check for cycles.<\/li>\n<\/ol>\n<h2>Testing and Execution<\/h2>\n<p>When you compile and execute this code, the following results will be printed:<\/p>\n<pre>\nEdges of the minimum spanning tree:\n1 - 2\n2 - 3\nTotal Weight: 3\n    <\/pre>\n<h2>Applications of Minimum Spanning Trees<\/h2>\n<p>Minimum spanning trees can be applied in various fields. For example:<\/p>\n<ul>\n<li>Network Design: Useful in determining paths that can be connected at minimal cost when designing computer networks.<\/li>\n<li>Road Network Construction: Can be used for constructing urban road networks at minimal cost.<\/li>\n<li>Power Grid Design: Spanning tree algorithms are utilized for cost reduction in power grids.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Today, we learned how to solve the minimum spanning tree problem using Kruskal&#8217;s Algorithm. This algorithm has a time complexity of <code>O(E log E)<\/code>, making it efficient relative to the number of edges. I recommend practicing problems like this multiple times as preparation for algorithm problem-solving. I hope it helps you with your coding test preparation in the future!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today, we will learn about the Minimum Spanning Tree problem that frequently appears in algorithm exams. In particular, we will delve deeply into how to solve this problem using Java. Problem Description Let&#8217;s assume there is a weighted connected graph as follows. All vertices of this graph are connected, and the weights are different &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33502\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java 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":[139],"tags":[],"class_list":["post-33502","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java 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\/33502\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today, we will learn about the Minimum Spanning Tree problem that frequently appears in algorithm exams. In particular, we will delve deeply into how to solve this problem using Java. Problem Description Let&#8217;s assume there is a weighted connected graph as follows. All vertices of this graph are connected, and the weights are different &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Finding Minimum Spanning Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33502\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:17:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:16+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\/33502\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33502\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Finding Minimum Spanning Tree\",\"datePublished\":\"2024-11-01T09:17:11+00:00\",\"dateModified\":\"2024-11-01T11:38:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33502\/\"},\"wordCount\":425,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33502\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33502\/\",\"name\":\"Java Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:17:11+00:00\",\"dateModified\":\"2024-11-01T11:38:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33502\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33502\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33502\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java 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":"Java 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\/33502\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today, we will learn about the Minimum Spanning Tree problem that frequently appears in algorithm exams. In particular, we will delve deeply into how to solve this problem using Java. Problem Description Let&#8217;s assume there is a weighted connected graph as follows. All vertices of this graph are connected, and the weights are different &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Finding Minimum Spanning Tree\"","og_url":"https:\/\/atmokpo.com\/w\/33502\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:17:11+00:00","article_modified_time":"2024-11-01T11:38:16+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\/33502\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33502\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Finding Minimum Spanning Tree","datePublished":"2024-11-01T09:17:11+00:00","dateModified":"2024-11-01T11:38:16+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33502\/"},"wordCount":425,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33502\/","url":"https:\/\/atmokpo.com\/w\/33502\/","name":"Java Coding Test Course, Finding Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:17:11+00:00","dateModified":"2024-11-01T11:38:16+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33502\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33502\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33502\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java 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\/33502","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=33502"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33502\/revisions"}],"predecessor-version":[{"id":33503,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33502\/revisions\/33503"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}