Breadth-First Search (BFS) is a graph traversal algorithm used to explore or search through a graph in a breadthward motion. It starts at a given vertex and explores all its neighbors before moving on to the next level of vertices. It has various applications. For example, it can be used to determining connectivity in a graph or to find the shortest path between two vertices.
The BFS algorithm can be implemented using a queue. Here is a basic outline of the algorithm:
A queue is a data structure that follows the principle of "first-in, first-out" (FIFO), where elements are added to the end and removed from the front.
Click on Auto Play to see the algorithm in action.
Queue:
Node ID:
Traversal Order: 0
0
➜
1
➜
2
➜
3
➜
4
➜
8
➜
9
➜
5
➜
7
➜
6
0-1
➜
0-2
➜
1-3
➜
1-4
➜
2-8
➜
2-9
➜
3-5
➜
3-7
➜
5-6