我有一个“组合”问题,需要找到一组不同的密钥,我试图找到一个优化的解决方案:
我有一个列表“l”:
l = [[1, 5],
[5, 7],
[4, 9],
[7, 9],
[50, 90],
[100, 200],
[90, 100],
[2, 90],
[7, 50],
[9, 21],
[5, 10],
[8, 17],
[11, 15],
[3, 11]]
每个Id都链接到另一个Id,但也可能通过另一个键链接到另一个键(见下图)。目标是以优化的方式找到属于同一集群的所有密钥
结果是:
[{1, 2, 4, 5, 7, 9, 10, 21, 50, 90, 100, 200}, {8, 17}, {3, 11, 15}]
我现在的代码是:
out = []
while len(l)>0:
first, *rest = l
first = set(first)
lf = -1
while len(first)>lf:
lf = len(first)
print(lf)
rest2 = []
for r in rest:
if len(first.intersection(set(r)))>0:
first |= set(r)
else:
rest2.append(r)
rest = rest2
out.append(first)
l = rest
我得到了之前显示的结果。当在200万条线路上使用它时,问题就出现了,而这些线路需要很长时间才能运行。你知道吗
有没有其他优化方法来解决这个问题?你知道吗
您可以将其视为在图形中查找connected components的问题:
这是union-find algorithm / disjoint set data structure的典型用例。在Python库AFAIK中没有实现,但是我总是倾向于在附近有一个实现,因为它非常有用。。。你知道吗
对于n个节点,这种方法的运行时复杂度应该是O(nlogn),每次都需要logn步骤才能到达(并更新)leader。你知道吗
相关问题 更多 >
编程相关推荐