{"id":11509,"date":"2022-12-29T10:54:06","date_gmt":"2022-12-29T10:54:06","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11509"},"modified":"2023-01-02T13:18:19","modified_gmt":"2023-01-02T13:18:19","slug":"dfs-program-in-c","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/","title":{"rendered":"DFS Program in C"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\" alt=\"\" \/><\/p>\n<p>In this article, we will learn what is DFS, and how DFS is different from BFS, we will also how DFS works with a dry run of an example, and we will also see how to write a DFS program in c.<\/p>\n<h2>What is DFS?<\/h2>\n<p>DFS stands for Depth First Search and it is used to traverse all the nodes of the graph. With this technique, first, we pick and node and after that, we traverse through all the possible nodes in that path or branch, and then we will do backtracking.<\/p>\n<p>Backtracking means when the DFS algorithm reaches a particular node that does not have any neighbors to visit. When it reaches a node that does not have any neighbors after that, it will backtrack to its previous node. Let&#8217;s see an example of DFS.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624771-dfs%20program%20in%20c1.png\" alt=\"\" \/><\/p>\n<p>In the above image, the DFS algorithm will start from the source node which is 1 and it has four neighbors (5, 3, 4, 2) we can choose anyone of them. If we choose 5 then our vised nodes are 1 and 5. Now, 5 does not have any neighbors so we will backtrack to its previous node which is 1 and now 1 has three non-visited neighbors (3, 4, 2). If we choose 3 then our path will become 1,5,3. Now, 3 has one non-visited neighbor(4) so we can choose only that node, and our path becomes 1,5,3,4. Now, 4 does not have any non-visited nodes thus we have to backtrack to the previous node 3 and it also does not have any non-visited neighbors thus we will backtrack to previous node 1, and 1 has only one non-visited node (2). We will go to 2 and our path will become 1,5,3,4,2. So the path becomes 1-&gt;5-&gt;3-&gt;4-&gt;2 if we use DFS traversal.<\/p>\n<h2>How DFS is different from BFS?<\/h2>\n<p>BFS stands for Breadth First Search. In this technique, we traverse through a graph level vise. We need to use the Queue data structure to perform BFS traversal. Let\u2019s see how BFS traversal works using an example.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624771-dfs%20program%20in%20c2.png\" alt=\"\" \/><\/p>\n<p>In the above example, first, we will traverse through level 1 thus our path will become 1. After that, we will traverse through level 2 and now our path will be 1, 5, 3, 2. Now, we will traverse through level 3 and our new path will become 1, 5, 3, 2, 6, 4, 9. At the last, we will traverse through level 4 and our new path will become 1, 5, 3, 2, 6, 4, 9, 8, 7. If we use the same example for DFS traversal then our path will become 1, 5, 6, 8, 3, 4, 7, 9, 2.<\/p>\n<h2>How DFS works?<\/h2>\n<p>Let\u2019s see how DFS traversal works using an example. We will also see a dry run of the example to understand it more clearly.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624799-dfs%20program%20in%20c3.png\" alt=\"\" \/><\/p>\n<p>In the above example, we have taken one graph and have also taken an array to keep track of visited and non-visited nodes and this array is initialized with 0 which means all the nodes are non-visited. In this example, we will start traversing from source node 1. Let\u2019s see further steps of DFS traversal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624802-dfs%20program%20in%20c4.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 1 as visited and we will look for non-visited neighbors. Node 1 has three non-visited neighbors 3, 2, and 5. We can choose anyone out of these three. We will now go for Node 3.<br \/>\n<strong>Path till now: 1<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624802-dfs%20program%20in%20c5.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 3 as a visited. Node 3 has only one non-visited neighbor 6. Thus, we have the only choice to go for node 6.<br \/>\n<strong>Path till now: 1 -&gt; 3<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624814-dfs%20program%20in%20c6.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 6 as visited. Node 6 does not have any non-visited neighbors so we need to backtrack to its previous node which is 3.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624816-dfs%20program%20in%20c7.png\" alt=\"\" \/><\/p>\n<p>In this step, node 3 is already visited. Node 3 does not have any non-visited neighbors. Thus, we will backtrack to the previous node of 3 which is 1.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624816-dfs%20program%20in%20c8.png\" alt=\"\" \/><\/p>\n<p>In this step, node 1 is already visited. Node 1 has two non-visited neighbors which are 2 and 5. We can choose any one out of these two nodes. We will choose node 2 first.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624822-dfs%20program%20in%20c9.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 2 as a visited. Node 2 has two non-visited neighbors which are 4 and 7. We can choose any one out of these two. We will go for node 4 first.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624822-dfs%20program%20in%20c10.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 4 as visited. Node 4 does not have any non-visited neighbors so we need to backtrack to its previous node which is 2.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624826-dfs%20program%20in%20c11.png\" alt=\"\" \/><\/p>\n<p>In this step, node 2 is already visited. Node 2 has only one non-visited neighbor 7. Thus, we have the only choice to go for node 7.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624826-dfs%20program%20in%20c12.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 7 as visited. Node 7 does not have any non-visited neighbors so we need to backtrack to its previous node which is 2<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4 -&gt; 7<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624830-dfs%20program%20in%20c13.png\" alt=\"\" \/><br \/>\n.<br \/>\nIn this step, node 2 is already visited. Node 2 does not have any non-visited neighbors so we need to backtrack to its previous node which is 1.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4 -&gt; 7<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624831-dfs%20program%20in%20c14.png\" alt=\"\" \/><\/p>\n<p>In this step, node 1 is already visited. Node 1 has only one non-visited neighbor which is 5. We can choose only one node which is 5. We will choose node 5.<br \/>\n<strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4 -&gt; 7<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624844-dfs%20program%20in%20c15.png\" alt=\"\" \/><\/p>\n<p>In this step, first, we will mark node 5 as visited. Node 5 does not have any non-visited neighbors so we need to backtrack to its previous node which is 1. And now 1 also does not have any non-visited neighbors so DFS traversal will end here.<\/p>\n<p><strong>Path till now: 1 -&gt; 3 -&gt; 6 -&gt; 2 -&gt; 4 -&gt; 7 -&gt; 5<\/strong><\/p>\n<h2>C Program of DFS (Depth First Search) in c:<\/h2>\n<p>We have already seen what is DFS and how DFS works. Now, We will see how to write the DFS program in c. Before writing the DFS program in c let\u2019s see the pseudo-code of the DFS algorithm.<\/p>\n<pre><code>DFS(graph, node)\n    node.visited = true\n    for each neighbor \u2208 graph.Adj[node]\n        If neighbor.visited == false\n            DFS(graph,neighbor)\n\nmain() {\n    For each node \u2208 graph\n        node.visited = false\n     For each node \u2208 graph\n       DFS(graph, node)\n}<\/code><\/pre>\n<p>Now let\u2019s see how to write the DFS program in c using the above algorithm. We will use the same example as above to write the DFS program in c.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11372 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11372 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11372 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11372 .wpsm_nav-tabs > li.active > a, #tab_container_11372 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11372 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11372 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11372 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11372 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11372 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11372 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11372 .wpsm_nav-tabs > li > a:hover , #tab_container_11372 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11372 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11372 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11372 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11372 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11372 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11372 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11372 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11372 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11372 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11372 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11372 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11372\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11372\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11372_1\" aria-controls=\"tabs_desc_11372_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11372\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11372_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">\/\/ dfs program in C\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct node {\r\n  int vertex;\r\n  struct node* next;\r\n};\r\n\r\nstruct node* createNode(int v);\r\n\r\nstruct Graph {\r\n  int totalVertices;\r\n  int* visited;\r\n  struct node** adjLists;\r\n};\r\n\r\nvoid DFS(struct Graph* graph, int vertex) {\r\n  struct node* adjList = graph-&gt;adjLists[vertex];\r\n  struct node* temp = adjList;\r\n\r\n  graph-&gt;visited[vertex] = 1;\r\n  printf(\"%d -&gt; \", vertex);\r\n\r\n  while (temp != NULL) {\r\n    int connectedVertex = temp-&gt;vertex;\r\n\r\n    if (graph-&gt;visited[connectedVertex] == 0) {\r\n      DFS(graph, connectedVertex);\r\n    }\r\n    temp = temp-&gt;next;\r\n  }\r\n}\r\n\r\n\r\nstruct node* createNode(int v) {\r\n  struct node* newNode = malloc(sizeof(struct node));\r\n  newNode-&gt;vertex = v;\r\n  newNode-&gt;next = NULL;\r\n  return newNode;\r\n}\r\n\r\nstruct Graph* createGraph(int vertices) {\r\n  struct Graph* graph = malloc(sizeof(struct Graph));\r\n  graph-&gt;totalVertices = vertices;\r\n\r\n  graph-&gt;adjLists = malloc(vertices * sizeof(struct node*));\r\n\r\n  graph-&gt;visited = malloc(vertices * sizeof(int));\r\n\r\n  int i;\r\n  for (i = 0; i &lt; vertices; i++) {\r\n    graph-&gt;adjLists[i] = NULL;\r\n    graph-&gt;visited[i] = 0;\r\n  }\r\n  return graph;\r\n}\r\n\r\nvoid addEdge(struct Graph* graph, int src, int dest) {\r\n  struct node* newNode = createNode(dest);\r\n  newNode-&gt;next = graph-&gt;adjLists[src];\r\n  graph-&gt;adjLists[src] = newNode;\r\n\r\n  newNode = createNode(src);\r\n  newNode-&gt;next = graph-&gt;adjLists[dest];\r\n  graph-&gt;adjLists[dest] = newNode;\r\n}\r\n\r\nvoid displayGraph(struct Graph* graph) {\r\n  int v;\r\n  for (v = 1; v &lt; graph-&gt;totalVertices; v++) {\r\n    struct node* temp = graph-&gt;adjLists[v];\r\n    printf(\"&#92;n%d =&gt;  \", v);\r\n    while (temp) {\r\n      printf(\"%d, \", temp-&gt;vertex);\r\n      temp = temp-&gt;next;\r\n    }\r\n    printf(\"&#92;n\");\r\n  }\r\n  printf(\"&#92;n\");\r\n}\r\n\r\nint main() {\r\n  struct Graph* graph = createGraph(8);\r\n  addEdge(graph, 1, 5);\r\n  addEdge(graph, 1, 2);\r\n  addEdge(graph, 1, 3);\r\n  addEdge(graph, 3, 6);\r\n  addEdge(graph, 2, 7);\r\n  addEdge(graph, 2, 4);\r\n  \r\n  printf(\"&#92;nThe Adjacency List of the Graph is:\");\r\n  displayGraph(graph);\r\n\r\n  printf(\"&#92;nDFS traversal of the graph: &#92;n\");\r\n  DFS(graph, 1);\r\n\r\n  return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11372 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11372 a\"),jQuery(\"#tab-content_11372\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>The Adjacency List of the Graph is:\n1 =>  3, 2, 5, \n2 =>  4, 7, 1, \n3 =>  6, 1, \n4 =>  2, \n5 =>  1, \n6 =>  3, \n7 =>  2, \n\nDFS traversal of the graph: \n1 -> 3 -> 6 -> 2 -> 4 -> 7 -> 5<\/code><\/pre>\n<p><strong>Time Complexity: O(V+E)<\/strong> where V is the number of nodes and E is the number of edges. We have to traverse through all the nodes and edges so the time complexity is O(V+E)<\/p>\n<p><strong>Space Complexity: O(V)<\/strong> because in the worst case we might need to store recursion calls for all the nodes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn what is DFS, and how DFS is different from BFS, we will also how DFS works with a dry run of an example, and we will also see how to write a DFS program in c. What is DFS? DFS stands for Depth First Search and it is used [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-11509","post","type-post","status-publish","format-standard","hentry","category-c-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>DFS (Depth First Search) Program in C<\/title>\n<meta name=\"description\" content=\"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DFS (Depth First Search) Program in C\" \/>\n<meta property=\"og:description\" content=\"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-29T10:54:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-02T13:18:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"DFS Program in C\",\"datePublished\":\"2022-12-29T10:54:06+00:00\",\"dateModified\":\"2023-01-02T13:18:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\"},\"wordCount\":1089,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\",\"articleSection\":[\"C Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\",\"name\":\"DFS (Depth First Search) Program in C\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\",\"datePublished\":\"2022-12-29T10:54:06+00:00\",\"dateModified\":\"2023-01-02T13:18:19+00:00\",\"description\":\"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C Programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/c-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"DFS Program in C\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DFS (Depth First Search) Program in C","description":"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.","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:\/\/prepbytes.com\/blog\/dfs-program-in-c\/","og_locale":"en_US","og_type":"article","og_title":"DFS (Depth First Search) Program in C","og_description":"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.","og_url":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-12-29T10:54:06+00:00","article_modified_time":"2023-01-02T13:18:19+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"DFS Program in C","datePublished":"2022-12-29T10:54:06+00:00","dateModified":"2023-01-02T13:18:19+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/"},"wordCount":1089,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg","articleSection":["C Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/","url":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/","name":"DFS (Depth First Search) Program in C","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg","datePublished":"2022-12-29T10:54:06+00:00","dateModified":"2023-01-02T13:18:19+00:00","description":"Learn what is DFS and how DFS works with an example, and we will also see how to write a DFS program in C coding language.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672310624667-dfs%20program%20in%20c.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/dfs-program-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"C Programming","item":"https:\/\/prepbytes.com\/blog\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"DFS Program in C"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11509"}],"version-history":[{"count":1,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11509\/revisions"}],"predecessor-version":[{"id":11510,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11509\/revisions\/11510"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}