welcome to my blog
并查集python版本
class UnionFindSet(object):
def __init__(self, nodes):
'''
初始化并查集
:param nodes: 传入的数据
'''
self.fatherMap = {}
self.setNumMap = {}
for node in nodes:
self.fatherMap[node] = node
self.setNumMap[node] = 1
def findFather(self, node):
'''
递归逻辑:返回当前节点的父节点; base case:当前节点的父节点是自己
:param node:
:return:
'''
father = self.fatherMap[node]
if (node != father):
father = self.findFather(father)
self.fatherMap[node] = father
return father
def isSameSet(self, a, b):
'''
判断两个节点a和b是否属于同一门派
:param a:
:param b:
:return:
'''
return self.findFather(a) == self.findFather(b)
def union(self, a, b):
'''
合并a所在的门派和b所在的门派
:param a:
:param b:
:return:
'''
if a is None or b is None:
return
aFather = self.findFather(a)
bFather = self.findFather(b)
if (aFather != bFather):
aNum = self.setNumMap[aFather]
bNum = self.setNumMap[bFather]
if (aNum <= bNum):
self.fatherMap[aFather] = bFather
self.setNumMap[bFather] = aNum + bNum
self.setNumMap.pop(aFather)
else:
self.fatherMap[bFather] = aFather
self.setNumMap[aFather] = aNum + bNum
self.setNumMap.pop(bFather)
if __name__ == '__main__':
nodes = ['剑魂', '红眼', '漫游', '元素', '魔道', '战法', '大枪', '散打', '弹药', '机械']
union_find = UnionFindSet(nodes)
union_find.union('剑魂', '红眼')
union_find.union('漫游', '大枪')
union_find.union('漫游', '弹药')
union_find.union('漫游', '机械')
union_find.union('元素', '魔道')
union_find.union('元素', '战法')
print(union_find.isSameSet('大枪', '弹药'))
print(union_find.isSameSet('剑魂', '战法'))
print(union_find.isSameSet('魔道', '散打'))
print(union_find.isSameSet('魔道', '战法'))