MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / depth_first_search

Function depth_first_search

matrix/max_area_of_island.py:33–52  ·  view source on GitHub ↗

Returns the current area of the island >>> depth_first_search(0, 0, set(), matrix) 0

(row: int, col: int, seen: set, mat: list[list[int]])

Source from the content-addressed store, hash-verified

31
32
33def depth_first_search(row: int, col: int, seen: set, mat: list[list[int]]) -> int:
34 """
35 Returns the current area of the island
36
37 >>> depth_first_search(0, 0, set(), matrix)
38 0
39 """
40 rows = len(mat)
41 cols = len(mat[0])
42 if is_safe(row, col, rows, cols) and (row, col) not in seen and mat[row][col] == 1:
43 seen.add((row, col))
44 return (
45 1
46 + depth_first_search(row + 1, col, seen, mat)
47 + depth_first_search(row - 1, col, seen, mat)
48 + depth_first_search(row, col + 1, seen, mat)
49 + depth_first_search(row, col - 1, seen, mat)
50 )
51 else:
52 return 0
53
54
55def find_max_area(mat: list[list[int]]) -> int:

Callers 1

find_max_areaFunction · 0.70

Calls 2

is_safeFunction · 0.70
addMethod · 0.45

Tested by

no test coverage detected