Created
March 4, 2019 03:18
-
-
Save leonmak/714a5a7619536a3e4d2857aa53abca9e to your computer and use it in GitHub Desktop.
Union Disjoint Find Set
This file contains hidden or 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