{"id":33612,"date":"2024-11-01T09:18:29","date_gmt":"2024-11-01T09:18:29","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33612"},"modified":"2024-11-01T11:47:25","modified_gmt":"2024-11-01T11:47:25","slug":"python-coding-test-course-breadth-first-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33612\/","title":{"rendered":"python coding test course, breadth-first search"},"content":{"rendered":"<p><body><\/p>\n<p>\n    Breadth-First Search (BFS) is a method for exploring graphs or trees, starting from a vertex and visiting all its adjacent vertices first, followed by the unvisited adjacent vertices. It can also be used as a shortest path search algorithm in certain cases. Today, we will proceed with a deep problem-solving approach along with the basic concept of BFS.\n<\/p>\n<h2>Problem: Numbering Complexes<\/h2>\n<h3>Problem Description<\/h3>\n<p>\n    In a given n x n array consisting of 0s and 1s, 1 represents the location of a house and 0 represents the absence of a house. Write a program that assigns each complex a unique number to distinguish all the houses. The size of a complex is defined as the number of houses belonging to it.\n<\/p>\n<h3>Input<\/h3>\n<pre>\nn (1 \u2264 n \u2264 25)\nAn n x n array composed of 0s and 1s\n<\/pre>\n<h3>Output<\/h3>\n<pre>\nPrint the size of each complex in ascending order\n<\/pre>\n<h2>Example Input<\/h2>\n<pre>\n7\n0 0 1 0 0 1 1\n0 0 1 0 1 1 1\n0 1 1 0 0 0 0\n0 0 0 0 1 0 0\n1 1 1 0 1 1 0\n0 0 0 0 0 0 0\n0 0 1 0 0 0 0\n<\/pre>\n<h2>Example Output<\/h2>\n<pre>\n1\n7\n<\/pre>\n<h2>Problem Approach<\/h2>\n<p>\n    To solve this problem, we will use the BFS algorithm to explore the graph. By traversing the provided 2D array, whenever we encounter a house (1), we will count all the connected houses using BFS to calculate the size of the complex. In the next step, we will explain the concept of BFS in detail.\n<\/p>\n<h2>Breadth-First Search (BFS) Concept<\/h2>\n<p>\n    BFS is implemented using a queue. It follows the following process:\n<\/p>\n<ol>\n<li>Add the starting node to the queue and mark it as visited.<\/li>\n<li>Remove a node from the queue and add all its adjacent nodes (that have not yet been visited) to the queue, marking them as visited.<\/li>\n<li>If the queue is not empty, return to step 2.<\/li>\n<\/ol>\n<h2>Coding Implementation<\/h2>\n<p>\n    I will write code to solve the problem using the BFS algorithm.\n<\/p>\n<pre><code>\nfrom collections import deque\n\ndef bfs(x, y, n, graph, visited):\n    queue = deque()\n    queue.append((x, y))\n    visited[x][y] = True\n\n    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n    size = 1  # store the size of the complex\n\n    while queue:\n        cx, cy = queue.popleft()  # current position\n        for dx, dy in directions:\n            nx, ny = cx + dx, cy + dy\n            if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and graph[nx][ny] == 1:\n                visited[nx][ny] = True\n                queue.append((nx, ny))\n                size += 1  # increase the count of connected houses\n\n    return size  # return the size of the complex\n\ndef find_complexes(n, graph):\n    visited = [[False] * n for _ in range(n)]\n    complexes = []\n\n    for i in range(n):\n        for j in range(n):\n            if graph[i][j] == 1 and not visited[i][j]:\n                size = bfs(i, j, n, graph, visited)\n                complexes.append(size)\n\n    return sorted(complexes)  # return the sizes in ascending order\n\n# Example input\nn = 7\ngraph = [\n    [0, 0, 1, 0, 0, 1, 1],\n    [0, 0, 1, 0, 1, 1, 1],\n    [0, 1, 1, 0, 0, 0, 0],\n    [0, 0, 0, 0, 1, 0, 0],\n    [1, 1, 1, 0, 1, 1, 0],\n    [0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 1, 0, 0, 0, 0]\n]\n\ncomplex_sizes = find_complexes(n, graph)\nprint(len(complex_sizes))  # print the number of complexes\nfor size in complex_sizes:\n    print(size)  # print the size of each complex\n<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n    The above code demonstrates how to calculate the size of complexes using BFS. The main functions are <code>find_complexes<\/code> and <code>bfs<\/code>.\n<\/p>\n<ul>\n<li><code>bfs(x, y, n, graph, visited)<\/code>: Executes BFS starting from the point (x, y) to calculate the size of the corresponding complex. It records each visited house using a queue and aggregates all connected houses.<\/li>\n<li><code>find_complexes(n, graph)<\/code>: Iterates through the given graph, finds houses (1), invokes BFS, and stores the calculated complex sizes in a list.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>\n    Breadth-First Search (BFS) is a useful search method that can be utilized in various fields. I hope you learned how to solve problems using BFS through this lecture. Understanding and applying the code during the problem-solving process will greatly assist you in your coding tests.\n<\/p>\n<h2>Additional Practice Problems<\/h2>\n<p>\n    Try solving the following problems:\n<\/p>\n<ul>\n<li>Finding the shortest path between specific nodes in the given graph<\/li>\n<li>Solving a maze problem using BFS<\/li>\n<\/ul>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Breadth-first_search\">Breadth-first search - Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/breadth-first-search-or-bfs-for-a-graph\/\">BFS for Graph - GeeksforGeeks<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Breadth-First Search (BFS) is a method for exploring graphs or trees, starting from a vertex and visiting all its adjacent vertices first, followed by the unvisited adjacent vertices. It can also be used as a shortest path search algorithm in certain cases. Today, we will proceed with a deep problem-solving approach along with the basic &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33612\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python coding test course, breadth-first search&#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-33612","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, breadth-first search - \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\/33612\/\" \/>\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, breadth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Breadth-First Search (BFS) is a method for exploring graphs or trees, starting from a vertex and visiting all its adjacent vertices first, followed by the unvisited adjacent vertices. It can also be used as a shortest path search algorithm in certain cases. Today, we will proceed with a deep problem-solving approach along with the basic &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, breadth-first search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33612\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:25+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, breadth-first search\",\"datePublished\":\"2024-11-01T09:18:29+00:00\",\"dateModified\":\"2024-11-01T11:47:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/\"},\"wordCount\":409,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33612\/\",\"name\":\"python coding test course, breadth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:29+00:00\",\"dateModified\":\"2024-11-01T11:47:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33612\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33612\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python coding test course, breadth-first search\"}]},{\"@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, breadth-first search - \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\/33612\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, breadth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Breadth-First Search (BFS) is a method for exploring graphs or trees, starting from a vertex and visiting all its adjacent vertices first, followed by the unvisited adjacent vertices. It can also be used as a shortest path search algorithm in certain cases. Today, we will proceed with a deep problem-solving approach along with the basic &hellip; \ub354 \ubcf4\uae30 \"python coding test course, breadth-first search\"","og_url":"https:\/\/atmokpo.com\/w\/33612\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:29+00:00","article_modified_time":"2024-11-01T11:47:25+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33612\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33612\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, breadth-first search","datePublished":"2024-11-01T09:18:29+00:00","dateModified":"2024-11-01T11:47:25+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33612\/"},"wordCount":409,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33612\/","url":"https:\/\/atmokpo.com\/w\/33612\/","name":"python coding test course, breadth-first search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:29+00:00","dateModified":"2024-11-01T11:47:25+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33612\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33612\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33612\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python coding test course, breadth-first search"}]},{"@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\/33612","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=33612"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33612\/revisions"}],"predecessor-version":[{"id":33613,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33612\/revisions\/33613"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}