Created
June 24, 2014 08:34
-
-
Save tnoda/e26173d7402356ea88fe to your computer and use it in GitHub Desktop.
Union-Find 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: | |
"""Weighted quick-union with path compression. | |
The original Java implementation is introduced at | |
https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf | |
>>> uf = UnionFind(10) | |
>>> for (p, q) in [(3, 4), (4, 9), (8, 0), (2, 3), (5, 6), (5, 9), | |
... (7, 3), (4, 8), (6, 1)]: | |
... uf.union(p, q) | |
>>> uf._id | |
[8, 3, 3, 3, 3, 3, 3, 3, 3, 3] | |
>>> uf.find(0, 1) | |
True | |
>>> uf._id | |
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3] | |
""" | |
def __init__(self, n): | |
self._id = list(range(n)) | |
self._sz = [1] * n | |
def _root(self, i): | |
j = i | |
while (j != self._id[j]): | |
self._id[j] = self._id[self._id[j]] | |
j = self._id[j] | |
return j | |
def find(self, p, q): | |
return self._root(p) == self._root(q) | |
def union(self, p, q): | |
i = self._root(p) | |
j = self._root(q) | |
if (self._sz[i] < self._sz[j]): | |
self._id[i] = j | |
self._sz[j] += self._sz[i] | |
else: | |
self._id[j] = i | |
self._sz[i] += self._sz[j] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! There is a bug in the union() method, I forked and created a fixed gist here https://gist.github.com/SonechkaGodovykh/18f60a3b9b3e6812c071456f61f9c5a6
Update your code, if you are interested in it.
Thanks.