3

The question is, given a matrix with all cells black or white, to find the maximum sized submatrix with all border cells black (the interior cells need not be black).

Currently I can at best think of an O(N^4) solution. For this, I make 2 auxiliary tables, one with count of consecutive black cells to the right in each row for each cell, and in the other, count of consecutive black cells in that column for each cell. Then I do it as follows:

for each row (i):
   for each cell (i,j):
     for each window (1..n-j):
       if auxrow[i,j+window]-auxrow[i,j] == window:   #so, all cells in this window is black
         colsleft = auxcol[i,j]
         colsright = auxcol[i,j+window]
         botttom_row = min(colsleft,colsright)
         for bot in (row..row+mincol):
            if auxrow[bot][j+window]-auxrow[bot][j] == window:
              maxlen = ... #do whatever to save this sub-matrix as answer

How to improve this solution? I saw some interesting discussion at Topcoder, particularly the answer by Rem (O(N^2*log N)), and the follow-up improvement suggested by Tomek, but I could understand neither solution! Can someone give a solution better than mine, or explain those algorithms?

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • 1
    I think it is a duplicate of http://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-nn-binary-matrix – Mikhail Nov 02 '12 at 22:25
  • 3
    Not exactly. There, all cells in the sub-matrix must be zero, here only the boundary elements should be... – SexyBeast Nov 02 '12 at 22:27
  • That's because they solve a different problem, one with **square** matrices. – n. m. could be an AI Nov 02 '12 at 22:32
  • Then I would like to have a solution for either case. My algo is for rectangle, but it can be tweaked for square also. – SexyBeast Nov 02 '12 at 22:35
  • Right. For square submatrix, the inner two loops can be discarded, as the height of the submatrix will be directly dictated by the window size. So it will be `O(N^2)`. How to optimize it for rectangular as well? There is an `O(N^3)~ solution at http://discuss.techinterview.org/default.asp?interview.11.445656.19 (question #3), which I couldn't understand. – SexyBeast Nov 02 '12 at 22:42
  • I edited this to have a better. In the future give your posts descriptive titles. – Tyler Durden Nov 02 '12 at 22:49
  • 1
    Why is this question being flagged for closed? Is there some weirdo out here whose mission in life is to close every question? – SexyBeast Nov 02 '12 at 22:55

1 Answers1

3

O(N^3) is possible.

For each cell compute consecutive black cells to the top and right (can be done in O(N^2) time).

For each column (say column i) you get one array of n elements, which maintains the right information say R_i.

Now given the array R_i, we compute n other arrays (so Omega(N^3) space) as follows:

For each d such that 1 <= d <= n, you create a new array of n elements, D_id, which has a n+1 at element j if the corresponding value in R_i, ie R_i[j] < d. If R_i[j] >= d, then the corresponding entry in D_id will be equal to j.

Basically given a d and an i, using array D_id, we can tell what cell in the ith column can potentially be an endpoint of a rectangle of width d.

Now preprocess each D_id for range minimum queries: i.e given a range [u,v] we can find the minimum value in the sub-array D_id[u...v] in O(1) time.

Since you spend O(N^2) time for each column, this step is O(N^3) time.

Now to find a rectangle you consider each row and pick every pair of points which can for one edge of a rectangle. Consider these points to be the bottom left and bottom right end points of you rectangle.

Say the points in consider are on columns i and j and the width of the rectangle is d.

Now find the maximum possible height of the rectangle (using the top values we computed earlier). Say it was h.

Now if the row you are considering is r, you now run a range minimum query on D_id on the range (r, r-h]. If the minimum is <= n, then you have found a rectangle.

Since there are N^3 such possible pairs you will consider (N^2 points per row), the total running time is O(N^3).

Trixter
  • 109
  • 3
  • Note: Max size of rectangle is ambiguous, but the above works for both area or perimeter. – Trixter Nov 03 '12 at 06:47
  • 1
    I really couldn't understand your algo. Can you please make it clear with an example? – SexyBeast Nov 03 '12 at 07:35
  • @Cupidvogel: What part(s) didn't you understand? – Trixter Nov 03 '12 at 19:55
  • Is this solution for a square submatrix, or rectangular? – SexyBeast Nov 03 '12 at 19:59
  • @Cupidvogel: Rectangle. I never mention a square! I now wonder, did you even read the full answer? I don't blame you though. Not a model answer, but I believe it works... – Trixter Nov 03 '12 at 20:09
  • You are right. The answer was lost on me quarter-way, so I didb't proceed further... – SexyBeast Nov 03 '12 at 20:30
  • @Cupidvogel: If you could tell where you specifically got lost, I could try to help... – Trixter Nov 04 '12 at 05:02
  • @Cupidvogel: Seems like you are not interested in putting in some effort yourself. Perhaps you are thinking the answer is wrong. In any case, good luck to you. Note to self: do not to put in so much effort writing answers for Cupidvogel. – Trixter Nov 05 '12 at 16:34
  • Sorry dude! I just went through an arduous interview experience with Adobe yesterday (I was selected eventually), so I didn't have enough energy to even login to SO, not to mention scanning the answer to find where it was lost on me! Of course I appreciate your effort, and I would like you to carry on with this effort! Problem is that I couldn't follow line 3, hence line 4, hence...blah blah blah. If you could just give an example and describe each step like you have done, but with reference to the example, it would be great! – SexyBeast Nov 05 '12 at 20:36
  • @Cupidvogel: Congratulations :-) I meant to ask, what part of line 3 didn't you understand? Is it the language/notation? – Trixter Nov 06 '12 at 16:58
  • Neither. I am just finding it difficult to follow the algorithm. Just add an example to back it up, and it will be great! – SexyBeast Nov 07 '12 at 13:23
  • @Cupidvogel: I might add one later, but I suggest you try coming up with one yourself while trying to follow the algorithm. I think what is written is enough to do that. – Trixter Nov 08 '12 at 15:02