Search
⌘K
Depth-First Search

Number of Islands

medium

DESCRIPTION (inspired by Leetcode.com)

You are given binary matrix grid of size m x n, where '1' denotes land and '0' signifies water. Determine the count of islands present in this grid. An island is defined as a region of contiguous land cells connected either vertically or horizontally, and it is completely encircled by water. Assume that the grid is bordered by water on all sides.

Input:

grid = [
[1,1,0,1],
[1,1,0,1],
[1,1,0,0],
]

Output:

2

Explanation

This solution uses depth-first search to traverse the grid and count the number of islands.
The algorithm starts at the first cell of the grid and explores all the adjacent cells that are part of the same island. To be part of the same island, a cell must be a 1 and must be adjacent to the current cell. During each recursive call, the algorithm first marks the current cell by changing its value to 0 so that it does not explore the same cell again.
We'll walkthrough how the algorithm detects 2 islands in the example grid:
[ [1, 1, 0, 1], [1, 1, 0, 1], [1, 1, 0, 0] ]

First Island

The algorithm starts by iterating over all cells in the grid. If it finds a 1, it increments the count variable and starts the depth-first search exploration from that cell. The algorithm explores all the reachable cells of the island, marking them as 0 so that it does not explore them again.
Visualization
def numIslands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
grid[r][c] = 0
if r + 1 < rows and grid[r + 1][c] == 1:
dfs(r + 1, c)
if r > 0 and grid[r - 1][c] == 1:
dfs(r - 1, c)
if c + 1 < cols and grid[r][c + 1] == 1:
dfs(r, c + 1)
if c > 0 and grid[r][c - 1] == 1:
dfs(r, c - 1)
return
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
1101110111000000i: 0j: 0count: 0

i = 0

0 / 19

Once the algorithm has explored all reachable cells of the island, it iterates over the remaining cells to find the start of the next island. When it finds a 1, it increments the count variable and starts the depth-first search exploration again from that cell, this time exploring all the reachable cells of the new island.

Second Island

Once the first call to dfs has returned, the algorithm continues iterating over the remaining cells. When it finds another cell with value of 1, then this is start of a new island.
Visualization
def numIslands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
grid[r][c] = 0
if r + 1 < rows and grid[r + 1][c] == 1:
dfs(r + 1, c)
if r > 0 and grid[r - 1][c] == 1:
dfs(r - 1, c)
if c + 1 < cols and grid[r][c + 1] == 1:
dfs(r, c + 1)
if c > 0 and grid[r][c - 1] == 1:
dfs(r, c - 1)
return
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
0001000100000000i: 0j: 0count: 1

recursive call

0 / 9

The algorithm continues this process until it has explored all the cells of the grid, at which point it returns the count variable, which contains the number of islands.

Animated Solution

|
2d-list of 0s and 1s
Try these examples:
Visualization
def numIslands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
grid[r][c] = 0
if r + 1 < rows and grid[r + 1][c] == 1:
dfs(r + 1, c)
if r > 0 and grid[r - 1][c] == 1:
dfs(r - 1, c)
if c + 1 < cols and grid[r][c + 1] == 1:
dfs(r, c + 1)
if c > 0 and grid[r][c - 1] == 1:
dfs(r, c - 1)
return
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
1101110111000000

number of islands

0 / 46

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Reading Progress

On This Page