Skip to content

Commit 63e5b94

Browse files
committed
Add Number Of Islands - algorithm
1 parent 341efd7 commit 63e5b94

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Algorithm Name: Number of Islands
3+
* Programming Language: Java
4+
* Category: Matrix, Graph, Depth-First Search (DFS)
5+
* Difficulty Level: Medium
6+
*
7+
* Author: Ojasvi Bakshi
8+
*
9+
* Algorithm Description:
10+
* Given an m x n 2D binary grid which represents a map of '1's (land) and '0's (water),
11+
* this algorithm returns the number of islands. An island is surrounded by water and is
12+
* formed by connecting adjacent lands horizontally or vertically.
13+
*
14+
* Time Complexity: O(m*n) - We visit each cell in the grid at most twice.
15+
* Space Complexity: O(m*n) - In the worst case (a grid full of land), the recursion
16+
* stack for DFS could go as deep as the number of cells.
17+
*/
18+
// package DSA_Code.Java.algorithms.matrix;
19+
20+
import java.util.Scanner;
21+
22+
public class NumberOfIslands {
23+
24+
/**
25+
* Main function to count the number of islands.
26+
*/
27+
public static int numIslands(char[][] grid) {
28+
if (grid == null || grid.length == 0) {
29+
return 0;
30+
}
31+
32+
int m = grid.length;
33+
int n = grid[0].length;
34+
int islandCount = 0;
35+
36+
for (int i = 0; i < m; i++) {
37+
for (int j = 0; j < n; j++) {
38+
// If we find a '1', it's a new island.
39+
// Increment count and sink the island with DFS.
40+
if (grid[i][j] == '1') {
41+
islandCount++;
42+
dfs(grid, i, j);
43+
}
44+
}
45+
}
46+
return islandCount;
47+
}
48+
49+
/**
50+
* Helper DFS function to find and "sink" all parts of an island.
51+
*/
52+
private static void dfs(char[][] grid, int r, int c) {
53+
int m = grid.length;
54+
int n = grid[0].length;
55+
56+
// Base cases for stopping recursion:
57+
// 1. Out of bounds (row or column)
58+
// 2. Current cell is water ('0')
59+
if (r < 0 || c < 0 || r >= m || c >= n || grid[r][c] == '0') {
60+
return;
61+
}
62+
63+
// Sink the current part of the island by changing it to '0'
64+
grid[r][c] = '0';
65+
66+
// Recursively call DFS for all 4 adjacent cells
67+
dfs(grid, r + 1, c); // down
68+
dfs(grid, r - 1, c); // up
69+
dfs(grid, r, c + 1); // right
70+
dfs(grid, r, c - 1); // left
71+
}
72+
73+
public static void main(String[] args) {
74+
Scanner sc = new Scanner(System.in);
75+
76+
System.out.print("Enter number of rows: ");
77+
int m = sc.nextInt();
78+
System.out.print("Enter number of columns: ");
79+
int n = sc.nextInt();
80+
81+
char[][] grid = new char[m][n];
82+
83+
System.out.println("Enter grid elements ('1' for land, '0' for water):");
84+
for (int i = 0; i < m; i++) {
85+
for (int j = 0; j < n; j++) {
86+
grid[i][j] = sc.next().charAt(0);
87+
}
88+
}
89+
90+
System.out.println("\nOriginal Grid:");
91+
for (char[] row : grid) {
92+
for (char cell : row) {
93+
System.out.print(cell + " ");
94+
}
95+
System.out.println();
96+
}
97+
98+
// Apply the algorithm
99+
int count = numIslands(grid);
100+
101+
System.out.println("\nNumber of islands found: " + count + " 🏞️");
102+
103+
sc.close();
104+
}
105+
}

0 commit comments

Comments
 (0)