Finding largest rectangle in 2D array

后端 未结 4 944
别那么骄傲
别那么骄傲 2021-01-12 20:12

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.

相关标签:
4条回答
  • 2021-01-12 20:37

    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.

    0 讨论(0)
  • 2021-01-12 20:39

    Use dynamic programming approach. Consider a function S(x,y) such that S(x,y) holds the area of the largest rectangle where (x,y) are the lowest-right-most corner cell of the rectangle; x is the row co-ordinate and y is the column co-ordinate of the rectangle.

    For example, in your figure, S(1,1) = 1, S(1,2)=2, S(2,1)=2, and S(2,2) = 4. But, S(3,1)=0, because this cell is filled. S(8,5)=40, which says that the largest rectangle for which the lowest-right cell is (8,5) has the area 40, which happens to be the optimum solution in this example.

    You can easily write a dynamic programming equation of S(x,y) from the value of S(x-1,y), S(x,y-1) and S(x-1,y-1). Using that you can obtain the values of all S(x,y) in O(mn) time, where m and n are the row and column dimension of the given table. Once, S(x,y) are know for all 1<=x <= m, and for all 1 <= y <= n, we simply need to find the x, and y for which S(x,y) is the largest; this step also takes O(mn) time. By keeping addition data, you can also find the side-length of the largest rectangle.

    The overall complexity is O(mn). To understand more on this, Read Chapter 15 or Cormen's algorithm book, specifically Section 15.4.

    0 讨论(0)
  • 2021-01-12 20:48

    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.

    0 讨论(0)
  • 2021-01-12 21:01

    A straight forward approach would be to do a loop through all the potential rectangles in the grid, figure out their area, and if it is greater than the current highest area, select it as the highest:

    var biggestFound
    for each potential rectangle:
        if area(this potential rectangle) > area(biggestFound)
            biggestFound = this potential rectangle
    

    Then you simply need to find the potential rectangles.

    for each square in grid:
        recursive loop 1:
            if not occupied:
                grow right until occupied, and return a rectangle
                grow down one and recurse (call loop 1)
    

    This will duplicate a lot of work (for example you will re-evaluate a lot of sub-rectangles), but it should give you an answer.

    Edit

    An alternate approach might be to start with a single square the size of the grid, and "subtract" occupied squares to end up with a final set of potential rectangles. There might be optimization opportunities here using quadtrees, and in ensuring that you keep split rectangles "in order", top to bottom, left to right, in case you need to re-combine rectangles farther down in the algorithm.

    If you are actually starting out with rectangular data (for your "populated grid" set), instead of a loose pixel grid, then you could easily get better perf out of a rectangle/region subtracting algorithm.

    I'm not going to post pseudo-code for this because the idea is completely experimental, and I have no idea if the perf will be any better for a loose pixel grid ;)

    Windows system "regions" and "dirty rectangles", as well as general "temporal caching" might be good inspiration here for more efficiency. There are also a lot of z-buffer tricks if this is for a graphics algorithm...

    0 讨论(0)
提交回复
热议问题