{"id":34392,"date":"2024-11-01T09:27:36","date_gmt":"2024-11-01T09:27:36","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34392"},"modified":"2024-11-01T11:41:27","modified_gmt":"2024-11-01T11:41:27","slug":"javascript-coding-test-course-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34392\/","title":{"rendered":"JavaScript Coding Test Course, Minimum Spanning Tree"},"content":{"rendered":"<p><body><\/p>\n<p>\n    Coding tests are a great way to evaluate programming skills. Today, we will look at how to solve the <strong>Minimum Spanning Tree (MST)<\/strong> problem.<br \/>\n    This problem is applicable in various fields, especially in problems related to computer networks.<br \/>\n    There are several algorithms to construct a minimum spanning tree, but particularly <strong>Kruskal&#8217;s Algorithm<\/strong> and <strong>Prim&#8217;s Algorithm<\/strong> are widely used.\n<\/p>\n<h2>Problem Description<\/h2>\n<p>\n    Let&#8217;s solve the problem of finding a minimum spanning tree given the edges that represent the graph below.<br \/>\n    Each edge is assigned a weight, and we need to find a connected graph where the total weight is minimized.\n<\/p>\n<h3>Problem Definition<\/h3>\n<pre>\n    Input:\n        [(1, 2, 4), (1, 3, 1), (2, 3, 2), (2, 4, 5), (3, 4, 8), (1, 4, 3)]\n    Output:\n        List of edges in the minimum spanning tree: [(1, 3, 1), (2, 3, 2), (1, 4, 3), (2, 4, 5)]\n        Sum of minimum weights: 11\n<\/pre>\n<h3>Problem Solving Process<\/h3>\n<h4>Step 1: Understanding Graph Data Structure<\/h4>\n<p>\n    A graph consists of vertices (nodes) and edges.<br \/>\n    Here, edges are provided in the form of tuples as `(start vertex, end vertex, weight)`.<br \/>\n    We need to construct the graph using this data.\n<\/p>\n<h4>Step 2: Sorting Edges<\/h4>\n<p>\n    Kruskal&#8217;s Algorithm first sorts the edges based on their weights.<br \/>\n    We sort the given list of edges in ascending order to be able to select the edges with the minimum weights.\n<\/p>\n<h4>Step 3: Cycle Detection Using Union-Find Structure<\/h4>\n<p>\n    To check whether a cycle occurs when adding edges, we use the <strong>Union-Find<\/strong> data structure.<br \/>\n    This data structure has two main functions:\n<\/p>\n<ul>\n<li>Find: Find which set a specific element belongs to<\/li>\n<li>Union: Merge the sets that two elements belong to<\/li>\n<\/ul>\n<p>\n    If no cycle occurs, we select the edge; otherwise, we ignore the edge.<br \/>\n    We define the basic Union-Find class and its methods required to implement the algorithm.\n<\/p>\n<h4>Step 4: Implementing MST<\/h4>\n<p>\n    Now we will combine the implementations of all steps to ultimately find the MST.<br \/>\n    Below is an example of the Kruskal&#8217;s algorithm implemented in JavaScript.\n<\/p>\n<pre>\n    class UnionFind {\n        constructor(size) {\n            this.parent = Array.from({length: size}, (_, index) => index);\n            this.rank = Array(size).fill(1);\n        }\n\n        find(node) {\n            if (this.parent[node] !== node) {\n                this.parent[node] = this.find(this.parent[node]);\n            }\n            return this.parent[node];\n        }\n\n        union(node1, node2) {\n            const root1 = this.find(node1);\n            const root2 = this.find(node2);\n            if (root1 !== root2) {\n                if (this.rank[root1] > this.rank[root2]) {\n                    this.parent[root2] = root1;\n                } else if (this.rank[root1] < this.rank[root2]) {\n                    this.parent[root1] = root2;\n                } else {\n                    this.parent[root2] = root1;\n                    this.rank[root1]++;\n                }\n            }\n        }\n\n        connected(node1, node2) {\n            return this.find(node1) === this.find(node2);\n        }\n    }\n\n    function kruskal(edges, numVertices) {\n        edges.sort((a, b) => a[2] - b[2]); \/\/ Sort edges by weight\n        const uf = new UnionFind(numVertices);\n        const mst = [];\n        let totalWeight = 0;\n\n        for (const [u, v, weight] of edges) {\n            if (!uf.connected(u - 1, v - 1)) {\n                uf.union(u - 1, v - 1);\n                mst.push([u, v, weight]);\n                totalWeight += weight;\n            }\n        }\n\n        return { mst, totalWeight };\n    }\n\n    const edges = [\n        [1, 2, 4],\n        [1, 3, 1],\n        [2, 3, 2],\n        [2, 4, 5],\n        [3, 4, 8],\n        [1, 4, 3]\n    ];\n    const numVertices = 4;\n\n    const { mst, totalWeight } = kruskal(edges, numVertices);\n    console.log(\"List of edges in the minimum spanning tree:\", mst);\n    console.log(\"Sum of minimum weights:\", totalWeight);\n<\/pre>\n<h4>Step 5: Analyzing Results<\/h4>\n<p>\n    The results obtained through the implemented algorithm are as follows.<br \/>\n    We analyze whether the algorithm works correctly by checking the list of edges in the minimum spanning tree and the total weight.<br \/>\n    As a result, we successfully formed the minimum spanning tree based on the given edges.\n<\/p>\n<ul>\n<li>List of edges in the minimum spanning tree: [(1, 3, 1), (2, 3, 2), (1, 4, 3), (2, 4, 5)]<\/li>\n<li>Sum of minimum weights: 11<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>\n    Today, we examined how to solve the minimum spanning tree problem using JavaScript.<br \/>\n    Various algorithms can be applied using the given list of edges, and we can understand the characteristics of graphs in the process.<br \/>\n    While tackling such problems, we can gain experience in algorithm performance analysis and optimization,<br \/>\n    and since this is a topic likely to appear in actual coding tests, be sure to practice sufficiently.\n<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coding tests are a great way to evaluate programming skills. Today, we will look at how to solve the Minimum Spanning Tree (MST) problem. This problem is applicable in various fields, especially in problems related to computer networks. There are several algorithms to construct a minimum spanning tree, but particularly Kruskal&#8217;s Algorithm and Prim&#8217;s Algorithm &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34392\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript 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":[141],"tags":[],"class_list":["post-34392","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript 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\/34392\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Coding tests are a great way to evaluate programming skills. Today, we will look at how to solve the Minimum Spanning Tree (MST) problem. This problem is applicable in various fields, especially in problems related to computer networks. There are several algorithms to construct a minimum spanning tree, but particularly Kruskal&#8217;s Algorithm and Prim&#8217;s Algorithm &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Minimum Spanning Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34392\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:27+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\/34392\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34392\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Minimum Spanning Tree\",\"datePublished\":\"2024-11-01T09:27:36+00:00\",\"dateModified\":\"2024-11-01T11:41:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34392\/\"},\"wordCount\":417,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34392\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34392\/\",\"name\":\"JavaScript Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:36+00:00\",\"dateModified\":\"2024-11-01T11:41:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34392\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34392\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34392\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript 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":"JavaScript 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\/34392\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Coding tests are a great way to evaluate programming skills. Today, we will look at how to solve the Minimum Spanning Tree (MST) problem. This problem is applicable in various fields, especially in problems related to computer networks. There are several algorithms to construct a minimum spanning tree, but particularly Kruskal&#8217;s Algorithm and Prim&#8217;s Algorithm &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Minimum Spanning Tree\"","og_url":"https:\/\/atmokpo.com\/w\/34392\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:36+00:00","article_modified_time":"2024-11-01T11:41:27+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\/34392\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34392\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Minimum Spanning Tree","datePublished":"2024-11-01T09:27:36+00:00","dateModified":"2024-11-01T11:41:27+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34392\/"},"wordCount":417,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34392\/","url":"https:\/\/atmokpo.com\/w\/34392\/","name":"JavaScript Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:36+00:00","dateModified":"2024-11-01T11:41:27+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34392\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34392\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34392\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript 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\/34392","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=34392"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34392\/revisions"}],"predecessor-version":[{"id":34393,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34392\/revisions\/34393"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}