forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest_square_area_in_matrix.py
188 lines (146 loc) · 5.47 KB
/
largest_square_area_in_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Question:
Given a binary matrix mat of size n * m, find out the maximum size square
sub-matrix with all 1s.
---
Example 1:
Input:
n = 2, m = 2
mat = [[1, 1],
[1, 1]]
Output:
2
Explanation: The maximum size of the square
sub-matrix is 2. The matrix itself is the
maximum sized sub-matrix in this case.
---
Example 2
Input:
n = 2, m = 2
mat = [[0, 0],
[0, 0]]
Output: 0
Explanation: There is no 1 in the matrix.
Approach:
We initialize another matrix (dp) with the same dimensions
as the original one initialized with all 0's.
dp_array(i,j) represents the side length of the maximum square whose
bottom right corner is the cell with index (i,j) in the original matrix.
Starting from index (0,0), for every 1 found in the original matrix,
we update the value of the current element as
dp_array(i,j)=dp_array(dp(i-1,j),dp_array(i-1,j-1),dp_array(i,j-1)) + 1.
"""
deflargest_square_area_in_matrix_top_down_approch(
rows: int, cols: int, mat: list[list[int]]
) ->int:
"""
Function updates the largest_square_area[0], if recursive call found
square with maximum area.
We aren't using dp_array here, so the time complexity would be exponential.
>>> largest_square_area_in_matrix_top_down_approch(2, 2, [[1,1], [1,1]])
2
>>> largest_square_area_in_matrix_top_down_approch(2, 2, [[0,0], [0,0]])
0
"""
defupdate_area_of_max_square(row: int, col: int) ->int:
# BASE CASE
ifrow>=rowsorcol>=cols:
return0
right=update_area_of_max_square(row, col+1)
diagonal=update_area_of_max_square(row+1, col+1)
down=update_area_of_max_square(row+1, col)
ifmat[row][col]:
sub_problem_sol=1+min([right, diagonal, down])
largest_square_area[0] =max(largest_square_area[0], sub_problem_sol)
returnsub_problem_sol
else:
return0
largest_square_area= [0]
update_area_of_max_square(0, 0)
returnlargest_square_area[0]
deflargest_square_area_in_matrix_top_down_approch_with_dp(
rows: int, cols: int, mat: list[list[int]]
) ->int:
"""
Function updates the largest_square_area[0], if recursive call found
square with maximum area.
We are using dp_array here, so the time complexity would be O(N^2).
>>> largest_square_area_in_matrix_top_down_approch_with_dp(2, 2, [[1,1], [1,1]])
2
>>> largest_square_area_in_matrix_top_down_approch_with_dp(2, 2, [[0,0], [0,0]])
0
"""
defupdate_area_of_max_square_using_dp_array(
row: int, col: int, dp_array: list[list[int]]
) ->int:
ifrow>=rowsorcol>=cols:
return0
ifdp_array[row][col] !=-1:
returndp_array[row][col]
right=update_area_of_max_square_using_dp_array(row, col+1, dp_array)
diagonal=update_area_of_max_square_using_dp_array(row+1, col+1, dp_array)
down=update_area_of_max_square_using_dp_array(row+1, col, dp_array)
ifmat[row][col]:
sub_problem_sol=1+min([right, diagonal, down])
largest_square_area[0] =max(largest_square_area[0], sub_problem_sol)
dp_array[row][col] =sub_problem_sol
returnsub_problem_sol
else:
return0
largest_square_area= [0]
dp_array= [[-1] *colsfor_inrange(rows)]
update_area_of_max_square_using_dp_array(0, 0, dp_array)
returnlargest_square_area[0]
deflargest_square_area_in_matrix_bottom_up(
rows: int, cols: int, mat: list[list[int]]
) ->int:
"""
Function updates the largest_square_area, using bottom up approach.
>>> largest_square_area_in_matrix_bottom_up(2, 2, [[1,1], [1,1]])
2
>>> largest_square_area_in_matrix_bottom_up(2, 2, [[0,0], [0,0]])
0
"""
dp_array= [[0] * (cols+1) for_inrange(rows+1)]
largest_square_area=0
forrowinrange(rows-1, -1, -1):
forcolinrange(cols-1, -1, -1):
right=dp_array[row][col+1]
diagonal=dp_array[row+1][col+1]
bottom=dp_array[row+1][col]
ifmat[row][col] ==1:
dp_array[row][col] =1+min(right, diagonal, bottom)
largest_square_area=max(dp_array[row][col], largest_square_area)
else:
dp_array[row][col] =0
returnlargest_square_area
deflargest_square_area_in_matrix_bottom_up_space_optimization(
rows: int, cols: int, mat: list[list[int]]
) ->int:
"""
Function updates the largest_square_area, using bottom up
approach. with space optimization.
>>> largest_square_area_in_matrix_bottom_up_space_optimization(2, 2, [[1,1], [1,1]])
2
>>> largest_square_area_in_matrix_bottom_up_space_optimization(2, 2, [[0,0], [0,0]])
0
"""
current_row= [0] * (cols+1)
next_row= [0] * (cols+1)
largest_square_area=0
forrowinrange(rows-1, -1, -1):
forcolinrange(cols-1, -1, -1):
right=current_row[col+1]
diagonal=next_row[col+1]
bottom=next_row[col]
ifmat[row][col] ==1:
current_row[col] =1+min(right, diagonal, bottom)
largest_square_area=max(current_row[col], largest_square_area)
else:
current_row[col] =0
next_row=current_row
returnlargest_square_area
if__name__=="__main__":
importdoctest
doctest.testmod()
print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))