Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Find height, width of the largest rectangle containing all 0's in the matrix

View dp.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#!/usr/bin/env python
"""Find height, width of the largest rectangle containing all 0's in the matrix.
The algorithm for `max_size()` is suggested by @j_random_hacker [1].
The algorithm for `max_rectangle_size()` is from [2].
The Python implementation [3] is dual licensed under CC BY-SA 3.0
and ISC license.
[1]: http://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-nn-binary-matrix#comment5169734_4671342
[2]: http://blog.csdn.net/arbuckle/archive/2006/05/06/710988.aspx
[3]: http://stackoverflow.com/a/4671342
Copyright (c) 2014, zed <isidore.john.r@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
from collections import namedtuple
from operator import mul
 
try:
reduce = reduce
except NameError:
from functools import reduce # py3k
 
Info = namedtuple('Info', 'start height')
 
def max_size(mat, value=0):
"""Find height, width of the largest rectangle containing all `value`'s.
For each row solve "Largest Rectangle in a Histrogram" problem [1]:
[1]: http://blog.csdn.net/arbuckle/archive/2006/05/06/710988.aspx
"""
it = iter(mat)
hist = [(el==value) for el in next(it, [])]
max_size = max_rectangle_size(hist)
for row in it:
hist = [(1+h) if el == value else 0 for h, el in zip(hist, row)]
max_size = max(max_size, max_rectangle_size(hist), key=area)
return max_size
 
def max_rectangle_size(histogram):
"""Find height, width of the largest rectangle that fits entirely under
the histogram.
>>> f = max_rectangle_size
>>> f([5,3,1])
(3, 2)
>>> f([1,3,5])
(3, 2)
>>> f([3,1,5])
(5, 1)
>>> f([4,8,3,2,0])
(3, 3)
>>> f([4,8,3,1,1,0])
(3, 3)
>>> f([1,2,1])
(1, 3)
Algorithm is "Linear search using a stack of incomplete subproblems" [1].
[1]: http://blog.csdn.net/arbuckle/archive/2006/05/06/710988.aspx
"""
stack = []
top = lambda: stack[-1]
max_size = (0, 0) # height, width of the largest rectangle
pos = 0 # current position in the histogram
for pos, height in enumerate(histogram):
start = pos # position where rectangle starts
while True:
if not stack or height > top().height:
stack.append(Info(start, height)) # push
elif stack and height < top().height:
max_size = max(max_size, (top().height, (pos - top().start)),
key=area)
start, _ = stack.pop()
continue
break # height == top().height goes here
 
pos += 1
for start, height in stack:
max_size = max(max_size, (height, (pos - start)), key=area)
 
return max_size
 
def area(size):
return reduce(mul, size)
 
import unittest
class TestCase(unittest.TestCase):
def test(self):
self.assertEqual(max_size(self.__s2m("""
0 0 0 0 1 0
0 0 1 0 0 1
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 1
0 0 1 0 0 0""")), (3, 4))
self.assertEqual(max_size([[1, 1], [0, 0]]), (1, 2))
self.assertEqual(max_size([[0, 0], [1, 1]]), (1, 2))
self.assertEqual(max_size([[1, 0], [1, 0]]), (2, 1))
self.assertEqual(max_size([[0, 1], [0, 1]]), (2, 1))
self.assertEqual(max_size(self.__s2m("""
0 0 0 0 1 0
0 0 1 0 0 1
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 1
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0""")), (7, 2))
self.assertEqual(max_size([[]]), (0, 0))
self.assertEqual(max_size([]), (0, 0))
self.assertEqual(max_size(self.__s2m("""
0 0 0 0 1 0
0 0 1 0 0 1
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0""")), (3, 5))
self.assertEqual(max_size(self.__s2m("""
0 0 0 0 1 0
0 0 0 0 0 0
0 0 1 0 0 1
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1""")), (8, 2))
self.assertEqual(max_size(self.__s2m("""
0 0 0 0 1 1 1
0 0 0 0 0 0 0
0 0 0 1 1 1 1
0 0 1 1 1 1 1
1 0 1 1 1 1 1
1 0 1 1 1 1 1
1 0 1 1 1 1 1
""")), (3, 3))
 
def __s2m(self, s):
return [map(int, line.split())
for line in s.splitlines() if line.strip()]
 
if __name__=="__main__":
import unittest; unittest.main()

I got the rather surprising result with the following test case:

print zheights 
array([[0, 0, 0, 0, 0, 0, 0, 0, 3, 2],
   [0, 4, 0, 2, 4, 0, 0, 1, 0, 0],
   [1, 0, 1, 0, 0, 0, 3, 0, 0, 4],
   [0, 0, 0, 0, 4, 2, 0, 0, 0, 0],
   [0, 0, 0, 2, 0, 0, 0, 0, 1, 0],
   [4, 3, 0, 0, 1, 2, 0, 0, 0, 0],
   [3, 0, 0, 0, 2, 0, 0, 0, 0, 4],
   [0, 0, 0, 1, 0, 3, 2, 4, 3, 2],
   [0, 3, 0, 0, 0, 2, 0, 1, 0, 0],
   [0, 0, 2, 0, 0, 0, 0, 1, 0, 0]])

max_size(zheights, value=0)
(True, 8)

I added this to the bottom of the max_size function:

row_length = np.sum(max_size[0])
column_length = np.sum(max_size[1])
return (row_length, column_length)
Owner
zed commented

True == 1 in Python i.e., the result is correct.
Boolean appears due to how the histogram is initialized: hist = [(el==value) for el in next(it, [])].

Even if the function had used numpy it is unclear why would you call np.sum() on an scalar integer result.

>>> isinstance(True, int)
True
>>> True+0
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.