Performing Breadth First Search recursively
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Breadth-First Search (BFS) is a common graph traversal algorithm that explores nodes layer by layer, making it suitable for exploring the shortest path in unweighted graphs. While BFS is typically implemented using a queue due to its iterative nature, it is possible to perform a BFS traversal recursively with careful restructuring. This article explores the recursive approach to BFS, providing technical insight and examples.
Understanding BFS
In BFS, the starting node is explored first, followed by all its neighboring nodes at the present depth, before proceeding to nodes at the next depth level. It contrasts with Depth-First Search (DFS), which explores as far as possible down one branch before backtracking.
Key Properties of BFS
- Time Complexity: , where is the number of vertices and is the number of edges.
- Space Complexity: , due to the need to track visited nodes.
- Shortest Path: Naturally finds the shortest path in an unweighted graph.
Recursive BFS Implementation
A traditional BFS uses a queue to maintain the frontier, i.e., the current layer of nodes being explored. The recursive approach to BFS uses a helper function to achieve a similar effect, managing the frontier layer by layer.
Recursive BFS Algorithm
- Base Step: Start by calling the BFS function with the root node, adding it to the visited set.
- Recursive Step: For each node in the current layer, explore its unvisited neighbors. Collect these neighbors and pass them to the next recursive call.
- Termination: The recursion terminates when there are no more nodes to explore.
Pseudo-code for Recursive BFS
Here's a simple pseudo-code illustration:
Example
Consider the following graph represented as an adjacency list:
Implementing recursive BFS on this graph starting from node 'A' would traverse the nodes as follows: A -> B -> C -> D -> E -> F.
Key Considerations
While recursive BFS is an interesting variant, it is not commonly used due to the following concerns:
- Call Stack Limitations: Recursive implementations are constrained by the maximum call stack size, making large graphs challenging.
- Inefficiency: The overhead of recursive calls can lead to inefficiencies compared to the iterative method.
- Clarity: Traditional iterative BFS is often more intuitive, especially as queue operations align naturally with BFS’s level-order exploration.
Summary Table
| Feature | BFS | Recursive BFS |
| Data Structure | Queue | Implicit Call Stack |
| Space Complexity | (D: max depth) | |
| Performance | Efficient | Less Efficient |
| Suitability for Large Graphs | Yes | Limited by stack depth |
| Intuition | Easier | Trickier |
Conclusion
Recursive BFS offers an alternative to the conventional graph traversal model and serves as an insightful academic exercise in converting iterative processes to recursive ones. However, in practical applications, especially involving large graphs, the traditional queue-based BFS remains the more effective solution due to its simplicity and efficiency in handling real-world constraints like memory and performance. Understanding both methods enables deeper insights into algorithm design and computational logic.
Related reading
- Performing DFS and BFS on a directed graph
- Perlin Noise for 1D?
- Permutation generator on C
- Permutation of array
- Permutations of a binary tree
- PHP algorithm to generate all combinations of a specific size from a single set
- Permutation of string as substring of another
- Permutation of String letters How to remove repeated permutations?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.