За O(N * log(n)), где n - число уникальных идентификаторов, N - сумма мощностей множеств.
Система непересекающихся множеств:
def union(superset):
parent = {}
def make_set(a):
if a not in parent: parent[a] = a
def find_set(a):
if parent[a] == a: return a
parent[a] = find_set(parent[a])
return parent[a]
def union_sets(a, b):
a = find_set(a)
b = find_set(b)
if a != b: parent[a] = b
for subset in superset:
prev = None
for v in subset:
make_set(v)
if prev: union_sets(prev, v)
prev = v
lists = {}
for uid in parent.keys():
pr = find_set(uid)
if pr not in lists: lists[pr] = []
lists[pr].append(uid)
return set([ frozenset(l) for l in lists.values() ])
>> union(changeset)
{frozenset({111, 136}),
frozenset({101, 102, 105}),
frozenset({110, 135}),
frozenset({103, 104, 106, 107, 109})}