0% completed
Breadth-First Search (BFS) is a graph traversal algorithm that explores a graph's vertices (nodes) level by level. It starts from a selected source node and moves outward to visit all the nodes at the same distance from the source before moving on to nodes at the following distance level.
BFS is particularly useful for finding the shortest path in unweighted graphs and for systematically exploring graphs.
Step-by-Step Algorithm for BFS
-
Graph Initialization:
- Create a graph with
V
vertices. - Represent the graph using an adjacency list, where each vertex has a list of its adjacent vertices.
- Create a graph with
-
Mark All Vertices as Unvisited:
- Initialize a
visited
boolean array of sizeV
, with all elements set tofalse
.
- Initialize a
-
Initialize BFS Traversal:
- Start from the given
startVertex
. - Mark
startVertex
as visited by settingvisited[startVertex] = true
. - Add
startVertex
to the queue.
- Start from the given
-
Perform BFS Traversal:
- While the queue is not empty:
- Dequeue a vertex (
currentVertex
) from the front of the queue. - Process the
currentVertex
(e.g., print its value). - For each neighbor of
currentVertex
(from its adjacency list):- If the neighbor is unvisited:
- Mark it as visited.
- Enqueue the neighbor into the queue.
- If the neighbor is unvisited:
- Dequeue a vertex (
- While the queue is not empty:
-
End Condition:
- The traversal ends when the queue becomes empty, meaning all reachable vertices from the
startVertex
have been visited.
- The traversal ends when the queue becomes empty, meaning all reachable vertices from the
Algorithm Walkthrough
Let's see how the Breadth First Search algorithm works with an example.
Input:
- Graph:
- Vertices: 5 (
0, 1, 2, 3, 4
) - Edges:
(0, 1), (0, 2), (0, 3), (1, 2), (2, 4)
- Vertices: 5 (
- Starting Vertex: 0
Execution Steps
-
Initialization:
visited = [false, false, false, false, false]
queue = []
-
Start BFS Traversal:
- Mark
0
as visited:visited = [true, false, false, false, false]
. - Enqueue
0
:queue = [0]
.
- Mark
-
First Iteration:
- Dequeue
0
:queue = []
. - Process
0
: Print0
. - Explore neighbors of
0
:1
is unvisited: Mark as visited (visited = [true, true, false, false, false]
) and enqueue:queue = [1]
.2
is unvisited: Mark as visited (visited = [true, true, true, false, false]
) and enqueue:queue = [1, 2]
.3
is unvisited: Mark as visited (visited = [true, true, true, true, false]
) and enqueue:queue = [1, 2, 3]
.
- Dequeue
-
Second Iteration:
- Dequeue
1
:queue = [2, 3]
. - Process
1
: Print1
. - Explore neighbors of
1
:0
is already visited: Skip.2
is already visited: Skip.
- Dequeue
-
Third Iteration:
- Dequeue
2
:queue = [3]
. - Process
2
: Print2
. - Explore neighbors of
2
:0
is already visited: Skip.1
is already visited: Skip.4
is unvisited: Mark as visited (visited = [true, true, true, true, true]
) and enqueue:queue = [3, 4]
.
- Dequeue
-
Fourth Iteration:
- Dequeue
3
:queue = [4]
. - Process
3
: Print3
. - Explore neighbors of
3
:0
is already visited: Skip.
- Dequeue
-
Fifth Iteration:
- Dequeue
4
:queue = []
. - Process
4
: Print4
. - Explore neighbors of
4
:2
is already visited: Skip.
- Dequeue
-
End:
- The queue is empty, so the BFS traversal is complete.
Output:
Breadth-First Traversal starting from vertex 0: 0 1 2 3 4
Implementation of Breadth First Search
Below is the implementation of Breadth-First Search (BFS) in different programming languages:
Complexity analysis
Time Complexity
-
Initialization:
- The
visited
array is initialized to track whether a vertex has been visited. This operation is O(V), whereV
is the number of vertices.
- The
-
Queue Operations:
- Each vertex is enqueued once when it is first discovered and dequeued once during processing. These operations together are O(V) over the course of the BFS traversal.
-
Exploration of Adjacent Vertices:
- The adjacency list for each vertex is iterated over to explore its neighbors.
- Each edge is visited exactly once when traversing its endpoints, contributing O(E), where
E
is the number of edges.
-
Total Time Complexity:
- The traversal involves visiting all vertices and all edges: O(V) \text{ (vertices) } + O(E) \text{ (edges) } = O(V + E)
Space Complexity
-
Visited Array:
- The
visited
boolean array stores one entry per vertex to track whether it has been visited. Space Requirement: O(V).
- The
-
Queue:
- In the worst case, the queue can hold all vertices in a connected component, requiring O(V) space.
-
Total Space Complexity:
- Overall Space Requirement: O(V) (from
visited
array and queue).
- Overall Space Requirement: O(V) (from
Final Words
This makes BFS efficient for graph traversal, particularly when combined with the adjacency list representation. BFS is generally efficient for searching and traversal when the graph is not too dense. For sparse graphs, where E is much smaller than V^2, the time complexity becomes almost linear, making BFS a reasonable choice for many practical applications.
BFS guarantees it visits nodes according to their distance from the source node. It is an efficient algorithm to find the shortest path in unweighted graphs. Additionally, BFS can find connected components, detect cycles, and solve graph-related problems. However, it may consume more memory than DFS, especially in graphs with a significant or infinite branching factor.