Breadth First Search (BFS)
About
Is a Graph or Tree traversal algorithm used to explore nodes and edges. It can also be used as a Search algorithm.
It is widely used in numerous situations such as shortest-path calculations, network analysis, and more.
It doesn't preserve shape of the traversal.
It uses Queue to store nodes in order.
How it works
BFS explores all nodes at the current "depth" (or level) before moving to nodes at the next depth.
In more Detail
In Graphs, you must also mark visited nodes, so that they don't get pushed again to the Queue.
In BST where you garantee order, you may add additional conditions to prioritize going left or right first, depending on the needle value.
At start node
N:Push
Nto theQueue.Push
N'schildren to theQueue.Process
Nand remove it from theQueue.
Go to next node
NatQueue:Push its children to the
Queue.Process node
N, and remove it fromQueue.
Repeat
2.until all nodes were processed.
Last updated