Skip to content

Commit efd2b30

Browse files
author
Priyal595
committed
move work to new branch
1 parent cf6641e commit efd2b30

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

matrix/count_islands_in_matrix.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# An island in matrix is a group of linked areas, all having the same value.
22
# This code counts number of islands in a given matrix, with including diagonal
33
# connections.
4-
5-
64
class Matrix: # Public class to implement a graph
7-
<<<<<<< Updated upstream
8-
=======
95
"""This public class represents the 2-Dimensional matrix to count
106
the number of islands.An island is the connected group of 1s,including the top,
117
down, right, left as well as the diagonal connections.
@@ -16,14 +12,34 @@ class Matrix: # Public class to implement a graph
1612
>>> matrix2.count_islands()
1713
1
1814
"""
19-
20-
>>>>>>> Stashed changes
2115
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
16+
"""Initialzes the matrix with the given number of rows, columns and matrix.
17+
Args:
18+
row (int): number of rows in the matrix
19+
col (int): number of columns in the matrix
20+
graph (list[list[bool]]): 2-D list of 0s and 1s representing the matrix
21+
"""
2222
self.ROW = row
2323
self.COL = col
2424
self.graph = graph
2525

2626
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
27+
"""This checks if the current cell can be included in the current island.
28+
Args:
29+
i (int): row index
30+
j (int): column index
31+
visited (list[list[bool]]): 2D list tracking the visited cells
32+
Returns:
33+
bool: True if the cell is valid and part of the island
34+
(1 for True and ) for False)
35+
>>> visited = [[False, False], [False, False]]
36+
>>> graph = [[1, 0], [0, 1]]
37+
>>> m = Matrix(2, 2, graph)
38+
>>> m.is_safe(0, 0, visited)
39+
1
40+
>>> m.is_safe(0, 1, visited)
41+
0
42+
"""
2743
return (
2844
0 <= i < self.ROW
2945
and 0 <= j < self.COL
@@ -32,6 +48,19 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
3248
)
3349

3450
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
51+
"""This is the recursive function to mark all the cells visited which
52+
are connected to (i, j) indices.
53+
Args:
54+
i (int): row index
55+
j (int): column index
56+
visited (list[list[bool]]): 2D list tracking the visited cells
57+
>>> visited = [[False, False], [False, False]]
58+
>>> graph = [[1, 1], [0, 1]]
59+
>>> m = Matrix(2, 2, graph)
60+
>>> m.diffs(0, 0, visited)
61+
>>> visited
62+
[[True, True], [False, True]]
63+
"""
3564
# Checking all 8 elements surrounding nth element
3665
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
3766
col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1]
@@ -41,6 +70,18 @@ def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
4170
self.diffs(i + row_nbr[k], j + col_nbr[k], visited)
4271

4372
def count_islands(self) -> int: # And finally, count all islands.
73+
"""
74+
This counts all the islands in the given matrix.
75+
Returns:
76+
int: the number of islands in the given matrix.
77+
Example -
78+
>>> mat = Matrix(1, 1, [[1]])
79+
>>> mat.count_islands()
80+
1
81+
>>> mat2 = Matrix(2, 2, [[0, 0], [0, 0]])
82+
>>> mat2.count_islands()
83+
0
84+
"""
4485
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
4586
count = 0
4687
for i in range(self.ROW):

0 commit comments

Comments
 (0)