{"id":34132,"date":"2024-11-01T09:24:34","date_gmt":"2024-11-01T09:24:34","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34132"},"modified":"2024-11-01T10:58:29","modified_gmt":"2024-11-01T10:58:29","slug":"c-coding-test-course-pathfinding-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34132\/","title":{"rendered":"C++ Coding Test Course, Pathfinding"},"content":{"rendered":"<p><body><\/p>\n<p>\n        In this course, we will implement an algorithm to find paths in graphs using C++.<br \/>\n        In particular, we aim to cover how to find the shortest path using BFS (Breadth-First Search).<br \/>\n        During this process, we will solve problems that frequently appear in actual coding tests, and<br \/>\n        we will explain the algorithm theory and implementation in detail.\n    <\/p>\n<h2>Problem Description<\/h2>\n<p>\n        You need to solve the problem of finding the shortest path between two points in a given graph.<br \/>\n        The graph consists of nodes and edges, with each node being connected to others.<br \/>\n        This problem is generally provided in the following format:\n    <\/p>\n<pre>\n    Input:\n    6 7\n    0 1\n    0 2\n    1 3\n    1 4\n    2 5\n    4 5\n    3 5\n    0 5\n\n    Output:\n    2\n    <\/pre>\n<p>\n        The first line indicates the number of nodes (vertices) and the number of edges,<br \/>\n        while the subsequent lines represent the connection information for each edge.<br \/>\n        The last line signifies the start and end nodes.\n    <\/p>\n<h2>Algorithm Theory<\/h2>\n<p>\n        Pathfinding in graphs can be performed using two methods: DFS (Depth-First Search) and BFS (Breadth-First Search).<br \/>\n        In this course, we will focus on finding the shortest path using BFS.<br \/>\n        BFS operates intuitively, proceeding by exploring all adjacent nodes using a queue.\n    <\/p>\n<h3>Principle of BFS<\/h3>\n<p>\n        The basic operating principle of BFS is as follows:\n    <\/p>\n<ol>\n<li>Add the starting node to the queue and mark it as visited.<\/li>\n<li>Dequeue a node and explore all its adjacent nodes.<\/li>\n<li>Add any unvisited adjacent nodes to the queue and mark them as visited.<\/li>\n<li>Repeat steps 2 and 3 until the queue is empty.<\/li>\n<\/ol>\n<p>\n        The reason BFS guarantees the shortest path is that it processes all nodes at the same level.<br \/>\n        By searching based on levels, the shortest path is ensured.\n    <\/p>\n<h2>Problem-Solving Process<\/h2>\n<p>\n        Now we will implement the algorithm necessary to solve the problem.<br \/>\n        Below is a C++ code example for problem-solving.\n    <\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\nusing namespace std;\n\nint bfs(vector<vector<int>&gt;&amp; graph, int start, int end) {\n    queue<int> q;\n    vector<bool> visited(graph.size(), false);\n    vector<int> distance(graph.size(), -1);\n    \n    q.push(start);\n    visited[start] = true;\n    distance[start] = 0;\n\n    while (!q.empty()) {\n        int node = q.front();\n        q.pop();\n        \n        for (int neighbor : graph[node]) {\n            if (!visited[neighbor]) {\n                visited[neighbor] = true;\n                distance[neighbor] = distance[node] + 1;\n                q.push(neighbor);\n                \n                if (neighbor == end) {\n                    return distance[neighbor];\n                }\n            }\n        }\n    }\n    return -1; \/\/ If there is no path \n}\n\nint main() {\n    int n, m;\n    cin &gt;&gt; n &gt;&gt; m;\n    vector<vector<int>&gt; graph(n);\n    \n    for (int i = 0; i &lt; m; i++) {\n        int u, v;\n        cin &gt;&gt; u &gt;&gt; v;\n        graph[u].push_back(v);\n        graph[v].push_back(u); \/\/ undirected graph\n    }\n\n    int start, end;\n    cin &gt;&gt; start &gt;&gt; end;\n\n    int result = bfs(graph, start, end);\n    cout &lt;&lt; result &lt;&lt; endl;\n\n    return 0;\n}\n    <\/vector<int><\/int><\/bool><\/int><\/vector<int><\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>\n        The code above takes the number of nodes and edges as input,<br \/>\n        constructs the graph based on the edge information, and then uses BFS to<br \/>\n        find the shortest path between the starting and ending nodes.\n    <\/p>\n<ol>\n<li>First, we include the necessary header files and declare <code>using namespace std;<\/code>.<\/li>\n<li>The <code>bfs<\/code> function takes the graph, starting node, and ending node as parameters.<\/li>\n<li>A queue is declared, and a vector <code>visited<\/code> to indicate visitation status is initialized.<\/li>\n<li>The starting node is added to the queue and marked as visited.<\/li>\n<li>The process continues while the queue is not empty, dequeuing nodes and exploring adjacent nodes.<\/li>\n<li>Unvisited adjacent nodes are added to the queue and their distance information is updated.<\/li>\n<li>When reaching the ending node, the corresponding distance is returned.<\/li>\n<li>The distance information is initialized to <code>-1<\/code>; when there is no path, it returns <code>-1<\/code>.<\/li>\n<\/ol>\n<h2>Results and Analysis<\/h2>\n<p>\n        Compiling the above code and executing it with the sample input will print<br \/>\n        the shortest path between the starting node and the ending node.<br \/>\n        Since BFS explores all adjacent nodes, it guarantees the shortest path.<br \/>\n        As the size of the graph increases, the search time increases proportionally, but<br \/>\n        BFS is a suitable algorithm for most graph problems.\n    <\/p>\n<h3>Complexity Analysis<\/h3>\n<p>\n        The time complexity of BFS is <code>O(V + E)<\/code>, where <code>V<\/code> is the number of nodes,<br \/>\n        and <code>E<\/code> is the number of edges. The memory complexity is also <code>O(V)<\/code>.<br \/>\n        This complexity can vary depending on the structure of the graph.<br \/>\n        In the worst case (e.g., complete graph), <code>E<\/code> can become <code>V^2<\/code>.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        In this course, we solved the problem of finding the shortest path in a graph using C++<br \/>\n        through the BFS algorithm. Understanding and implementing BFS is<br \/>\n        an important aspect of coding tests, so be sure to practice thoroughly.<br \/>\n        I hope you can build your skills through a wider variety of problems in the future.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this course, we will implement an algorithm to find paths in graphs using C++. In particular, we aim to cover how to find the shortest path using BFS (Breadth-First Search). During this process, we will solve problems that frequently appear in actual coding tests, and we will explain the algorithm theory and implementation in &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34132\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Pathfinding&#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-34132","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, Pathfinding - \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\/34132\/\" \/>\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, Pathfinding - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this course, we will implement an algorithm to find paths in graphs using C++. In particular, we aim to cover how to find the shortest path using BFS (Breadth-First Search). During this process, we will solve problems that frequently appear in actual coding tests, and we will explain the algorithm theory and implementation in &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Pathfinding&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34132\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:29+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\/34132\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34132\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Pathfinding\",\"datePublished\":\"2024-11-01T09:24:34+00:00\",\"dateModified\":\"2024-11-01T10:58:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34132\/\"},\"wordCount\":579,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34132\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34132\/\",\"name\":\"C++ Coding Test Course, Pathfinding - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:34+00:00\",\"dateModified\":\"2024-11-01T10:58:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34132\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34132\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34132\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Pathfinding\"}]},{\"@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, Pathfinding - \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\/34132\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Pathfinding - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this course, we will implement an algorithm to find paths in graphs using C++. In particular, we aim to cover how to find the shortest path using BFS (Breadth-First Search). During this process, we will solve problems that frequently appear in actual coding tests, and we will explain the algorithm theory and implementation in &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Pathfinding\"","og_url":"https:\/\/atmokpo.com\/w\/34132\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:34+00:00","article_modified_time":"2024-11-01T10:58:29+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\/34132\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34132\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Pathfinding","datePublished":"2024-11-01T09:24:34+00:00","dateModified":"2024-11-01T10:58:29+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34132\/"},"wordCount":579,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34132\/","url":"https:\/\/atmokpo.com\/w\/34132\/","name":"C++ Coding Test Course, Pathfinding - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:34+00:00","dateModified":"2024-11-01T10:58:29+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34132\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34132\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34132\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Pathfinding"}]},{"@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\/34132","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=34132"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34132\/revisions"}],"predecessor-version":[{"id":34133,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34132\/revisions\/34133"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}