Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding largest rectangle in 2D array

I need an algorithm which can parse a 2D array and return the largest continuous rectangle. For reference, look at the image I made demonstrating my question.

enter image description here

like image 683
Johnathan Avatar asked May 09 '11 01:05

Johnathan


2 Answers

Generally you solve these sorts of problems using what are called scan line algorithms. They examine the data one row (or scan line) at a time building up the answer you are looking for, in your case candidate rectangles.

Here's a rough outline of how it would work.

Number all the rows in your image from 0..6, I'll work from the bottom up.

Examining row 0 you have the beginnings of two rectangles (I am assuming you are only interested in the black square). I'll refer to rectangles using (x, y, width, height). The two active rectangles are (1,0,2,1) and (4,0,6,1). You add these to a list of active rectangles. This list is sorted by increasing x coordinate.

You are now done with scan line 0, so you increment your scan line.

Examining row 1 you work along the row seeing if you have any of the following:

  • new active rectangles
  • space for existing rectangles to grow
  • obstacles which split existing rectangles
  • obstacles which require you to remove a rectangle from the active list

As you work along the row you will see that you have a new active rect (0,1,8,1), we can grow one of existing active ones to (1,0,2,2) and we need to remove the active (4,0,6,1) replacing it with two narrower ones. We need to remember this one. It is the largest we have seen to far. It is replaced with two new active ones: (4,0,4,2) and (9,0,1,2)

So at the send of scan line 1 we have:

  • Active List: (0,1,8,1), (1,0,2,2), (4,0,4,2), (9, 0, 1, 2)
  • Biggest so far: (4,0,6,1)

You continue in this manner until you run out of scan lines.

The tricky part is coding up the routine that runs along the scan line updating the active list. If you do it correctly you will consider each pixel only once.

Hope this helps. It is a little tricky to describe.

like image 110
idz Avatar answered Oct 05 '22 23:10

idz


I like a region growing approach for this.

  • For each open point in ARRAY
  • grow EAST as far as possible
  • grow WEST as far as possible
  • grow NORTH as far as possible by adding rows
  • grow SOUTH as far as possible by adding rows
  • save the resulting area for the seed pixel used
  • After looping through each point in ARRAY, pick the seed pixel with the largest area result

...would be a thorough, but maybe not-the-most-efficient way to go about it.

I suppose you need to answer the philosophical question "Is a line of points a skinny rectangle?" If a line == a thin rectangle, you could optimize further by:

  • Create a second array of integers called LINES that has the same dimensions as ARRAY
  • Loop through each point in ARRAY
  • Determine the longest valid line to the EAST that begins at each point and save its length in the corresponding cell of LINES.
  • After doing this for each point in ARRAY, loop through LINES
  • For each point in LINES, determine how many neighbors SOUTH have the same length value or less.
  • Accept a SOUTHERN neighbor with a smaller length if doing so will increase the area of the rectangle.
  • The largest rectangle using that seed point is (Number_of_acceptable_southern_neighbors*the_length_of_longest_accepted_line)
  • As the largest rectangular area for each seed is calculated, check to see if you have a new max value and save the result if you do.
  • And... you could do this without allocating an array LINES, but I thought using it in my explanation made the description simpler.
  • And... I think you need to do this same sort of thing with VERTICAL_LINES and EASTERN_NEIGHBORS, or some cases might miss big rectangles that are tall and skinny. So maybe this second algorithm isn't so optimized after all.

Use the first method to check your work. I think Knuth said "...premature optimization is the root of all evil."

HTH,

Perry


ADDENDUM:Several edits later, I think this answer deserves a group upvote.

like image 45
Perry Horwich Avatar answered Oct 06 '22 00:10

Perry Horwich