
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find area of largest island in a matrix in Python
Suppose we have a binary matrix. Here 1 represents land and 0 represents water, And an island is a group of 1s that are neighboring whose perimeter is surrounded by water. We can assume that the edges of the matrix are surrounded by water. We have to find the area of the largest island in matrix.
So, if the input is like
0 | 0 | 1 | 1 | 1 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 1 | 1 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 1 | 0 |
then the output will be 6.
To solve this, we will follow these steps −
- Define a function dfs() . This will take matrix, r, c
- total := total + 1
- matrix[r, c] := 0
- if r - 1 >= 0 and matrix[r - 1, c] is same as 1, then
- dfs(matrix, r - 1, c)
- if c - 1 >= 0 and matrix[r, c - 1] is same as 1, then
- dfs(matrix, r, c - 1)
- if r + 1 < r_len and matrix[r + 1, c] is same as 1, then
- dfs(matrix, r + 1, c)
- if c + 1 < c_len and matrix[r, c + 1] is same as 1, then
- dfs(matrix, r, c + 1)
- From the main method, do the following −
- r_len := row count of matrix
- c_len := column count of matrix
- max_island := 0
- for r in range 0 to r_len - 1, do
- for c in range 0 to c_len - 1, do
- if matrix[r, c] is same as 1, then
- total := 0
- dfs(matrix, r, c)
- max_island := maximum of max_island, total
- if matrix[r, c] is same as 1, then
- for c in range 0 to c_len - 1, do
- return max_island
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, matrix): self.r_len = len(matrix) self.c_len = len(matrix[0]) max_island = 0 for r in range(self.r_len): for c in range(self.c_len): if matrix[r][c] == 1: self.total = 0 self.dfs(matrix, r, c) max_island = max(max_island, self.total) return max_island def dfs(self, matrix, r, c): self.total += 1 matrix[r][c] = 0 if r - 1 >= 0 and matrix[r - 1][c] == 1: self.dfs(matrix, r - 1, c) if c - 1 >= 0 and matrix[r][c - 1] == 1: self.dfs(matrix, r, c - 1) if r + 1 < self.r_len and matrix[r + 1][c] == 1: self.dfs(matrix, r + 1, c) if c + 1 < self.c_len and matrix[r][c + 1] == 1: self.dfs(matrix, r, c + 1) ob = Solution() matrix = [ [0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 0] ] print(ob.solve(matrix))
Input
matrix = [ [0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 0] ]
Output
6
- Related Articles
- Program to find area of largest square of 1s in a given matrix in python
- Program to find number of distinct island shapes from a given matrix in Python
- Program to find largest island after changing one water cell to land cell in Python
- Program to find largest rectangle area under histogram in python
- Program to find area of largest submatrix by column rearrangements in Python
- Program to find the perimeter of an island shape in Python
- C++ Program to Find Largest Rectangular Area in a Histogram
- Python program to find largest number in a list
- C++ program to find the Area of the Largest Triangle inscribed in a Hexagon?
- Program to find area of a polygon in Python
- Program to find diagonal sum of a matrix in Python
- Largest Triangle Area in Python
- Python program to find the largest number in a list
- Python Program to find the largest element in a tuple
- Program to find largest merge of two strings in Python

Advertisements