{"id":34552,"date":"2024-11-01T09:29:19","date_gmt":"2024-11-01T09:29:19","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34552"},"modified":"2024-11-01T11:40:47","modified_gmt":"2024-11-01T11:40:47","slug":"javascript-coding-test-course-finding-the-shortest-path","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34552\/","title":{"rendered":"JavaScript Coding Test Course, Finding the Shortest Path"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this lecture, we will cover one of the JavaScript coding test problems for employment, &#8216;Finding the Shortest Path.&#8217; This problem will be very helpful in understanding and utilizing the basic principles of algorithms and data structures. It is a common topic in coding interviews, and understanding priority queues and graph theory is essential.<\/p>\n<h2>Problem Description<\/h2>\n<p>We assume that we can represent the relationship between several cities and roads as a graph. Each city is represented as a node, and roads are represented as edges between nodes. We need to find the shortest path from one city to another.<\/p>\n<h3>Problem Definition<\/h3>\n<p>Given the number of cities <code>n<\/code> and the number of roads <code>m<\/code>, find the shortest path from a specific city <code>k<\/code> to all other cities. The information about the roads is given as a weight <code>w<\/code> connecting <code>u<\/code> and <code>v<\/code>. The weight represents the length of the road.<\/p>\n<h3>Input Format<\/h3>\n<pre><code>\nn, m, k\nu1, v1, w1\nu2, v2, w2\n...\num, vm, wm\n<\/code><\/pre>\n<h3>Output Format<\/h3>\n<pre><code>\nArray of the lengths of the shortest paths\n<\/code><\/pre>\n<p>For example, let&#8217;s assume the following input is given:<\/p>\n<pre><code>\n5 6 1\n1 2 2\n1 3 3\n2 3 1\n2 4 2\n3 4 4\n4 5 1\n<\/code><\/pre>\n<p>In the above example, the lengths of the shortest paths from city 1 to the other cities are as follows:<\/p>\n<pre><code>\n0\n2\n3\n4\n5\n<\/code><\/pre>\n<p>Since we start from city 1, the length of the shortest path includes 0 for itself and the shortest distances to the other cities.<\/p>\n<h2>Solution Approach<\/h2>\n<p>To solve this problem, we can use the <strong>Dijkstra Algorithm<\/strong>. The Dijkstra algorithm is used to find the shortest path from a specific starting node to all other nodes in a graph, utilizing a priority queue.<\/p>\n<h3>Description of the Dijkstra Algorithm<\/h3>\n<p>1) Initialization: Set the distance of the starting city to 0 and the distances of the remaining cities to infinity.<br \/>\n2) Storing visited cities: Keep track of visited cities separately to prevent duplicate calculations.<br \/>\n3) Using a priority queue: Select the city with the shortest distance to proceed. Update the distances of the neighboring cities.<br \/>\n4) Repeat: Continue the above process until all cities are visited.<\/p>\n<h2>JavaScript Implementation Example<\/h2>\n<p>Now, let&#8217;s implement the above algorithm in JavaScript.<\/p>\n<pre><code>\nfunction dijkstra(n, edges, start) {\n    const INF = Infinity;\n    const graph = Array.from({ length: n + 1 }, () =&gt; []);\n    const dist = Array(n + 1).fill(INF);\n    dist[start] = 0;\n    const pq = new MinPriorityQueue();\n\n    edges.forEach(([u, v, w]) =&gt; {\n        graph[u].push([v, w]);\n        graph[v].push([u, w]); \/\/ For undirected graphs\n    });\n\n    pq.enqueue(start, 0);\n\n    while (!pq.isEmpty()) {\n        const { element: current, priority: currentDist } = pq.dequeue();\n        \n        if (currentDist &gt; dist[current]) continue; \/\/ If already visited with the shortest distance\n        \n        for (const [neighbor, weight] of graph[current]) {\n            const newDist = currentDist + weight;\n            if (newDist &lt; dist[neighbor]) {\n                dist[neighbor] = newDist;\n                pq.enqueue(neighbor, newDist);\n            }\n        }\n    }\n    return dist.slice(1); \/\/ Exclude index 0\n}\n\nclass MinPriorityQueue {\n    constructor() {\n        this.items = [];\n    }\n\n    enqueue(element, priority) {\n        this.items.push({ element, priority });\n        this.items.sort((a, b) =&gt; a.priority - b.priority);\n    }\n\n    dequeue() {\n        return this.items.shift(); \/\/ Returns the one with the lowest priority\n    }\n\n    isEmpty() {\n        return this.items.length === 0;\n    }\n}\n\n\/\/ Example usage\nconst n = 5;\nconst edges = [\n    [1, 2, 2],\n    [1, 3, 3],\n    [2, 3, 1],\n    [2, 4, 2],\n    [3, 4, 4],\n    [4, 5, 1]\n];\nconst start = 1;\nconsole.log(dijkstra(n, edges, start)); \/\/ [0, 2, 3, 4, 5]\n<\/code><\/pre>\n<h2>Summary of the Problem Solving Process<\/h2>\n<p>1) Initialize the number of nodes and edges.<br \/>\n2) Represent the roads (edges) between cities as a graph.<br \/>\n3) Use the Dijkstra algorithm to calculate the shortest paths.<br \/>\n4) Output the results of the shortest paths.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we learned how to implement graph algorithms in JavaScript through the &#8216;Finding the Shortest Path&#8217; problem. The Dijkstra algorithm is simple to implement and can be applied to various problems, making it very useful. It often appears not only in coding tests but also in algorithm competitions, so be sure to master it.<\/p>\n<p>Additionally, in order to gain a deeper understanding of algorithms, it is good to study various variations of the shortest path algorithms (such as the Bellman-Ford algorithm, Floyd-Warshall algorithm, etc.). By understanding the differences and use cases of these algorithms, you will gain a broader perspective.<\/p>\n<p>I hope this is helpful, and I look forward to meeting you with more interesting topics in the next lecture. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this lecture, we will cover one of the JavaScript coding test problems for employment, &#8216;Finding the Shortest Path.&#8217; This problem will be very helpful in understanding and utilizing the basic principles of algorithms and data structures. It is a common topic in coding interviews, and understanding priority queues and graph theory is essential. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34552\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript 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":[141],"tags":[],"class_list":["post-34552","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript 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\/34552\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Finding the Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this lecture, we will cover one of the JavaScript coding test problems for employment, &#8216;Finding the Shortest Path.&#8217; This problem will be very helpful in understanding and utilizing the basic principles of algorithms and data structures. It is a common topic in coding interviews, and understanding priority queues and graph theory is essential. &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Finding the Shortest Path&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34552\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:29:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:40:47+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\/34552\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34552\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Finding the Shortest Path\",\"datePublished\":\"2024-11-01T09:29:19+00:00\",\"dateModified\":\"2024-11-01T11:40:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34552\/\"},\"wordCount\":486,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34552\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34552\/\",\"name\":\"JavaScript Coding Test Course, Finding the Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:29:19+00:00\",\"dateModified\":\"2024-11-01T11:40:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34552\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34552\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34552\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript 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":"JavaScript 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\/34552\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Finding the Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this lecture, we will cover one of the JavaScript coding test problems for employment, &#8216;Finding the Shortest Path.&#8217; This problem will be very helpful in understanding and utilizing the basic principles of algorithms and data structures. It is a common topic in coding interviews, and understanding priority queues and graph theory is essential. &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Finding the Shortest Path\"","og_url":"https:\/\/atmokpo.com\/w\/34552\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:29:19+00:00","article_modified_time":"2024-11-01T11:40:47+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\/34552\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34552\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Finding the Shortest Path","datePublished":"2024-11-01T09:29:19+00:00","dateModified":"2024-11-01T11:40:47+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34552\/"},"wordCount":486,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34552\/","url":"https:\/\/atmokpo.com\/w\/34552\/","name":"JavaScript Coding Test Course, Finding the Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:29:19+00:00","dateModified":"2024-11-01T11:40:47+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34552\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34552\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34552\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript 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\/34552","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=34552"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34552\/revisions"}],"predecessor-version":[{"id":34553,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34552\/revisions\/34553"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}