Breadth First Search can be used to find the shortest distance between some starting vertex and remaining vertex of the graph. In this method, all the vertices are stored in a Queue and each vertex of the graph is visited or explored once. The oldest vertex (added first) in the Queue is explored first. To traverse the graph using breadth first search, Adjacent List of a graph is created first.
EXAMPLE
For Example : Consider the following Graph
The Adjacent List is
11 -> 12,18,13
12-> 11,14,15
13-> 11,16,17
14-> 12,18
15-> 12,18
16-> 13,18
17-> 13,18
18-> 14,15,11,16,17
Let us suppose that starting node is 11, The steps to traverse graph using Breadth First Search Is
Step 1: Add 11 to queue
Front: 1 Queue: 11
Rear:1
Step 2: Delete element at Front position (11) and add all the adjacent nodes of deletd not that are not visited.
Front: 2 Queue: X,12, 18, 13
Rear:4
Step 3: Delete element at Front position (12) and add all the adjacent nodes of deletd not that are not visited.
Front: 3 Queue: X,X,18, 13, 14, 15
Rear:6
11 already visited; i.e. in Queue or deleted
Step 4: Delete element at Front position (18) and add all the adjacent nodes of deletd not that are not visited.
Front: 4 Queue: X,X,X,13, 14, 15, 16,17
Rear:8
11,14,15 already visited; i.e. in Queue or deleted
Step 5: Delete element at Front position (13) and add all the adjacent nodes of deletd not that are not visited.
Front: 5 Queue: X,X,X,X, 14, 15, 16,17
Rear:8
No vertex added becoz 11,16,17 already visited; i.e. in Queue or deleted
Step 6: Delete element at Front position (14) and add all the adjacent nodes of deletd not that are not visited.
Front: 6 Queue: X,X,X,X, X, 15, 16,17
Rear:8
No vertex added becoz 12 and 18 already visited; i.e. in Queue or deleted
Step 7: Delete element at Front position (15) and add all the adjacent nodes of deletd not that are not visited.
Front: 7 Queue: X,X,X,X, X,X,16,17
Rear:8
No vertex added becoz 12 and 18 already visited; i.e. in Queue or deleted
Step 8: Delete element at Front position (16) and add all the adjacent nodes of deletd not that are not visited.
Front: 8 Queue: X,X,X,X, X,X,X,17
Rear:8
No vertex added becoz 13 and 18 already visited; i.e. in Queue or deleted
Step 9: Delete element at Front position (17) and add all the adjacent nodes of deletd not that are not visited.
Front: 8 Queue: X,X,X,X, X,X,X,X
Rear:8
No vertex added becoz 13 and 18 already visited; i.e. in Queue or deleted
Queue EMPTY. STOP.
Therefore Breadth First Search is 11,12,18,13,14,15,16,17
ALGORITHM
Step 1: Initialize all the vertices and create adjacent list.
Step 2: Put Starting vertex in Queue.
Step 3: Repeat Step 4 and 5 until Queue is empty.
Step 4: Remove front node from the Queue.
Step 5: Add to the rear of the Queue all the neighbors of the deleted node that are not visited or added in a Queue.
Step 6: Exit
APPLICATIONS
- Used to test whether graph is connected or not
- To find the cycles in the graphs
- To find the path with minimum number of edges between start vertex and current vertex or reporting that no such path exists.