美文网首页
并查集UFS实现模板

并查集UFS实现模板

作者: 钢筋铁骨 | 来源:发表于2022-04-23 00:00 被阅读0次
# 不带权重的并查集
class UnionFindSet:
    def __init__(self):
        self.father = {} # key: 节点
    def add(self, x):
        if x not in self.father:
            self.father[x] = None
    # 查询根节点的同时进行路径压缩
    def findRoot(self, x):
        root = x
        while self.father[root] is not None:
            root = self.father[root]
        # root为当前的根节点
        # 路径压缩
        if x != root:
            original_father = self.father[x]
            root = self.findRoot(original_father)
            self.father[x] = root
        return root
    def union(self, x, y):
        # 此处进行了查找
        root_x, root_y = self.findRoot(x), self.findRoot(y)
        if root_x != root_y:
            self.father[root_x] = root_y
    # 判断xy是否在一个集合中
    def isConnected(self, x, y):
        root_x ,root_y = self.findRoot(x), self.findRoot(y)
        if root_x != root_y: # 不在一个集合中
            return False
        return True
# 带权重的并查集
class UnionFindSet:
    def __init__(self):
        self.father={} # key:节点,value:father
        self.weights={} # 节点到父节点的权重
    def add(self, x):
        if x not in self.father:
            self.father[x] = None
            self.weights[x] = 1
    # 查询根节点的同时进行路径压缩并修改权重,递归写法
    def findRoot2(self, x):
        root = x
        # 顺着x往上窜,一直找到x的祖先root
        while self.father[root] is not None:
            root = self.father[root]
        # root为x的根节点
        # 路径压缩并修改权重:迭代 改为 递归
        if x != root:
            # 再顺着x往上窜一遍,一边窜一边把每个节点的父节点改成root
            original_father = self.father[x]
            root = self.findRoot(original_father)# 递归的方式往上找
            self.father[x] = root
            self.weights[x] = self.weights[x] * self.weights[original_father]
        return root
    # 迭代写法
    def findRoot(self, x):
        root = x
        base = 1 # 权重放大倍数
        # 顺着x往上窜,一直找到x的祖先root
        while self.father[root] is not None:
            root = self.father[root]
            base *= self.weights[root] # 总倍数
        # root为x的根节点,路径压缩.
        while x != root:
            original_father = self.father[x]
            self.father[x] = root
            self.weights[x] *= base
            # 离根节点越近,放大倍数越小。
            base /= self.weights[original_father]
            x = original_father
        return root

    def union(self, x, y, value):
        # value 为 x/y的值
        # 此处进行了查找,weights已经改变了
        root_x, root_y = self.findRoot(x), self.findRoot(y)
        # x,y的根节点相等,说明不需要合并
        if root_x == root_y:
            return
        # x指向y或者y指向x都行,整个并查集都一致就行
        if root_x != root_y:
            self.father[root_x] = root_y
            """
             这个公式需要推导
             x/y = 2 ==> 2*y/x = 基本单位
             记根节点为s = 1,也就是root_x
            """
            self.weights[root_x] = self.weights[y] * value / self.weights[x]
    # 判断x,y是否在一个集合中,如果在一个集合中则返回除法的结果否则返回-1
    def isConnected(self, x, y):
        # 注意此处执行了查询,修改了权重
        root_x ,root_y = self.findRoot(x), self.findRoot(y)
        if root_x != root_y: # 不在一个集合中
            return -1
        return self.weights[x] / self.weights[y]

相关文章

  • 并查集UFS实现模板

  • 并查集UFS

    简介 并查集(Union Find Set)是一种高级数据结构,主要用于图存储中不同集合的合并与查询问题。并查集这...

  • LeetCode 分类刷题 —— Union Find

    Union Find 的 Tips: 灵活使用并查集的思想,熟练掌握并查集的模板,模板中有两种并查集的实现方式,一...

  • 模板

    并查集模板

  • 并查集

    并查集在LeetCode周赛里面经常会用到,所以可以准备好模板以节省比赛做题时间。以下并查集类Python3实现修...

  • 并查集模板

    以PAT甲级1114为例,写了个并查集模板,记录下来。题目就不列了,感兴趣去官网找一下

  • 并查集模板

  • 并查集模板

  • 并查集模板

    并查集学习可查看网站[https://oi-wiki.org/ds/dsu/]

  • 并查集练习

    并查集 参考《算法笔记》 三要素:并,查,集。 并差集与一个算法有关,kruskal算法,就可通过并查集实现。 1...

网友评论

      本文标题:并查集UFS实现模板

      本文链接:https://www.haomeiwen.com/subject/vlvoertx.html