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
Many readers in the previous article expressed interest in the Union-Find algorithm, so in this article, I will take a few LeetCode problems to talk about the ingenious use of this algorithm.
8
8
9
9
First, let's recall that the Union-Find algorithm solves the problem of dynamic connectivity of the graph, but the algorithm itself is not difficult. Your ability which is to abstract the original problem into a question about graph theory to abstract the problem determines whether you can solve it.
@@ -83,7 +83,7 @@ Some readers may ask, **Since the path compression, does the weight balance of t
83
83
84
84
In my opinion, when it comes to time complexity, indeed, it is also O (1) without the need for weight balance. However, if the size array is added, the efficiency is still slightly higher, such as the following:
85
85
86
-

86
+

87
87
88
88
If weight balance optimization is carried out, case 1 will surely be obtained, without weight optimization, case 2 may occur. The `while` loop of path compression is triggered only when the height is 3, so case 1 will not trigger path compression at all, while case 2 will perform path compression many times to compress the nodes in the third layer to the second layer.
89
89
@@ -101,9 +101,9 @@ For instance, Surrounded Regions of question 130: Given a 2D board containing `X
101
101
void solve(char[][] board);
102
102
```
103
103
104
-
Note that `O` must be surrounded by four sides in order to be replaced with`X`, that is, `O` on the corner must not be enclosed, and further,`O` connected to `O` on the corner `Will not be surrounded by`X` and will not be replaced.
104
+
Note that `O` must be surrounded by four sides in order to be replaced with`X`, that is, `O` on the corner must not be enclosed, and further,`O` connected to `O` on the corner Will not be surrounded by`X` and will not be replaced.
105
105
106
-

106
+

107
107
108
108
PS: This reminds me of the chess game "Othello" when I was a kid. As long as you use two pieces to sandwich each other's pieces, the opponent's pieces will be replaced with yours. Similarly, the pieces occupying the four corners are invincible, and the side pieces connected to it are also invincible (cannot be clipped).
109
109
@@ -113,7 +113,7 @@ This problem can also be solved with the Union-Find algorithm. Although the impl
113
113
114
114
**Those `O` which do not need to be replaced have a common ancestor called` dummy`. These `O` and` dummy` are connected to each other,however, those `O` that need to be replaced are not connected to` dummy`**.
115
115
116
-

116
+

117
117
118
118
This is the core idea of Union-Find and it is easy to understand the code if you understand this diagram.
119
119
@@ -166,7 +166,7 @@ void solve(char[][] board) {
166
166
}
167
167
```
168
168
169
-
This code is very long. In fact, it is just the realization of the previous idea. Only the `O` connected to the boundary`O` have the connectivity with `dummy 'and they will not be replaced.
169
+
This code is very long. In fact, it is just the realization of the previous idea. Only the `O` connected to the boundary`O` have the connectivity with `dummy`and they will not be replaced.
170
170
171
171
To be honest, the Union-Find algorithm solves this simple problem. It can be a bit of a killer. It can solve more complex and more technical problems. **The main idea is to add virtual nodes in a timely manner. Dynamic connectivity**.
172
172
@@ -178,7 +178,7 @@ Given an array equations of strings that represent relationships between variab
178
178
179
179
Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.
180
180
181
-
The core idea of solving the problem is that **divide the expressions in `equations` into two parts according to `==` and `!=`, First process the expressions of `==`, so that they collude into martial arts through equality. `!=` Expression to check if the inequality relationship breaks the connectivity of the equality relationship**.
181
+
The core idea of solving the problem is that **divide the expressions in `equations` into two parts according to `==` and `!=`, First process the expressions of `==`, so that they are connected. `!=` Expression to check if the inequality relationship breaks the connectivity of the equality relationship**.
Today I will talk about the Union-Find algorithm, which is often referred to as the Disjoint-Set algorithm, mainly to solve the problem of "dynamic connectivity" in graph theory. Nouns look a little confusing, but they ’re really easy to understand. We will explain it later. Moreover, the application of this algorithm is also very interesting.
8
8
9
9
Speaking of this Union-Find, it should be my "Enlightenment Algorithm", this algorithm was introduced at the beginning of *Algorithms 4th edition*, I have to say that this algorithm shocked me! Later I discovered that leetcode also has related topics and is very interesting. Moreover, the solution given in *Algorithms 4th edition* can be further optimized. With only a small modification, the time complexity can be reduced to O (1).
0 commit comments