{"id":34862,"date":"2024-11-01T09:32:51","date_gmt":"2024-11-01T09:32:51","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34862"},"modified":"2024-11-01T11:26:12","modified_gmt":"2024-11-01T11:26:12","slug":"swift-coding-test-course-finding-minimum-cost","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34862\/","title":{"rendered":"Swift Coding Test Course, Finding Minimum Cost"},"content":{"rendered":"<article>\n<header>\n<p>In this lecture, we will cover the &#8216;Minimum Cost Pathfinding&#8217; problem, which is frequently presented in coding tests, and examine step-by-step how to solve it using Swift. We will guide you on understanding the algorithm as well as efficiently solving the problem using Swift&#8217;s features.<\/p>\n<\/header>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n            This problem entails finding a path that minimizes the cost of traveling from one city to another.<br \/>\n            Each city is represented as a node in a graph, and cities are connected by roads.<br \/>\n            Each road has a different cost associated with traveling on it.<br \/>\n            The goal is to find the minimum cost to reach the destination city from the starting city.\n        <\/p>\n<p>\n            Here is an example of the problem:\n        <\/p>\n<blockquote>\n<p><strong>Input:<\/strong><\/p>\n<p>The number of cities N, and the number of roads M<\/p>\n<p>Following this, M lines will provide information about each road.<\/p>\n<p>The road information is given as (A, B, C), indicating that traveling from city A to city B requires a cost of C.<\/p>\n<p>Lastly, the starting city and destination city are provided.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>The minimum cost from the starting city to the destination city<\/p>\n<\/blockquote>\n<\/section>\n<section>\n<h2>Problem Solving Strategy<\/h2>\n<p>\n            To solve this problem, we will use Dijkstra&#8217;s algorithm.<br \/>\n            Dijkstra&#8217;s algorithm is a method for finding the shortest path in a weighted graph and is particularly effective for handling non-negative costs.<br \/>\n            The main idea of this algorithm is to select the nearest unvisited node based on the shortest path found so far.\n        <\/p>\n<p>\n            Here is how to solve the problem step by step using Dijkstra&#8217;s algorithm:\n        <\/p>\n<ol>\n<li>Construct the graph in the form of an adjacency list.<\/li>\n<li>Initialize an array to store the minimum cost to each city.<\/li>\n<li>Begin at the starting city and update the information for the adjacent cities.<\/li>\n<li>Use a priority queue to determine the next city to visit.<\/li>\n<li>Repeat until the destination city is reached.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Swift Code Implementation<\/h2>\n<p>\n            Now, let&#8217;s implement the code in Swift according to the above strategy.<br \/>\n            Here is an example code implementing Dijkstra&#8217;s algorithm:\n        <\/p>\n<pre><code>\nimport Foundation\n\nstruct Edge {\n    let to: Int\n    let cost: Int\n}\n\nfunc dijkstra(start: Int, end: Int, graph: [[Edge]]) -> Int {\n    var minCosts = Array(repeating: Int.max, count: graph.count)\n    var priorityQueue = [(cost: Int, vertex: Int)]()\n    minCosts[start] = 0\n    priorityQueue.append((0, start))\n    \n    while !priorityQueue.isEmpty {\n        let current = priorityQueue.removeFirst()\n        \n        if current.vertex == end {\n            return current.cost\n        }\n        \n        for edge in graph[current.vertex] {\n            let newCost = current.cost + edge.cost\n            if newCost < minCosts[edge.to] {\n                minCosts[edge.to] = newCost\n                priorityQueue.append((newCost, edge.to))\n            }\n        }\n        \n        priorityQueue.sort { $0.cost < $1.cost }\n    }\n    \n    return minCosts[end] == Int.max ? -1 : minCosts[end]\n}\n\n\/\/ Example of the graph\nlet N = 5  \/\/ Number of cities\nlet M = 7  \/\/ Number of roads\nvar graph = [[Edge]](repeating: [], count: N)\n\nlet roads: [(Int, Int, Int)] = [\n    (0, 1, 10),\n    (0, 2, 5),\n    (1, 2, 2),\n    (1, 3, 1),\n    (2, 1, 3),\n    (2, 3, 9),\n    (3, 4, 2)\n]\n\nfor road in roads {\n    graph[road.0].append(Edge(to: road.1, cost: road.2))\n    graph[road.1].append(Edge(to: road.0, cost: road.2)) \/\/ Assuming bidirectional roads\n}\n\n\/\/ Starting city and destination city\nlet start = 0\nlet end = 4\nlet minCost = dijkstra(start: start, end: end, graph: graph)\n\nprint(\"Minimum cost: \\(minCost)\") \/\/ Output: Minimum cost: 12\n        <\/code><\/pre>\n<\/section>\n<section>\n<h2>Code Explanation<\/h2>\n<p>\n            The Swift code above implements an algorithm that finds the minimum cost to travel from one city to another using several functions.<br \/>\n            The <code>dijkstra<\/code> function, starting from each city, receives connection information about the cities through the <code>graph<\/code> parameter and returns the minimum cost via the starting and destination cities.\n        <\/p>\n<p>\n<strong>1. Edge Structure:<\/strong><br \/>\n            Stores connection information between cities. <code>to<\/code> is the index of the connected city, and <code>cost<\/code> is the cost of that movement.\n        <\/p>\n<p>\n<strong>2. dijkstra Function:<\/strong><br \/>\n            This is the main logic for calculating the minimum cost. It explores the cities adjacent to the starting city and continuously updates the minimum cost.<br \/>\n            A priority queue is used to determine the next move to the closest city.\n        <\/p>\n<p>\n<strong>3. Graph Structure:<\/strong><br \/>\n            The graph is constructed in an adjacency list format and contains information about the roads connected to each city.\n        <\/p>\n<p>\n<strong>4. Result:<\/strong> Outputs the minimum cost from the given starting city to the destination city.\n        <\/p>\n<\/section>\n<section>\n<h2>Result Analysis<\/h2>\n<p>\n            In the code above, based on the input cities and road information, the minimum cost to travel from starting city 0 to destination city 4 is calculated as 12.<br \/>\n            This indicates that an appropriate connection and cost management were achieved.<br \/>\n            Therefore, we can conclude that the optimized path provided through Dijkstra's algorithm can indeed be found.\n        <\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n            In this lecture, we applied Dijkstra's algorithm to solve the minimum cost problem using Swift.<br \/>\n            We learned how to understand the algorithm and implement it in actual code.<br \/>\n            Understanding and utilizing the algorithm well will provide a foundation that can be useful in various coding tests and real-world applications.\n        <\/p>\n<p>\n            Additionally, to tackle more complex problems, it is advisable to learn various algorithms and continuously practice whenever encountering problems.<br \/>\n            In the next lecture, we will cover other algorithms to solve more problems and further enhance our skills.\n        <\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>In this lecture, we will cover the &#8216;Minimum Cost Pathfinding&#8217; problem, which is frequently presented in coding tests, and examine step-by-step how to solve it using Swift. We will guide you on understanding the algorithm as well as efficiently solving the problem using Swift&#8217;s features. Problem Description This problem entails finding a path that minimizes &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34862\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Finding Minimum Cost&#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":[129],"tags":[],"class_list":["post-34862","post","type-post","status-publish","format-standard","hentry","category-swift-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift Coding Test Course, Finding Minimum Cost - \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\/34862\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Coding Test Course, Finding Minimum Cost - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this lecture, we will cover the &#8216;Minimum Cost Pathfinding&#8217; problem, which is frequently presented in coding tests, and examine step-by-step how to solve it using Swift. We will guide you on understanding the algorithm as well as efficiently solving the problem using Swift&#8217;s features. Problem Description This problem entails finding a path that minimizes &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Finding Minimum Cost&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34862\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:32:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:12+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\/34862\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34862\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Finding Minimum Cost\",\"datePublished\":\"2024-11-01T09:32:51+00:00\",\"dateModified\":\"2024-11-01T11:26:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34862\/\"},\"wordCount\":628,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34862\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34862\/\",\"name\":\"Swift Coding Test Course, Finding Minimum Cost - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:32:51+00:00\",\"dateModified\":\"2024-11-01T11:26:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34862\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34862\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34862\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Finding Minimum Cost\"}]},{\"@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":"Swift Coding Test Course, Finding Minimum Cost - \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\/34862\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Finding Minimum Cost - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this lecture, we will cover the &#8216;Minimum Cost Pathfinding&#8217; problem, which is frequently presented in coding tests, and examine step-by-step how to solve it using Swift. We will guide you on understanding the algorithm as well as efficiently solving the problem using Swift&#8217;s features. Problem Description This problem entails finding a path that minimizes &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Finding Minimum Cost\"","og_url":"https:\/\/atmokpo.com\/w\/34862\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:32:51+00:00","article_modified_time":"2024-11-01T11:26:12+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\/34862\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34862\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Finding Minimum Cost","datePublished":"2024-11-01T09:32:51+00:00","dateModified":"2024-11-01T11:26:12+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34862\/"},"wordCount":628,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34862\/","url":"https:\/\/atmokpo.com\/w\/34862\/","name":"Swift Coding Test Course, Finding Minimum Cost - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:32:51+00:00","dateModified":"2024-11-01T11:26:12+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34862\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34862\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34862\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Finding Minimum Cost"}]},{"@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\/34862","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=34862"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34862\/revisions"}],"predecessor-version":[{"id":34863,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34862\/revisions\/34863"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}