6

I'm trying to implement Maximum Rectangle Algorithm from Dr. Dobbs (Listing four) with Python. It works mostly, but one specific case gives wrong results and I cannot figure out why.

Here's my source code:

from collections import namedtuple

Point = namedtuple('Point', ('X', 'Y'))

#Y      0  1  2      X
arr = [[0, 0, 0, ], #0
       [1, 0, 0, ], #1
       [0, 0, 1, ], #2
       ]

def area(ll, ur):
    if (ll.X < 0) or (ll.Y < 0) or (ur.X < 0) or (ur.Y < 0):
        return 0.
    return ((ur.X - ll.X) + 1) * ((ur.Y - ll.Y) + 1)

def update_cache(a, c, x):
    M = len(a[0])
    N = len(a)
    for y in range(M):
        if a[x][y] == 0:
            c[y] = c[y] + 1
        else:
            c[y] = 0

def mrp(a):
    best_ll = Point(-1, -1)
    best_ur = Point(-1, -1)
    M = len(a[0]) 
    N = len(a)
    c = [0 for x in range(M + 1)]
    stack = []
    for x in range(N-1, -1, -1):

        update_cache(a, c, x)        
        width = 0
        for y in range(M + 1):
            if c[y] > width:
                stack.append((y, width))                
                width = c[y]
            if c[y] < width:
                while True:
                    y0, w0 = stack.pop()
                    if (width * (y - y0)) > area(best_ll, best_ur):
                        best_ll = Point(x, y0)
                        best_ur = Point(x + width - 1, y - 1)
                    width = w0
                    if (c[y] >= width):
                        break
                width = c[y] 
                if width == 0:
                    stack.append((y0, width))
    return best_ll, best_ur

And here's the result:

>>> mrp(arr)
(Point(X=0, Y=0), Point(X=1, Y=2))

As you can see, the first point is wrong, but I cannot figure out where and why it goes wrong. Changing the arr gives right results.

Edit: I noticed I've changed the values of the array compared to article. This changes the comparision in update_cache. 0=clear and 1=reserved. I'm looking for result (Point(X=0, Y=1), Point(X=1, Y=2)).

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
Harriv
  • 6,029
  • 6
  • 44
  • 76
  • 1
    related: [Find largest rectangle containing only zeros in an N×N binary matrix](http://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-nn-binary-matrix) – jfs Dec 29 '11 at 02:54
  • 3
    I implemented in Javascript based on this and the Dr. Dobbs article if it's any use to anyone: http://www.codinghands.co.uk/blog/2013/02/javascript-implementation-omn-maximal-rectangle-algorithm/ – codinghands Feb 23 '13 at 18:03

2 Answers2

4

Last stack.append should be:

stack.append((y0, w0))
Harriv
  • 6,029
  • 6
  • 44
  • 76
  • unfortunately, the first point is still `(0, 0)` with this change. – Tcll Oct 29 '21 at 23:53
  • to explain your runtime process (I ran everything through an IDEA debugger and monitored the changes), everything happens within the while loop, `best_ll` starts out at `(2, 0)` on the first update, then changes to `(1, 0)` before `(0, 0)` and returned, and `best_ur` remains the same from the start to returned. – Tcll Oct 30 '21 at 00:57
0

here's an optimal solution modified from another answer here: (made slightly faster and more readable) https://stackoverflow.com/a/30418912/2131849

arr = [
    [0, 0, 0, ],
    [1, 0, 0, ],
    [0, 0, 1, ],
]

nrows = len(arr)
ncols = len(arr[0])

w = [[0]*ncols for n in range(nrows)]
h = [[0]*ncols for n in range(nrows)]

skip = 1
area_max = 0
rect = (0,0,0,0)

for r in range(nrows):
    for c in range(ncols):
        if arr[r][c] == skip: continue
        h[r][c] = 1 if not r else h[r-1][c]+1
        w[r][c] = 1 if not c else w[r][c-1]+1
        minw = w[r][c]
        for dh in range(h[r][c]):
            minw = min(minw, w[r-dh][c])
            area = (dh+1)*minw
            if area > area_max[0]:
                area_max = area
                rect = (r-dh, c-minw+1, r, c)

print('area:', area_max)
print('(Point(X=%s, Y=%s), Point(X=%s, Y=%s))'%rect)

result is exactly what you want:

area: 4
(Point(X=0, Y=1), Point(X=1, Y=2))

Tcll
  • 7,140
  • 1
  • 20
  • 23