{"id":34074,"date":"2024-11-01T09:23:50","date_gmt":"2024-11-01T09:23:50","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34074"},"modified":"2024-11-01T10:53:32","modified_gmt":"2024-11-01T10:53:32","slug":"c-coding-test-course-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34074\/","title":{"rendered":"C# Coding Test Course, Minimum Spanning Tree"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Introduction<\/h2>\n<p>\n    In the field of computer science and programming, algorithms are essential tools for solving problems. In particular, coding tests are one of the very important aspects for developers, requiring an understanding of algorithms and data structures. In this course, we will cover the concept of <strong>Minimum Spanning Tree (MST)<\/strong> and algorithm problems that utilize it.\n<\/p>\n<h2>2. What is a Minimum Spanning Tree?<\/h2>\n<p>\n    A minimum spanning tree is a tree with the minimum sum of edge weights that includes all vertices and does not contain cycles among the subgraphs of a connected graph. It is important to understand the vertices, edges, and weights of the graph. Minimum spanning trees are mainly used in <strong>clustering<\/strong>, <strong>network design<\/strong>, and <strong>solving various problems with minimal distances maintained<\/strong>.\n<\/p>\n<h3>2.1 Properties of MST<\/h3>\n<ul>\n<li>All vertices must be included.<\/li>\n<li>No cycles exist.<\/li>\n<li>The sum of the weights is minimal.<\/li>\n<\/ul>\n<h2>3. Algorithms to find a Minimum Spanning Tree<\/h2>\n<p>\n    The methods to find a minimum spanning tree are the <strong>Kruskal&#8217;s algorithm<\/strong> and <strong>Prim&#8217;s algorithm<\/strong>. Both algorithms employ different approaches but ultimately produce the same result.\n<\/p>\n<h3>3.1 Kruskal&#8217;s Algorithm<\/h3>\n<p>\n    Kruskal&#8217;s algorithm builds the spanning tree by selecting edges starting from the smallest weight. This algorithm sorts the given edges in ascending order of weight and then selects them one by one. Care must be taken to avoid cycles.\n<\/p>\n<h3>3.2 Prim&#8217;s Algorithm<\/h3>\n<p>\n    Prim&#8217;s algorithm starts from a single vertex and selects the edge with the least weight that is connected to that vertex. This method is more efficient when the number of vertices is large.\n<\/p>\n<h2>4. Problem Description<\/h2>\n<p>\n    Now, let&#8217;s look at the process of solving a real problem. The following is a problem to find a minimum spanning tree.\n<\/p>\n<h3>Problem: Finding the Minimum Spanning Tree<\/h3>\n<p>\n<strong>Input:<\/strong><\/p>\n<ul>\n<li>Number of vertices <code>V<\/code> (1 \u2264 V \u2264 1000)<\/li>\n<li>Number of edges <code>E<\/code> (1 \u2264 E \u2264 10000)<\/li>\n<li>Each edge is given in the form <code>u, v, w<\/code>, where <strong>u<\/strong> and <strong>v<\/strong> are the vertex numbers, and <strong>w<\/strong> is the weight of the edge connecting the two vertices.<\/li>\n<\/ul>\n<p>\n<strong>Output:<\/strong> Outputs the sum of the weights of the edges forming the minimum spanning tree.\n<\/p>\n<h2>5. Problem Solving<\/h2>\n<p>\n    To solve the problem, I will use Kruskal&#8217;s algorithm. This algorithm proceeds in the following steps.\n<\/p>\n<h3>5.1 Edge Sorting<\/h3>\n<p>\n    Sort all the edges received as input in ascending order of their weights. The sorted edges will act as the criteria for selecting edges that form the minimum spanning tree.\n<\/p>\n<h3>5.2 Cycle Check<\/h3>\n<p>\n    Select the sorted edges one by one and check if the two vertices belong to different sets. For this, the <strong>Union-Find<\/strong> data structure is used.\n<\/p>\n<h3>5.3 Adding Edges and Summing Weights<\/h3>\n<p>\n    If no cycle occurs, add the edge to the list of edges of the minimum spanning tree and sum its weight. This process repeats until all edges are processed.\n<\/p>\n<h3>5.4 C# Implementation<\/h3>\n<p>\n    Now, let\u2019s implement the above process in C# code.\n<\/p>\n<pre><code class=\"language-csharp\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Edge\n{\n    public int U { get; set; }\n    public int V { get; set; }\n    public int Weight { get; set; }\n}\n\nclass UnionFind\n{\n    private int[] parent;\n\n    public UnionFind(int size)\n    {\n        parent = new int[size];\n        for (int i = 0; i &lt; size; i++)\n            parent[i] = i;\n    }\n\n    public int Find(int u)\n    {\n        if (parent[u] != u)\n            parent[u] = Find(parent[u]);\n        return parent[u];\n    }\n\n    public void Union(int u, int v)\n    {\n        int rootU = Find(u);\n        int rootV = Find(v);\n        if (rootU != rootV)\n            parent[rootU] = rootV;\n    }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        int V = int.Parse(Console.ReadLine());\n        int E = int.Parse(Console.ReadLine());\n        List<edge> edges = new List<edge>();\n\n        for (int i = 0; i &lt; E; i++)\n        {\n            string[] input = Console.ReadLine().Split();\n            edges.Add(new Edge { U = int.Parse(input[0]), V = int.Parse(input[1]), Weight = int.Parse(input[2]) });\n        }\n\n        edges = edges.OrderBy(e =&gt; e.Weight).ToList();\n        UnionFind uf = new UnionFind(V + 1);\n        int totalWeight = 0;\n\n        foreach (var edge in edges)\n        {\n            if (uf.Find(edge.U) != uf.Find(edge.V))\n            {\n                uf.Union(edge.U, edge.V);\n                totalWeight += edge.Weight;\n            }\n        }\n\n        Console.WriteLine(totalWeight);\n    }\n}<\/edge><\/edge><\/code><\/pre>\n<h2>6. Time Complexity<\/h2>\n<p>\n    The overall time complexity of Kruskal&#8217;s algorithm is as follows.\n<\/p>\n<ul>\n<li>Edge sorting: <code>O(E log E)<\/code><\/li>\n<li>Union-Find: <code>O(E \u03b1(V))<\/code> (\u03b1 is the inverse function of the Ackermann function)<\/li>\n<\/ul>\n<p>\n    Therefore, the overall time complexity is <code>O(E log E)<\/code>. This provides relatively efficient performance even when the number of edges is large.\n<\/p>\n<h2>7. Conclusion<\/h2>\n<p>\n    In this course, we learned about minimum spanning trees. We learned how to find a minimum spanning tree using Kruskal&#8217;s algorithm and practiced the process through C# code. Minimum spanning trees can be applied to various graph problems and play an important role in developing algorithmic thinking. In the next course, we will explore a wider variety of algorithms and data structures.\n<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In the field of computer science and programming, algorithms are essential tools for solving problems. In particular, coding tests are one of the very important aspects for developers, requiring an understanding of algorithms and data structures. In this course, we will cover the concept of Minimum Spanning Tree (MST) and algorithm problems that &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34074\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# 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":[90],"tags":[],"class_list":["post-34074","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# 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\/34074\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction In the field of computer science and programming, algorithms are essential tools for solving problems. In particular, coding tests are one of the very important aspects for developers, requiring an understanding of algorithms and data structures. In this course, we will cover the concept of Minimum Spanning Tree (MST) and algorithm problems that &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Minimum Spanning Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34074\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:32+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\/34074\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34074\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Minimum Spanning Tree\",\"datePublished\":\"2024-11-01T09:23:50+00:00\",\"dateModified\":\"2024-11-01T10:53:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34074\/\"},\"wordCount\":576,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34074\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34074\/\",\"name\":\"C# Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:50+00:00\",\"dateModified\":\"2024-11-01T10:53:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34074\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34074\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34074\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# 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":"C# 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\/34074\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction In the field of computer science and programming, algorithms are essential tools for solving problems. In particular, coding tests are one of the very important aspects for developers, requiring an understanding of algorithms and data structures. In this course, we will cover the concept of Minimum Spanning Tree (MST) and algorithm problems that &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Minimum Spanning Tree\"","og_url":"https:\/\/atmokpo.com\/w\/34074\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:50+00:00","article_modified_time":"2024-11-01T10:53:32+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\/34074\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34074\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Minimum Spanning Tree","datePublished":"2024-11-01T09:23:50+00:00","dateModified":"2024-11-01T10:53:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34074\/"},"wordCount":576,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34074\/","url":"https:\/\/atmokpo.com\/w\/34074\/","name":"C# Coding Test Course, Minimum Spanning Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:50+00:00","dateModified":"2024-11-01T10:53:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34074\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34074\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34074\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# 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\/34074","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=34074"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34074\/revisions"}],"predecessor-version":[{"id":34075,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34074\/revisions\/34075"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}