码迷,mamicode.com
首页 > 编程语言 > 详细

python实现并查表

时间:2018-07-04 21:02:49      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:优化   return   大小   使用   查找   pre   python   name   dict   

class UnionFindSet(object):
    """并查表"""
    def __init__(self, data_list):
        """初始化两个字典,一个保存节点的父节点,另外一个保存父节点的大小
        初始化的时候,将节点的父节点设为自身,size设为1"""
        self.father_dict = {}
        self.size_dict = {}

        for node in data_list:
            self.father_dict[node] = node
            self.size_dict[node] = 1

    def find_head(self, node):
        """使用递归的方式来查找父节点

        在查找父节点的时候,顺便把当前节点移动到父节点上面
        这个操作算是一个优化
        """
        father = self.father_dict[node]
        if(node != father):
            father = self.find_head(father)
        self.father_dict[node] = father
        return father

    def is_same_set(self, node_a, node_b):
        """查看两个节点是不是在一个集合里面"""
        return self.find_head(node_a) == self.find_head(node_b)

    def union(self, node_a, node_b):
        """将两个集合合并在一起"""
        if node_a is None or node_b is None:
            return

        a_head = self.find_head(node_a)
        b_head = self.find_head(node_b)

        if(a_head != b_head):
            a_set_size = self.size_dict[a_head]
            b_set_size = self.size_dict[b_head]
            if(a_set_size >= b_set_size):
                self.father_dict[b_head] = a_head
                self.size_dict[a_head] = a_set_size + b_set_size
            else:
                self.father_dict[a_head] = b_head
                self.size_dict[b_head] = a_set_size + b_set_size

if __name__ == __main__:
    a = [1,2,3,4,5]
    union_find_set = UnionFindSet(a)
    union_find_set.union(1,2)
    union_find_set.union(3,5)
    union_find_set.union(3,1)
    print(union_find_set.is_same_set(2,5))  # True

 

python实现并查表

标签:优化   return   大小   使用   查找   pre   python   name   dict   

原文地址:https://www.cnblogs.com/jiaxin359/p/9265208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!