Breadth-First Search/Traversal in a Graph.
What is Breadth First Search:
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root and explores the neighbor nodes first, before moving to the next level neighbors. (Reference — Wiki)
Mit Open Courseware session on Breadth first search
- For Graph as well we will use the Queue for performing the BFS.
- We will use the boolean to keep a track of the nodes because unlike tree during traversal we might keep moving into the circles by visiting same nodes repeatedly.
- In our example we are using adjacency List for the Graph Representation.
Output: 0 2 1 3 4 5
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.