{"id":33760,"date":"2024-11-01T09:20:03","date_gmt":"2024-11-01T09:20:03","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33760"},"modified":"2024-11-01T11:46:48","modified_gmt":"2024-11-01T11:46:48","slug":"python-coding-test-course-finding-the-shortest-path","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33760\/","title":{"rendered":"python coding test course, finding the shortest path"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this article, I would like to talk about the preparation process for algorithm exams for employment. In particular, we will delve deeply into the problem of <strong>finding the shortest path<\/strong>. This problem can be encountered in various situations, and the shortest path algorithm, a fundamental concept in graph theory, is frequently asked in job interviews.<\/p>\n<h2>Definition of the Shortest Path Problem<\/h2>\n<p>The shortest path problem is the problem of finding the shortest path between two nodes in a given graph. Here, the graph is a mathematical representation of networks such as roads and communication networks, composed of vertices and edges. We can use various algorithms to solve this problem, and we will focus on Dijkstra&#8217;s algorithm here.<\/p>\n<h3>Understanding Dijkstra&#8217;s Algorithm<\/h3>\n<p>Dijkstra&#8217;s algorithm is an efficient algorithm for finding the shortest path from a single vertex to all other vertices in a weighted graph. The algorithm works as follows:<\/p>\n<ol>\n<li>Choose a starting vertex and set the distance for that vertex to 0.<\/li>\n<li>Update the distances of the vertices connected to the starting vertex.<\/li>\n<li>Select the vertex with the shortest updated distance and mark it as &#8216;visited&#8217;.<\/li>\n<li>Repeat the process of selecting vertices connected to the visited vertex and updating the distances.<\/li>\n<li>Repeat this process until all vertices are visited.<\/li>\n<\/ol>\n<h2>Problem Statement<\/h2>\n<p>In this lecture, we will address the problem of finding the shortest path between two vertices when given a graph. The problem can be summarized in the following form:<\/p>\n<div class=\"example\">\n<h3>Problem: Finding the Shortest Path<\/h3>\n<p>Given a directed graph and its weights, determine the shortest path distance from vertex <code>S<\/code> to vertex <code>T<\/code>.<\/p>\n<p>Input:<\/p>\n<pre><code>5 7\n0 1 2\n0 2 3\n1 2 1\n1 3 5\n2 4 2\n3 4 1\n4 3 3\n0 4<\/code><\/pre>\n<p>Output: <code>5<\/code><\/p>\n<p>Explanation: The paths from vertex 0 to vertex 4 are 0\u21921\u21922\u21924 or 0\u21922\u21924, and the shortest distance of the two paths is 5.<\/p>\n<\/div>\n<h2>Problem-Solving Process<\/h2>\n<h3>1. Setting Up the Graph Structure<\/h3>\n<p>First, to solve the above problem, we need to set up the graph in an appropriate data structure. Generally, using an adjacency list is efficient in terms of memory and processing speed. In Python, this can be implemented using a dictionary.<\/p>\n<h3>2. Implementing Dijkstra&#8217;s Algorithm<\/h3>\n<p>Next, the libraries needed to implement Dijkstra&#8217;s algorithm are as follows:<\/p>\n<pre><code>import heapq\nimport sys\nfrom collections import defaultdict\n<\/code><\/pre>\n<p>Here, <code>heapq<\/code> is used for the priority queue, and <code>defaultdict<\/code> is used to easily implement the adjacency list.<\/p>\n<h3>3. Example Python Code<\/h3>\n<p>Now, let&#8217;s write the complete code to find the shortest path:<\/p>\n<pre><code>\ndef dijkstra(graph, start, end):\n    # Initialize distances\n    distance = {node: float('inf') for node in graph}\n    distance[start] = 0\n    priority_queue = [(0, start)]  # (distance, vertex)\n\n    while priority_queue:\n        current_distance, current_node = heapq.heappop(priority_queue)\n\n        # Ignore if a greater value than the current node's distance comes in\n        if current_distance > distance[current_node]:\n            continue\n\n        # Visit neighboring nodes\n        for neighbor, weight in graph[current_node]:\n            distance_via_neighbor = current_distance + weight\n            if distance_via_neighbor < distance[neighbor]:\n                distance[neighbor] = distance_via_neighbor\n                heapq.heappush(priority_queue, (distance_via_neighbor, neighbor))\n\n    return distance[end]\n\n# Define the graph\ngraph = defaultdict(list)\nedges = [\n    (0, 1, 2),\n    (0, 2, 3),\n    (1, 2, 1),\n    (1, 3, 5),\n    (2, 4, 2),\n    (3, 4, 1),\n    (4, 3, 3)\n]\n\nfor u, v, w in edges:\n    graph[u].append((v, w))\n\n# Calculate the shortest path\nstart, end = 0, 4\nresult = dijkstra(graph, start, end)\nprint(result)  # Output result: 5\n<\/code><\/pre>\n<h2>4. Code Explanation<\/h2>\n<p>The code above uses Dijkstra's algorithm to find the shortest path in the given graph. Each comment allows you to understand the code step by step, but to summarize briefly:<\/p>\n<ol>\n<li>Set the graph in dictionary form.<\/li>\n<li>Initialize the distance of the starting vertex and add it to the priority queue.<\/li>\n<li>Pop a vertex from the queue, investigate its neighbors, update distances, and add them to the queue.<\/li>\n<li>After calculating the distances for all vertices, return the distance to the final destination vertex.<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>The shortest path finding algorithm is one of the important topics in computer science, which may seem simple but can actually be modified in various ways. In addition to Dijkstra's algorithm, there are several methods available, including Bellman-Ford and A* algorithms. In this article, we explored the most basic and widely used Dijkstra's algorithm.<\/p>\n<p>In the future, we will continue to tackle various algorithm problems and delve into more advanced topics. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this article, I would like to talk about the preparation process for algorithm exams for employment. In particular, we will delve deeply into the problem of finding the shortest path. This problem can be encountered in various situations, and the shortest path algorithm, a fundamental concept in graph theory, is frequently asked in &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33760\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python coding test course, finding the shortest path&#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-33760","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 shortest path - \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\/33760\/\" \/>\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 shortest path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this article, I would like to talk about the preparation process for algorithm exams for employment. In particular, we will delve deeply into the problem of finding the shortest path. This problem can be encountered in various situations, and the shortest path algorithm, a fundamental concept in graph theory, is frequently asked in &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, finding the shortest path&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33760\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:46:48+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\/33760\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33760\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, finding the shortest path\",\"datePublished\":\"2024-11-01T09:20:03+00:00\",\"dateModified\":\"2024-11-01T11:46:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33760\/\"},\"wordCount\":548,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33760\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33760\/\",\"name\":\"python coding test course, finding the shortest path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:20:03+00:00\",\"dateModified\":\"2024-11-01T11:46:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33760\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33760\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33760\/#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 shortest path\"}]},{\"@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 shortest path - \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\/33760\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, finding the shortest path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this article, I would like to talk about the preparation process for algorithm exams for employment. In particular, we will delve deeply into the problem of finding the shortest path. This problem can be encountered in various situations, and the shortest path algorithm, a fundamental concept in graph theory, is frequently asked in &hellip; \ub354 \ubcf4\uae30 \"python coding test course, finding the shortest path\"","og_url":"https:\/\/atmokpo.com\/w\/33760\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:20:03+00:00","article_modified_time":"2024-11-01T11:46:48+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\/33760\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33760\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, finding the shortest path","datePublished":"2024-11-01T09:20:03+00:00","dateModified":"2024-11-01T11:46:48+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33760\/"},"wordCount":548,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33760\/","url":"https:\/\/atmokpo.com\/w\/33760\/","name":"python coding test course, finding the shortest path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:20:03+00:00","dateModified":"2024-11-01T11:46:48+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33760\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33760\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33760\/#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 shortest path"}]},{"@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\/33760","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=33760"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33760\/revisions"}],"predecessor-version":[{"id":33761,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33760\/revisions\/33761"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}