{"id":34112,"date":"2024-11-01T09:24:17","date_gmt":"2024-11-01T09:24:17","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34112"},"modified":"2024-11-01T10:58:34","modified_gmt":"2024-11-01T10:58:34","slug":"c-coding-test-course-dfs-and-bfs-programs-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34112\/","title":{"rendered":"C++ Coding Test Course, DFS and BFS Programs"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>This article describes how to solve C++ coding test problems using DFS (Depth First Search) and BFS (Breadth First Search) algorithms. It will explain the concepts and characteristics of each algorithm, along with specific code examples and a step-by-step problem-solving process based on them.<\/p>\n<\/header>\n<section>\n<h2>1. DFS (Depth First Search) Algorithm<\/h2>\n<p>DFS is one of the algorithms for traversing graphs, also known as depth-first search. It starts from a vertex of the graph and continues to explore deeper into adjacent vertices. When no further depth can be explored, it returns to the last visited vertex to explore other adjacent vertices.<\/p>\n<h3>1.1 Characteristics of DFS<\/h3>\n<ul>\n<li>Can be easily implemented using a stack or recursion.<\/li>\n<li>Visits every vertex once, so the time complexity is O(V + E), where V is the number of vertices and E is the number of edges.<\/li>\n<li>Requires additional memory to store information about visited vertices during execution.<\/li>\n<\/ul>\n<h3>1.2 Implementation of DFS<\/h3>\n<pre>\n                <code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;stack&gt;\n\nusing namespace std;\n\nvoid DFS(int start, vector<bool> &amp;visited, const vector<vector<int>&gt; &amp;adj) {\n    stack<int> s;\n    s.push(start);\n\n    while (!s.empty()) {\n        int node = s.top();\n        s.pop();\n\n        if (!visited[node]) {\n            visited[node] = true;\n            cout &lt;&lt; node &lt;&lt; ' ';\n\n            for (int i = adj[node].size() - 1; i &gt;= 0; --i) {\n                if (!visited[adj[node][i]]) {\n                    s.push(adj[node][i]);\n                }\n            }\n        }\n    }\n}\n                <\/int><\/vector<int><\/bool><\/code>\n            <\/pre>\n<\/section>\n<section>\n<h2>2. BFS (Breadth First Search) Algorithm<\/h2>\n<p>BFS is one of the algorithms for traversing graphs, also known as breadth-first search. It starts from a vertex of the graph and visits all adjacent vertices first before progressing deeper.<\/p>\n<h3>2.1 Characteristics of BFS<\/h3>\n<ul>\n<li>Implemented using a queue.<\/li>\n<li>Visits every vertex once, so the time complexity is O(V + E).<\/li>\n<li>Suitable for finding the shortest path.<\/li>\n<\/ul>\n<h3>2.2 Implementation of BFS<\/h3>\n<pre>\n                <code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n\nusing namespace std;\n\nvoid BFS(int start, vector<bool> &amp;visited, const vector<vector<int>&gt; &amp;adj) {\n    queue<int> q;\n    visited[start] = true;\n    q.push(start);\n\n    while (!q.empty()) {\n        int node = q.front();\n        q.pop();\n        cout &lt;&lt; node &lt;&lt; ' ';\n\n        for (int i = 0; i &lt; adj[node].size(); ++i) {\n            if (!visited[adj[node][i]]) {\n                visited[adj[node][i]] = true;\n                q.push(adj[node][i]);\n            }\n        }\n    }\n}\n                <\/int><\/vector<int><\/bool><\/code>\n            <\/pre>\n<\/section>\n<section>\n<h2>3. Problem: Graph Traversal (Application of DFS and BFS)<\/h2>\n<p>Problem description: Write a program to count the number of connected components in a given disconnected graph. Given the vertices and edges of the graph, use DFS and BFS algorithms to explore the sets of connected vertices and calculate the number of connected components.<\/p>\n<h3>3.1 Input Format<\/h3>\n<p>The first line will contain the number of vertices N (1 &lt;= N &lt;= 1000) and the number of edges M (1 &lt;= M &lt;= 100,000). The following M lines will contain the edge information.<\/p>\n<h3>3.2 Output Format<\/h3>\n<p>Output the number of connected components.<\/p>\n<h3>3.3 Example<\/h3>\n<pre>\n                <code>\nInput:\n5 3\n1 2\n2 3\n4 5\n\nOutput:\n2\n                <\/code>\n            <\/pre>\n<h3>3.4 Solution Process<\/h3>\n<p>This problem can be solved by using both DFS and BFS algorithms to count the number of connected components.<\/p>\n<ol>\n<li>Represent the graph in the form of an adjacency list.<\/li>\n<li>Initialize a visited array to record visited vertices.<\/li>\n<li>Iterate through each vertex; every time an unvisited vertex is found, use DFS or BFS to visit all vertices included with that vertex.<\/li>\n<li>Increase the count every time a connected component is found and finally print the count.<\/li>\n<\/ol>\n<h3>3.5 C++ Code Example<\/h3>\n<pre>\n                <code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;stack&gt;\n#include &lt;queue&gt;\n\nusing namespace std;\n\nvoid DFS(int start, vector<bool> &amp;visited, const vector<vector<int>&gt; &amp;adj) {\n    stack<int> s;\n    s.push(start);\n\n    while (!s.empty()) {\n        int node = s.top();\n        s.pop();\n\n        if (!visited[node]) {\n            visited[node] = true;\n\n            for (int i = 0; i &lt; adj[node].size(); ++i) {\n                if (!visited[adj[node][i]]) {\n                    s.push(adj[node][i]);\n                }\n            }\n        }\n    }\n}\n\nint main() {\n    int N, M;\n    cin &gt;&gt; N &gt;&gt; M;\n\n    vector<vector<int>&gt; adj(N + 1);\n    vector<bool> visited(N + 1, false);\n\n    for (int i = 0; i &lt; M; i++) {\n        int u, v;\n        cin &gt;&gt; u &gt;&gt; v;\n        adj[u].push_back(v);\n        adj[v].push_back(u); \/\/ Add in both directions for undirected graph.\n    }\n\n    int componentCount = 0;\n\n    for (int i = 1; i &lt;= N; i++) {\n        if (!visited[i]) {\n            DFS(i, visited, adj);\n            componentCount++;\n        }\n    }\n\n    cout &lt;&lt; componentCount &lt;&lt; endl;\n    \n    return 0;\n}\n                <\/bool><\/vector<int><\/int><\/vector<int><\/bool><\/code>\n            <\/pre>\n<\/section>\n<footer>\n<h3>4. Conclusion<\/h3>\n<p>In this article, we explored how to use DFS and BFS algorithms for graph traversal and solve related problems. We carefully examined the characteristics of each algorithm, how to implement them, and the problem-solving process. Since DFS and BFS are very important algorithms for graph-related problems, it is crucial to practice them repeatedly. I hope you continue to solve various problems and deepen your understanding of algorithms.<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes how to solve C++ coding test problems using DFS (Depth First Search) and BFS (Breadth First Search) algorithms. It will explain the concepts and characteristics of each algorithm, along with specific code examples and a step-by-step problem-solving process based on them. 1. DFS (Depth First Search) Algorithm DFS is one of the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34112\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, 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":[111],"tags":[],"class_list":["post-34112","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, 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\/34112\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This article describes how to solve C++ coding test problems using DFS (Depth First Search) and BFS (Breadth First Search) algorithms. It will explain the concepts and characteristics of each algorithm, along with specific code examples and a step-by-step problem-solving process based on them. 1. DFS (Depth First Search) Algorithm DFS is one of the &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, DFS and BFS Programs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34112\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:34+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=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, DFS and BFS Programs\",\"datePublished\":\"2024-11-01T09:24:17+00:00\",\"dateModified\":\"2024-11-01T10:58:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/\"},\"wordCount\":464,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34112\/\",\"name\":\"C++ Coding Test Course, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:17+00:00\",\"dateModified\":\"2024-11-01T10:58:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34112\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34112\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, 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":"C++ Coding Test Course, 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\/34112\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This article describes how to solve C++ coding test problems using DFS (Depth First Search) and BFS (Breadth First Search) algorithms. It will explain the concepts and characteristics of each algorithm, along with specific code examples and a step-by-step problem-solving process based on them. 1. DFS (Depth First Search) Algorithm DFS is one of the &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, DFS and BFS Programs\"","og_url":"https:\/\/atmokpo.com\/w\/34112\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:17+00:00","article_modified_time":"2024-11-01T10:58:34+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":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34112\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34112\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, DFS and BFS Programs","datePublished":"2024-11-01T09:24:17+00:00","dateModified":"2024-11-01T10:58:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34112\/"},"wordCount":464,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34112\/","url":"https:\/\/atmokpo.com\/w\/34112\/","name":"C++ Coding Test Course, DFS and BFS Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:17+00:00","dateModified":"2024-11-01T10:58:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34112\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34112\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34112\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, 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\/34112","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=34112"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34112\/revisions"}],"predecessor-version":[{"id":34113,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34112\/revisions\/34113"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}