Created
March 4, 2019 03:18
-
-
Save leonmak/3f5759a4606f33c5e431cadf0772a1d1 to your computer and use it in GitHub Desktop.
Union Disjoint Find Set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if not root, find parent, set parent, return parent | |
p = map(chr, range(n)) | |
def find(p, x): | |
if p[x] != x: | |
p[x] = find(p, p[x]) | |
return p[x] | |
def union(p, i, j): | |
pi, pj = find(p, i), find(p, j) | |
if pi != pj : | |
p[pi] = pj | |
def is_connected(p, i, j): | |
return find(p, u) == find(p, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment