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

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.

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.

Find the largest chessboard surface in 2D Matrix

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.

Find cluster boundary in given matrix

Given matrix contains many clusters. Cluster is represented by 1's.
For Example:
0 1 1 1 0
1 1 1 0 0
0 0 0 1 1
1 1 0 0 1
In this example, there are 3 clusters (connected 1's horizontally or vertically).
Now suppose that matrix size is very big and it contains too many clusters.
Now my question is, I want to know the boundary of all the clusters.
For example given matrix:
0 0 1 1 0
0 1 1 1 1
0 0 1 1 0
0 0 0 1 0
Now output should be coordinates of bold locations:
0 0 1 1 0
0 1 1 1 1
0 0 1 1 0
0 0 0 1 0
Consider matrix is huge with many such clusters, suggest optimized way of finding boundaries of all clusters.
A 1 is on the boundary of some cluster only when it has an 0 as one of its immediate neighbors or when it's on the outline of the matrix, so the naive method would be outputting the coordinates of 1's on the outline, then going through all the other matrix cells and outputting the coordinates of 1's satisfying the said condition.
It doesn't look like there is a way to optimize this algorithm, though.
Suppose we have a huge matrix with only one random 1 inside it. There is no way to find it without going through each and every cell out there.
There is also no way to skip 1's of the same cluster by following the outline of it.
Consider this example:
0 0 0 1 1 1 1 1 1 1
0 0 0 1 0 0 0 0 0 1
0 1 1 1 1 1 0 1 0 1
0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 1 1 1 1 1
One cluster can easily fit inside another one, so following the outline is not an option.

How to create a symmetric matrix of 1's and 0's with constant row and column sum

I'm trying to find an elegant algorithm for creating an N x N matrix of 1's and 0's, under the restrictions:
each row and each column must sum to Q (to be picked freely)
the diagonal must be 0's
the matrix must be symmetrical.
It is not strictly necessary for the matrix to be random (both random and non-random solutions are interesting, however), so for Q even, simply making each row a circular shift of the vector
[0 1 1 0 ... 0 0 0 ... 0 1 1] (for Q=4)
is a valid solution.
However, how to do this for Q odd? Or how to do it for Q even, but in a random fashion?
For those curious, I'm trying to test some phenomena on abstract networks.
I apologize if this has already been answered before, but none of the questions I could find had the symmetric restriction, which seems to make it much more complicated. I don't have a proof that such a matrix always exists, but I do assume so.
The object that you're trying to construct is known more canonically as an undirected d-regular graph (where d = Q). By the handshaking theorem, N and Q cannot both be odd. If Q is even, then connect vertex v to v + k modulo N for k in {-Q/2, -Q/2 + 1, ..., -1, 1, ..., Q/2 - 1, Q/2}. If Q is odd, then N is even. Construct a (Q - 1)-regular graph as before and then add connections from v to v + N/2 modulo N.
If you want randomness, there's a Markov chain whose limiting distribution is uniform on d-regular graphs. You start with any d-regular graph. Repeatedly pick vertices v, w, x, y at random. Whenever the induced subgraph looks like
v----w
x----y ,
flip it to
v w
| |
x y .
You can perhaps always follow your circular shift algorithm, when possible.
The only condition you need to follow while using the circular shift algorithm is to maintain the symmetric nature in the first row.
i.e. keeping Q 1's in the first row so that Q[0,1] to Q[0,N-1] {Assuming 0 indexed rows and cols, Q[0,0] is 0.} is symmetric, a simple example being 110010011.
Hence, N = 10, Q = 5, you can get many possible arrangements such as:
0 1 0 0 1 1 1 0 0 1
1 0 1 0 0 1 1 1 0 0
0 1 0 1 0 0 1 1 1 0
0 0 1 0 1 0 0 1 1 1
1 0 0 1 0 1 0 0 1 1
1 1 0 0 1 0 1 0 0 1
1 1 1 0 0 1 0 1 0 0
0 1 1 1 0 0 1 0 1 0
0 0 1 1 1 0 0 1 0 1
1 0 0 1 1 1 0 0 1 0
or
0 1 1 0 0 1 0 0 1 1
1 0 1 1 0 0 1 0 0 1
1 1 0 1 1 0 0 1 0 0
0 1 1 0 1 1 0 0 1 0
0 0 1 1 0 1 1 0 0 1
1 0 0 1 1 0 1 1 0 0
0 1 0 0 1 1 0 1 1 0
0 0 1 0 0 1 1 0 1 1
1 0 0 1 0 0 1 1 0 1
1 1 0 0 1 0 0 1 1 0
But as you can see for odd N(that means even N-1) and odd Q there can't be any such symmetric distribution.. Hope it helped.

How can I find a solution of binary matrix equation AX = B?

Given an m*n binary matrix A, m*p binary matrix B, where n > m what is an efficient algorithm to compute X such that AX=B?
For example:
A =
1 1 0 0 1 1 0 1 0 0
1 1 0 0 1 0 1 0 0 1
0 1 1 0 1 0 1 0 1 0
1 1 1 1 1 0 0 1 1 0
0 1 1 0 1 0 1 1 1 0
B =
0 1 0 1 1 0 1 1 0 1 0 0 1 0
0 0 1 0 1 1 0 0 0 1 0 1 0 0
0 1 1 0 0 0 1 1 0 0 1 1 0 0
0 0 1 1 1 1 0 0 0 1 1 0 0 0
1 0 0 1 0 0 1 0 1 0 0 1 1 0
Note, when I say binary matrix I mean matrix defined over the field Z_2, that is, where all arithmetic is mod 2.
If it is of any interest, this is a problem I am facing in generating suitable matrices for a random error correction code.
You can do it with row reduction: Place B to the right of A, and then swap rows (in the whole thing) to get a 1 in row 0, col 0; then xor that row to any other row that has a '1' in column 0, so you have only a single 1 in column 0. Then move to the next column; if [1,1] is zero then swap row 1 with a later row that has a 1 there, then xor rows to make it the only 1 in the column. Assuming 'A' is a square matrix and a solution exists, then you eventually have converted A to unity, and B is replaced with the solution to Ax=B.
If n > m, you have a system with more unknowns than equations, so you can solve for some of the unknowns, and set the others to zero. During the row reduction, if there are no values in a column which have a '1' to use (below the rows already reduced) you can skip that column and make the corresponding unknown zero (you can do this at most n-m times).

Resources