找到N×N二进制矩阵中仅包含零的最大矩形

Raj*_*pal 73 arrays algorithm

给定NxN二进制矩阵(仅包含0或1),我们如何才能找到包含全0的最大矩形?

例:

      I
    0 0 0 0 1 0
    0 0 1 0 0 1
II->0 0 0 0 0 0
    1 0 0 0 0 0
    0 0 0 0 0 1 <--IV
    0 0 1 0 0 0
            IV 
Run Code Online (Sandbox Code Playgroud)

对于上面的例子,它是一个6×6的二进制矩阵.在这种情况下,返回值将是单元格1:(2,1)和单元格2:(4,4).得到的子矩阵可以是正方形或矩形.返回值也可以是所有0的最大子矩阵的大小,在该示例中为3×4.

jfs*_*jfs 43

这是基于@j_random_hacker在评论中提出的"直方图中最大矩形"问题的解决方案:

[算法]通过从上到下迭代行来工作,每行解决这个问题,其中"直方图"中的"条形"由从当前行开始的所有零的不间断向上轨迹组成(列具有高度0如果它在当前行中有1).

输入矩阵mat可以是任意可迭代的,例如文件或网络流.一次只需要一行.

#!/usr/bin/env python
from collections import namedtuple
from operator import mul

Info = namedtuple('Info', 'start height')

def max_size(mat, value=0):
    """Find height, width of the largest rectangle containing all `value`'s."""
    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.
    """
    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)
Run Code Online (Sandbox Code Playgroud)

解决方案是O(N),N矩阵中的元素数量在哪里.它需要O(ncols)额外的内存,其中ncols是矩阵中的列数.

测试的最新版本位于https://gist.github.com/776423

  • @j_random_hacker:我已经更新了我的答案,使用了基于"直方图"的算法. (6认同)
  • 这看起来很棒,但是,我正在尝试实际找到最大的矩形(如,返回坐标).这个算法将可靠地返回该区域,但是一旦我知道,一个人怎么会发现3列x 2行矩形的位置,其左上角位于[3,5](例如)? (4认同)
  • 基本问题是,在这里跟踪相邻点的(区域)最大区域矩形并不总是足够的.我知道唯一正确的O(N)算法是通过从上到下迭代行来解决这个问题:http://stackoverflow.com/questions/4311694/maximize-the-rectangular-area-直方图下,"直方图"中的"条形"由从当前行开始的所有零的不间断向上轨迹组成(如果当前行中有1,则列具有高度0). (3认同)
  • 好的尝试,但这失败了`max_size([[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,0,1,1,1,1,1]]*3)`,返回(2 ,4)左上方有3x3的正方形. (2认同)

Saj*_*ain 30

请查看最大化直方图下的矩形区域,然后继续阅读下面的解决方案.

Traverse the matrix once and store the following;

For x=1 to N and y=1 to N    
F[x][y] = 1 + F[x][y-1] if A[x][y] is 0 , else 0

Then for each row for x=N to 1 
We have F[x] -> array with heights of the histograms with base at x.
Use O(N) algorithm to find the largest area of rectangle in this histogram = H[x]

From all areas computed, report the largest.
Run Code Online (Sandbox Code Playgroud)

时间复杂度为O(N*N)= O(N²)(对于NxN二进制矩阵)

例:

Initial array    F[x][y] array
 0 0 0 0 1 0     1 1 1 1 0 1
 0 0 1 0 0 1     2 2 0 2 1 0
 0 0 0 0 0 0     3 3 1 3 2 1
 1 0 0 0 0 0     0 4 2 4 3 2
 0 0 0 0 0 1     1 5 3 5 4 0
 0 0 1 0 0 0     2 6 0 6 5 1

 For x = N to 1
 H[6] = 2 6 0 6 5 1 -> 10 (5*2)
 H[5] = 1 5 3 5 4 0 -> 12 (3*4)
 H[4] = 0 4 2 4 3 2 -> 10 (2*5)
 H[3] = 3 3 1 3 2 1 -> 6 (3*2)
 H[2] = 2 2 0 2 1 0 -> 4 (2*2)
 H[1] = 1 1 1 1 0 1 -> 4 (1*4)

 The largest area is thus H[5] = 12
Run Code Online (Sandbox Code Playgroud)


tom*_*sen 11

这是一个Python3解决方案,除了最大矩形的区域外还返回位置:

#!/usr/bin/env python3

import numpy

s = '''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'''

nrows = 6
ncols = 6
skip = 1
area_max = (0, [])

a = numpy.fromstring(s, dtype=int, sep=' ').reshape(nrows, ncols)
w = numpy.zeros(dtype=int, shape=a.shape)
h = numpy.zeros(dtype=int, shape=a.shape)
for r in range(nrows):
    for c in range(ncols):
        if a[r][c] == skip:
            continue
        if r == 0:
            h[r][c] = 1
        else:
            h[r][c] = h[r-1][c]+1
        if c == 0:
            w[r][c] = 1
        else:
            w[r][c] = 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, [(r-dh, c-minw+1, r, c)])

print('area', area_max[0])
for t in area_max[1]:
    print('Cell 1:({}, {}) and Cell 2:({}, {})'.format(*t))
Run Code Online (Sandbox Code Playgroud)

输出:

area 12
Cell 1:(2, 1) and Cell 2:(4, 4)
Run Code Online (Sandbox Code Playgroud)


dma*_*rra 5

这是 JF Sebastians 方法翻译成 C#:

private Vector2 MaxRectSize(int[] histogram) {
        Vector2 maxSize = Vector2.zero;
        int maxArea = 0;
        Stack<Vector2> stack = new Stack<Vector2>();

        int x = 0;
        for (x = 0; x < histogram.Length; x++) {
            int start = x;
            int height = histogram[x];
            while (true) {
                if (stack.Count == 0 || height > stack.Peek().y) {
                    stack.Push(new Vector2(start, height));

                } else if(height < stack.Peek().y) {
                    int tempArea = (int)(stack.Peek().y * (x - stack.Peek().x));
                    if(tempArea > maxArea) {
                        maxSize = new Vector2(stack.Peek().y, (x - stack.Peek().x));
                        maxArea = tempArea;
                    }

                    Vector2 popped = stack.Pop();
                    start = (int)popped.x;
                    continue;
                }

                break;
            }
        }

        foreach (Vector2 data in stack) {
            int tempArea = (int)(data.y * (x - data.x));
            if(tempArea > maxArea) {
                maxSize = new Vector2(data.y, (x - data.x));
                maxArea = tempArea;
            }
        }

        return maxSize;
    }

    public Vector2 GetMaximumFreeSpace() {
        // STEP 1:
        // build a seed histogram using the first row of grid points
        // example: [true, true, false, true] = [1,1,0,1]
        int[] hist = new int[gridSizeY];
        for (int y = 0; y < gridSizeY; y++) {
            if(!invalidPoints[0, y]) {
                hist[y] = 1;
            }
        }

        // STEP 2:
        // get a starting max area from the seed histogram we created above.
        // using the example from above, this value would be [1, 1], as the only valid area is a single point.
        // another example for [0,0,0,1,0,0] would be [1, 3], because the largest area of contiguous free space is 3.
        // Note that at this step, the heigh fo the found rectangle will always be 1 because we are operating on
        // a single row of data.
        Vector2 maxSize = MaxRectSize(hist);
        int maxArea = (int)(maxSize.x * maxSize.y);

        // STEP 3:
        // build histograms for each additional row, re-testing for new possible max rectangluar areas
        for (int x = 1; x < gridSizeX; x++) {
            // build a new histogram for this row. the values of this row are
            // 0 if the current grid point is occupied; otherwise, it is 1 + the value
            // of the previously found historgram value for the previous position. 
            // What this does is effectly keep track of the height of continous avilable spaces.
            // EXAMPLE:
            //      Given the following grid data (where 1 means occupied, and 0 means free; for clairty):
            //          INPUT:        OUTPUT:
            //      1.) [0,0,1,0]   = [1,1,0,1]
            //      2.) [0,0,1,0]   = [2,2,0,2]
            //      3.) [1,1,0,1]   = [0,0,1,0]
            //
            //  As such, you'll notice position 1,0 (row 1, column 0) is 2, because this is the height of contiguous
            //  free space.
            for (int y = 0; y < gridSizeY; y++) {                
                if(!invalidPoints[x, y]) {
                    hist[y] = 1 + hist[y];
                } else {
                    hist[y] = 0;
                }
            }

            // find the maximum size of the current histogram. If it happens to be larger
            // that the currently recorded max size, then it is the new max size.
            Vector2 maxSizeTemp = MaxRectSize(hist);
            int tempArea = (int)(maxSizeTemp.x * maxSizeTemp.y);
            if (tempArea > maxArea) {
                maxSize = maxSizeTemp;
                maxArea = tempArea;
            }
        }

        // at this point, we know the max size
        return maxSize;            
    }
Run Code Online (Sandbox Code Playgroud)

对此有几点需要注意:

  1. 此版本旨在与 Unity API 一起使用。通过用 KeyValuePair 替换 Vector2 的实例,您可以轻松地使其更通用。Vector2 仅用于方便地存储两个值。
  2. invalidPoints[] 是一个 bool 数组,其中 true 表示网格点“正在使用”,而 false 表示不是。