{"id":33860,"date":"2024-11-01T09:21:20","date_gmt":"2024-11-01T09:21:20","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33860"},"modified":"2024-11-01T10:55:47","modified_gmt":"2024-11-01T10:55:47","slug":"c-coding-test-course-dfs-and-bfs-programs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33860\/","title":{"rendered":"C# Coding Test Course, DFS and BFS Programs"},"content":{"rendered":"<p><body><\/p>\n<p>To get a job as a software developer, algorithm tests are important. In particular, DFS (Depth-First Search) and BFS (Breadth-First Search) are fundamental concepts in graph traversal and are utilized in many problems. This article will introduce problems that use DFS and BFS algorithms and explain the problem-solving process in detail.<\/p>\n<h2>Problem Description<\/h2>\n<p>First, let\u2019s assume there is a graph as follows. This graph consists of vertices (Vertex) and edges (Edge). We will solve the problem of exploring this graph to find a specific vertex.<\/p>\n<h3>Problem<\/h3>\n<p>Given a graph, implement the DFS and BFS algorithms to find a specific vertex from a given starting vertex.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>Number of vertices: <code>n<\/code> (2 \u2264 n \u2264 100)<\/li>\n<li>Number of edges: <code>m<\/code> (1 \u2264 m \u2264 300)<\/li>\n<li>List of edges: Each edge consists of two vertices (u, v)<\/li>\n<li>Starting vertex: <code>start<\/code><\/li>\n<li>Vertex to find: <code>target<\/code><\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>If it is possible to reach the target vertex from the starting vertex, output <code>true<\/code>; otherwise, output <code>false<\/code>.<\/p>\n<h2>Problem Solving<\/h2>\n<p>This problem can be solved using the DFS and BFS algorithms. Both methods are widely used techniques for graph traversal. First, let\u2019s examine the principles of both algorithms.<\/p>\n<h3>DFS (Depth-First Search)<\/h3>\n<p>DFS is depth-first traversal, where one node is deeply explored, and if there are no more nodes to explore, it backtracks to explore other nodes. It can be implemented using a stack.<\/p>\n<h3>BFS (Breadth-First Search)<\/h3>\n<p>BFS is breadth-first traversal, where all neighboring nodes of one node are explored before moving on to the next neighboring node. It can be implemented using a queue data structure.<\/p>\n<h2>Implementation<\/h2>\n<p>Now, let\u2019s implement the DFS and BFS algorithms in C#.<\/p>\n<h3>1. Graph Representation<\/h3>\n<p>A graph can be represented in an adjacency list form. For each node, the connected nodes are stored in a list.<\/p>\n<pre><code class=\"language-csharp\">\npublic class Graph\n{\n    private int vertices; \/\/ Number of vertices\n    private List<int>[] adjList; \/\/ Adjacency list\n\n    public Graph(int n)\n    {\n        this.vertices = n;\n        adjList = new List<int>[n];\n        for (int i = 0; i < n; i++)\n        {\n            adjList[i] = new List<int>();\n        }\n    }\n\n    \/\/ Method to add an edge\n    public void AddEdge(int u, int v)\n    {\n        adjList[u].Add(v);\n        adjList[v].Add(u); \/\/ Undirected graph\n    }\n\n    public List<int> GetNeighbors(int vertex)\n    {\n        return adjList[vertex];\n    }\n}\n<\/int><\/int><\/int><\/int><\/code><\/pre>\n<h3>2. DFS Implementation<\/h3>\n<p>DFS can be implemented recursively as follows.<\/p>\n<pre><code class=\"language-csharp\">\npublic class DFS\n{\n    private bool[] visited;\n    private Graph graph;\n\n    public DFS(Graph g)\n    {\n        this.graph = g;\n        this.visited = new bool[g.vertices];\n    }\n\n    public bool Search(int start, int target)\n    {\n        \/\/ Mark the current node as visited\n        if (start == target) return true;\n        visited[start] = true;\n\n        foreach (int neighbor in graph.GetNeighbors(start))\n        {\n            if (!visited[neighbor] && Search(neighbor, target))\n            {\n                return true; \/\/ Target found\n            }\n        }\n        return false; \/\/ Target not found\n    }\n}\n<\/code><\/pre>\n<h3>3. BFS Implementation<\/h3>\n<p>BFS is implemented by using a queue to explore the next nodes.<\/p>\n<pre><code class=\"language-csharp\">\nusing System.Collections.Generic;\n\npublic class BFS\n{\n    private bool[] visited;\n    private Graph graph;\n\n    public BFS(Graph g)\n    {\n        this.graph = g;\n        this.visited = new bool[g.vertices];\n    }\n\n    public bool Search(int start, int target)\n    {\n        Queue<int> queue = new Queue<int>();\n        queue.Enqueue(start);\n        visited[start] = true;\n\n        while (queue.Count > 0)\n        {\n            int current = queue.Dequeue();\n            if (current == target) return true;\n\n            foreach (int neighbor in graph.GetNeighbors(current))\n            {\n                if (!visited[neighbor])\n                {\n                    queue.Enqueue(neighbor);\n                    visited[neighbor] = true;\n                }\n            }\n        }\n        return false; \/\/ Target not found\n    }\n}\n<\/int><\/int><\/code><\/pre>\n<h2>Test Cases<\/h2>\n<p>Now let&#8217;s create a simple graph to test the implemented algorithms.<\/p>\n<pre><code class=\"language-csharp\">\npublic class MainClass\n{\n    public static void Main(string[] args)\n    {\n        Graph graph = new Graph(5);\n        graph.AddEdge(0, 1);\n        graph.AddEdge(0, 2);\n        graph.AddEdge(1, 3);\n        graph.AddEdge(1, 4);\n        graph.AddEdge(2, 4);\n\n        DFS dfs = new DFS(graph);\n        BFS bfs = new BFS(graph);\n\n        Console.WriteLine(\"DFS: \" + dfs.Search(0, 4)); \/\/ true\n        Console.WriteLine(\"BFS: \" + bfs.Search(0, 4)); \/\/ true\n\n        Console.WriteLine(\"DFS (No path): \" + dfs.Search(0, 5)); \/\/ false\n        Console.WriteLine(\"BFS (No path): \" + bfs.Search(0, 5)); \/\/ false\n    }\n}\n<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>In this lecture, we examined how to solve graph traversal problems using DFS and BFS algorithms. Each traversal method has its strengths and weaknesses, and the appropriate method should be chosen based on the nature of the problem. DFS is advantageous for problems that require less memory usage and deep exploration but may take longer to find a path. On the other hand, BFS is advantageous for finding the shortest path but may use more memory.<\/p>\n<p>Through this article, I hope you understand DFS and BFS and develop the ability to handle various coding test problems.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\">Geeks for Geeks<\/a><\/li>\n<li><a href=\"https:\/\/leetcode.com\">LeetCode<\/a><\/li>\n<li>Books on algorithms<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Coding tests are an important process for evaluating a developer&#8217;s basic capabilities. The DFS and BFS algorithms are fundamental methods for graph traversal, making them excellent for solidifying your foundational skills. I hope you will learn various algorithms and data structures and improve your skills through practice.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To get a job as a software developer, algorithm tests are important. In particular, DFS (Depth-First Search) and BFS (Breadth-First Search) are fundamental concepts in graph traversal and are utilized in many problems. This article will introduce problems that use DFS and BFS algorithms and explain the problem-solving process in detail. Problem Description First, let\u2019s &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33860\/\" 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":[90],"tags":[],"class_list":["post-33860","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"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\/33860\/\" \/>\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=\"To get a job as a software developer, algorithm tests are important. In particular, DFS (Depth-First Search) and BFS (Breadth-First Search) are fundamental concepts in graph traversal and are utilized in many problems. This article will introduce problems that use DFS and BFS algorithms and explain the problem-solving process in detail. Problem Description First, let\u2019s &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, DFS and BFS Programs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33860\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:21:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:55:47+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, DFS and BFS Programs\",\"datePublished\":\"2024-11-01T09:21:20+00:00\",\"dateModified\":\"2024-11-01T10:55:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/\"},\"wordCount\":481,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33860\/\",\"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:21:20+00:00\",\"dateModified\":\"2024-11-01T10:55:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33860\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33860\/#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\/33860\/","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":"To get a job as a software developer, algorithm tests are important. In particular, DFS (Depth-First Search) and BFS (Breadth-First Search) are fundamental concepts in graph traversal and are utilized in many problems. This article will introduce problems that use DFS and BFS algorithms and explain the problem-solving process in detail. Problem Description First, let\u2019s &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, DFS and BFS Programs\"","og_url":"https:\/\/atmokpo.com\/w\/33860\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:21:20+00:00","article_modified_time":"2024-11-01T10:55:47+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33860\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33860\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, DFS and BFS Programs","datePublished":"2024-11-01T09:21:20+00:00","dateModified":"2024-11-01T10:55:47+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33860\/"},"wordCount":481,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33860\/","url":"https:\/\/atmokpo.com\/w\/33860\/","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:21:20+00:00","dateModified":"2024-11-01T10:55:47+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33860\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33860\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33860\/#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\/33860","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=33860"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33860\/revisions"}],"predecessor-version":[{"id":33861,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33860\/revisions\/33861"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}