{"id":58,"date":"2014-12-25T05:29:04","date_gmt":"2014-12-25T05:29:04","guid":{"rendered":"http:\/\/theoryofprogramming.wordpress.com\/?p=58"},"modified":"2023-11-10T13:47:30","modified_gmt":"2023-11-10T13:47:30","slug":"breadth-first-search-algorithm","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2014\/12\/25\/breadth-first-search-algorithm\/","title":{"rendered":"Breadth First Search Algorithm"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Note<\/strong> &#8211; Theory of Coding is also on <strong>YouTube<\/strong>! This post has a video. Watch it at &#8211; <a href=\"https:\/\/www.youtube.com\/watch?v=5mG-qBRhvKQ\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/watch?v=5mG-qBRhvKQ<\/a><\/p>\n\n\n<div class=\"su-youtube su-u-responsive-media-yes\"><iframe loading=\"lazy\" width=\"600\" height=\"400\" src=\"https:\/\/www.youtube.com\/embed\/5mG-qBRhvKQ?\" frameborder=\"0\" allowfullscreen allow=\"autoplay; encrypted-media; picture-in-picture\" title=\"\"><\/iframe><\/div>\n\n\n\n<p>Hoping you&#8217;ll support the YouTube channel just like you have greatly supported the website! \ud83d\ude42<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Hello people&#8230;! In this post I will explain one of the most widely used Graph Search Algorithms, the Breadth First Search (BFS) Algorithm. Once you have learned this, you would have gained a new weapon in your arsenal, and you can start solving good number of Graph Theory related competitive programming questions.<\/p>\n\n\n\n<p>What we do in a BFS is a simple step-by-step process, which is &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start from a vertex <strong>S<\/strong>. Let this vertex be at, what is called&#8230;. &#8220;Level 0&#8221;.<\/li>\n\n\n\n<li>Find all the other vertices that are immediately accessible from this starting vertex <strong>S<\/strong>, i.e., they are only a single edge away (the adjacent vertices).<\/li>\n\n\n\n<li>Mark these adjacent vertices to be at &#8220;Level 1&#8221;.<\/li>\n\n\n\n<li>There will be a challenge that you might be coming back to the same vertex due to a loop or a ring in the graph. If this happens your BFS will take <strong>\u221e<\/strong> time. So, you will go only to those vertices&nbsp;who do not have their &#8220;Level&#8221; set to some value.<\/li>\n\n\n\n<li>Mark which is the parent vertex of the current vertex you&#8217;re at, i.e., the vertex from which you accessed the current vertex. Do this for all the vertices at Level 1.<\/li>\n\n\n\n<li>Now, find all those vertices that are a single edge away from all the vertices which are at &#8220;Level 1&#8221;. These new set of vertices will be at &#8220;Level 2&#8221;.<\/li>\n\n\n\n<li>Repeat this process until you run out of graph.<\/li>\n<\/ol>\n\n\n\n<p>This might sound heavy&#8230; Well atleast it would sound heavy to me if I heard it for the first time&#8230;! Many questions may pop-up in your mind, like, &#8220;How are we gonna do all that&#8230;?!&#8221;. Well, for now, focus on the concept, we will talk about the code later. And remember, we are talking about an Undirected Graph here. We will talk about Directed Graphs later. To understand the above stated steps, examine the picture below &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"2033\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=650%2C2033&#038;ssl=1\" alt=\"Breadth First Search Algorithm - Step-by-Step\" class=\"wp-image-60\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?w=650&amp;ssl=1 650w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=96%2C300&amp;ssl=1 96w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=327%2C1024&amp;ssl=1 327w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=491%2C1536&amp;ssl=1 491w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=230%2C719&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=350%2C1095&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs1-e1437332087912.jpg?resize=480%2C1501&amp;ssl=1 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" data-recalc-dims=\"1\" \/><\/a><figcaption class=\"wp-element-caption\">Breadth First Search Algorithm &#8211; Step-by-Step<\/figcaption><\/figure>\n\n\n\n<p>The sketch clearly shows you&nbsp;how we explore the vertices adjacent to a vertex and mark their levels. If you have noticed, whenever there were two ways of accessing the same vertex from multiple vertices of the same Level, i.e., in the diagram, Vertex 3 was accessible from Vertex 2 and Vertex 8, we preferred its parent to be Vertex 8, the larger number. Why is that so&#8230;? We will learn that in a short moment.<\/p>\n\n\n\n<p>The concepts that I wish to emphasize from the above picture are, how BFS can tell you the shortest path from a given vertex (the start vertex) to all the other vertices and the number of edges or, the &#8220;length of the path&#8221;, would be the <strong>Level of that Vertex<\/strong> itself. This is a very important feature of the BFS, you will understand this more clearly when I explain it with the help of an example in a different post.<\/p>\n\n\n\n<p>Now, having got some knowledge about the BFS, it is a good thing to do an exercise on this topic to really get the flow going. Try applying&nbsp;BFS on the Graph given. All you have to do is to implement the step-by-step process and get that final figure which I got above. And make sure you label the Levels and Parents for each vertex in the end.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"280\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?resize=650%2C280&#038;ssl=1\" alt=\"Breadth First Search Practise Question\" class=\"wp-image-61\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?w=650&amp;ssl=1 650w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?resize=300%2C129&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?resize=230%2C99&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?resize=350%2C151&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2014\/12\/bfs2-e1437332036860.jpg?resize=480%2C207&amp;ssl=1 480w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" data-recalc-dims=\"1\" \/><\/a><figcaption class=\"wp-element-caption\">Breadth First Search Practise Question<\/figcaption><\/figure>\n\n\n\n<p>Now, we come to the code part of the Breadth First Search, in C. We use the same Adjacency List that we used in our discussion of <strong><a href=\"http:\/\/theoryofprogramming.azurewebsites.net\/2014\/12\/24\/graph-theory-basics\/\">Graph Theory Basics<\/a><\/strong>. Coming back to our BFS discussion, the level of each vertex is stored in a separate array and so is the case for parent of each vertex. The three arrays are initialized to appropriate values. Now recall our step-by-step process that was stated earlier. Try to put that in terms of pseudocode and then proper&nbsp;code. Take a while to think how you would do that. If you could code it, you are marvelous&#8230;! \ud83d\ude00 &#8230; If not, don&#8217;t fret, I put my code below&#8230;!<\/p>\n\n\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C++ STL (using a Queue)<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Java<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C++ STL<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C++ STL (using a Queue)\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/3c1121aa970b5b7fe3d760743ca470ee.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Java\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/624049c8db972e12a7c7396dfa16478e.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/7002affba41ced3ab8017d73983e94ea.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C++ STL\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/24ff003d8cfdb680a78630c930b0ad58.js\"><\/script><br \/>\n<\/div><\/div><\/div>\n\n\n\n<p>Try using the&nbsp;above example given&nbsp;as practice&nbsp;as the sample input to your&nbsp;code, so that you can easily compare the results. Now, the important point here is when we insert a vertex into the Adjacency List, we follow Head Insertion to get O(1) Insertion. What happens due to this is that, the Linked List will have numbers in the descending order.<\/p>\n\n\n\n<p>So, when we are applying BFS&nbsp;for adjacent vertices of a given node, the Vertex with the higher number is met first. And then we explore starting by that higher numbered Vertex. This is why whenever we had a choice in approaching a vertex, we preferred approaching from the vertex which had the bigger number, why&#8230;? Due to Head Insertion&#8230;! If you had used Tail Insertion, this would be reversed.<\/p>\n\n\n\n<p>This is the overview of Breadth First Search Algorithm. It has a computational complexity of <strong>O(|V| + |E|)<\/strong>, which is pretty fast. But why <strong>O(|V| + |E|)<\/strong>&#8230;? If you think about the algorithm a little bit, we cover all the <strong>|V|<\/strong> vertices level-by-level, checking all the <strong>|E|<\/strong> edges twice (for an Undirected Graph). Once the level is set for a subset of vertices we don&#8217;t visit them again. Put an itty-bitty thought into it and I&#8217;m sure you&#8217;ll get the idea&#8230;! \ud83d\ude09 &#8230; But, the code above runs slower than O(|V| + |E|)&#8230; To achieve the proper complexity we must use a Queue.<\/p>\n\n\n\n<p>We can use BFS in the following scenarios &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shortest Path or Quickest Path (if all edges have equal weight).<\/li>\n\n\n\n<li>Finding the Length of Shortest Path between two Vertices.<\/li>\n<\/ul>\n\n\n\n<p>With the knowledge of BFS you can start solving Graph Theory related problems. It really depends on your logic how you will apply the BFS to the given problem. And there is only one way to develop it&#8230; Practice&#8230;! So, keep practicing&#8230; Feel free to comment your doubts..! Happy Coding&#8230;! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note &#8211; Theory of Coding is also on YouTube! This post has a video. Watch it at &#8211; https:\/\/www.youtube.com\/watch?v=5mG-qBRhvKQ Hoping you&#8217;ll support [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[17,19,32,40,50],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-graph-theory","tag-adjacency-list","tag-algorithms","tag-breadth-first-search","tag-data-structures","tag-graph-theory"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":2,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":3711,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions\/3711"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}