Created
November 7, 2017 21:09
-
-
Save limabeans/a6cfab44ff200db69821c8de715a3380 to your computer and use it in GitHub Desktop.
union find data structure in python
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
class UnionFind: | |
def __init__(self, N): | |
self.parent=[i for i in range(N+1)] | |
def findParent(self, v): | |
if self.parent[v]==v: return v | |
else: return self.findParent(self.parent[v]) | |
def union(self, v, w): | |
vp=self.findParent(v) | |
wp=self.findParent(w) | |
self.parent[vp]=wp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment