{"id":25857,"date":"2017-10-25T21:24:50","date_gmt":"2017-10-25T15:54:50","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25857"},"modified":"2017-10-25T22:10:58","modified_gmt":"2017-10-25T16:40:58","slug":"25857-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/25857-2\/","title":{"rendered":"Python algorithm &#8211; Breadth First Traversal or BFS for a Graph"},"content":{"rendered":"<p>Breadth First Traversal\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>PYTHON Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Program%20to%20print%20BFS%20traversal%20from%20a%20given%20source%0A%23%20vertex.%20BFS(int%20s)%20traverses%20vertices%20reachable%0A%23%20from%20s.%0Afrom%20collections%20import%20defaultdict%0A%20%0A%23%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%0A%23%20list%20representation%0Aclass%20Graph%3A%0A%20%0A%20%20%20%20%23%20Constructor%0A%20%20%20%20def%20__init__(self)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20default%20dictionary%20to%20store%20graph%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20defaultdict(list)%0A%20%0A%20%20%20%20%23%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20def%20addEdge(self%2Cu%2Cv)%3A%0A%20%20%20%20%20%20%20%20self.graph%5Bu%5D.append(v)%0A%20%0A%20%20%20%20%23%20Function%20to%20print%20a%20BFS%20of%20graph%0A%20%20%20%20def%20BFS(self%2C%20s)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%20%5BFalse%5D*(len(self.graph))%0A%20%0A%20%20%20%20%20%20%20%20%23%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20%20%20%20%20queue%20%3D%20%5B%5D%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20source%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20queue.append(s)%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%20%3D%20True%0A%20%0A%20%20%20%20%20%20%20%20while%20queue%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Dequeue%20a%20vertex%20from%20queue%20and%20print%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20queue.pop(0)%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20s%2C%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Get%20all%20adjacent%20vertices%20of%20the%20dequeued%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20vertex%20s.%20If%20a%20adjacent%20has%20not%20been%20visited%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20then%20mark%20it%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20self.graph%5Bs%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.append(i)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20True%0A%20%0A%20%0A%23%20Driver%20code%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Ag%20%3D%20Graph()%0Ag.addEdge(0%2C%201)%0Ag.addEdge(0%2C%202)%0Ag.addEdge(1%2C%202)%0Ag.addEdge(2%2C%200)%0Ag.addEdge(2%2C%203)%0Ag.addEdge(3%2C%203)%0A%20%0Aprint%20%22Following%20is%20Breadth%20First%20Traversal%20(starting%20from%20vertex%202)%22%0Ag.BFS(2)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Neelam%20Yadav\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<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python algorithm &#8211; Breadth First Traversal or BFS for a Graph &#8211; Breadth First Traversal for a graph is similar to Breadth First Traversal of a tree<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,74168,73906,4148],"tags":[83579,80887,83581,83580,83578,82368,75954,75757,75743,75739,82371,82370,75742,80881],"class_list":["post-25857","post","type-post","status-publish","format-standard","hentry","category-coding","category-dfs-and-bfs","category-graph-algorithms","category-python","tag-bfs-code-in-c","tag-bfs-example","tag-bfs-program-in-c","tag-bfs-program-in-c-using-adjacency-list","tag-bfs-python","tag-breadth-first-search-algorithm-with-example","tag-breadth-first-search-c","tag-breadth-first-search-in-c","tag-breadth-first-search-java","tag-breadth-first-search-pseudocode","tag-breadth-first-search-tree","tag-breadth-first-search-vs-depth-first-search","tag-depth-first-search","tag-depth-first-search-algorithm-with-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25857","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=25857"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25857\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}