{"id":33564,"date":"2024-11-01T09:17:53","date_gmt":"2024-11-01T09:17:53","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33564"},"modified":"2024-11-01T12:29:32","modified_gmt":"2024-11-01T12:29:32","slug":"%ea%b0%95%ec%a2%8c-on-python-coding-test-dfs-and-bfs-programs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33564\/","title":{"rendered":"Python Coding Test, DFS and BFS Programs"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Introduction<\/h2>\n<p>\n        Recently, many companies have been conducting coding tests using algorithms. In these coding tests,<br \/>\n        graph traversal algorithms like DFS (Depth-First Search) and BFS (Breadth-First Search) are frequently featured.<br \/>\n        This article will summarize the basic concepts of DFS and BFS, and based on this, present an algorithm problem<br \/>\n        and explain the problem-solving process in detail.\n    <\/p>\n<h2>2. Overview of DFS and BFS<\/h2>\n<h3>2.1 DFS (Depth-First Search)<\/h3>\n<p>\n        DFS is a method that starts from a vertex of the graph and explores as far as possible along each branch<br \/>\n        before backtracking to the last visited vertex to continue the exploration. It can be implemented using<br \/>\n        a stack data structure or a recursive function. The process of DFS is as follows:\n    <\/p>\n<ul>\n<li>Add the starting vertex to the stack and mark it as visited.<\/li>\n<li>Pop a vertex from the top of the stack and add an unvisited adjacent vertex to the stack.<\/li>\n<li>Repeat the above steps until all vertices have been explored.<\/li>\n<\/ul>\n<h3>2.2 BFS (Breadth-First Search)<\/h3>\n<p>\n        BFS is a method that starts from a vertex of the graph and explores all its adjacent vertices first,<br \/>\n        and then explores the adjacent vertices of those adjacent vertices. It is implemented using a queue data structure.<br \/>\n        The process of BFS is as follows:\n    <\/p>\n<ul>\n<li>Add the starting vertex to the queue and mark it as visited.<\/li>\n<li>Dequeue a vertex and add unvisited adjacent vertices of that vertex to the queue.<\/li>\n<li>Repeat the above steps until all vertices have been explored.<\/li>\n<\/ul>\n<h2>3. Introduction to Algorithm Problem<\/h2>\n<h3>Problem: Path Finding<\/h3>\n<p>\n        Here is a problem of finding a path using DFS and BFS.<br \/>\n        <strong>Problem Statement:<\/strong> Given a graph and two vertices A and B,<br \/>\n        check if there exists a path from A to B.<br \/>\n        The graph is provided in the form of an adjacency list.\n    <\/p>\n<pre>\n        input:\n        graph = {\n            'A': ['B', 'C'],\n            'B': ['D'],\n            'C': ['D', 'E'],\n            'D': ['F'],\n            'E': [],\n            'F': []\n        }\n        start = 'A'\n        end = 'F'\n        output: True (There exists a path from A to F)\n    <\/pre>\n<h2>4. Problem-Solving Process<\/h2>\n<h3>4.1 Finding Path using DFS<\/h3>\n<p>\n        The method to find a path using DFS is as follows.<br \/>\n        It can be implemented using a recursive function and a set data structure can be used to keep track of visited vertices.\n    <\/p>\n<pre>\n        def dfs(graph, start, end, visited):\n            if start == end:\n                return True\n            \n            visited.add(start)\n            \n            for neighbor in graph[start]:\n                if neighbor not in visited:\n                    if dfs(graph, neighbor, end, visited):\n                        return True\n            \n            return False\n        \n        graph = {\n            'A': ['B', 'C'],\n            'B': ['D'],\n            'C': ['D', 'E'],\n            'D': ['F'],\n            'E': [],\n            'F': []\n        }\n        start = 'A'\n        end = 'F'\n        visited = set()\n        result = dfs(graph, start, end, visited)\n    <\/pre>\n<p>\n        The above code allows for path finding using DFS.<br \/>\n        Initially, when the starting vertex A is given, it checks the visit status of that vertex and then explores the adjacent vertices<br \/>\n        using recursive calls. If it reaches vertex F and returns True, it signifies that a path exists.\n    <\/p>\n<h3>4.2 Finding Path using BFS<\/h3>\n<p>\n        The method to find a path using BFS involves maintaining a record of visited vertices using a queue.\n    <\/p>\n<pre>\n        from collections import deque\n        \n        def bfs(graph, start, end):\n            queue = deque([start])\n            visited = set([start])\n            \n            while queue:\n                vertex = queue.popleft()\n                \n                if vertex == end:\n                    return True\n                \n                for neighbor in graph[vertex]:\n                    if neighbor not in visited:\n                        visited.add(neighbor)\n                        queue.append(neighbor)\n                        \n            return False\n        \n        graph = {\n            'A': ['B', 'C'],\n            'B': ['D'],\n            'C': ['D', 'E'],\n            'D': ['F'],\n            'E': [],\n            'F': []\n        }\n        start = 'A'\n        end = 'F'\n        result = bfs(graph, start, end)\n    <\/pre>\n<p>\n        Using the above code implemented for BFS, we can explore each adjacent vertex one by one<br \/>\n        through the queue to check if there is a path from A to F.<br \/>\n        The queue is used during the exploration process, and it also keeps track of visited vertices.\n    <\/p>\n<h2>5. Conclusion<\/h2>\n<p>\n        DFS and BFS are distinct search methods, each suitable for various problems depending on their characteristics.<br \/>\n        If one wants to explore the depth of the structure first, DFS is advantageous, while BFS is preferable for finding the shortest path.<br \/>\n        This article demonstrated the implementation and differences between the two algorithms through a simple path-finding problem.<br \/>\n        Based on this foundation, one can attempt to tackle more complex algorithm problems.\n    <\/p>\n<h2>6. References<\/h2>\n<ul>\n<li>Data Structures and Algorithms in Python by Michael T. Goodrich<\/li>\n<li>Introduction to Algorithms by Thomas H. Cormen<\/li>\n<li>Python Documentation: <a href=\"https:\/\/docs.python.org\/3\/\">https:\/\/docs.python.org\/3\/<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Recently, many companies have been conducting coding tests using algorithms. In these coding tests, graph traversal algorithms like DFS (Depth-First Search) and BFS (Breadth-First Search) are frequently featured. This article will summarize the basic concepts of DFS and BFS, and based on this, present an algorithm problem and explain the problem-solving process in &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33564\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test, DFS and BFS Programs&#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-33564","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, DFS and BFS Programs - \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\/33564\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Recently, many companies have been conducting coding tests using algorithms. In these coding tests, graph traversal algorithms like DFS (Depth-First Search) and BFS (Breadth-First Search) are frequently featured. This article will summarize the basic concepts of DFS and BFS, and based on this, present an algorithm problem and explain the problem-solving process in &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test, DFS and BFS Programs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33564\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:17:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:29:32+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\/33564\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33564\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test, DFS and BFS Programs\",\"datePublished\":\"2024-11-01T09:17:53+00:00\",\"dateModified\":\"2024-11-01T12:29:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33564\/\"},\"wordCount\":544,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33564\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33564\/\",\"name\":\"Python Coding Test, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:17:53+00:00\",\"dateModified\":\"2024-11-01T12:29:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33564\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33564\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33564\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test, DFS and BFS Programs\"}]},{\"@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, DFS and BFS Programs - \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\/33564\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Recently, many companies have been conducting coding tests using algorithms. In these coding tests, graph traversal algorithms like DFS (Depth-First Search) and BFS (Breadth-First Search) are frequently featured. This article will summarize the basic concepts of DFS and BFS, and based on this, present an algorithm problem and explain the problem-solving process in &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test, DFS and BFS Programs\"","og_url":"https:\/\/atmokpo.com\/w\/33564\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:17:53+00:00","article_modified_time":"2024-11-01T12:29:32+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\/33564\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33564\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test, DFS and BFS Programs","datePublished":"2024-11-01T09:17:53+00:00","dateModified":"2024-11-01T12:29:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33564\/"},"wordCount":544,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33564\/","url":"https:\/\/atmokpo.com\/w\/33564\/","name":"Python Coding Test, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:17:53+00:00","dateModified":"2024-11-01T12:29:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33564\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33564\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33564\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test, DFS and BFS Programs"}]},{"@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\/33564","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=33564"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33564\/revisions"}],"predecessor-version":[{"id":38056,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33564\/revisions\/38056"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}