Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2f958f

Browse files
committed
684-redundant-connection.md Perfected UnionFind solution.
1 parent 2689bea commit b2f958f

File tree

2 files changed

+186
-136
lines changed

2 files changed

+186
-136
lines changed

en/1-1000/684-redundant-connection.md

Lines changed: 93 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ Output: [1,4]
4848
- `UnionFind` algorithm typically has three methods:
4949
- The `unite(node1, node2)` operation is used to merge two trees.
5050
- The `find_root(node)` method is used to return the root of a node.
51-
- The `same_root(node1, node2) == true` method is used to determine whether two nodes are in the same tree.
51+
- The `is_same_root(node1, node2) == true` method is used to determine whether two nodes are in the same tree.
5252

5353
## Approach (UnionFind algorithm)
5454
1. Initially, each node is in its own group.
5555
1. Iterate `edges` data and `unite(node1, node2)`.
56-
1. As soon as `same_root(node1, node2) == true` (a cycle will be formed), return `[node1, node2]`.
56+
1. As soon as `is_same_root(node1, node2) == true` (a cycle will be formed), return `[node1, node2]`.
5757

5858
## Complexity
5959
* Time: `O(n)`.
@@ -62,14 +62,11 @@ Output: [1,4]
6262
## Python
6363
```python
6464
class Solution:
65-
def __init__(self):
66-
self.parent = None
67-
6865
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
69-
self.parent = list(range(len(edges) + 1))
66+
self.parents = list(range(len(edges) + 1))
7067

7168
for x, y in edges:
72-
if self.same_root(x, y):
69+
if self.is_same_root(x, y):
7370
return [x, y]
7471

7572
self.unite(x, y)
@@ -78,34 +75,38 @@ class Solution:
7875
root_x = self.find_root(x)
7976
root_y = self.find_root(y)
8077

81-
self.parent[root_y] = root_x # Error-prone point
78+
self.parents[root_y] = root_x # Error-prone point 1
8279

8380
def find_root(self, x):
84-
if x == self.parent[x]:
81+
parent = self.parents[x]
82+
83+
if x == parent:
8584
return x
8685

87-
self.parent[x] = self.find_root(self.parent[x])
86+
root = self.find_root(parent) # Error-prone point 2
8887

89-
return self.parent[x]
88+
self.parents[x] = root # Error-prone point 3
89+
90+
return root
9091

91-
def same_root(self, x, y):
92+
def is_same_root(self, x, y):
9293
return self.find_root(x) == self.find_root(y)
9394
```
9495

9596
## Java
9697
```java
9798
class Solution {
98-
private int[] parent;
99+
private int[] parents;
99100

100101
public int[] findRedundantConnection(int[][] edges) {
101-
parent = new int[edges.length + 1];
102+
parents = new int[edges.length + 1];
102103

103-
for (var i = 0; i < parent.length; i++) {
104-
parent[i] = i;
104+
for (var i = 0; i < parents.length; i++) {
105+
parents[i] = i;
105106
}
106107

107108
for (var edge : edges) {
108-
if (sameRoot(edge[0], edge[1])) {
109+
if (isSameRoot(edge[0], edge[1])) {
109110
return edge;
110111
}
111112

@@ -119,20 +120,24 @@ class Solution {
119120
int rootX = findRoot(x);
120121
int rootY = findRoot(y);
121122

122-
parent[rootY] = rootX; // Error-prone point 1
123+
parents[rootY] = rootX; // Error-prone point 1
123124
}
124125

125126
private int findRoot(int x) {
126-
if (x == parent[x]) {
127+
var parent = parents[x];
128+
129+
if (x == parent) {
127130
return x;
128131
}
129132

130-
parent[x] = findRoot(parent[x]); // Error-prone point 2
133+
var root = findRoot(parent); // Error-prone point 2
131134

132-
return parent[x];
135+
parents[x] = root; // Error-prone point 3
136+
137+
return root;
133138
}
134139

135-
private boolean sameRoot(int x, int y) {
140+
private boolean isSameRoot(int x, int y) {
136141
return findRoot(x) == findRoot(y);
137142
}
138143
}
@@ -144,11 +149,11 @@ class Solution {
144149
public:
145150
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
146151
for (auto i = 0; i <= edges.size(); i++) {
147-
parent.push_back(i);
152+
parents.push_back(i);
148153
}
149154

150155
for (auto& edge : edges) {
151-
if (sameRoot(edge[0], edge[1])) {
156+
if (isSameRoot(edge[0], edge[1])) {
152157
return edge;
153158
}
154159

@@ -159,70 +164,78 @@ public:
159164
}
160165

161166
private:
162-
vector<int> parent;
167+
vector<int> parents;
163168

164169
void unite(int x, int y) {
165170
int root_x = findRoot(x);
166171
int root_y = findRoot(y);
167172

168-
parent[root_y] = root_x; // Error-prone point 1
173+
parents[root_y] = root_x; // Error-prone point 1
169174
}
170175

171176
int findRoot(int x) {
172-
if (x == parent[x]) {
177+
auto parent = parents[x];
178+
179+
if (x == parent) {
173180
return x;
174181
}
175182

176-
parent[x] = findRoot(parent[x]); // Error-prone point 2
183+
auto root = findRoot(parent); // Error-prone point 2
177184

178-
return parent[x];
185+
parents[x] = root; // Error-prone point 3
186+
187+
return root;
179188
}
180189

181-
bool sameRoot(int x, int y) {
190+
bool isSameRoot(int x, int y) {
182191
return findRoot(x) == findRoot(y);
183192
}
184193
};
185194
```
186195
187196
## JavaScript
188197
```javascript
189-
let parent
198+
let parents
190199
191200
var findRedundantConnection = function(edges) {
192-
parent = []
201+
parents = []
193202
for (let i = 0; i <= edges.length; i++) {
194-
parent.push(i)
203+
parents.push(i)
195204
}
196205
197206
for (let [x, y] of edges) {
198-
if (sameRoot(x, y)) {
207+
if (isSameRoot(x, y)) {
199208
return [x, y]
200209
}
201210
202211
unite(x, y)
203212
}
204213
205-
return sameRoot(source, destination)
214+
return isSameRoot(source, destination)
206215
};
207216
208217
function unite(x, y) {
209218
rootX = findRoot(x)
210219
rootY = findRoot(y)
211220
212-
parent[rootY] = rootX // Error-prone point 1
221+
parents[rootY] = rootX // Error-prone point 1
213222
}
214223
215224
function findRoot(x) {
216-
if (x == parent[x]) {
225+
const parent = parents[x]
226+
227+
if (x == parent) {
217228
return x
218229
}
219230
220-
parent[x] = findRoot(parent[x]) // Error-prone point 2
231+
const root = findRoot(parent) // Error-prone point 2
221232
222-
return parent[x]
233+
parents[x] = root // Error-prone point 3
234+
235+
return root
223236
}
224237
225-
function sameRoot(x, y) {
238+
function isSameRoot(x, y) {
226239
return findRoot(x) == findRoot(y)
227240
}
228241
```
@@ -231,18 +244,18 @@ function sameRoot(x, y) {
231244
```c#
232245
public class Solution
233246
{
234-
int[] parent;
247+
int[] parents;
235248

236249
public int[] FindRedundantConnection(int[][] edges)
237250
{
238-
parent = new int[edges.Length + 1];
251+
parents = new int[edges.Length + 1];
239252

240-
for (int i = 0; i < parent.Length; i++)
241-
parent[i] = i;
253+
for (int i = 0; i < parents.Length; i++)
254+
parents[i] = i;
242255

243256
foreach (int[] edge in edges)
244257
{
245-
if (sameRoot(edge[0], edge[1]))
258+
if (isSameRoot(edge[0], edge[1]))
246259
{
247260
return edge;
248261
}
@@ -258,20 +271,24 @@ public class Solution
258271
int rootX = findRoot(x);
259272
int rootY = findRoot(y);
260273

261-
parent[rootY] = rootX; // Error-prone point 1
274+
parents[rootY] = rootX; // Error-prone point 1
262275
}
263276

264277
int findRoot(int x)
265278
{
266-
if (x == parent[x])
279+
int parent = parents[x];
280+
281+
if (x == parent)
267282
return x;
268283

269-
parent[x] = findRoot(parent[x]); // Error-prone point 2
284+
int root = findRoot(parent); // Error-prone point 2
270285
271-
return parent[x];
286+
parents[x] = root; // Error-prone point 3
287+
288+
return root;
272289
}
273290

274-
bool sameRoot(int x, int y)
291+
bool isSameRoot(int x, int y)
275292
{
276293
return findRoot(x) == findRoot(y);
277294
}
@@ -280,16 +297,16 @@ public class Solution
280297

281298
## Go
282299
```go
283-
var parent []int
300+
var parents []int
284301

285302
func findRedundantConnection(edges [][]int) []int {
286-
parent = make([]int, len(edges) + 1)
287-
for i := 0; i < len(parent); i++ {
288-
parent[i] = i
303+
parents = make([]int, len(edges) + 1)
304+
for i := 0; i < len(parents); i++ {
305+
parents[i] = i
289306
}
290307

291308
for _, edge := range edges {
292-
if sameRoot(edge[0], edge[1]) {
309+
if isSameRoot(edge[0], edge[1]) {
293310
return edge
294311
}
295312

@@ -303,32 +320,36 @@ func unite(x, y int) {
303320
rootX := findRoot(x)
304321
rootY := findRoot(y)
305322

306-
parent[rootY] = rootX // Error-prone point 1
323+
parents[rootY] = rootX // Error-prone point 1
307324
}
308325

309326
func findRoot(x int) int {
310-
if x == parent[x] {
327+
parent := parents[x];
328+
329+
if x == parent {
311330
return x
312331
}
313332

314-
parent[x] = findRoot(parent[x]) // Error-prone point 2
333+
root := findRoot(parent) // Error-prone point 2
315334

316-
return parent[x]
335+
parents[x] = root // Error-prone point 3
336+
337+
return root
317338
}
318339

319-
func sameRoot(x, y int) bool {
340+
func isSameRoot(x, y int) bool {
320341
return findRoot(x) == findRoot(y)
321342
}
322343
```
323344

324345
## Ruby
325346
```ruby
326347
def find_redundant_connection(edges)
327-
@parent = []
328-
(0..edges.size).each { |i| @parent << i }
348+
@parents = []
349+
(0..edges.size).each { |i| @parents << i }
329350

330351
edges.each do |edge|
331-
if same_root(edge[0], edge[1])
352+
if is_same_root(edge[0], edge[1])
332353
return edge
333354
end
334355

@@ -340,20 +361,24 @@ def unite(x, y)
340361
root_x = find_root(x)
341362
root_y = find_root(y)
342363

343-
@parent[root_y] = root_x # Error-prone point 1
364+
@parents[root_y] = root_x # Error-prone point 1
344365
end
345366

346367
def find_root(x)
347-
if x == @parent[x]
368+
parent = @parents[x]
369+
370+
if x == parent
348371
return x
349372
end
350373

351-
@parent[x] = find_root(@parent[x]) # Error-prone point 2
374+
root = find_root(parent) # Error-prone point 2
375+
376+
@parents[x] = root # Error-prone point 3
352377

353-
@parent[x]
378+
root
354379
end
355380

356-
def same_root(x, y)
381+
def is_same_root(x, y)
357382
find_root(x) == find_root(y)
358383
end
359384
```

0 commit comments

Comments
 (0)