{"id":25853,"date":"2017-10-25T22:05:55","date_gmt":"2017-10-25T16:35:55","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25853"},"modified":"2017-10-25T22:05:55","modified_gmt":"2017-10-25T16:35:55","slug":"java-algorithm-breadth-first-traversal-bfs-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-breadth-first-traversal-bfs-graph\/","title":{"rendered":"Java 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 adjacency list representation of graphs. STL\u2018s list container is used to store lists of adjacent nodes and queue of nodes needed for BFS traversal.<\/p>\n<p><strong>JAVA Programming:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20print%20BFS%20traversal%20from%20a%20given%20source%20vertex.%0A%2F%2F%20BFS(int%20s)%20traverses%20vertices%20reachable%20from%20s.%0Aimport%20java.io.*%3B%0Aimport%20java.util.*%3B%0A%20%0A%2F%2F%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%20list%0A%2F%2F%20representation%0Aclass%20Graph%0A%7B%0A%20%20%20%20private%20int%20V%3B%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20private%20LinkedList%3CInteger%3E%20adj%5B%5D%3B%20%2F%2FAdjacency%20Lists%0A%20%0A%20%20%20%20%2F%2F%20Constructor%0A%20%20%20%20Graph(int%20v)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20V%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20adj%20%3D%20new%20LinkedList%5Bv%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cv%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20adj%5Bi%5D%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Function%20to%20add%20an%20edge%20into%20the%20graph%0A%20%20%20%20void%20addEdge(int%20v%2Cint%20w)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20adj%5Bv%5D.add(w)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20prints%20BFS%20traversal%20from%20a%20given%20source%20s%0A%20%20%20%20void%20BFS(int%20s)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Mark%20all%20the%20vertices%20as%20not%20visited(By%20default%0A%20%20%20%20%20%20%20%20%2F%2F%20set%20as%20false)%0A%20%20%20%20%20%20%20%20boolean%20visited%5B%5D%20%3D%20new%20boolean%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20%20%20%20%20LinkedList%3CInteger%3E%20queue%20%3D%20new%20LinkedList%3CInteger%3E()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Mark%20the%20current%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%3Dtrue%3B%0A%20%20%20%20%20%20%20%20queue.add(s)%3B%0A%20%0A%20%20%20%20%20%20%20%20while%20(queue.size()%20!%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%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%20%20%20%20%20s%20%3D%20queue.poll()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(s%2B%22%20%22)%3B%0A%20%0A%20%20%20%20%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%20%20%20%20%2F%2F%20If%20a%20adjacent%20has%20not%20been%20visited%2C%20then%20mark%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20Iterator%3CInteger%3E%20i%20%3D%20adj%5Bs%5D.listIterator()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(i.hasNext())%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%20int%20n%20%3D%20i.next()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(!visited%5Bn%5D)%0A%20%20%20%20%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%20%20%20%20%20visited%5Bn%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.add(n)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%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%20%0A%20%20%20%20%2F%2F%20Driver%20method%20to%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Graph%20g%20%3D%20new%20Graph(4)%3B%0A%20%0A%20%20%20%20%20%20%20%20g.addEdge(0%2C%201)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(0%2C%202)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(1%2C%202)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(2%2C%200)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(2%2C%203)%3B%0A%20%20%20%20%20%20%20%20g.addEdge(3%2C%203)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Following%20is%20Breadth%20First%20Traversal%20%22%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22(starting%20from%20vertex%202)%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20g.BFS(2)%3B%0A%20%20%20%20%7D%0A%7D\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[ad type=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 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,2139],"tags":[75740,75741,75757,75755,75739,75754,75742,75756],"class_list":["post-25853","post","type-post","status-publish","format-standard","hentry","category-coding","category-dfs-and-bfs","category-graph-algorithms","category-java","tag-breadth-first-search-algorithm","tag-breadth-first-search-example","tag-breadth-first-search-in-c","tag-breadth-first-search-java-source-code","tag-breadth-first-search-pseudocode","tag-breadth-first-search-tree-java","tag-depth-first-search","tag-dfs-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25853","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=25853"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25853\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}