Reference: LeetCode
Difficulty: Medium

My Post: Java DFS & BFS Solutions Using Marked Array, Queue

Problem

Given a 2d grid map of '1's (land) and '0's (water), count 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 all surrounded by water.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
11110
11010
11000
00000
Output: 1

Input:
11000
11000
00100
00011
Output: 3

Analysis

DFS

It is very obvious that this problem can be solved by using an DFS approach.

Note: Be careful of the conditions that we dfs a place, which include boundary check and whether it is an island.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
boolean[][] marked = new boolean[m][n]; // or visit by setting value as '0'
int[][] direction = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int islandCount = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (!marked[i][j] && grid[i][j] == '1') {
dfs(i, j, grid, marked, direction);
++islandCount;
}
}
}
return islandCount;
}

private void dfs(int i, int j, char[][] grid, boolean[][] marked, int[][] direction) {
int m = grid.length;
int n = grid[0].length;
marked[i][j] = true; // visit;
for (int[] dir : direction) {
int x = i + dir[0];
int y = j + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (!marked[x][y] && grid[x][y] == '1') {
dfs(x, y, grid, marked, direction);
}
}
}
}

Time: $O(MN)$
Space: $O(MN)$. If we modify the original grid array, can we reduce the complexity to $O(1)$? No. It is because DFS goes by a call stack.

BFS

A similar idea based on BFS using a queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
boolean[][] marked = new boolean[m][n]; // or visit by setting value as '0'
int[][] direction = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int islandCount = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (!marked[i][j] && grid[i][j] == '1') {
bfs(i, j, grid, marked, direction);
++islandCount;
}
}
}
return islandCount;
}

private void bfs(int i, int j, char[][] grid, boolean[][] marked, int[][] direction) {
int m = grid.length;
int n = grid[0].length;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { i, j });
marked[i][j] = true;
// bfs
while (queue.size() > 0) {
int[] curr = queue.poll();
for (int[] dir : direction) {
int x = curr[0] + dir[0];
int y = curr[1] + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (!marked[x][y] && grid[x][y] == '1') {
queue.offer(new int[] { x, y });
marked[x][y] = true; // mark when enqueued
}
}
}
}
}

Time: $O(MN)$
Space: $O(\min{(M, N)})$. In the worst case where the grid is filled with lands, the size of queue can be $O(\min{(M, N)})$ as follows.