Skip to content

Instantly share code, notes, and snippets.

@leonmak
Created March 4, 2019 03:18
Show Gist options
  • Save leonmak/3f5759a4606f33c5e431cadf0772a1d1 to your computer and use it in GitHub Desktop.
Save leonmak/3f5759a4606f33c5e431cadf0772a1d1 to your computer and use it in GitHub Desktop.
Union Disjoint Find Set
# 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