aboutsummaryrefslogtreecommitdiffstats
path: root/python/dataStructures.py
blob: 1e972fc76baf416b4430ecc3045a7e370f04e295 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class DisjointSet:
    def __init__(self, nbElements):
        self.parent = []
        self.rank = []
        for i in range(0, nbElements):
            self.parent[i] = i
            self.rank[i] = 0

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)

        # x and y already belong to the same set
        if xRoot == yRoot:
            return

        # merge the set of x and y
        if self.rank[xRoot] < self.rank[yRoot]:
            self.parent[xRoot] = yRoot
        elif self.rank[xRoot] > self.rank[yRoot]:
            self.parent[yRoot] = xRoot
        else:
            self.parent[xRoot] = yRoot
            self.rank[yRoot] += 1

    def inSameSet(self, x, y):
        return self.find(x) == self.find(y)