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: tutorials/graph-theory/disjoint-set-union.md
+24-24
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: 'Disjoint Set Union (DSU)'
3
-
description: 'Disjoint Set Union is a data structure that allows us to combine any two sets into one.'
2
+
title: "Disjoint Set Union (DSU)"
3
+
description: "Disjoint Set Union is a data structure that allows us to combine any two sets into one."
4
4
hide_table_of_contents: true
5
5
keywords:
6
6
- leetcode
@@ -14,7 +14,7 @@ keywords:
14
14
15
15
## Overview
16
16
17
-
A set is a collection of elements. If two sets have no common elements, then they are called disjoint sets. For example, {1, 2} and {3, 4} are disjoint sets while {1, 2} and {1, 3} are not because they have a common element $1$.
17
+
A set is a collection of elements. If two sets have no common elements, then they are called disjoint sets. For example, $\{1, 2\}$ and $\{3, 4\}$ are disjoint sets while $\{1, 2\}$ and $\{1, 3\}$ are not because they have a common element $1$.
18
18
19
19
Disjoint Set Union (or DSU or Union Find) is a data structure that allows us to combine any two sets into one. Let's say we have $10$ elements and we initialise an array $root$ with a size of $10$. Here we have $10$ sets and each individual element in the set is the parent.
20
20
@@ -38,15 +38,15 @@ bool unite(int x, int y) {
38
38
}
39
39
```
40
40
41
-
If we need to check whether two elements have the same parent, then we need a function $get$ to check it. To implement that, we simply check if the target element $x$ is $root[x]$, otherwise we can call the same function recursively until we have the root. In other word, the parent would be
41
+
If we need to check whether two elements have the same parent, then we need a function $get$ to check it. To implement that, we simply check if the target element $x$ is $root[x]$, otherwise we can call the same function recursively until we have the root. In other word, the parent would be
42
42
43
43
```cpp
44
44
intget(int x) {
45
45
return x == root[x] ? x : get(root[x]);
46
46
}
47
47
```
48
48
49
-
However, the above implementation is not efficient as each call depends on $n$ while we need to optimize it nearly constant time.
49
+
However, the above implementation is not efficient as each call depends on $n$ while we need to optimize it nearly constant time.
50
50
51
51
One way to optimize it is compress the path. For example, if the root element is $1$ and we have the chain like $1$ -> $2$ -> $3$ -> $4$. If we write it vertically, element $1$ is on the top level, element $2$ is on the second level, element $3$ is on the third level and so on. We can compress these into the same level, i.e. element $2$, $3$ and $4$ would be on the second level only so that we don't need to talk all the nodes between the root and the source. This would achieve $O(log n)$ per call on average.
0 commit comments