Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a square matrix, where each cell is black or white. Design an algorithm to find the max sub-square such that all 4 borders are black

Given a square matrix, where each cell is black or white. Design an algorithm to find the max sub-square such that all 4 borders are black.

I have O(n^2) algorithm:

Scan each column from left to right, for each cell in each column, scan each row to find the max sub-square with back borders.

Are there better solutions ?

thanks

like image 466
user1002288 Avatar asked Nov 11 '11 17:11

user1002288


People also ask

What is the order of the largest square sub Matrix?

The largest square submatrix is formed by cells (0, 2) , (3, 2) , (0, 5) , and (3, 5) . The brute-force solution is to consider every square submatrix and check if it is surrounded by all 1's . We keep track of the dimensions of the largest square submatrix seen and finally return it.

What is square Submatrix?

A sub-matrix is a matrix obtained from the given matrix by deletion of several (possibly, zero or all) rows/columns from the beginning and several (possibly, zero or all) rows/columns from the end. A square matrix is a matrix which has the same number of rows and columns.

What is square matrix in data structure?

A square matrix is a matrix which includes elements in the form of Rows and Columns. Below is an example of a 5×5 matrix. A 5×5 Square Matrix. A Matrix is accessed by: Matrix_Name[row_index][column_index]

What is square matrix array?

A square matrix is a square array of numbers where the number of rows and columns are equal. The plural of matrix is matrices. Each number in the matrix is called an entry. Each entry is labeled based on its position in the matrix.


1 Answers

O(n^2) is possible. I guess it is optimal, as you have n^2 cells.

Notice that the top-left corner and the bottom-right corner of any square lie along the same diagonal.

Now if we could process each diagonal in O(n) time, we would have an O(n^2) time algorithm.

Say we have a candidate for a top-left corner. We can compute the number of continuous black cells below it, and to the right of it and take the minimum of the two and call it T.

For a bottom-right candidate, we can compute the number of continuous black cells to the left of it, and to the top and take the minimum of the two, call it B.

Once we have the two numbers T and B, we can tell if the given top-left, bottom-right candidate actually give us a square with all black borders.

Now those two numbers can be computed for each cell, in O(n^2) time by making four passes through the whole matrix (How?).

So let us assume that is done.

Now consider a diagonal. Our aim is to find a top-left,bottom-right pair along this diagonal in O(n) time.

Let us assume we have the T's computed in an array T[1...m] where m is the length of the diagonal. Similarly we have an array B[1...m]. T[1] corresponds to the top-left end of the diagonal and T[m] to the bottom-right. Similarly with B.

Now we process the diagonal as follows, for each top-left candidate i, we try to find a bottom-right candidate j which will give the largest square. Notice that j >= i. Also notice that if (i,j) is a candidate, then (i',j) isn't, where i' > i.

Note that i and j form a square if T[i] >= j-i+1 and B[j] >= j-i+1.

i.e. T[i] +i - 1 >= j and B[j] -j - 1 >= -i.

So we form new arrays such that TL[k] = T[k] + k -1 and BR[k] = B[k] -k - 1.

So given the two new arrays TL and BR, and an i, we need to answer the following queries:

What is the largest j such that TL[i] >= j and BR[j] >= -i ?

Now suppose we were able to process BR for range maximum queries (can be done in O(m) time).

Now given TL[i], in the range [i, TL[i]] we find the maximum value of BR, say BR[j1].

Now if BR[j1] >= -i, we find the maximum of BR in the range [j1, TL[i]] and continue this way.

Once we find the (TL[i],BR[j]) candidate, we can ignore the array BR[1...j] for future i.

This allows us to process each diagonal in O(n) time, giving an O(n^2) total algorithm.

I have left out a lot of details and given a sketch, as it was already too long. Feel free to edit this with clarifications.

Phew.

like image 56
user127.0.0.1 Avatar answered Oct 17 '22 13:10

user127.0.0.1