You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: thinkings/union-find.en.md
+2-29Lines changed: 2 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Union Find Data Structure
2
2
3
-
Leetcode has many problems concerning the union-find data structure. To be specific, the official number is 30(until 2020-02-20). And some problems, though not labeled with `Union Find`, can be solved more easily by applying this data structure. A problem-solving pattern can be found among this kind of problem. Once you have grasped the pattern, you can solve these problems with higher speed and fewer mistakes, which is the benefit of using patterns.
3
+
Leetcode has many problems concerning the union-find data structure. To be specific, the official number is 30(until 2020-02-20). And some problems, though not labeled with `Union Find`, can be solved more easily by applying this data structure. A problem-solving pattern can be found among this kind of problem. Once you have grasped the pattern, you can solve these problems with higher speed and fewer mistakes, which is the benefit of using patterns.
4
4
5
5
Related problems:
6
6
@@ -100,9 +100,9 @@ class UF:
100
100
101
101
deffind(self, x):
102
102
while x !=self.parent[x]:
103
-
x =self.parent[x]
104
103
# path compression
105
104
self.parent[x] =self.parent[self.parent[x]];
105
+
x =self.parent[x]
106
106
return x
107
107
defunion(self, p, q):
108
108
ifself.connected(p, q): return
@@ -117,30 +117,3 @@ class UF:
117
117
defconnected(self, p, q):
118
118
returnself.find(p) ==self.find(q)
119
119
```
120
-
121
-
The code above implements path compression with recursion, which, though is easier to write, contains the risk of stack overflow. The following is how we can do it with iteration.
0 commit comments