{"id":34420,"date":"2024-11-01T09:27:56","date_gmt":"2024-11-01T09:27:56","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34420"},"modified":"2024-11-01T11:41:20","modified_gmt":"2024-11-01T11:41:20","slug":"javascript-coding-test-course-dfs-and-bfs-program","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34420\/","title":{"rendered":"JavaScript Coding Test Course, DFS and BFS Program"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>Welcome to the blog! Today, we will learn both the theory and practical implementation by solving problems using DFS (Depth-First Search) and BFS (Breadth-First Search) algorithms.<\/p>\n<\/header>\n<section>\n<h2>Problem Description<\/h2>\n<p>We will tackle the problem of finding the shortest path to a specific node in a given undirected graph. The graph is provided in the form of an adjacency list, and the shortest path can be found using BFS. DFS can be used to verify the existence of a path rather than finding the shortest path.<\/p>\n<h3>Problem<\/h3>\n<pre><code>\nInput:\n- n: number of vertices (1 \u2264 n \u2264 10^4)\n- edges: list of edges (undirected)\n- start: starting vertex\n- end: ending vertex\n\nOutput:\n- List of edges that form the shortest path from start to end\n            <\/code><\/pre>\n<h3>Example<\/h3>\n<p><strong>Input:<\/strong> <code>n = 5<\/code>, <code>edges = [[1, 2], [1, 3], [2, 4], [3, 4], [4, 5]]<\/code>, <code>start = 1<\/code>, <code>end = 5<\/code><\/p>\n<p><strong>Output:<\/strong> <code>[1, 2, 4, 5]<\/code> or <code>[1, 3, 4, 5]<\/code><\/p>\n<\/section>\n<section>\n<h2>Theory<\/h2>\n<h3>DFS (Depth-First Search)<\/h3>\n<p>\n                DFS is a method that starts from a node in the graph and explores as far as possible along each branch before backtracking. This algorithm can be implemented recursively or using a stack. The advantage of DFS is its low memory usage, making it useful when deep node exploration is needed.\n            <\/p>\n<h3>BFS (Breadth-First Search)<\/h3>\n<p>\n                BFS explores all neighboring nodes at the present depth prior to moving on to nodes at the next depth level. It is implemented using a queue and is particularly suitable for finding the shortest path. BFS is very useful in finding the shortest path, ensuring that it exists if a shortest path is available.\n            <\/p>\n<\/section>\n<section>\n<h2>Solution<\/h2>\n<h3>Step 1: Build the Graph<\/h3>\n<p>First, let&#8217;s build the graph using an adjacency list.<\/p>\n<pre><code class=\"language-javascript\">\nfunction buildGraph(n, edges) {\n    const graph = Array.from({ length: n + 1 }, () =&gt; []);\n    for (const [u, v] of edges) {\n        graph[u].push(v);\n        graph[v].push(u); \/\/ Since it's an undirected graph, add both ways\n    }\n    return graph;\n}\n\nconst n = 5;\nconst edges = [[1, 2], [1, 3], [2, 4], [3, 4], [4, 5]];\nconst graph = buildGraph(n, edges);\nconsole.log(graph);\n            <\/code><\/pre>\n<h3>Step 2: Implement BFS<\/h3>\n<p>Now, let&#8217;s find the shortest path from the starting vertex to the ending vertex using BFS.<\/p>\n<pre><code>\nfunction bfs(graph, start, end) {\n    const queue = [[start]];\n    const visited = new Set([start]);\n\n    while (queue.length &gt; 0) {\n        const path = queue.shift();\n        const node = path[path.length - 1];\n\n        if (node === end) {\n            return path; \/\/ Return the shortest path found\n        }\n\n        for (const neighbor of graph[node]) {\n            if (!visited.has(neighbor)) {\n                visited.add(neighbor);\n                queue.push([...path, neighbor]); \/\/ Add neighboring node to current path\n            }\n        }\n    }\n    return []; \/\/ Return empty array if no path found\n}\n\nconst start = 1,\n      end = 5;\n\nconst result = bfs(graph, start, end);\nconsole.log(result);\n            <\/code><\/pre>\n<h3>Step 3: Implement DFS<\/h3>\n<p>Now, let&#8217;s use DFS to check the existence of a path and how to find that path.<\/p>\n<pre><code>\nfunction dfs(graph, start, end, visited = new Set(), path = []) {\n    visited.add(start);\n    path.push(start);\n    \n    if (start === end) {\n        return path; \/\/ Return when the path is found\n    }\n\n    for (const neighbor of graph[start]) {\n        if (!visited.has(neighbor)) {\n            const resultPath = dfs(graph, neighbor, end, visited, [...path]);\n            if (resultPath.length &gt; 0) {\n                return resultPath; \/\/ Return when a path is discovered\n            }\n        }\n    }\n    return []; \/\/ Return empty array if no path found\n}\n\nconst dfsResult = dfs(graph, start, end);\nconsole.log(dfsResult);\n            <\/code><\/pre>\n<\/section>\n<footer>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we implemented DFS and BFS algorithms using JavaScript and learned how to solve graph problems through them. While BFS is useful for finding the shortest path, DFS is suitable for path exploration, indicating that both algorithms can be used according to different situations. In the next lecture, we will further develop this theory and challenge ourselves to solve various graph problems.<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the blog! Today, we will learn both the theory and practical implementation by solving problems using DFS (Depth-First Search) and BFS (Breadth-First Search) algorithms. Problem Description We will tackle the problem of finding the shortest path to a specific node in a given undirected graph. The graph is provided in the form of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34420\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, DFS and BFS Program&#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-34420","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, DFS and BFS Program - \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\/34420\/\" \/>\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, DFS and BFS Program - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Welcome to the blog! Today, we will learn both the theory and practical implementation by solving problems using DFS (Depth-First Search) and BFS (Breadth-First Search) algorithms. Problem Description We will tackle the problem of finding the shortest path to a specific node in a given undirected graph. The graph is provided in the form of &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, DFS and BFS Program&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34420\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:20+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\/34420\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34420\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, DFS and BFS Program\",\"datePublished\":\"2024-11-01T09:27:56+00:00\",\"dateModified\":\"2024-11-01T11:41:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34420\/\"},\"wordCount\":330,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34420\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34420\/\",\"name\":\"JavaScript Coding Test Course, DFS and BFS Program - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:56+00:00\",\"dateModified\":\"2024-11-01T11:41:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34420\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34420\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34420\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, DFS and BFS Program\"}]},{\"@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, DFS and BFS Program - \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\/34420\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, DFS and BFS Program - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Welcome to the blog! Today, we will learn both the theory and practical implementation by solving problems using DFS (Depth-First Search) and BFS (Breadth-First Search) algorithms. Problem Description We will tackle the problem of finding the shortest path to a specific node in a given undirected graph. The graph is provided in the form of &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, DFS and BFS Program\"","og_url":"https:\/\/atmokpo.com\/w\/34420\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:56+00:00","article_modified_time":"2024-11-01T11:41:20+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\/34420\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34420\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, DFS and BFS Program","datePublished":"2024-11-01T09:27:56+00:00","dateModified":"2024-11-01T11:41:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34420\/"},"wordCount":330,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34420\/","url":"https:\/\/atmokpo.com\/w\/34420\/","name":"JavaScript Coding Test Course, DFS and BFS Program - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:56+00:00","dateModified":"2024-11-01T11:41:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34420\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34420\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34420\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, DFS and BFS Program"}]},{"@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\/34420","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=34420"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34420\/revisions"}],"predecessor-version":[{"id":34421,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34420\/revisions\/34421"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}