Skip to content

Instantly share code, notes, and snippets.

@leonmak
Created March 4, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmak/29997fbf545d8e40ea918e9851d317a3 to your computer and use it in GitHub Desktop.
Save leonmak/29997fbf545d8e40ea918e9851d317a3 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