{"id":25837,"date":"2017-10-25T20:57:26","date_gmt":"2017-10-25T15:27:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25837"},"modified":"2017-10-25T20:57:26","modified_gmt":"2017-10-25T15:27:26","slug":"breadth-first-traversal-bfs-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/breadth-first-traversal-bfs-graph\/","title":{"rendered":"Cpp algorithm &#8211; Breadth First Traversal or BFS for a Graph"},"content":{"rendered":"<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Breadth-first_search\">Breadth First Traversal<\/a>\u00a0for a graph is similar to Breadth First Traversal of a tree.<span id=\"more-18382\"><\/span> The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the starting vertex.<br \/>\nFor example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don\u2019t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Breadth First Traversal of the following graph is 2, 0, 3, 1.<\/p>\n<p>Following are C++ and Java implementations of simple Breadth First Traversal from a given source.<\/p>\n<p>The C++ implementation uses <a href=\"http:\/\/en.wikipedia.org\/wiki\/Adjacency_list\">adjacency list representation<\/a> of graphs. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Standard_Template_Library\">STL<\/a>\u2018s <a href=\"http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialC++STL.html#LIST\">list container<\/a> is used to store lists of adjacent nodes and queue of nodes needed for BFS traversal.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20Program%20to%20print%20BFS%20traversal%20from%20a%20given%20source%20vertex.%20BFS(int%20s)%20%0A%2F%2F%20traverses%20vertices%20reachable%20from%20s.%0A%23include%3Ciostream%3E%0A%23include%20%3Clist%3E%0A%20%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%20list%20representation%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E%20*adj%3B%20%20%20%20%2F%2F%20Pointer%20to%20an%20array%20containing%20adjacency%20lists%0Apublic%3A%0A%20%20%20%20Graph(int%20V)%3B%20%20%2F%2F%20Constructor%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%20%2F%2F%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20void%20BFS(int%20s)%3B%20%20%2F%2F%20prints%20BFS%20traversal%20from%20a%20given%20source%20s%0A%7D%3B%0A%20%0AGraph%3A%3AGraph(int%20V)%0A%7B%0A%20%20%20%20this-%3EV%20%3D%20V%3B%0A%20%20%20%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%0A%7D%0A%20%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%20%2F%2F%20Add%20w%20to%20v%E2%80%99s%20list.%0A%7D%0A%20%0Avoid%20Graph%3A%3ABFS(int%20s)%0A%7B%0A%20%20%20%20%2F%2F%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20bool%20*visited%20%3D%20new%20bool%5BV%5D%3B%0A%20%20%20%20for(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20list%3Cint%3E%20queue%3B%0A%20%0A%20%20%20%20%2F%2F%20Mark%20the%20current%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20visited%5Bs%5D%20%3D%20true%3B%0A%20%20%20%20queue.push_back(s)%3B%0A%20%0A%20%20%20%20%2F%2F%20\u2019i\u2019%20will%20be%20used%20to%20get%20all%20adjacent%20vertices%20of%20a%20vertex%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%0A%20%20%20%20while(!queue.empty())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Dequeue%20a%20vertex%20from%20queue%20and%20print%20it%0A%20%20%20%20%20%20%20%20s%20%3D%20queue.front()%3B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20s%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20%20%20%20%20queue.pop_front()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Get%20all%20adjacent%20vertices%20of%20the%20dequeued%20vertex%20s%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20a%20adjacent%20has%20not%20been%20visited%2C%20then%20mark%20it%20visited%0A%20%20%20%20%20%20%20%20%2F%2F%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20for(i%20%3D%20adj%5Bs%5D.begin()%3B%20i%20!%3D%20adj%5Bs%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if(!visited%5B*i%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5B*i%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.push_back(*i)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20methods%20of%20graph%20class%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0A%20%20%20%20Graph%20g(4)%3B%0A%20%20%20%20g.addEdge(0%2C%201)%3B%0A%20%20%20%20g.addEdge(0%2C%202)%3B%0A%20%20%20%20g.addEdge(1%2C%202)%3B%0A%20%20%20%20g.addEdge(2%2C%200)%3B%0A%20%20%20%20g.addEdge(2%2C%203)%3B%0A%20%20%20%20g.addEdge(3%2C%203)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Following%20is%20Breadth%20First%20Traversal%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20%22(starting%20from%20vertex%202)%20%5Cn%22%3B%0A%20%20%20%20g.BFS(2)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Following is Breadth First Traversal (starting from vertex 2)\r\n2 0 3 1\r\n<\/pre>\n<p>Note that the above code traverses only the vertices reachable from a given source vertex. All the vertices may not be reachable from a given vertex (example Disconnected graph). To print all the vertices, we can modify the BFS function to do traversal starting from all nodes one by one (Like the <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/18212\">DFS modified version<\/a>) .<\/p>\n<p>Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm &#8211; Breadth First Traversal or BFS for a Graph &#8211; Graph Algorithms &#8211; Breadth First Traversal for a graph is similar to Breadth First Traversal.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83567,74168,73906],"tags":[75740,75741,75743,75744,75739,75737,75742,75738],"class_list":["post-25837","post","type-post","status-publish","format-standard","hentry","category-cpp-algorithm","category-dfs-and-bfs","category-graph-algorithms","tag-breadth-first-search-algorithm","tag-breadth-first-search-example","tag-breadth-first-search-java","tag-breadth-first-search-program-in-c","tag-breadth-first-search-pseudocode","tag-c-program-of-breadth-first-search-in-a-graph","tag-depth-first-search","tag-dfs-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25837"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25837\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}