{"id":33500,"date":"2024-11-01T09:17:10","date_gmt":"2024-11-01T09:17:10","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33500"},"modified":"2024-11-01T11:38:17","modified_gmt":"2024-11-01T11:38:17","slug":"java-coding-test-course-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33500\/","title":{"rendered":"Java Coding Test Course, Minimum Spanning Tree"},"content":{"rendered":"<p><body><\/p>\n<h2>1. What is a Minimum Spanning Tree?<\/h2>\n<p>A Minimum Spanning Tree (MST) is a tree that includes all vertices in a weighted undirected graph while minimizing the total weight. Minimum Spanning Trees are used in network design, clustering, and various optimization problems.<\/p>\n<h2>2. Overview of Algorithms<\/h2>\n<p>The representative algorithms for finding a Minimum Spanning Tree are Kruskal&#8217;s Algorithm and Prim&#8217;s Algorithm. These two algorithms construct the MST in different ways.<\/p>\n<h3>2.1. Kruskal&#8217;s Algorithm<\/h3>\n<p>Kruskal&#8217;s Algorithm works by sorting the edges of the graph in ascending order based on their weights and then adding edges to the MST in a way that avoids forming cycles.<\/p>\n<h3>2.2. Prim&#8217;s Algorithm<\/h3>\n<p>Prim&#8217;s Algorithm starts from a starting vertex and chooses the lowest weight edge from the currently connected vertices to expand the MST.<\/p>\n<h3>2.3. Choosing an Algorithm<\/h3>\n<p>Both algorithms can efficiently find the MST, but Kruskal is more suitable for sparse graphs with fewer edges, while Prim is better for dense graphs with fewer vertices.<\/p>\n<h2>3. Problem Statement<\/h2>\n<h3>Problem: Create a Minimum Spanning Tree<\/h3>\n<p>Given a weighted undirected graph, find the Minimum Spanning Tree. The input format is as follows:<\/p>\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>Next, <code>E<\/code> lines each contain <code>u<\/code>, <code>v<\/code>, <code>w<\/code>, representing an edge connecting vertex <code>u<\/code> and <code>v<\/code> with weight <code>w<\/code>.<\/li>\n<\/ul>\n<p>Output the total weight of the Minimum Spanning Tree.<\/p>\n<h2>4. Problem Solving Approach<\/h2>\n<p>To solve the problem, follow these steps:<\/p>\n<ol>\n<li>Read the input values.<\/li>\n<li>Sort the edges by weight.<\/li>\n<li>Use a Union-Find data structure to detect cycles.<\/li>\n<li>Add edges to the MST as long as they do not form cycles.<\/li>\n<li>Calculate the total weight of the MST and print it.<\/li>\n<\/ol>\n<h2>5. Java Code Implementation<\/h2>\n<h3>5.1. Union-Find Data Structure Implementation<\/h3>\n<pre class=\"code\">\nclass UnionFind {\n    private int[] parent;\n    private int[] rank;\n\n    public UnionFind(int size) {\n        parent = new int[size];\n        rank = new int[size];\n        for (int i = 0; i &lt; size; i++) {\n            parent[i] = i;\n            rank[i] = 0;\n        }\n    }\n\n    public int find(int p) {\n        if (parent[p] != p) {\n            parent[p] = find(parent[p]);\n        }\n        return parent[p];\n    }\n\n    public void union(int p, int q) {\n        int rootP = find(p);\n        int rootQ = find(q);\n        if (rootP != rootQ) {\n            if (rank[rootP] &gt; rank[rootQ]) {\n                parent[rootQ] = rootP;\n            } else if (rank[rootP] &lt; rank[rootQ]) {\n                parent[rootP] = rootQ;\n            } else {\n                parent[rootQ] = rootP;\n                rank[rootP]++;\n            }\n        }\n    }\n}\n    <\/pre>\n<h3>5.2. Kruskal&#8217;s Algorithm Implementation<\/h3>\n<pre class=\"code\">\nimport java.util.*;\n\nclass Edge implements Comparable<Edge> {\n    int u, v, weight;\n\n    public 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 Integer.compare(this.weight, other.weight);\n    }\n}\n\npublic class MinimumSpanningTree {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int V = scanner.nextInt();\n        int E = scanner.nextInt();\n\n        PriorityQueue<Edge> edgeList = new PriorityQueue<>();\n        for (int i = 0; i &lt; E; i++) {\n            int u = scanner.nextInt();\n            int v = scanner.nextInt();\n            int weight = scanner.nextInt();\n            edgeList.add(new Edge(u, v, weight));\n        }\n\n        UnionFind uf = new UnionFind(V);\n        int totalWeight = 0;\n\n        while (!edgeList.isEmpty()) {\n            Edge edge = edgeList.poll();\n            if (uf.find(edge.u) != uf.find(edge.v)) {\n                uf.union(edge.u, edge.v);\n                totalWeight += edge.weight;\n            }\n        }\n\n        System.out.println(totalWeight);\n    }\n}\n    <\/pre>\n<h2>6. Code Explanation<\/h2>\n<p>The above code implements Kruskal&#8217;s Algorithm. First, it reads the number of vertices and edges from the input and stores the information for each edge. It then sorts the edges by weight and uses the Union-Find data structure to select edges that do not form cycles. Finally, it prints the total weight of the selected edges.<\/p>\n<h2>7. Time Complexity<\/h2>\n<p>The time complexity of Kruskal&#8217;s Algorithm is generally <code>O(E log E)<\/code>, which is the complexity for sorting the edges. The Union-Find operations are very efficient and are performed in almost constant time.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>The Minimum Spanning Tree is an important concept in graph theory and is applied in various optimization problems. By understanding and implementing Kruskal&#8217;s and Prim&#8217;s algorithms, one can easily find the Minimum Spanning Tree. Through this lecture, I hope you gain a solid understanding of the MST concept and develop the ability to apply it to actual coding test problems.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is a Minimum Spanning Tree? A Minimum Spanning Tree (MST) is a tree that includes all vertices in a weighted undirected graph while minimizing the total weight. Minimum Spanning Trees are used in network design, clustering, and various optimization problems. 2. Overview of Algorithms The representative algorithms for finding a Minimum Spanning Tree &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33500\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java 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":[139],"tags":[],"class_list":["post-33500","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, 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\/33500\/\" \/>\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, 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 tree that includes all vertices in a weighted undirected graph while minimizing the total weight. Minimum Spanning Trees are used in network design, clustering, and various optimization problems. 2. Overview of Algorithms The representative algorithms for finding a Minimum Spanning Tree &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Minimum Spanning Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33500\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:17:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:17+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\/33500\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33500\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Minimum Spanning Tree\",\"datePublished\":\"2024-11-01T09:17:10+00:00\",\"dateModified\":\"2024-11-01T11:38:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33500\/\"},\"wordCount\":442,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33500\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33500\/\",\"name\":\"Java Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:17:10+00:00\",\"dateModified\":\"2024-11-01T11:38:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33500\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33500\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33500\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java 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":"Java 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\/33500\/","og_locale":"ko_KR","og_type":"article","og_title":"Java 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 tree that includes all vertices in a weighted undirected graph while minimizing the total weight. Minimum Spanning Trees are used in network design, clustering, and various optimization problems. 2. Overview of Algorithms The representative algorithms for finding a Minimum Spanning Tree &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Minimum Spanning Tree\"","og_url":"https:\/\/atmokpo.com\/w\/33500\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:17:10+00:00","article_modified_time":"2024-11-01T11:38:17+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\/33500\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33500\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Minimum Spanning Tree","datePublished":"2024-11-01T09:17:10+00:00","dateModified":"2024-11-01T11:38:17+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33500\/"},"wordCount":442,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33500\/","url":"https:\/\/atmokpo.com\/w\/33500\/","name":"Java Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:17:10+00:00","dateModified":"2024-11-01T11:38:17+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33500\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33500\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33500\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java 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\/33500","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=33500"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33500\/revisions"}],"predecessor-version":[{"id":33501,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33500\/revisions\/33501"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}