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