Find the largest chessboard surface in 2D Matrix - algorithm

I'm having a bit of difficulty by solving an exercise, hope I can get some help from you.
Given a 2D Array with N rows and M columns, and the array has only elements with the value 0 and 1.
Find the largest surface that is similar to a chessboard(where 0 are the white squares and 1 the black squares) and print the size of the surface(number of squares).
Constraints:
2<=N<=1000
2<=M<=1000
Example:
N=4, M=5
0 1 1 0 1
1 0 1 0 1
0 0 1 1 0
1 1 0 1 1
Output: Number of squares=5 (row 2-from column 1 to column 5)
Example:
N=3, M=4
0 0 1 0
1 1 0 0
1 0 1 0
Output: Number of squares=6 (from row 1 to row 3- from column 2 to column 3)

Flip every second cell in a checkerboard pattern
Find the largest rectangle containing only 0s or only 1s.
See Find largest rectangle containing only zeros in an N×N binary matrix for help on the 2nd part.
For your second example:
0 0 1 0
1 1 0 0
1 0 1 0
flipping every 2nd cell produces:
0 1 1 1
0 1 1 0
1 1 1 1
And you can see the rectangle of 1s that you're looking for.

Related

Finding the largest submatrix contain only one zero and 1's in a matrix

I need to find a submatrix that only contain a 0,but others are 1.
Here's an example:
5*4 matrix
1 0 1 1 1
1 1 1 0 0
1 1 1 0 1
0 1 0 1 1
And the answer will be
3*3
1 0 1
1 1 1
1 1 1
I tried to use other similar questions' methods.But so far I can't figure out how to calculate it without using brutal solution.

Finding biggest square sub matrix has values 1 in a Matrix which has values 1 and 0

i have axb matrix has values 1 or 0, I need to find the largest square sub matrix includes only ones. And i need to understdand how to do it. i mean i neeed algorthim. Example:
Matrix is 5x5 [ 1 1 1 1 1
1 1 1 0 0
1 1 1 0 0
1 0 1 1 1
1 1 1 1 1 ] largest is 3x3 , starting position 0,0 and return value 3
another example:
Matrix is 5x5 [ 0 1 1 1 1
0 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 0 1 0 1 ]
largest is 4x4 , starting position 0,1 and return value 4
Since program will be done multiple programing languages i need an algorithm. But you can basicly write a code for "C" to explain...
I do not know if this is the optimal solution but it is a simple idea.
Take you 5x5 matrix and create from it a 4x4 matrix in the following way:
Each 2x2 matrix it turning to a 1x1 matrix which is 1 if all of the original numbers where 1 and else - 0.
Matrix is 5x5 [ 1 1 1 1 1
1 1 1 0 0
1 1 1 0 0
1 0 1 1 1
1 1 1 1 1 ]
Will turn to
Matrix is 4x4 [ 1 1 0 0
1 1 0 0
0 0 0 0
0 0 1 1 ]
Now we repeat the process:
Matrix is 3x3 [ 1 0 0
0 0 0
0 0 0 ]
And again:
Matrix is 2x2 [ 0 0
0 0 ]
Now this is all zeros, we can search for the smallest matrix that still contains 1's. This can tell us where the original 1's matrix was and the size of the matrix relatively to the size of the original matrix can tell us how large it was. In our case it's top left in 3x3 so originally it was a 3x3 matrix at the top left.
The action we are performing is actually a convolution with 2x2 matrix of 1's and than rounding down. A Lot of programing languages have a library with this functionality.
The time complexity for a single convolution is O(n^2), but we are performing this n times so the time complexity is O(n^3).
Another example:
Matrix is 5x5 [ 0 1 1 1 1
0 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 0 1 0 1 ]
Matrix is 4x4 [ 0 1 1 1
0 1 1 1
1 1 1 1
0 0 0 0 ]
Matrix is 3x3 [ 0 1 1
0 1 1
0 0 0 ]
Matrix is 2x2 [ 0 1
0 0 ]
Matrix is 1x1 [ 0 ]
So the top right in the 2x2 is the last left which mean the original was top right 4x4.

How to calculate the max number of evenly spaced points on a grid

I need to create a grid that has x columns, y rows, where each cell in the grid can have a point in it, and each point is at least z horizontal/vertical spaces away from all other points.
1) Is there a simple method to calculate the maximum number of points I can place given the grid size and a spacing of z?
Here's an example 5x5 with z = 2. In this case the max number of points is 13.
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
and here's an example 5x5 where z = 3. In this case the max number of points is 6.
1 0 0 1 0
0 0 0 0 0
0 1 0 0 1
0 0 0 0 0
1 0 0 1 0
I'd like to be able, given x, y, and z, the number of points possible to plot.
2) What's the most efficient way of populating the grid in this manner?

How to make all entries of 4x4 matrix to either 1 or 0 when only one operation is allowed?

I have a 4x4 matrix. Say
0 1 1 0
0 0 1 0
0 1 1 0
0 1 1 1
My task is to transform the matrix to either
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
or
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Single operation is defined as pick any element of the matrix and then replace the element and elements to its left, right, up and down with their xor with 1.
Example
0 1 1 0
0 0 1 0
0 1 1 0
0 1 1 1
operation on marked element will transform the matrix to
0 1 0 0
0 1 0 1
0 1 0 0
0 1 1 1
My objective is to calculate the minimum number of operations needed to get the final result.
I don't even understand under what category this problem lies? Is this branch and bound, bactracking or something else.
What you've described is called lights out puzzle. There's relevant OEIS that gives you the minimal number of nontrivial switch flippings needed to solve the all-ones lights out problem on an n X n square.
Wikipedia describes a general sketch of algorithm how to solve it. You might be also interested in this answer here on SO, which includes a link to example implementation. Citing the answer:
The idea is to set up a matrix representing the button presses a column vector representing the lights and then to use standard matrix simplification techniques to determine which buttons to press. It runs in polynomial time and does not require any backtracking.

An algorithm to detect permutations of Hankel matrices

I am trying to write code to detect if a matrix is permutation of a Hankel matrix but I can't think of an efficient solution other than very slow brute force. Here is the spec.
Input: An n by n matrix M whose entries are 1 or 0.
Input format: Space separated rows. One row per line. For example
0 1 1 1
0 1 0 1
0 1 0 0
1 0 1 1
Output: A permutation of the rows and columns of M so that M is a Hankel matrix if that is possible. A Hankel matrix has constant skew-diagonals (positive sloping diagonals).
When I say a permutation, I mean we can apply one permutation to the order of the rows and a possibly different one to the columns.
I would be very grateful for any ideas.
Without Loss of Generality, we will assume that there are fewer 0's than 1's. We can then find the possible diagonals in a Hankel Matrix that could be 0's to give us the appropriate number of 0's in the entire matrix. And, this will give us the possible Hankel matrices. From there, you can count the number of 0's in each column, and compare it to the number of 0's in the columns of the original matrix. Once you have done this, you have a much smaller space in which to perform a brute force search: permuting on columns and rows that have the right number of 0's.
Example: OP's suggested a 4x4 matrix with 7 0's. We need to partition this using the set {4,3,3,2,2,1,1}. So, or partitions would be:
{4,3}
{4,2,1} (2 of these matrices)
{3,3,1}
{3,2,2}
{3,2,1,1} (2 of these matrices)
And this gives us the Hankel Matrices (excluding symmetries)
1 1 0 0 1 1 1 0 0 1 1 0 1 1 0 1
1 0 0 1 1 1 0 1 1 1 0 1 1 0 1 0
0 0 1 1 1 0 1 0 1 0 1 0 0 1 0 1
0 1 1 1 0 1 0 0 0 1 0 1 1 0 1 0
1 0 0 1 0 1 1 1 0 1 0 1
0 0 1 1 1 1 1 0 1 0 1 1
0 1 1 0 1 1 0 0 0 1 1 0
1 1 0 1 1 0 0 0 1 1 0 0
The original matrix had columns with 3, 1, 2, and 1 0's in its four columns. Comparing this to the 7 possible Hankel matrices gives us 2 possibilities
1 1 1 0 0 1 1 1
1 1 0 1 1 1 1 0
1 0 1 0 1 1 0 0
0 1 0 0 1 0 0 0
Now, there are only 4 possible permutations that could map the original matrix to each of these: we have only 1 choice based on the columns with 2 and 3 0's, but 2 choices for the columns with 1 0's, and also 2 choices for the rows with 1 0's. Checking those permutations, we see that the following Hankel matrix is a permutation of the original
0 1 1 1
1 1 1 0
1 1 0 0
1 0 0 0
The one thing which the first answer to this question got right is that permuting the rows and columns doesn't change the row sums or column sums.
Another easy observation is that in a Hankel matrix, the difference in row sum between two consecutive rows is -1, 0, or 1, and each case gives us a constraint on the rows. If the difference is 0 then the entering variable is equal to the exiting variable; otherwise we know which is 0 and which is 1.
0 1 1 1
0 1 0 1
0 1 0 0
1 0 1 1
has row sums 3, 2, 1, 3. The orders which respect the difference requirement are 1 2 3 3 and 3 3 2 1, and wlog we can discard reversals because reversing the row and column permutations just rotates the matrix by 180 degrees. Therefore we reduce to considering four permuted matrices (two possible orderings of the 3s in the row sums, and two in the column sums):
0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0
1 1 0 1 0 1 1 1 1 1 1 0 0 1 1 1
We could actually have taken the analysis further by observing that by forcing the initial rows to have sums 1 and 2 we constrain the order of the columns with sum 3, since
0 0 1 0
0 0 1 1
is not a valid initial two rows of a Hankel matrix. Whether or not this kind of reasoning is easy to implement depends on your programming paradigm.
Note that in the worst case this kind of reasoning still doesn't leave a polynomial number of cases to brute force through.
Here are some ideas.
1)
Row and Column permutations preserve the row and column sums:
1 0 1 0 - 2
0 0 0 1 - 1 row sums
1 0 0 0 - 1
1 1 1 0 - 3
| | | |
3 1 2 1
column sums
Whichever way you permute the rows, the row sums will still be {2, 1, 1, 3} in some permutation; the column sums will be unchanged. And vice versa. Hankel matrices and their permutations will always have the same set of row sums as column sums. This gives you a quick test to rule out a set of non-viable matrices.
2)
I posit that Hankel matrices can always be permuted in such a way that their row and column sums are in ascending order, and the result is still a Hankel matrix:
0 1 1 0 - 2 0 0 0 1 - 1
1 1 0 0 - 2 0 0 1 1 - 2
1 0 1 1 - 3 --> 0 1 1 0 - 2
0 0 1 0 - 1 1 1 0 1 - 3
| | | | | | | |
2 2 3 1 1 2 2 3
Therefore if a matrix can be permuted into a Hankel matrix, then it can also be permuted into a Hankel matrix of ascending row and column sum. That is, we can reduce the number of permutations needed to test by only testing permutations where the row and column sums are in ascending order.
3)
I posit further that for any Hankel matrix where two or more rows have the same sum, every permutation of columns has a matching permutation of rows that also produces a Hankel matrix. That is, if a Hankel matrix exists for one permutation of columns, then it exists for every permutation of columns - since we can simply apply that same permutation to the corresponding rows and achieve a symmetrical result.
The upshot is that we only need to test permutations of rows or columns, not rows and columns.
Applied to the original example:
1 0 1 0 - 2 0 0 0 1 0 1 0 0 - 1 0 0 0 1
0 0 0 1 - 1 1 0 0 0 0 0 0 1 - 1 0 1 0 0
1 0 0 0 - 1 --> 1 0 1 0 --> 0 0 1 1 - 2 --> 0 0 1 1 = Hankel!
1 1 1 0 - 3 1 1 1 0 1 0 1 1 - 3 1 0 1 1
| | | |
3 1 2 1 permute rows into| ditto | try swapping
ascending order | for columns | top 2 rows
4)
I posit, finally, that every Hankel matrix where there are multiple rows and columns with the same sum can be permuted into another Hankel matrix with the property that those rows and columns are in increasing order when read as binary numbers - reading left-to-right for rows and top-to-bottom for columns. That is:
0 1 1 0 0 1 0 1 0 0 1 1
1 0 0 1 0 1 1 0 0 1 0 1 New
1 0 1 0 --> 1 0 0 1 --> 1 0 1 0 Hankel
0 1 0 1 1 0 1 0 1 1 0 0
Original rows columns
Hankel ascending ascending
If this is true (and I'm still undecided), then we only ever need to create and test one permutation of any given input matrix. That permutation puts both the rows and columns in order of ascending sum, and in the case of equal sums, orders them by their binary number interpretations. If this resultant matrix is not Hankel, then there is no permutation that will make it Hankel.
Hope that gets you on the way to an algorithm!
Addendum: Counterexamples?
Trying #orlp's example:
0 0 1 0 0 0 1 0 0 0 0 1
0 1 0 1 0 1 0 1 0 1 1 0
1 0 1 1 --> 0 1 1 1 --> 0 1 1 1
0 1 1 1 1 0 1 1 1 0 1 1
(A) (B) (C)
A: Original Hankel. Row sums are 1, 2, 3, 3; Rows 3 and 4 are not in binary order.
B: Swap rows 3 and 4. Columns 3 and 4 are not in binary order.
C: Swap columns 3 and 4. Result is Hankel and satisfies all the properties.
Trying #Degustaf's example:
1 1 0 1 0 1 0 0 0 0 1 0
1 0 1 0 1 0 0 1 0 1 0 1
0 1 0 0 --> 1 0 1 0 --> 1 0 0 1
1 0 0 1 1 1 0 1 0 1 1 1
(A) (B) (C)
A: Original Hankel matrix. Row sums are 3, 2, 1, 2.
B: Rearrange so that the row sums are 1, 2, 2, 3, and the rows of sum 2 are in ascending binary order (i.e. 1001, 1010)
C: Rearrange column sums to 1, 2, 2, 3, with the two columns of sum 2 in order (0101, 1001). Result is Hankel and satisfies all the properties. Note also that the permutation on the columns matches the permutation on the rows: the new column order from the old one is {3, 4, 2, 1}, the same operation to get from A to B.
Note: I suggest the binary order (#4) only for tiebreak situations on the row or column sum, not as a replacement for the sort in (#2).

Resources