DFS and BFS Time and Space complexities of 'Number of islands' on Leetcode
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In this article, we will delve into the time and space complexities of Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms when applied to the "Number of Islands" problem on LeetCode. This problem is a classic graph traversal problem often used to understand different strategies for searching and traversing a grid. We will provide both a technical explanation and relevant examples to clarify these concepts.
The "Number of Islands" Problem
The "Number of Islands" problem can be described as follows: Given a 2D grid of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are surrounded by water.
Understanding DFS and BFS
Depth-First Search (DFS)
DFS is a graph traversal algorithm that starts at the root (selecting some arbitrary node as the root in the case of a forest) and explores as far as possible along each branch before backtracking. In the context of the "Number of Islands," DFS explores each piece of land (cell with '1') and marks all adjacent lands as visited by turning them into '0' or using a boolean visited matrix. This effectively counts one island at a time.
Breadth-First Search (BFS)
BFS is another graph traversal algorithm which explores the neighbor nodes at the present depth prior to moving on to nodes at the next depth level. In the "Number of Islands" problem, BFS uses a queue to traverse the grid level-wise. Every time BFS visits a new island, it queues the unvisited adjacent lands and processes them until all connected components are explored.
Time and Space Complexity
DFS
- Time Complexity: , where is the number of rows and is the number of columns in the grid. This is because, in the worst case, we examine each cell once.
- Space Complexity: due to the recursion stack used for DFS. In the worst case, where the grid is filled with land, the recursion stack could be as large as the number of cells in the grid.
BFS
- Time Complexity: , similar to DFS. We visit each cell once in the loop.
- Space Complexity: for BFS. This is because the queue can grow up to the size of either the smaller dimension. In the worst-case scenario, the space complexity could become if all pieces of land are linearly distributed.
Technical Example
Consider a simple grid like this:
1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1
- Recursive Limitations: One should be cautious while implementing DFS due to potential stack overflow issues for large grids, especially in programming languages with less support for tail-recursion.
- Iterative DFS: Iterative DFS can be used to avoid the limitations of recursion by employing explicit stack data structures.
- Grid Size and Complexity: For very large grids, BFS can be more space-efficient, provided the size considers the queue's growth potential.
- Uniformity of Data Distribution: The performance of both algorithms depends on the uniformity of '1's in the grid. A sparse grid with limited islands may perform better with BFS due to its level-order nature.

