Skip to content

Commit 7785ade

Browse files
authored
fix(tutorials): syntax in dsu page (#715)
1 parent 02b22b7 commit 7785ade

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

tutorials/graph-theory/disjoint-set-union.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
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."
44
hide_table_of_contents: true
55
keywords:
66
- leetcode
@@ -14,7 +14,7 @@ keywords:
1414

1515
## Overview
1616

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$.
1818

1919
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.
2020

@@ -38,15 +38,15 @@ bool unite(int x, int y) {
3838
}
3939
```
4040

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
4242

4343
```cpp
4444
int get(int x) {
4545
return x == root[x] ? x : get(root[x]);
4646
}
4747
```
4848
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.
5050
5151
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.
5252
@@ -137,26 +137,26 @@ int main() {
137137
return 0;
138138
}
139139
```
140+
140141
export const suggestedProblems = [
141-
{
142-
"problemName": "2421. Number of Good Paths",
143-
"difficulty": "Hard",
144-
"leetCodeLink": "https://leetcode.com/problems/number-of-good-paths/",
145-
"solutionLink": "../../solutions/2400-2499/number-of-good-paths-hard"
146-
},
147-
{
148-
"problemName": "2382. Maximum Segment Sum After Removals",
149-
"difficulty": "Hard",
150-
"leetCodeLink": "https://leetcode.com/problems/maximum-segment-sum-after-removals/",
151-
"solutionLink": ""
152-
},
153-
{
154-
"problemName": "1061. Lexicographically Smallest Equivalent String",
155-
"difficulty": "Medium",
156-
"leetCodeLink": "https://leetcode.com/problems/lexicographically-smallest-equivalent-string/",
157-
"solutionLink": "../../solutions/1000-1099/lexicographically-smallest-equivalent-string-medium"
158-
}
142+
{
143+
"problemName": "2421. Number of Good Paths",
144+
"difficulty": "Hard",
145+
"leetCodeLink": "https://leetcode.com/problems/number-of-good-paths/",
146+
"solutionLink": "../../solutions/2400-2499/number-of-good-paths-hard"
147+
},
148+
{
149+
"problemName": "2382. Maximum Segment Sum After Removals",
150+
"difficulty": "Hard",
151+
"leetCodeLink": "https://leetcode.com/problems/maximum-segment-sum-after-removals/",
152+
"solutionLink": ""
153+
},
154+
{
155+
"problemName": "1061. Lexicographically Smallest Equivalent String",
156+
"difficulty": "Medium",
157+
"leetCodeLink": "https://leetcode.com/problems/lexicographically-smallest-equivalent-string/",
158+
"solutionLink": "../../solutions/1000-1099/lexicographically-smallest-equivalent-string-medium"
159+
}
159160
]
160161

161162
<Table title="Suggested Problems" data={suggestedProblems} />
162-

0 commit comments

Comments
 (0)