Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count number of overlapping islands in two maps in Python
Suppose we have two binary matrices mat1 and mat2. Here 1 represents land and 0 represents water. If there is a group of 1s (land) surrounded by water, it's called an island. We need to find the number of islands that exist in both mat1 and mat2 at the exact same coordinates.
Problem Understanding
Given two matrices, we need to count overlapping islands where both matrices have land (1) at the same positions.
For example, if mat1 =
| 1 | 0 | 1 |
| 1 | 0 | 0 |
| 1 | 0 | 0 |
And mat2 =
| 1 | 0 | 1 |
| 1 | 0 | 0 |
| 1 | 0 | 1 |
The overlapping islands are highlighted below:
| 1 | 0 | 1 |
| 1 | 0 | 0 |
| 1 | 0 | 1 |
There are two overlapping islands: the left column forms one island, and the top-right cell forms another island.
Algorithm Steps
To solve this problem, we follow these steps:
- First, eliminate non-overlapping land cells by marking positions where matrices differ
- Use a recursive function to mark connected components (islands)
- Count remaining islands by traversing the modified matrices
Implementation
def solve(mat1, mat2):
r = len(mat1)
c = len(mat1[0])
last_row = r - 1
last_col = c - 1
def mark(i, j):
mat1[i][j] = mat2[i][j] = 0
if i and (mat1[i - 1][j] or mat2[i - 1][j]):
mark(i - 1, j)
if j and (mat1[i][j - 1] or mat2[i][j - 1]):
mark(i, j - 1)
if j < last_col and (mat1[i][j + 1] or mat2[i][j + 1]):
mark(i, j + 1)
if i < last_row and (mat1[i + 1][j] or mat2[i + 1][j]):
mark(i + 1, j)
# Mark all non-overlapping cells
for i in range(r):
for j in range(c):
if mat1[i][j] != mat2[i][j]:
mark(i, j)
# Count overlapping islands
islands = 0
for i in range(r):
for j in range(c):
if mat1[i][j]:
islands += 1
mark(i, j)
return islands
# Test example
mat1 = [
[1, 0, 1],
[1, 0, 0],
[1, 0, 0]
]
mat2 = [
[1, 0, 1],
[1, 0, 0],
[1, 0, 0]
]
print("Number of overlapping islands:", solve(mat1, mat2))
Number of overlapping islands: 2
How It Works
The algorithm works in two phases:
-
Elimination Phase: Remove all cells where the matrices differ using the
mark()function - Counting Phase: Count connected components of remaining land cells
The mark() function performs a depth-first search to mark all connected cells as water (0), effectively removing entire islands or isolated land cells.
Example with Different Matrices
def solve(mat1, mat2):
r = len(mat1)
c = len(mat1[0])
last_row = r - 1
last_col = c - 1
def mark(i, j):
mat1[i][j] = mat2[i][j] = 0
if i and (mat1[i - 1][j] or mat2[i - 1][j]):
mark(i - 1, j)
if j and (mat1[i][j - 1] or mat2[i][j - 1]):
mark(i, j - 1)
if j < last_col and (mat1[i][j + 1] or mat2[i][j + 1]):
mark(i, j + 1)
if i < last_row and (mat1[i + 1][j] or mat2[i + 1][j]):
mark(i + 1, j)
for i in range(r):
for j in range(c):
if mat1[i][j] != mat2[i][j]:
mark(i, j)
islands = 0
for i in range(r):
for j in range(c):
if mat1[i][j]:
islands += 1
mark(i, j)
return islands
# Example with no overlapping islands
mat1 = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
mat2 = [
[0, 1, 1],
[1, 0, 1],
[1, 1, 0]
]
print("Overlapping islands:", solve(mat1, mat2))
Overlapping islands: 0
Conclusion
This algorithm efficiently finds overlapping islands by first eliminating non-overlapping land cells, then counting connected components. The time complexity is O(r × c) where r and c are the matrix dimensions.
