mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-12 09:39:49 +00:00
Merge d02fb3b4537a7ec657faf9a0451b38fe4e681f0a into e59d819d091efdb30e385f4ecfe9ab5d36c3be71
This commit is contained in:
commit
9e13543553
@ -4,12 +4,43 @@
|
|||||||
|
|
||||||
|
|
||||||
class Matrix: # Public class to implement a graph
|
class Matrix: # Public class to implement a graph
|
||||||
def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
|
def __init__(self, graph: list[list[bool]]) -> None:
|
||||||
self.ROW = row
|
"""
|
||||||
self.COL = col
|
Initialise matrix with number of rows, columns, and graph.
|
||||||
|
|
||||||
|
>>> m = Matrix([[True, False, False, False],
|
||||||
|
... [True, False, True, False],
|
||||||
|
... [False, False, True, True]])
|
||||||
|
>>> m.ROW
|
||||||
|
3
|
||||||
|
>>> m.COL
|
||||||
|
4
|
||||||
|
>>> m.graph
|
||||||
|
[[True, False, False, False],
|
||||||
|
...[True, False, True, False],
|
||||||
|
...[False, False, True, True]]
|
||||||
|
"""
|
||||||
self.graph = graph
|
self.graph = graph
|
||||||
|
self.ROW = len(graph)
|
||||||
|
self.COL = len(graph[0])
|
||||||
|
|
||||||
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
|
def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
|
||||||
|
"""
|
||||||
|
>>> visited = [[False, False, False],
|
||||||
|
... [False, False, False],
|
||||||
|
... [False, False, False]]
|
||||||
|
>>> m = Matrix([[True, False, False],
|
||||||
|
... [False, False, True],
|
||||||
|
... [False, False, True]])
|
||||||
|
>>> m.is_safe(0, 0, visited)
|
||||||
|
True
|
||||||
|
>>> m.is_safe(0, 2, visited)
|
||||||
|
False
|
||||||
|
>>> m.is_safe(-1, 2, visited)
|
||||||
|
False
|
||||||
|
>>> m.is_safe(1, 5, visited)
|
||||||
|
False
|
||||||
|
"""
|
||||||
return (
|
return (
|
||||||
0 <= i < self.ROW
|
0 <= i < self.ROW
|
||||||
and 0 <= j < self.COL
|
and 0 <= j < self.COL
|
||||||
@ -18,7 +49,21 @@ class Matrix: # Public class to implement a graph
|
|||||||
)
|
)
|
||||||
|
|
||||||
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
|
def diffs(self, i: int, j: int, visited: list[list[bool]]) -> None:
|
||||||
# Checking all 8 elements surrounding nth element
|
"""
|
||||||
|
Checking all 8 elements surrounding nth element.
|
||||||
|
|
||||||
|
>>> visited = [[False, False, False],
|
||||||
|
... [False, False, False],
|
||||||
|
... [False, False, False]]
|
||||||
|
>>> m = Matrix([[True, True, False],
|
||||||
|
... [False, True, False],
|
||||||
|
... [True, False, True]])
|
||||||
|
>>> m.diffs(0, 0, visited)
|
||||||
|
>>> visited
|
||||||
|
[[True, True, False],
|
||||||
|
...[False, True, False],
|
||||||
|
...[True, False, True]]
|
||||||
|
"""
|
||||||
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
|
row_nbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order
|
||||||
col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1]
|
col_nbr = [-1, 0, 1, -1, 1, -1, 0, 1]
|
||||||
visited[i][j] = True # Make those cells visited
|
visited[i][j] = True # Make those cells visited
|
||||||
@ -26,12 +71,24 @@ class Matrix: # Public class to implement a graph
|
|||||||
if self.is_safe(i + row_nbr[k], j + col_nbr[k], visited):
|
if self.is_safe(i + row_nbr[k], j + col_nbr[k], visited):
|
||||||
self.diffs(i + row_nbr[k], j + col_nbr[k], visited)
|
self.diffs(i + row_nbr[k], j + col_nbr[k], visited)
|
||||||
|
|
||||||
def count_islands(self) -> int: # And finally, count all islands.
|
def count_islands(self) -> int:
|
||||||
|
"""
|
||||||
|
>>> m = Matrix([[True, True, False, False],
|
||||||
|
... [False, True, False, True],
|
||||||
|
... [True, False, False, True]])
|
||||||
|
>>> m.count_islands()
|
||||||
|
2
|
||||||
|
>>> m2 = Matrix([[True, True, False],
|
||||||
|
... [True, False, False],
|
||||||
|
... [False, False, True]])
|
||||||
|
>>> m2.count_islands()
|
||||||
|
2
|
||||||
|
"""
|
||||||
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
|
visited = [[False for j in range(self.COL)] for i in range(self.ROW)]
|
||||||
count = 0
|
count = 0
|
||||||
for i in range(self.ROW):
|
for i in range(self.ROW):
|
||||||
for j in range(self.COL):
|
for j in range(self.COL):
|
||||||
if visited[i][j] is False and self.graph[i][j] == 1:
|
if not visited[i][j] and self.graph[i][j]:
|
||||||
self.diffs(i, j, visited)
|
self.diffs(i, j, visited)
|
||||||
count += 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
|
Loading…
x
Reference in New Issue
Block a user