Open In App

Python Program for Maximum size square sub-matrix with all 1s

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
        • S[i][j] = 0
  • 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




# Python3 code for Maximum size
# square sub-matrix with all 1s
 
def printMaxSubSquare(M):
    R = len(M) # no. of rows in M[][]
    C = len(M[0]) # no. of columns in M[][]
 
    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,
    # here we have set the first row and first column of S same as input matrix, other entries are set to 0
 
    # Update other entries
    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
 
    # Find the maximum entry and
    # indices of maximum entry in S[][]
    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("")
 
 
# Driver Program
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)
 
# This code is contributed by Soumen Ghosh


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




# Python code for Maximum size square
# sub-matrix with all 1s
# (space optimized solution)
 
R = 6
C = 5
 
 
def printMaxSubSquare(M):
 
    global R, C
    Max = 0
 
    # set all elements of S to 0 first
    S = [[0 for col in range(C)]for row in range(2)]
 
    # Construct the entries
    for i in range(R):
        for j in range(C):
 
            # Compute the entrie at the current position
            Entrie = M[i][j]
            if(Entrie):
                if(j):
                    Entrie = 1 + min(S[1][j - 1], min(S[0][j - 1], S[1][j]))
 
            # Save the last entrie and add the new one
            S[0][j] = S[1][j]
            S[1][j] = Entrie
 
            # Keep track of the max square length
            Max = max(Max, Entrie)
 
    # Print the square
    print("Maximum size sub-matrix is: ")
    for i in range(Max):
        for j in range(Max):
            print("1", end=" ")
        print()
 
 
# Driver code
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)
 
# This code is contributed by shinjanpatra


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!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads