{"id":6250,"date":"2025-09-16T15:10:37","date_gmt":"2025-09-16T09:40:37","guid":{"rendered":"https:\/\/study.madeeasy.in\/?p=6250"},"modified":"2025-09-16T15:13:57","modified_gmt":"2025-09-16T09:43:57","slug":"graph-searching","status":"publish","type":"post","link":"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching","title":{"rendered":"Graph Searching: DFS and BFS Algorithms Explained"},"content":{"rendered":"<p style=\"text-align: justify;\">When we characterize the running time of a graph algorithm on a given graph G = (V, E), we usually measure the size of the input in terms of the number of vertices |V| and the number of edges |E| of the graph.<\/p>\n<p style=\"text-align: justify;\">That is, we describe the size of the input with two parameters, not just one.<\/p>\n<h2 style=\"text-align: justify;\"><strong> GRAPH SEARCHING<\/strong><\/h2>\n<p style=\"text-align: justify;\"><strong>Searching a graph:<\/strong><\/p>\n<p style=\"text-align: justify;\">Systematically follow the edges of a graph to visit the vertices of the graph. Used to discover the structure of a graph.<br \/>\nStandard graph-searching algorithms:<\/p>\n<p style=\"text-align: justify;\">\u2022 Breadth-first Search (BFS)<br \/>\n\u2022 Depth-first Search (DFS)<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Depth-first-Search-DFS\" >Depth-first Search (DFS)<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Each-vertex-has-two-timestamps\" >Each vertex has two timestamps:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Application-of-Depth-First-Search\" >Application of Depth First Search<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Breadth-First-Search\" >Breadth-First Search<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Definitions\" >Definitions:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.madeeasy.in\/study\/cs-it\/algorithms\/graph-searching\/#Applications-of-Breadth-First-Search\" >Applications of Breadth First Search<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Depth-first-Search-DFS\"><\/span><strong>Depth-first Search (DFS)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Main Idea:<\/strong> DFS is a systematic method of visiting the vertices of a graph. Its general step requires that if we are currently visiting vertex \u2018u\u2019, then we next visit a vertex adjacent to \u2018u\u2019 which has not yet been visited. If no such vertex exists then we return to the vertex visited just before u, and the search is repeated until every vertex in that component of the graph has been visited.<\/p>\n<p style=\"text-align: justify;\">DFS uses a strategy that searches \u201cdeeper\u201d in the graph whenever possible, unlike BFS which discovers all vertices at distance \u2018k\u2019 from the source before discovering any vertices at distance k+1.<\/p>\n<p style=\"text-align: justify;\">The predecessor subgraph produced by DFS may be composed of several trees, because the search may be repeated from several sources. This predecessor subgraph forms a depth-first forest depth-first forest \u2018E\u2019 composed of several depth-first trees and the edges in \u2018E\u2019 are called tree edges. On the other hand the predecessor subgraph of BFS forms a tree.<\/p>\n<p style=\"text-align: justify;\">Besides creating a depth first forest, depth first search also timestamps each vertex.<\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Each-vertex-has-two-timestamps\"><\/span><strong>Each vertex has two timestamps:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ol>\n<li style=\"text-align: justify;\">The first timestamp d[v] records when v is discovered (and grayed).<\/li>\n<li style=\"text-align: justify;\">The second timestamp f[v] records when the search finishes examining v\u2019s adjacency list (and blackens v).<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">The procedure DFS below records when it discovers vertex u in the variable d[u]and when it finishes vertex u in the variable f [u]. These timestamps are integers between 1 and 2|v|, since, there is one discovery event and one finishing event for each of the |v| vertices. For every vertex v,<\/p>\n<p style=\"text-align: center;\">d[v]&lt;f[v]<\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Application-of-Depth-First-Search\"><\/span><strong> Application of Depth First Search<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ol>\n<li style=\"text-align: justify;\">We can determine the articulation point through depth first search.<\/li>\n<li style=\"text-align: justify;\">We can determine bridges through depth-first search.<\/li>\n<li style=\"text-align: justify;\">We can determine number of connected components through depth first search.<\/li>\n<li style=\"text-align: justify;\">We can also determine cycle.<\/li>\n<\/ol>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Breadth-First-Search\"><\/span><strong> Breadth-First Search<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li style=\"text-align: justify;\">Input: Graph G = (V, E), either directed or undirected, and source vertex s \u2208 V.<\/li>\n<li style=\"text-align: justify;\">Output:(a) d[v] = distance (smallest number of edges, or shortest path) from s to v, for all v \u2208 V. d[v] = \u221e if v is not reachable from s.<br \/>\n(b) p[v] = u such that (u, v) is last edge on shortest path s to v. u is v\u2019s predecessor.<br \/>\n(c) Builds breadth-first tree with root s that contains all reachable vertices<\/li>\n<\/ul>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Definitions\"><\/span><strong> Definitions:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li style=\"text-align: justify;\">Path between vertices u and v: Sequence of vertices (v1, v2,. . . , vk) such that u = v1 and v = vk, and (vi , vi +1) \u2208 E, for all 1 \u2264 i \u2264 k \u2013 1.<\/li>\n<li style=\"text-align: justify;\">Length of the path: Number of edges in the path.<\/li>\n<li style=\"text-align: justify;\">Path is simple if no vertex is repeated.<\/li>\n<li style=\"text-align: justify;\">Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier.<br \/>\n(a) A vertex is \u201cdiscovered\u201d the first time it is encountered during the search.<br \/>\n(b) A vertex is \u201cfinished\u201d if all vertices adjacent to it have been discovered.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify;\">Colors the vertices to keep track of progress.<br \/>\n(a) White \u2013 Undiscovered.<br \/>\n(b) Gray \u2013 Discovered but not finished.<br \/>\n(c) Black \u2013 Finished.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify;\">Colors are required only to reason about the algorithm. Can be implemented without colors.<\/li>\n<\/ul>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Applications-of-Breadth-First-Search\"><\/span><strong>Applications of Breadth First Search<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ol>\n<li style=\"text-align: justify;\">To test if a graph is bipartite.<\/li>\n<li style=\"text-align: justify;\">Cycle detection in undirected graph.<\/li>\n<li style=\"text-align: justify;\">To find if there is a path between two vertices.<\/li>\n<li>Finding all nodes within one connected components<\/li>\n<li>Number of connected components.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>When we characterize the running time of a graph algorithm on a given graph G = (V, E), we usually<\/p>\n","protected":false},"author":1,"featured_media":6276,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1831,3],"tags":[1841,1842,1852,1853],"class_list":["post-6250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-cs-it","tag-breadth-first-search","tag-depth-first-search-dfs","tag-dfs-and-bfs","tag-dfs-and-bfs-difference"],"_links":{"self":[{"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/posts\/6250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/comments?post=6250"}],"version-history":[{"count":0,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/posts\/6250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/media\/6276"}],"wp:attachment":[{"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/media?parent=6250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/categories?post=6250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madeeasy.in\/study\/wp-json\/wp\/v2\/tags?post=6250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}