Python Program for Maximum size square sub-matrix with all 1s
Last Updated :
24 Oct, 2023
Write a Python program for a given binary matrix, the task is to find out the maximum size square sub-matrix with all 1s.
Approach:
Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an auxiliary size matrix S[][] in which each entry S[i][j] represents the size of the square sub-matrix with all 1s including M[i][j] where M[i][j] is the rightmost and bottom-most entry in sub-matrix.
Step-by-step approach:
- Construct a sum matrix S[R][C] for the given M[R][C].
- Copy first row and first columns as it is from M[][] to S[][]
- For other entries, use the following expressions to construct S[][]
- If M[i][j] is 1 then
- S[i][j] = min(S[i][j-1], S[i-1][j], S[i-1][j-1]) + 1
- Else If M[i][j] is 0 then
- Find the maximum entry in S[R][C]
- Using the value and coordinates of maximum entry in S[i], print sub-matrix of M[][]
Below is the implementation of the above approach:
Python3
def printMaxSubSquare(M):
R = len (M)
C = len (M[ 0 ])
S = []
for i in range (R):
temp = []
for j in range (C):
if i = = 0 or j = = 0 :
temp + = M[i][j],
else :
temp + = 0 ,
S + = temp,
for i in range ( 1 , R):
for j in range ( 1 , C):
if (M[i][j] = = 1 ):
S[i][j] = min (S[i][j - 1 ], S[i - 1 ][j],
S[i - 1 ][j - 1 ]) + 1
else :
S[i][j] = 0
max_of_s = S[ 0 ][ 0 ]
max_i = 0
max_j = 0
for i in range (R):
for j in range (C):
if (max_of_s < S[i][j]):
max_of_s = S[i][j]
max_i = i
max_j = j
print ( "Maximum size sub-matrix is: " )
for i in range (max_i, max_i - max_of_s, - 1 ):
for j in range (max_j, max_j - max_of_s, - 1 ):
print (M[i][j], end = " " )
print ("")
M = [[ 0 , 1 , 1 , 0 , 1 ],
[ 1 , 1 , 0 , 1 , 0 ],
[ 0 , 1 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 1 , 1 ],
[ 0 , 0 , 0 , 0 , 0 ]]
printMaxSubSquare(M)
|
Output
Maximum size sub-matrix is:
1 1 1
1 1 1
1 1 1
Time Complexity: O(m*n), where m is the number of rows and n is the number of columns in the given matrix.
Auxiliary Space: O(m*n), where m is the number of rows and n is the number of columns in the given matrix.
Python Program for Maximum size square sub-matrix with all 1s using Dynamic Programming:
In order to compute an entry at any position in the matrix we only need the current row and the previous row.
Below is the implementation of the above approach:
Python3
R = 6
C = 5
def printMaxSubSquare(M):
global R, C
Max = 0
S = [[ 0 for col in range (C)] for row in range ( 2 )]
for i in range (R):
for j in range (C):
Entrie = M[i][j]
if (Entrie):
if (j):
Entrie = 1 + min (S[ 1 ][j - 1 ], min (S[ 0 ][j - 1 ], S[ 1 ][j]))
S[ 0 ][j] = S[ 1 ][j]
S[ 1 ][j] = Entrie
Max = max ( Max , Entrie)
print ( "Maximum size sub-matrix is: " )
for i in range ( Max ):
for j in range ( Max ):
print ( "1" , end = " " )
print ()
M = [[ 0 , 1 , 1 , 0 , 1 ],
[ 1 , 1 , 0 , 1 , 0 ],
[ 0 , 1 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 1 , 1 ],
[ 0 , 0 , 0 , 0 , 0 ]]
printMaxSubSquare(M)
|
Output
Maximum size sub-matrix is:
1 1 1
1 1 1
1 1 1
Time Complexity: O(m*n) where m is the number of rows and n is the number of columns in the given matrix.
Auxiliary space: O(n) where n is the number of columns in the given matrix.
Please refer complete article on Maximum size square sub-matrix with all 1s for more details!
Share your thoughts in the comments
Please Login to comment...