@@ -48,12 +48,12 @@ Output: [1,4]
48
48
- ` UnionFind ` algorithm typically has three methods:
49
49
- The` unite(node1, node2) ` operation is used to merge two trees.
50
50
- 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.
52
52
53
53
##Approach (UnionFind algorithm)
54
54
1 . Initially, each node is in its own group.
55
55
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] ` .
57
57
58
58
##Complexity
59
59
* Time:` O(n) ` .
@@ -62,14 +62,11 @@ Output: [1,4]
62
62
##Python
63
63
``` python
64
64
class Solution :
65
- def __init__ (self ):
66
- self .parent= None
67
-
68
65
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 ))
70
67
71
68
for x, yin edges:
72
- if self .same_root (x, y):
69
+ if self .is_same_root (x, y):
73
70
return [x, y]
74
71
75
72
self .unite(x, y)
@@ -78,34 +75,38 @@ class Solution:
78
75
root_x= self .find_root(x)
79
76
root_y= self .find_root(y)
80
77
81
- self .parent [root_y]= root_x# Error-prone point
78
+ self .parents [root_y]= root_x# Error-prone point 1
82
79
83
80
def find_root (self ,x ):
84
- if x== self .parent[x]:
81
+ parent= self .parents[x]
82
+
83
+ if x== parent:
85
84
return x
86
85
87
- self .parent[x] = self .find_root(self . parent[x])
86
+ root = self .find_root(parent) # Error-prone point 2
88
87
89
- return self .parent[x]
88
+ self .parents[x]= root# Error-prone point 3
89
+
90
+ return root
90
91
91
- def same_root (self ,x ,y ):
92
+ def is_same_root (self ,x ,y ):
92
93
return self .find_root(x)== self .find_root(y)
93
94
```
94
95
95
96
##Java
96
97
``` java
97
98
class Solution {
98
- private int []parent ;
99
+ private int []parents ;
99
100
100
101
public int []findRedundantConnection (int [][]edges ) {
101
- parent = new int [edges. length+ 1 ];
102
+ parents = new int [edges. length+ 1 ];
102
103
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;
105
106
}
106
107
107
108
for (var edge: edges) {
108
- if (sameRoot (edge[0 ], edge[1 ])) {
109
+ if (isSameRoot (edge[0 ], edge[1 ])) {
109
110
return edge;
110
111
}
111
112
@@ -119,20 +120,24 @@ class Solution {
119
120
int rootX= findRoot(x);
120
121
int rootY= findRoot(y);
121
122
122
- parent [rootY]= rootX;// Error-prone point 1
123
+ parents [rootY]= rootX;// Error-prone point 1
123
124
}
124
125
125
126
private int findRoot (int x ) {
126
- if (x== parent[x]) {
127
+ var parent= parents[x];
128
+
129
+ if (x== parent) {
127
130
return x;
128
131
}
129
132
130
- parent[x] = findRoot(parent[x] );// Error-prone point 2
133
+ var root = findRoot(parent);// Error-prone point 2
131
134
132
- return parent[x];
135
+ parents[x]= root;// Error-prone point 3
136
+
137
+ return root;
133
138
}
134
139
135
- private boolean sameRoot (int x ,int y ) {
140
+ private boolean isSameRoot (int x ,int y ) {
136
141
return findRoot(x)== findRoot(y);
137
142
}
138
143
}
@@ -144,11 +149,11 @@ class Solution {
144
149
public:
145
150
vector<int > findRedundantConnection(vector<vector<int >>& edges) {
146
151
for (auto i = 0; i <= edges.size(); i++) {
147
- parent .push_back(i);
152
+ parents .push_back(i);
148
153
}
149
154
150
155
for (auto& edge : edges) {
151
- if (sameRoot (edge[0], edge[1])) {
156
+ if (isSameRoot (edge[0], edge[1])) {
152
157
return edge;
153
158
}
154
159
@@ -159,70 +164,78 @@ public:
159
164
}
160
165
161
166
private:
162
- vector<int >parent ;
167
+ vector<int >parents ;
163
168
164
169
void unite(int x, int y) {
165
170
int root_x = findRoot(x);
166
171
int root_y = findRoot(y);
167
172
168
- parent [root_y] = root_x; // Error-prone point 1
173
+ parents [root_y] = root_x; // Error-prone point 1
169
174
}
170
175
171
176
int findRoot(int x) {
172
- if (x == parent[x]) {
177
+ auto parent = parents[x];
178
+
179
+ if (x == parent) {
173
180
return x;
174
181
}
175
182
176
- parent[x] = findRoot(parent[x] ); // Error-prone point 2
183
+ auto root = findRoot(parent); // Error-prone point 2
177
184
178
- return parent[x];
185
+ parents[x] = root; // Error-prone point 3
186
+
187
+ return root;
179
188
}
180
189
181
- boolsameRoot (int x, int y) {
190
+ boolisSameRoot (int x, int y) {
182
191
return findRoot(x) == findRoot(y);
183
192
}
184
193
};
185
194
```
186
195
187
196
## JavaScript
188
197
```javascript
189
- letparent
198
+ letparents
190
199
191
200
var findRedundantConnection = function(edges) {
192
- parent = []
201
+ parents = []
193
202
for (let i = 0; i <= edges.length; i++) {
194
- parent .push(i)
203
+ parents .push(i)
195
204
}
196
205
197
206
for (let [x, y] of edges) {
198
- if (sameRoot (x, y)) {
207
+ if (isSameRoot (x, y)) {
199
208
return [x, y]
200
209
}
201
210
202
211
unite(x, y)
203
212
}
204
213
205
- returnsameRoot (source, destination)
214
+ returnisSameRoot (source, destination)
206
215
};
207
216
208
217
function unite(x, y) {
209
218
rootX = findRoot(x)
210
219
rootY = findRoot(y)
211
220
212
- parent [rootY] = rootX // Error-prone point 1
221
+ parents [rootY] = rootX // Error-prone point 1
213
222
}
214
223
215
224
function findRoot(x) {
216
- if (x == parent[x]) {
225
+ const parent = parents[x]
226
+
227
+ if (x == parent) {
217
228
return x
218
229
}
219
230
220
- parent[x] = findRoot(parent[x] ) // Error-prone point 2
231
+ const root = findRoot(parent) // Error-prone point 2
221
232
222
- return parent[x]
233
+ parents[x] = root // Error-prone point 3
234
+
235
+ return root
223
236
}
224
237
225
- functionsameRoot (x, y) {
238
+ functionisSameRoot (x, y) {
226
239
return findRoot(x) == findRoot(y)
227
240
}
228
241
```
@@ -231,18 +244,18 @@ function sameRoot(x, y) {
231
244
``` c#
232
245
public class Solution
233
246
{
234
- int []parent ;
247
+ int []parents ;
235
248
236
249
public int []FindRedundantConnection (int [][]edges )
237
250
{
238
- parent = new int [edges .Length + 1 ];
251
+ parents = new int [edges .Length + 1 ];
239
252
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 ;
242
255
243
256
foreach (int []edge in edges )
244
257
{
245
- if (sameRoot (edge [0 ],edge [1 ]))
258
+ if (isSameRoot (edge [0 ],edge [1 ]))
246
259
{
247
260
return edge ;
248
261
}
@@ -258,20 +271,24 @@ public class Solution
258
271
int rootX = findRoot (x );
259
272
int rootY = findRoot (y );
260
273
261
- parent [rootY ]= rootX ;// Error-prone point 1
274
+ parents [rootY ]= rootX ;// Error-prone point 1
262
275
}
263
276
264
277
int findRoot (int x )
265
278
{
266
- if (x == parent [x ])
279
+ int parent = parents [x ];
280
+
281
+ if (x == parent )
267
282
return x ;
268
283
269
- parent [ x ] = findRoot (parent [ x ] );// Error-prone point 2
284
+ int root = findRoot (parent );// Error-prone point 2
270
285
271
- return parent [x ];
286
+ parents [x ]= root ;// Error-prone point 3
287
+
288
+ return root ;
272
289
}
273
290
274
- bool sameRoot (int x ,int y )
291
+ bool isSameRoot (int x ,int y )
275
292
{
276
293
return findRoot (x )== findRoot (y );
277
294
}
@@ -280,16 +297,16 @@ public class Solution
280
297
281
298
##Go
282
299
``` go
283
- var parent []int
300
+ var parents []int
284
301
285
302
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
289
306
}
290
307
291
308
for _ ,edge := range edges {
292
- if sameRoot (edge[0 ], edge[1 ]) {
309
+ if isSameRoot (edge[0 ], edge[1 ]) {
293
310
return edge
294
311
}
295
312
@@ -303,32 +320,36 @@ func unite(x, y int) {
303
320
rootX := findRoot (x)
304
321
rootY := findRoot (y)
305
322
306
- parent [rootY] = rootX// Error-prone point 1
323
+ parents [rootY] = rootX// Error-prone point 1
307
324
}
308
325
309
326
func findRoot (x int )int {
310
- if x == parent[x] {
327
+ parent := parents[x];
328
+
329
+ if x == parent {
311
330
return x
312
331
}
313
332
314
- parent[x] =findRoot (parent[x] )// Error-prone point 2
333
+ root : =findRoot (parent)// Error-prone point 2
315
334
316
- return parent[x]
335
+ parents[x] = root// Error-prone point 3
336
+
337
+ return root
317
338
}
318
339
319
- func sameRoot (x ,y int )bool {
340
+ func isSameRoot (x ,y int )bool {
320
341
return findRoot (x) ==findRoot (y)
321
342
}
322
343
```
323
344
324
345
##Ruby
325
346
``` ruby
326
347
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 }
329
350
330
351
edges.eachdo |edge |
331
- if same_root (edge[0 ], edge[1 ])
352
+ if is_same_root (edge[0 ], edge[1 ])
332
353
return edge
333
354
end
334
355
@@ -340,20 +361,24 @@ def unite(x, y)
340
361
root_x= find_root(x)
341
362
root_y= find_root(y)
342
363
343
- @parent [root_y]= root_x# Error-prone point 1
364
+ @parents [root_y]= root_x# Error-prone point 1
344
365
end
345
366
346
367
def find_root (x )
347
- if x== @parent [x]
368
+ parent= @parents [x]
369
+
370
+ if x== parent
348
371
return x
349
372
end
350
373
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
352
377
353
- @parent [x]
378
+ root
354
379
end
355
380
356
- def same_root (x ,y )
381
+ def is_same_root (x ,y )
357
382
find_root(x)== find_root(y)
358
383
end
359
384
```