Skip to content

Commit 9f0c4bf

Browse files
committed
Improved unionfind via iterative path compression
1 parent f5dfb52 commit 9f0c4bf

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/tdamapper/utils/unionfind.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ def __init__(self, X):
55
self.__size = {x: 1 for x in X}
66

77
def find(self, x):
8-
y = x
9-
while y != self.__parent[y]:
10-
y = self.__parent[y]
11-
return y
8+
root = x
9+
while root != self.__parent[root]:
10+
root = self.__parent[root]
11+
tmp = x
12+
while tmp != root:
13+
parent = self.__parent[tmp]
14+
self.__parent[tmp] = root
15+
tmp = parent
16+
return root
1217

1318
def union(self, x, y):
1419
x, y = self.find(x), self.find(y)

0 commit comments

Comments
 (0)