gpt4 book ai didi

python - 识别矩阵中最大的连通分量

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:18:13 25 4
gpt4 key购买 nike

我有一个带有 1 和 0 的 python numpy 矩阵,我需要确定矩阵中 1 的最大“集合”: http://imgur.com/4JPZufS

矩阵最多可包含 960.000 个元素,因此我想避免暴力解决方案。

解决这个问题最明智的方法是什么?

最佳答案

您可以使用名为 disjoint-set 的数据结构(here 是一个 python 实现)。该数据结构专为此类任务而设计。

如果当前元素为 1,则遍历行,检查是否有任何已遍历的邻居为 1。如果是,则将此元素添加到其集合中。如果有超过 1 个 union 那些集合。如果没有邻居是 1 创建一个新的集合。最后输出最大的集合。

这将按如下方式工作:

def MakeSet(x):
x.parent = x
x.rank = 0
x.size = 1

def Union(x, y):
xRoot = Find(x)
yRoot = Find(y)
if xRoot.rank > yRoot.rank:
yRoot.parent = xRoot
elif xRoot.rank < yRoot.rank:
xRoot.parent = yRoot
elif xRoot != yRoot: # Unless x and y are already in same set, merge them
yRoot.parent = xRoot
xRoot.rank = xRoot.rank + 1
x.size += y.size
y.size = x.size

def Find(x):
if x.parent == x:
return x
else:
x.parent = Find(x.parent)
return x.parent

""""""""""""""""""""""""""""""""""""""""""

class Node:
def __init__ (self, label):
self.label = label
def __str__(self):
return self.label

rows = [[1, 0, 0], [1, 1, 0], [1, 0, 0]]
setDict = {}
for i, row in enumerate(rows):
for j, val in enumerate(row):
if row[j] == 0:
continue
node = Node((i, j))
MakeSet(node)
if i > 0:
if rows[i-1][j] == 1:
disjointSet = setDict[(i-1, j)]
Union(disjointSet, node)
if j > 0:
if row[j-1] == 1:
disjointSet = setDict[(i, j-1)]
Union(disjointSet, node)
setDict[(i, j)] = node
print max([l.size for l in setDict.values()])

>> 4

这是一个完整的工作示例,其中包含从上面的链接获取的不相交集的代码。

关于python - 识别矩阵中最大的连通分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com