python中union-find数据结构的实现
pyuf的Python项目详细描述
python中Union-Find数据结构的实现。
数据被分成不同的组(“分区”)。每个小组都有名字, 以及一组物体。通过此设置,我们希望支持两个操作:
- ^{tt2}$: Return the name of the group where x belongs to.
- ^{tt3}$: Given two groups, merge them into one. The new meta-group contains all the elements from ^{tt4}$ and ^{tt5}$.
示例用法:
>>>space=UnionFindSpace(Partition("letters","abcdef"),Partition("numbers",range(5)),)>>>space.find("a")letters>>>space.find(1)numbers>>>partition=space.union("letters","numbers")>>>partition.nameletters_numbers# It's possible to indicate the name of the group>>>partition=space.union("letters","numbers","alpha")>>>partition.namealpha