Answers for "how to use set to find union in python"

2

union find python

class UnionFind:
    def __init__(self) -> None:
        super().__init__()
        self.representor = {}

    def make_set(self, key):
        self.representor[key] = key

    def find(self, key):
        return self.representor[key]

    def union(self, x, y):
        x_r, y_r = self.find(x), self.find(y)
        for k, r in self.representor.items():
            if r == y_r:
                self.representor[k] = x_r
Posted by: Guest on October-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language