Discover how a simple search method can solve a tricky counting problem in maps!
Why Number of Islands BFS and DFS in DSA C#?
Imagine you have a big map made of squares, where some squares are water and some are land. You want to count how many separate islands are on the map. Doing this by looking at each square and trying to remember which ones you already counted can be very confusing and slow.
Trying to count islands by checking each square manually is slow and easy to make mistakes. You might count the same island twice or miss some parts because it's hard to track connected land squares without a clear method.
Using BFS (Breadth-First Search) or DFS (Depth-First Search) helps you explore all connected land squares starting from one piece of land. This way, you can mark the entire island as counted in one go, making counting fast and accurate.
int countIslands(char[][] grid) {
int count = 0;
for (int i = 0; i < grid.Length; i++) {
for (int j = 0; j < grid[0].Length; j++) {
if (grid[i][j] == '1') {
count++;
// Manually try to find connected land
}
}
}
return count;
}int countIslands(char[][] grid) {
int count = 0;
for (int i = 0; i < grid.Length; i++) {
for (int j = 0; j < grid[0].Length; j++) {
if (grid[i][j] == '1') {
count++;
DFS(grid, i, j);
}
}
}
return count;
}
void DFS(char[][] grid, int row, int col) {
// Explore all connected land and mark visited
}This method lets you quickly and correctly count all separate islands on a map, no matter how big or complex the land shapes are.
Think about a drone flying over a flooded area. It needs to count how many dry land patches (islands) are left to plan rescue operations. BFS or DFS helps the drone's software count these islands fast and without errors.
Manual counting is slow and error-prone.
BFS and DFS explore connected land to count islands accurately.
This approach works well for maps of any size or shape.
Practice
Solution
Step 1: Understand the problem statement
The problem asks to count distinct islands, where islands are groups of connected '1's (land) in the grid.Step 2: Identify what is being counted
We count how many separate groups of connected land cells exist, not their size or perimeter.Final Answer:
Count how many groups of connected '1's (land) exist -> Option DQuick Check:
Number of islands = count of connected land groups [OK]
- Confusing island count with island size
- Counting water cells instead of land
- Ignoring connectivity rules (horizontal and vertical)
Solution
Step 1: Understand C# 2D array initialization
In C#, jagged arrays (array of arrays) are declared as char[][] and initialized with new char[][] and inner arrays. Number of Islands uses jagged arrays for grid[r][c] access and grid[0].Length.Step 2: Check each option's syntax
char[][] grid = new char[][] { new char[] {'1','0'}, new char[] {'0','1'} };: char[][] grid = new char[][] { new char[] {'1','0'}, new char[] {'0','1'} }; correctly uses jagged array syntax.
char[,] grid = new char[][] { {'1','0'}, {'0','1'} };: declares char[,] (rectangular) but tries jagged init - type mismatch.
char[,] grid = { {'1','0'}, {'0','1'} };: valid rectangular char[,], but incompatible with grid[r][c] and grid[0].Length.
char[][] grid = new char[2][2] { {'1','0'}, {'0','1'} };: new char[2][2] invalid syntax (incorrect brackets).Final Answer:
char[][] grid = new char[][] { new char[] {'1','0'}, new char[] {'0','1'} }; -> Option CQuick Check:
Jagged array with new keyword = correct syntax [OK]
- Omitting 'new' keyword in array initialization
- Confusing jagged arrays with rectangular arrays
- Using wrong braces or missing inner arrays
char[][] grid = new char[][] {
new char[] {'1','1','0','0'},
new char[] {'1','0','0','1'},
new char[] {'0','0','1','1'},
new char[] {'0','0','0','0'}
};
int CountIslands(char[][] grid) {
int count = 0;
int rows = grid.Length;
int cols = grid[0].Length;
bool[][] visited = new bool[rows][];
for (int i = 0; i < rows; i++) visited[i] = new bool[cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == '1' && !visited[r][c]) {
BFS(r, c, grid, visited);
count++;
}
}
}
return count;
}
void BFS(int r, int c, char[][] grid, bool[][] visited) {
int[] dr = { -1, 1, 0, 0 };
int[] dc = { 0, 0, -1, 1 };
Queue<(int, int)> queue = new Queue<(int, int)>();
queue.Enqueue((r, c));
visited[r][c] = true;
while (queue.Count > 0) {
var (row, col) = queue.Dequeue();
for (int i = 0; i < 4; i++) {
int nr = row + dr[i];
int nc = col + dc[i];
if (nr >= 0 && nr < grid.Length && nc >= 0 && nc < grid[0].Length && grid[nr][nc] == '1' && !visited[nr][nc]) {
queue.Enqueue((nr, nc));
visited[nr][nc] = true;
}
}
}
}
Console.WriteLine(CountIslands(grid));Solution
Step 1: Analyze the grid for connected islands
Islands are groups of '1's connected horizontally or vertically. The grid has these islands:
- Island 1: (0,0),(0,1),(1,0)
- Island 2: (1,3)
- Island 3: (2,2),(2,3)Step 2: BFS visits each island once
The BFS marks all connected '1's as visited. Each BFS call increments count by 1. Total BFS calls = number of islands = 3.Final Answer:
3 -> Option AQuick Check:
Count islands by BFS calls = 3 [OK]
- Counting diagonally connected '1's as one island
- Missing isolated single '1's as islands
- Not marking visited cells properly
void DFS(int r, int c, char[][] grid, bool[][] visited) {
if (r < 0 || r >= grid.Length || c < 0 || c >= grid[0].Length || grid[r][c] == '0')
return;
visited[r][c] = true;
DFS(r - 1, c, grid, visited);
DFS(r + 1, c, grid, visited);
DFS(r, c - 1, grid, visited);
DFS(r, c + 1, grid, visited);
}
int CountIslands(char[][] grid) {
int count = 0;
int rows = grid.Length;
int cols = grid[0].Length;
bool[][] visited = new bool[rows][];
for (int i = 0; i < rows; i++) visited[i] = new bool[cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == '1' && !visited[r][c]) {
DFS(r, c, grid, visited);
count++;
}
}
}
return count;
}Solution
Step 1: Identify missing visited check in DFS
The DFS function does not check if a cell is already visited before recursive calls, causing infinite recursion or revisiting.Step 2: Fix by adding visited check before recursion
Adding "if (visited[r][c]) return;" before marking visited prevents revisiting cells and infinite loops.Final Answer:
Add a check for visited[r][c] before recursive DFS calls -> Option BQuick Check:
Prevent revisiting by checking visited before DFS calls [OK]
- Not checking visited before recursion
- Confusing base case conditions
- Modifying grid instead of using visited array
Solution
Step 1: Understand connectivity rules
Original problem connects cells horizontally and vertically (4 directions). To include diagonals, add those directions.Step 2: Modify direction arrays for 8 directions
Add diagonal moves (-1,-1), (-1,1), (1,-1), (1,1) to dr and dc arrays to explore all neighbors.Final Answer:
Add diagonal directions (-1,-1), (-1,1), (1,-1), (1,1) to the direction arrays -> Option AQuick Check:
Include diagonals by adding their directions [OK]
- Ignoring diagonal neighbors
- Confusing BFS and DFS data structures
- Changing grid values instead of using visited array
Ready to prove it?
Pass the test and this pattern moves unknown
