c - 查找稀疏矩阵中元素周围的最大区域

标签 c algorithm matrix sparse-matrix

我有一个稀疏矩阵数组,我需要在其中找到元素周围的最高空白区域。区域应为矩形或正方形。在该区域中不应存在其他元素。算法足以开发代码。有什么算法可以实现这一点吗?

最佳答案

由于您提到对解决方案的效率没有要求,因此这里有一个强力方法。

Let M denote the matrix
Let n be the number of rows
Let m be the number of columns
Let maxRowSize be 0, initially
Let maxColSize be 0, initially
Let maxRowStart be 0, initially
Let maxColStart be 0, initially
for top from 0 to n:
    for left from 0 to m:
        numNonEmptyElements = 0
        if M[top][left] is non-empty:
            numNonEmptyElements = 1
        for bottom from i to n:
            if M[bottom][left] is non-empty AND numNonEmptyElements == 1:
                break
            for right from 0 to m:
                if M[bottom][right] is non-empty:
                    numNonEmptyElements += 1
                if numNonEmptyElements > 1:
                    break
                if (right - left + 1) * (bottom - top + 1) > maxRowSize * maxColSize:
                    maxRowSize = bottom - top + 1
                    maxColSize = right - left + 1
                    maxRowStart = top
                    maxColStart = left
return any of the maxRowSize, maxColSize, maxRowStart, maxColStart you need

从循环中可以看出,伪代码的时间复杂度为O(N2M2),N 和 M是矩阵的行和列的大小,效率很低。

关于c - 查找稀疏矩阵中元素周围的最大区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199552/

相关文章:

c - glibc - 获取具有包含指定地址的符号的共享库句柄

c++ - 递归函数错误Dev-C++

c++ - 预期类型说明符位于 'QwtLog10ScaleEngine' 之前?

c# - 从 C 移植到 C# 时是否需要 __W64?

algorithm - 计算具有 3 个循环的算法的复杂性

algorithm - 定义递归算法复杂度

python - 如何在python中并行for循环?

c++ - 将 RotateAxisAngle 反转回角度

python - 硬编码 6x6 numpy 矩阵的简单方法是什么?

c - 关于链表和节点插入的基本问题