This question already has an answer here:
I would like to merge sets with common elements. For example
input = set([frozenset([1,2,3,4]), frozenset([3,4,5,6,7,8]), frozenset([1,1000]),
frozenset([100, 200]), frozenset([100, 300, 400])])
Result:
set([frozenset([1,2,3,4,5,6,7,8, 1000]), frozenset([100,200,300,400])])
What would be an efficient way to achieve this?
Here are what I tried. Firstly, I did something simple but wrong
The problem with this code is that the merging may not be complete, depending on the order of the
input.pop()
. Supposeinput={frozenset([0, 1]), frozenset([2,3]), frozenset([1,2])}
and the three elements get popped out and looped over in this assignment order, thenresults={frozenset([0,1,2]), frozenset([2,3])}
.Then I implemented the answer in this post. It first builds an adjacency list of a new graph, each node corresponds to one
frozenset
element of theinput
. An edge exists between two nodes (frozenset
s) if they share common elements. Then depth first graph traversal is used to find the connected components of this new graph.Use the built-in set.union() -> only include results where the length of set.intersection() is greater than 0.
This answer explains at a high level how to implement what you want. If you want a specific solution, you'll have to show your efforts to solve the problem first.