{"id":33776,"date":"2024-11-01T09:20:15","date_gmt":"2024-11-01T09:20:15","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33776"},"modified":"2024-11-01T11:46:44","modified_gmt":"2024-11-01T11:46:44","slug":"python-coding-test-course-finding-the-minimum-spanning-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33776\/","title":{"rendered":"python coding test course, finding the minimum spanning tree"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today we will learn about an important concept in graph theory called <strong>Minimum Spanning Tree (MST)<\/strong>. A Minimum Spanning Tree is a tree that includes all connected vertices while minimizing the total weight of the edges. This concept is utilized in various fields such as network design, road connectivity, and clustering.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is as follows:<\/p>\n<blockquote>\n<p>Write a program to find the Minimum Spanning Tree of a given <strong>undirected weighted graph<\/strong>. The graph is given within the constraints <code>1 \u2264 V \u2264 1000<\/code> (number of vertices) and <code>1 \u2264 E \u2264 10000<\/code> (number of edges), with all edge weights in the range <code>1 \u2264 w \u2264 1000<\/code>.<\/p>\n<\/blockquote>\n<h2>Problem Solving Approach<\/h2>\n<p>There are several algorithms to find the Minimum Spanning Tree, but the two most commonly used are <strong>Prim&#8217;s Algorithm<\/strong> and <strong>Kruskal&#8217;s Algorithm<\/strong>. Here, we will describe how to solve the problem using Prim&#8217;s Algorithm.<\/p>\n<h3>Overview of Prim&#8217;s Algorithm<\/h3>\n<p>Prim&#8217;s Algorithm is an algorithm that proceeds by always selecting the edge with the minimum weight, following these steps:<\/p>\n<ol>\n<li>Select a starting vertex. (Any vertex can be chosen as the starting point.)<\/li>\n<li>Select the edge with the smallest weight among the edges connected to the currently selected vertex.<\/li>\n<li>Add the vertex connected by the selected edge to the tree.<\/li>\n<li>Repeat steps 2 and 3 until all vertices are included.<\/li>\n<\/ol>\n<h3>Time Complexity of Prim&#8217;s Algorithm<\/h3>\n<p>The time complexity of Prim&#8217;s Algorithm depends on the data structure used. Using a heap (priority queue) results in a complexity of <code>O(E log V)<\/code>, while using an adjacency matrix results in a complexity of <code>O(V<sup>2<\/sup>)<\/code>. Generally, using a heap is more efficient.<\/p>\n<h2>Implementation<\/h2>\n<p>Now let&#8217;s implement Prim&#8217;s Algorithm. Below is the Python code:<\/p>\n<pre><code class=\"language-python\">\nimport heapq  # Importing to use heap\n\ndef prim(graph, start):\n    mst = []  # List for the Minimum Spanning Tree\n    visited = set()  # Set of visited vertices\n    min_heap = [(0, start)]  # Initializing heap (weight, vertex)\n\n    total_weight = 0  # Total weight\n\n    while min_heap:\n        weight, vertex = heapq.heappop(min_heap)  # Selecting edge with minimum weight\n        if vertex not in visited:\n            visited.add(vertex)  # Mark as visited\n            total_weight += weight  # Summing weights\n            mst.append((weight, vertex))\n\n            for next_vertex, next_weight in graph[vertex].items():\n                if next_vertex not in visited:\n                    heapq.heappush(min_heap, (next_weight, next_vertex))  # Adding to heap\n\n    return mst, total_weight  # Returning Minimum Spanning Tree and total weight\n\n# Graph definition (adjacency list)\ngraph = {\n    1: {2: 3, 3: 1},\n    2: {1: 3, 3: 1, 4: 6},\n    3: {1: 1, 2: 1, 4: 5},\n    4: {2: 6, 3: 5}\n}\n\nmst, total_weight = prim(graph, 1)\nprint(\"Minimum Spanning Tree:\", mst)\nprint(\"Total Weight:\", total_weight)\n    <\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>The above code defines a function called <code>prim<\/code>. This function takes the graph and the starting vertex as arguments and returns the Minimum Spanning Tree along with its total weight.<\/p>\n<ul>\n<li><code>min_heap<\/code>: Manages the currently selectable edges using a heap.<\/li>\n<li><code>visited<\/code>: Tracks the selected vertices to prevent duplicates.<\/li>\n<li>After selecting the minimum weight edge from the heap, it adds the corresponding vertex to the tree and adds the connected vertices to the heap.<\/li>\n<\/ul>\n<h2>Running Test Cases<\/h2>\n<p>When you run the code above, you will get the following results:<\/p>\n<pre><code class=\"language-text\">\nMinimum Spanning Tree: [(0, 1), (1, 3), (1, 2)]\nTotal Weight: 2\n    <\/code><\/pre>\n<p>Here, the Minimum Spanning Tree includes vertices 1, 2, and 3, with a total weight of 2.<\/p>\n<h2>Complex Example<\/h2>\n<p>You can also find the Minimum Spanning Tree for more complex graphs using the same method. For example, consider a graph with multiple vertices and edges:<\/p>\n<pre><code class=\"language-python\">\n# Defining a complex graph\ncomplex_graph = {\n    'A': {'B': 4, 'H': 8},\n    'B': {'A': 4, 'C': 8, 'H': 11},\n    'C': {'B': 8, 'D': 7, 'F': 4, 'I': 2},\n    'D': {'C': 7, 'E': 9, 'F': 14},\n    'E': {'D': 9, 'F': 10},\n    'F': {'C': 4, 'D': 14, 'E': 10, 'G': 2},\n    'G': {'F': 2, 'H': 1, 'I': 6},\n    'H': {'A': 8, 'B': 11, 'G': 1},\n    'I': {'C': 2, 'G': 6}\n}\n\nmst_complex, total_weight_complex = prim(complex_graph, 'A')\nprint(\"Minimum Spanning Tree of the complex graph:\", mst_complex)\nprint(\"Total Weight:\", total_weight_complex)\n    <\/code><\/pre>\n<h3>Result Interpretation<\/h3>\n<p>In this complex graph, multiple selections are needed to find the Minimum Spanning Tree. Prim&#8217;s Algorithm is effective in choosing optimal edges to construct the tree. The execution result displays the final Minimum Spanning Tree and its total weight.<\/p>\n<h2>Conclusion<\/h2>\n<p>The Minimum Spanning Tree plays an important role in network design and optimization problems. Through this tutorial, we have implemented Prim&#8217;s Algorithm in Python and applied it to real problems. Verifying the algorithm&#8217;s accuracy through various test cases is also an important process. I hope this helps you prepare for coding tests through problem-solving using algorithms!<\/p>\n<h3>Additional Learning Resources<\/h3>\n<p>Besides Prim&#8217;s Algorithm, it&#8217;s also beneficial to explore other methods like Kruskal&#8217;s Algorithm. Here are some resources to help understand the algorithms better:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/minimum-spanning-tree\/\">GeeksforGeeks &#8211; Minimum Spanning Tree<\/a><\/li>\n<li><a href=\"https:\/\/www.khanacademy.org\/computing\/computer-science\/algorithms\/graph-representations\/prim-s-algorithm\/a\/prim-s-algorithm\">Khan Academy &#8211; Prim&#8217;s Algorithm<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will learn about an important concept in graph theory called Minimum Spanning Tree (MST). A Minimum Spanning Tree is a tree that includes all connected vertices while minimizing the total weight of the edges. This concept is utilized in various fields such as network design, road connectivity, and clustering. Problem Description The &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33776\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python coding test course, finding the 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":[145],"tags":[],"class_list":["post-33776","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python coding test course, finding the 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\/33776\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will learn about an important concept in graph theory called Minimum Spanning Tree (MST). A Minimum Spanning Tree is a tree that includes all connected vertices while minimizing the total weight of the edges. This concept is utilized in various fields such as network design, road connectivity, and clustering. Problem Description The &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, finding the minimum spanning tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33776\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:46:44+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\/33776\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33776\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, finding the minimum spanning tree\",\"datePublished\":\"2024-11-01T09:20:15+00:00\",\"dateModified\":\"2024-11-01T11:46:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33776\/\"},\"wordCount\":544,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33776\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33776\/\",\"name\":\"python coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:20:15+00:00\",\"dateModified\":\"2024-11-01T11:46:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33776\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33776\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33776\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python coding test course, finding the 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":"python coding test course, finding the 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\/33776\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will learn about an important concept in graph theory called Minimum Spanning Tree (MST). A Minimum Spanning Tree is a tree that includes all connected vertices while minimizing the total weight of the edges. This concept is utilized in various fields such as network design, road connectivity, and clustering. Problem Description The &hellip; \ub354 \ubcf4\uae30 \"python coding test course, finding the minimum spanning tree\"","og_url":"https:\/\/atmokpo.com\/w\/33776\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:20:15+00:00","article_modified_time":"2024-11-01T11:46:44+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\/33776\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33776\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, finding the minimum spanning tree","datePublished":"2024-11-01T09:20:15+00:00","dateModified":"2024-11-01T11:46:44+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33776\/"},"wordCount":544,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33776\/","url":"https:\/\/atmokpo.com\/w\/33776\/","name":"python coding test course, finding the minimum spanning tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:20:15+00:00","dateModified":"2024-11-01T11:46:44+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33776\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33776\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33776\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python coding test course, finding the 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\/33776","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=33776"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33776\/revisions"}],"predecessor-version":[{"id":33777,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33776\/revisions\/33777"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}