簡體   English   中英

在一組集合中找到成對的有效方法

[英]An efficient way of finding pairs of sets in a set of sets

我有一組集合:{1,2,3...},我想找到所有滿足 ∩≠∅ 的對 (, )。

我能想出的唯一方法是重復條件檢查 Σ 時間。

如果我可以用類似 python 的偽代碼來表示它:

from copy import deepcopy

setA = {A1, A2, A3, A4....}
setACopy = deepcopy(setA)

intersectingPairs = set()

for Ai in setA:
    for Aj in setACopy:
        if isIntersect(Ai, Aj):
            intersectingPairs.add((Ai, Aj))
    setACopy.remove(Ai)

我希望隨着setA的大小增加,這將非常耗時。

有沒有更好的算法可以參考?

您可以線性解析數據以構建查找表,而不是單獨檢查每一對。 如果連接稀疏,這可以很快產生您想要的唯一對。

from itertools import combinations
from collections import defaultdict
from pprint import pprint

A1 = {1, 2, 3}
A2 = {2, 4, 6}
A3 = {1, 3, 5}
A4 = {2, 3, 5}
A5 = {8, 9}

# Change them to tuples so they are hashable
superset = set((tuple(subset) for subset in [A1, A2, A3, A4, A5]))

# build families of all sets with a specific element
lookup = defaultdict(list)
for subset in superset:
    for element in subset:
        lookup[element].append(subset)

# If the families are small, brute force from there
pairs = set()
for __, family in lookup.items():
    for result in combinations(family, 2):
        pairs.add(result)

pprint (pairs)

如果連接不是稀疏的,這甚至可能比查看每一對更糟糕。 但如果它們很稀疏,它可能會非常快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM