Finding maximum size sub-matrix of all 1's in a matrix having 1's and 0's?

An O(N) (number of elements) solution: A 1 1 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 0 Generate an array C where each element represents the number of 1s above and including it, up until the first 0 C 1 1 0 0 1 0 0 2 1 1 2 1 1 3 2 2 3 0 0 0 3 3 0 0 We want to find the row R and left, right indices l r that maximizes (r-l+1)*min(CRl..r) Here is an algorithm to inspect each row in O(cols) time: Maintain a stack of pairs (h, i) where CRi-1 h_top Push (h, i) Else: While h_cur best If h_cur>h_top Push (h, i_lastpopped) An example of this in execution for the third row in our example: I =0 1 2 3 4 5 Ci=1 3 2 2 3 0 (3, 4) S= (3, 1) (2, 1) (2, 1) (2, 1) (1, 0) (1, 0) (1, 0) (1, 0) (1, 0) (0,-1) (0,-1) (0,-1) (0,-1) (0,-1) (0,-1) i=0, Ci=1 ) Push (1, 0) i=1, Ci=3 ) Push (3, 1) i=2, Ci=2 ) Pop (3, 1) Check whether (2-1)*3=3 is a new best         The last I popped was 1, so push (2, 1) i=3, Ci=2 ) h_cur=h_top so do nothing i=4, Ci=3 ) Push (3, 4) i=5, Ci=0 ) Pop (3, 4) Check whether (5-4)*3=3 is a new best         Pop (2, 1) Check whether (5-1)*2=8 is a new best.

An O(N) (number of elements) solution: A 1 1 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 0 Generate an array C where each element represents the number of 1s above and including it, up until the first 0. C 1 1 0 0 1 0 0 2 1 1 2 1 1 3 2 2 3 0 0 0 3 3 0 0 We want to find the row R, and left, right indices l , r that maximizes (r-l+1)*min(CRl..r). Here is an algorithm to inspect each row in O(cols) time: Maintain a stack of pairs (h, i), where CRi-1 h_top Push (h, i).

Else: While h_cur best. If h_cur>h_top Push (h, i_lastpopped). An example of this in execution for the third row in our example: I =0 1 2 3 4 5 Ci=1 3 2 2 3 0 (3, 4) S= (3, 1) (2, 1) (2, 1) (2, 1) (1, 0) (1, 0) (1, 0) (1, 0) (1, 0) (0,-1) (0,-1) (0,-1) (0,-1) (0,-1) (0,-1) i=0, Ci=1) Push (1, 0).

I=1, Ci=3) Push (3, 1). I=2, Ci=2) Pop (3, 1). Check whether (2-1)*3=3 is a new best.

€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒThe last I popped was 1, so push (2, 1). I=3, Ci=2) h_cur=h_top so do nothing. I=4, Ci=3) Push (3, 4).

I=5, Ci=0) Pop (3, 4). Check whether (5-4)*3=3 is a new best. €ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒPop (2, 1).

Check whether (5-1)*2=8 is a new best. €ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒPop (1, 0). Check whether (5-0)*1=5 is a new best.

€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒâ€ƒEnd. (Okay, we should probably add an extra term Ccols=0 on the end for good measure).

1 +1. I wasn't able to quite follow your reasoning (especially what h is exactly), but I see now that for each row, you're effectively calling a linear-time algorithm for finding the largest rectangle under a histogram whose baseline is that row. That "subroutine" seems to match algorithm #4 here: blog.csdn.Net/arbuckle/archive/2006/05/06/710988.

Aspx – j_random_hacker Dec 20 '10 at 5:14 Thanks for this answer. An elaborative post based on this is presented here. – Akash Agrawal Sep 5 at 18:33.

Here's an O(numCols*numLines^2) algorithm. Let Sij = sum of the first I elements of column j. I will work the algorithm on this example: M 1 1 0 0 1 0 0 1 1 1 0 1 1 1 1 1 0 0 0 0 1 1 0 0 We have: S 1 1 0 0 1 0 1 2 1 1 1 1 2 3 2 2 1 1 2 3 3 3 1 1 Now consider the problem of finding the maximum subarray of all ones in a one-dimensional array.

This can be solved using this simple algorithm: append 0 to the end of your array max = 0, temp = 0 for I = 1 to array. Size do if arrayi = 1 then ++temp else if temp > max then max = temp temp = 0 For example, if you have this 1d array: 1 2 3 4 5 6 1 1 0 1 1 1 you'd do this: First append a 0: 1 2 3 4 5 6 7 1 1 0 1 1 1 0 Now, notice that whenever you hit a 0, you know where a sequence of contiguous ones ends. Therefore, if you keep a running total (temp variable) of the current number of ones, you can compare that total with the maximum so far (max variable) when you hit a zero, and then reset the running total.

This will give you the maximum length of a contiguous sequence of ones in the variable max. Now you can use this subalgorithm to find the solution for your problem. First of all append a 0 column to your matrix. Then compute S.

Then: max = 0 for I = 1 to M. NumLines do for j = I to M. NumLines do temp = 0 for k = 1 to M.

NumCols do if Sjk - Si-1k = j - I + 1 then temp += j - I + 1 else if temp > max then max = temp temp = 0 Basically, for each possible height of a subarray (there are O(numLines^2) possible heights), you find the one with maximum area having that height by applying the algorithm for the one-dimensional array (in O(numCols)). Consider the following "picture": M 1 1 0 0 1 0 0 I 0 1 1 1 0 1 0 j 1 1 1 1 0 0 0 0 0 1 1 0 0 0 This means that we have the height j - I + 1 fixed. Now, take all the elements of the matrix that are between I and j inclusively: 0 1 1 1 0 1 0 1 1 1 1 0 0 0 Notice that this resembles the one-dimensional problem.

Let's sum the columns and see what we get: 1 2 2 2 0 1 0 Now, the problem is reduced to the one-dimensional case, with the exception that we must find a subsequence of contiguous j - I + 1 (which is 2 in this case) values. This means that each column in our j - I + 1 "window" must be full of ones. We can check for this efficiently by using the S matrix.

To understand how S works, consider a one-dimensional case again: let si = sum of the first I elements of the vector a. Then what is the sum of the subsequence ai..j? It's the sum of all the elements up to and including aj, minus the sum of all those up to and including ai-1, meaning sj - si-1.

The 2d case works the same, except we have an s for each column. I hope this is clear, if you have any more questions please ask. I don't know if this fits your needs, but I think there's also an O(numLines*numCols) algorithm, based on dynamic programming.

I can't figure it out yet, except for the case where the subarray you're after is square. Someone might have better insight however, so wait a bit more.

1 good solution (and I don't think O(n*m) complexity can be achieved in general case, this is probably as good as it gets) BTW, lower-left value of S in your example seems to be wrong. Also, I think you might get more upvotes if explain main idea more clearly (although it's difficult to do with words, idea is more 'visual'). – Nikita Rybak Sep 27 '10 at 21:40 @Nikita Rybak - thanks for your input, I corrected the mistake in S and tried to better explain the idea.

– IVlad Sep 27 '10 at 22:13 Somebody told me that this problem can be solved in O(n^2logn) also. Anyone for O(n^2logn)? – Akhil Sep 27 '10 at 22:27.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions