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 find distance of shortest bridge between islands in Python
Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands.
So, if the input is like:
| 0 | 0 | 1 |
| 1 | 0 | 1 |
| 1 | 0 | 0 |
then the output will be 1. This will connect (1,0) to (1,2) points.
Approach
To solve this problem, we use a two-step approach:
- Step 1: Use DFS to find all cells of the first island
- Step 2: Use BFS from the first island's boundary to find the shortest path to any cell of the second island
Algorithm Steps
The algorithm works as follows −
- Find the first island using DFS and mark all its cells
- Add all water cells adjacent to the first island to a queue for BFS
- Use BFS to expand from the first island until we reach the second island
- Return the distance when we find the first cell of the second island
Implementation
import collections
class Solution:
def solve(self, mat):
row = len(mat)
col = len(mat[0])
def dfs(i, j, island_cells):
if (i, j) in island_cells:
return
if mat[i][j] == 0:
return
island_cells.add((i, j))
# Check all 4 directions
if i - 1 >= 0:
dfs(i - 1, j, island_cells)
if i + 1 < row:
dfs(i + 1, j, island_cells)
if j - 1 >= 0:
dfs(i, j - 1, island_cells)
if j + 1 < col:
dfs(i, j + 1, island_cells)
# Find first island
seen = set()
for i in range(row):
if len(seen) > 0:
break
for j in range(col):
if mat[i][j] == 1:
dfs(i, j, seen)
break
# BFS from first island boundary
q = collections.deque()
# Add water cells adjacent to first island
for land in seen:
i, j = land
if i - 1 >= 0 and mat[i - 1][j] == 0:
q.append((i - 1, j, 1))
if i + 1 < row and mat[i + 1][j] == 0:
q.append((i + 1, j, 1))
if j - 1 >= 0 and mat[i][j - 1] == 0:
q.append((i, j - 1, 1))
if j + 1 < col and mat[i][j + 1] == 0:
q.append((i, j + 1, 1))
# BFS to find shortest path to second island
while len(q) > 0:
i, j, dist = q.popleft()
if (i, j) in seen:
continue
seen.add((i, j))
# If we found land (second island)
if mat[i][j] == 1:
return dist - 1
# Add adjacent cells to queue
if i - 1 >= 0:
q.append((i - 1, j, dist + 1))
if i + 1 < row:
q.append((i + 1, j, dist + 1))
if j - 1 >= 0:
q.append((i, j - 1, dist + 1))
if j + 1 < col:
q.append((i, j + 1, dist + 1))
# Test the solution
ob = Solution()
matrix = [
[0, 0, 1],
[1, 0, 1],
[1, 0, 0],
]
print(ob.solve(matrix))
The output of the above code is −
1
How It Works
The algorithm works in two phases:
- DFS Phase: Identifies all cells belonging to the first island found
- BFS Phase: Starts from the water cells adjacent to the first island and expands outward until it reaches the second island
The BFS ensures we find the shortest path because it explores cells level by level, guaranteeing the first time we reach the second island gives us the minimum distance.
Time and Space Complexity
- Time Complexity: O(m × n), where m and n are the matrix dimensions
- Space Complexity: O(m × n) for the seen set and queue
Conclusion
This problem combines DFS for island identification and BFS for shortest path finding. The key insight is to start BFS from the boundary of one island to find the minimum bridge length to another island.
