https://github.com/networkx/networkx/commit/1203164760f7be90d5c3a50ab7ab99f04453af8f Fix UnionFind set extraction (#3224) Paths are not guaranteed to be fully pruned by the current implementation if subtrees are unioned, e.g.: > uf = UnionFind() > uf.union(1, 2) > uf.union(3, 4) > uf.union(4, 5) > uf.union(1, 5) In the current implementation, parents[1]=3 and parents[2]=1. Thus, the mere call of networkx.utils.groups(parents) will yield a wrong result: [set([2]), set([1, 3, 4, 5])] This patch fixes this behavior by simply doing a "find" operation (which, in turn, does full path pruning) on every key before calling groups. --- networkx/utils/union_find.py.orig 2019-04-11 14:52:34.000000000 -0600 +++ networkx/utils/union_find.py 2019-09-11 09:27:32.240303742 -0600 @@ -90,6 +90,10 @@ class UnionFind: [['x', 'y'], ['z']] """ + # Ensure fully pruned paths + for x in self.parents.keys(): + _ = self[x] # Evaluated for side-effect only + # TODO In Python 3.3+, this should be `yield from ...`. for block in groups(self.parents).values(): yield block