ó ŸÃÒYc@s=dZddlmZddlmZddd„ƒYZdS(s Union-find data structure. iÿÿÿÿ(tgroupby(tgroupst UnionFindcBs>eZdZdd„Zd„Zd„Zd„Zd„ZRS(sžUnion-find data structure. Each unionFind instance X maintains a family of disjoint sets of hashable objects, supporting the following two methods: - X[item] returns a name for the set containing the given item. Each set is named by an arbitrarily-chosen one of its members; as long as the set remains unchanged it will keep the same name. If the item is not yet part of a set in X, a new singleton set is created for it. - X.union(item1, item2, ...) merges the sets containing each item into a single larger set. If any item is not yet part of a set in X, it is added to X as one of the members of the merged set. Union-find data structure. Based on Josiah Carlson's code, http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/215912 with significant additional changes by D. Eppstein. http://www.ics.uci.edu/~eppstein/PADS/UnionFind.py cCsV|dkrd}ni|_i|_x(|D] }d|j|<||j|>> partition = UnionFind('xyz') >>> sorted(map(sorted, partition.to_sets())) [['x'], ['y'], ['z']] >>> partition.union('x', 'y') >>> sorted(map(sorted, partition.to_sets())) [['x', 'y'], ['z']] N(RRtvalues(Rtblock((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/union_find.pytto_setsQscsƒg|D]}ˆ|^q}t|d‡fd†ƒ}xD|D]<}||kr?ˆj|cˆj|7<|ˆj|fsN(tmaxRR(RtobjectsRtrootstheaviestR((Rso/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/union_find.pytunionbs   N( t__name__t __module__t__doc__RR RRRR(((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/union_find.pyRs     N((Rt itertoolsRtnetworkx.utilsRR(((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/utils/union_find.pyt s