{"id":33620,"date":"2024-11-01T09:18:36","date_gmt":"2024-11-01T09:18:36","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33620"},"modified":"2024-11-01T11:47:23","modified_gmt":"2024-11-01T11:47:23","slug":"python-coding-test-course-dijkstra","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33620\/","title":{"rendered":"python coding test course, Dijkstra"},"content":{"rendered":"<p><body><\/p>\n<p>Hello, everyone! Today, we will take a deep dive into the <strong>Dijkstra&#8217;s Algorithm<\/strong> and solve problems that are frequently asked in coding tests. Dijkstra&#8217;s Algorithm is an algorithm used to find the shortest path in a weighted graph, primarily used to solve network shortest path problems.<\/p>\n<h2>1. Overview of Dijkstra&#8217;s Algorithm<\/h2>\n<p>Dijkstra&#8217;s Algorithm, developed by Edsger Dijkstra in 1956, is a shortest path algorithm used to find the shortest path from a specific vertex to all other vertices. This algorithm does not allow negative weights and assumes that the graph is connected.<\/p>\n<h3>1.1 Algorithm Operating Principle<\/h3>\n<ul>\n<li>Select a starting vertex and set its distance to 0.<\/li>\n<li>Initialize the distance of all other vertices to infinity.<\/li>\n<li>Select the vertex with the shortest distance among the vertices whose minimum distance has not yet been determined.<\/li>\n<li>Update the distances of adjacent vertices based on the selected vertex.<\/li>\n<li>Repeat until the distances of all vertices are determined.<\/li>\n<\/ul>\n<h2>2. Problem Description<\/h2>\n<p>Now let&#8217;s apply Dijkstra&#8217;s Algorithm through a real problem.<\/p>\n<h3>Problem: Find the Shortest Path<\/h3>\n<p>Write a program to find the shortest path from a specific starting vertex to all other vertices given a graph provided as input.<\/p>\n<h4>Input Conditions<\/h4>\n<ul>\n<li>The first line contains the number of vertices <code>V<\/code> and the number of edges <code>E<\/code>. (1 \u2264 V \u2264 20, 1 \u2264 E \u2264 100)<\/li>\n<li>From the second line onwards, information about each edge is provided over E lines. Edge information is given in the form of the starting vertex <code>A<\/code>, ending vertex <code>B<\/code>, and weight <code>C<\/code>.<\/li>\n<li>The last line contains the starting vertex <code>K<\/code>.<\/li>\n<\/ul>\n<h4>Output Conditions<\/h4>\n<p>Output the shortest distance from the starting vertex <code>K<\/code> to all other vertices. If there is no path to any vertex, output <code>INF<\/code>.<\/p>\n<h2>3. Problem Solving Process<\/h2>\n<h3>3.1 Graph Representation<\/h3>\n<p>First, we represent the graph as an <strong>adjacency list<\/strong> based on the given edge information. In Python, we can use a dictionary to store each vertex and the other vertices connected to it along with their weights.<\/p>\n<pre><code>from collections import defaultdict\nimport heapq\n\ndef create_graph(edges):\n    graph = defaultdict(list)\n    for A, B, C in edges:\n        graph[A].append((B, C))\n    return graph\n<\/code><\/pre>\n<h3>3.2 Implementing Dijkstra&#8217;s Algorithm<\/h3>\n<p>Now we will implement Dijkstra&#8217;s Algorithm. This algorithm efficiently updates the shortest path found so far using a priority queue.<\/p>\n<pre><code>def dijkstra(graph, start, V):\n    distances = {vertex: float('inf') for vertex in range(1, V + 1)}\n    distances[start] = 0\n    priority_queue = [(0, start)]\n\n    while priority_queue:\n        current_distance, current_vertex = heapq.heappop(priority_queue)\n\n        if current_distance &gt; distances[current_vertex]:\n            continue\n\n        for neighbor, weight in graph[current_vertex]:\n            distance = current_distance + weight\n\n            if distance &lt; distances[neighbor]:\n                distances[neighbor] = distance\n                heapq.heappush(priority_queue, (distance, neighbor))\n\n    return distances\n<\/code><\/pre>\n<h3>3.3 Final Code<\/h3>\n<p>We compile all elements to write the final code, which also includes the input handling and result processing parts.<\/p>\n<pre><code>def main():\n    V, E = map(int, input().split())\n    edges = [tuple(map(int, input().split())) for _ in range(E)]\n    K = int(input())\n\n    graph = create_graph(edges)\n    distances = dijkstra(graph, K, V)\n\n    for vertex in range(1, V + 1):\n        if distances[vertex] == float('inf'):\n            print(\"INF\")\n        else:\n            print(distances[vertex])\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n<h2>4. Time Complexity<\/h2>\n<p>The time complexity of Dijkstra&#8217;s Algorithm varies depending on the data structures used. Typically, when using a priority queue, the time complexity is O((V + E) log V), where <code>V<\/code> is the number of vertices and <code>E<\/code> is the number of edges.<\/p>\n<h2>5. Conclusion<\/h2>\n<p>In this lecture, we learned how to solve the shortest path problem using Dijkstra&#8217;s Algorithm and handled real input. This algorithm can be utilized in various fields such as networking and game development, so it&#8217;s good to have a firm grasp of it.<\/p>\n<p>Practice various scenarios through additional problems. In the next lecture, we will look at another algorithm. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! Today, we will take a deep dive into the Dijkstra&#8217;s Algorithm and solve problems that are frequently asked in coding tests. Dijkstra&#8217;s Algorithm is an algorithm used to find the shortest path in a weighted graph, primarily used to solve network shortest path problems. 1. Overview of Dijkstra&#8217;s Algorithm Dijkstra&#8217;s Algorithm, developed by &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33620\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python coding test course, Dijkstra&#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-33620","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, Dijkstra - \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\/33620\/\" \/>\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, Dijkstra - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone! Today, we will take a deep dive into the Dijkstra&#8217;s Algorithm and solve problems that are frequently asked in coding tests. Dijkstra&#8217;s Algorithm is an algorithm used to find the shortest path in a weighted graph, primarily used to solve network shortest path problems. 1. Overview of Dijkstra&#8217;s Algorithm Dijkstra&#8217;s Algorithm, developed by &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, Dijkstra&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33620\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:23+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\/33620\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33620\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, Dijkstra\",\"datePublished\":\"2024-11-01T09:18:36+00:00\",\"dateModified\":\"2024-11-01T11:47:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33620\/\"},\"wordCount\":470,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33620\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33620\/\",\"name\":\"python coding test course, Dijkstra - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:36+00:00\",\"dateModified\":\"2024-11-01T11:47:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33620\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33620\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33620\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python coding test course, Dijkstra\"}]},{\"@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, Dijkstra - \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\/33620\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, Dijkstra - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, everyone! Today, we will take a deep dive into the Dijkstra&#8217;s Algorithm and solve problems that are frequently asked in coding tests. Dijkstra&#8217;s Algorithm is an algorithm used to find the shortest path in a weighted graph, primarily used to solve network shortest path problems. 1. Overview of Dijkstra&#8217;s Algorithm Dijkstra&#8217;s Algorithm, developed by &hellip; \ub354 \ubcf4\uae30 \"python coding test course, Dijkstra\"","og_url":"https:\/\/atmokpo.com\/w\/33620\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:36+00:00","article_modified_time":"2024-11-01T11:47:23+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\/33620\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33620\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, Dijkstra","datePublished":"2024-11-01T09:18:36+00:00","dateModified":"2024-11-01T11:47:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33620\/"},"wordCount":470,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33620\/","url":"https:\/\/atmokpo.com\/w\/33620\/","name":"python coding test course, Dijkstra - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:36+00:00","dateModified":"2024-11-01T11:47:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33620\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33620\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33620\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python coding test course, Dijkstra"}]},{"@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\/33620","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=33620"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33620\/revisions"}],"predecessor-version":[{"id":33621,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33620\/revisions\/33621"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}