def make_set(self, id: SetID) -> None: if id in self._sets: raise DuplicateIDException() n = Node( parent=None, id=id, rank=0, size=1, ) n.parent = n self._sets[id] = n self._size += 1
def UNION_RANK(root_a: Node, root_b: Node) -> None: if root_a.rank < root_b.rank: root_a, root_b = root_b, root_a elif root_a.rank == root_b.rank: root_a.rank += 1 root_b.parent = root_a
def FIND_PATH_SPLITTING(node: Node) -> Node: while node.parent is not node: next = node.parent node.parent = next.parent node = next return node
def FIND_PATH_COMPRESSION(node: Node) -> Node: root = node while root.parent is not root: root = root.parent while node.parent is not root: parent = node.parent node.parent = root node = parent return root
def UNION_SIZE(root_a: Node, root_b: Node) -> None: if root_a.rank < root_b.rank: root_a, root_b = root_b, root_a root_b.parent = root_a root_a.size = root_b.size + root_a.size
def UNION_NAIVE(a: Node, b: Node) -> None: a.parent = b
def FIND_PATH_HALVING(node: Node) -> Node: while node.parent is not node: node.parent = node.parent.parent node = node.parent return node