Python find the largest square in the matrix dynamic programming

Mahi008

I'm learning algorithm, as a beginner I have a matrix as follows (Python) :

matrix = """
   ...o..o.o
   ...oo....
   ...o....o
   ..o.ooo..
   o...o....
   .oo......
   ..o....o.
   .oo......
   .........
"""

where "o" is an obstacle and I need to find the biggest square in this matrix. and replace corresponding '.' with 'x' like below

"""
   xxxo..o.o
   xxxoo....
   xxxo....o
   ..o.ooo..
   o...o....
   .ooxxxx..
   ..oxxxxo.
   .ooxxxx..
   ...xxxx..
""

found similar questions here(SO), but nothing helped.

Marco Luzzara

It can be done with a complexity of O(n²) using dynamic programming. The idea is that you have a bigger square only when the up, the left and the up-left squares have the same dimension. Otherwise the max square of the current cell is just the smallest square among the squares considered above, increased by 1. Here is the code:

matrix = """
   ...o..o.o
   ...oo....
   ...o....o
   ..o.ooo..
   o........
   .oo......
   ..o....o.
   .oo......
   .........
"""

matrix = [list(r) for r in matrix.split()]
        
dp = [[0] * len(matrix) for _ in range(len(matrix))]
# max_square_dim, row, column
max_squares = [(0, None, None)]

for ri, r in enumerate(matrix):
    for ci, c in enumerate(r):
        dp[ri][ci] = 0 if c == 'o' \
            else (1 if ri == 0 or ci == 0 
            else min(dp[ri - 1][ci], dp[ri][ci - 1], dp[ri - 1][ci - 1]) + 1)
        
        max_squares = [(dp[ri][ci], ri, ci)] if dp[ri][ci] > max_squares[0][0] \
            else (max_squares + [(dp[ri][ci], ri, ci)] if dp[ri][ci] == max_squares[0][0]
            else max_squares)
            

for max_square in max_squares:
    for r in matrix[max_square[1] - max_square[0] + 1:max_square[1] + 1]:
        r[max_square[2] - max_square[0] + 1:max_square[2] + 1] = ['x'] * max_square[0]
      
result = '\n'.join([''.join(r) for r in matrix])
        
print(result)

In the end, when you have to replace the biggest square with all xs, you simply retrieve the indexes of the bottom-right vertex of the max square (stored in max_square) and do a list substitution.


EDIT: in case you have multiple biggest square, instead of declaring a single max_square, you have a list of them (in the update code I renamed it to max_squares). Then, every time you encounter a square with the same dimension as the max one, you just append it to the max_squares. However, consider the possibility of overlapping squares.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find largest square of palindrome in a matrix

Algorithm to find largest identical-row square in matrix

Dynamic programming: finding largest triangle

How to find the biggest sum of a square in a matrix using python

Find sum of the third square of the matrix

find the largest and the second largest numbers out of five numbers in C programming

find the row of a matrix with the largest element in column i

Find the largest basin size in a given matrix

Find length of the largest region in Boolean Matrix

Fill diagonals of a square matrix in python

Dynamic programming algorithm for n x n matrix of integers to find maximum sum

could not find function "is.square.matrix"

Algorithm to find the largest square number smaller than n

How to find linearly independent vectors belonging to the null space of a non-square matrix? (Python)

Find largest value from multiple colums in each group of row index in Python, arrange those values diagonally in matrix, and find determinant

Dynamic programming: find the maximum revenue

Dynamic Matrix in Python?

Matrix Sum Top down Dynamic Programming

Efficient Dynamic Programming rectangular queries in a boolean matrix

compute matrix distance using dynamic programming

Final product of matrix multiplication in dynamic programming?

Square Matrix in Python with values -1 or 0 or 1

python pandas crosstab sumproduct square matrix

Change square matrix in diagonal without numpy - Python

How to square the individual matrix value using python?

Check if a 2d matrix is square in python

Creating a square matrix from a .txt file in Python

How to find largest count using table in a matrix in R

Quickest way to find the nth largest value in a numpy Matrix