Approach When answering the question, "How would you count the number of islands in a 2D grid map consisting of '1's (land) and '0's (water), where an island is defined as a group of adjacent lands connected horizontally or vertically?" , follow this…
Approach
When answering the question, "How would you count the number of islands in a 2D grid map consisting of '1's (land) and '0's (water), where an island is defined as a group of adjacent lands connected horizontally or vertically?", follow this structured framework:
- Understand the Problem: Identify what constitutes an island and the grid representation.
- Choose a Method: Decide on a traversal technique (Depth-First Search (DFS) or Breadth-First Search (BFS)).
- Implement the Algorithm: Write pseudocode or actual code to demonstrate your solution.
- Explain Your Solution: Walk through the logic of your implementation.
- Discuss Complexity: Address time and space complexity of your approach.
Key Points
- Definition Clarity: Clearly define what an island is.
- Algorithm Selection: Be prepared to justify your choice of DFS or BFS.
- Edge Cases: Consider scenarios like a grid full of water or land.
- Efficiency: Discuss the use of visited data structures to avoid counting the same island multiple times.
Standard Response
To tackle the problem of counting the number of islands in a 2D grid, we can use the Depth-First Search (DFS) approach. Below is a structured explanation and implementation.
Step 1: Understand the Problem
An island is defined as a group of connected '1's (land) in the grid, where connectivity is only vertical or horizontal. Water is represented by '0's. Our goal is to count the distinct islands.
Step 2: Choose a Method
- Start from any unvisited land cell (1).
- Mark all connected land cells as visited.
- Increment the island count every time we initiate a DFS from an unvisited land cell.
- We can use Depth-First Search (DFS) to explore each island:
Step 3: Implement the Algorithm
Here is a sample implementation in Python:
def numIslands(grid):
if not grid:
return 0
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == '0':
return
# Mark the current cell as visited by converting '1' to '0'
grid[i][j] = '0'
# Explore all adjacent cells (up, down, left, right)
dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1': # Found an unvisited land
dfs(i, j) # Start DFS to mark all connected lands
count += 1 # Increment island count
return countStep 4: Explain Your Solution
- Grid Traversal: We loop through each cell in the 2D grid.
- DFS Execution: Upon finding an unvisited land cell (1), we initiate a DFS which marks all connected parts of the island as visited (changing '1' to '0').
- Count Increment: Each time we start a DFS from an unvisited land, we increment our island count.
Step 5: Discuss Complexity
- Time Complexity: O(M * N), where M is the number of rows and N is the number of columns. Each cell is visited once.
- Space Complexity: O(M * N) in the worst case due to the recursion stack.
Tips & Variations
Common Mistakes to Avoid
- Forgetting to mark cells as visited, leading to infinite loops.
- Not considering edge cases, such as empty grids or grids with no islands.
Alternative Ways to Answer
- BFS Approach: Instead of DFS, you could use BFS to explore the grid:
- Implement BFS using a queue to track cells to visit.
Example BFS implementation:
Verve AI Editorial Team
Question Bank



