Time and Space Complexity of Breadth First Search (BFS)
Last Updated :
09 Apr, 2025
The Breadth First Search (BFS) algorithm is used to traverse a graph. It starts at a node of the graph and visits all nodes at the current depth level before moving on to the nodes at the next depth level.
Although there are other methods for graph traversal, BFS is commonly used for its level-wise exploration. BFS iteratively explores every vertex in the graph, starting from a chosen node, and classifies vertices as visited or non-visited. It uses a queue to explore each node's neighbors, ensuring that all nodes at the current level are processed before moving to the next level.
C++
BFS(graph, Node):
create an empty queue Q
create a visited set
enqueue Node and mark as visited
while Q is not empty:
node = dequeue from Q
process(node)
for each neighbor of node:
if neighbor is not visited:
enqueue neighbor to Q
mark neighbor as visited
Time Complexity of Breadth First Search (BFS):
Let's first look at the pseudo code to understand the time complexity
At first glance, the structure of a typical BFS implementation — with an outer while loop to process nodes from the queue and an inner for loop to examine all adjacent neighbors—might suggest a nested loop behavior, which some could mistakenly interpret as having a higher time complexity. However, the key point is that the apparent nesting does not multiply the time by V and E independently. Here’s why:
- Outer While Loop – Node Processing: The while loop runs as long as there are nodes in the queue. Since each node is enqueued only once (tracked by the visited array), the loop executes once for each node, meaning it runs a total of V times (where V is the number of vertices). Each node is processed exactly once when it is dequeued, so the total time in the while loop is proportional to the number of nodes, i.e., O(V).
- Inner For Loop – Edge Processing: In each iteration of the while loop, the for loop processes the neighbors of the current node. Each edge is examined once across all iterations, making the total edge processing proportional to E. In an undirected graph, edges are counted twice (once for each endpoint), but this does not affect the overall complexity. Therefore, the edge processing is O(E).
Putting It All Together:
- Node Work: The total time in processing nodes is proportional to V since each node is visited exactly once in the outer while loop.
- Edge Work: The total time in processing edges (neighbors of nodes) is proportional to E, as each edge is examined exactly once in the inner for loop, during its first encounter.
Therefore, even though the algorithm has a nested while and for loop structure, the total work is determined by the number of nodes and edges. The time complexity of the traversal is O(V + E), which holds for the best, average, and worst cases.
Auxiliary Space of Breadth First Search (BFS):
The auxiliary space complexity of Breadth-First Search algorithm is O(V), where V is the number of vertices in the graph. Here's why the auxiliary space complexity is O(V):
Queue Data Structure:
- BFS typically uses a queue to keep track of the vertices to visit.
- At most, the queue will contain all vertices reachable from the starting vertex.
- Since the maximum number of vertices in the queue is bounded by the number of vertices in the graph, the space complexity of the queue is O(V).
Visited Array:
- BFS often employs a visited array to keep track of visited vertices to avoid revisiting them.
- This array has a size equal to the number of vertices in the graph, contributing to an auxiliary space complexity of O(V).
Additional Space:
- While traversing the graph, BFS may use other auxiliary data structures or variables.
- However, the space required by these additional elements is typically minimal compared to the queue and visited array and does not significantly affect the overall space complexity.
Therefore, the auxiliary space complexity of the Breadth-First Search algorithm is dominated by the queue and visited array, both of which require O(V) space, making the overall space complexity O(V).