@@ -54,12 +54,12 @@ This graph may have multiple **connected components**.
54
54
- ` UnionFind ` algorithm typically has three methods:
55
55
- The ` unite(node1, node2) ` operation can be used to merge two trees.
56
56
- The ` find_root(node) ` method can be used to return the root of a node.
57
- - The ` same_root (node1, node2)` method is used to determine whether two nodes are in the same tree.
57
+ - The ` is_same_root (node1, node2)` method is used to determine whether two nodes are in the same tree.
58
58
59
59
## Approach (UnionFind algorithm)
60
60
1 . Initially, each node is in its own group.
61
61
1 . Iterate ` edges ` data and ` unite(node1, node2) ` .
62
- 1 . Return ` same_root (source, destination)` .
62
+ 1 . Return ` is_same_root (source, destination)` .
63
63
64
64
## Complexity
65
65
* Time: ` O(n) ` .
@@ -69,32 +69,33 @@ This graph may have multiple **connected components**.
69
69
### Standard UnionFind algorithm (recommended)
70
70
``` python
71
71
class Solution :
72
- def __init__ (self ):
73
- self .parent = None
74
-
75
72
def validPath (self , n : int , edges : List[List[int ]], source : int , destination : int ) -> bool :
76
- self .parent = list (range (n))
73
+ self .parents = list (range (n))
77
74
78
75
for x, y in edges:
79
76
self .unite(x, y)
80
77
81
- return self .same_root (source, destination)
78
+ return self .is_same_root (source, destination)
82
79
83
80
def unite (self , x , y ):
84
81
root_x = self .find_root(x)
85
82
root_y = self .find_root(y)
86
83
87
- self .parent [root_y] = root_x # Error-prone point 1
84
+ self .parents [root_y] = root_x # Error-prone point 1
88
85
89
86
def find_root (self , x ):
90
- if x == self .parent[x]:
87
+ parent = self .parents[x]
88
+
89
+ if x == parent:
91
90
return x
92
91
93
- self .parent[x] = self .find_root(self .parent[x]) # Error-prone point 2
92
+ root = self .find_root(parent) # Error-prone point 2
93
+
94
+ self .parents[x] = root # Error-prone point 3
94
95
95
- return self .parent[x]
96
+ return root
96
97
97
- def same_root (self , x , y ):
98
+ def is_same_root (self , x , y ):
98
99
return self .find_root(x) == self .find_root(y)
99
100
```
100
101
@@ -145,40 +146,44 @@ class Solution:
145
146
## Java
146
147
``` java
147
148
class Solution {
148
- private int [] parent ;
149
+ private int [] parents ;
149
150
150
151
public boolean validPath (int n , int [][] edges , int source , int destination ) {
151
- parent = new int [n];
152
+ parents = new int [n];
152
153
153
154
for (var i = 0 ; i < n; i++ ) {
154
- parent [i] = i;
155
+ parents [i] = i;
155
156
}
156
157
157
158
for (var edge : edges) {
158
159
unite(edge[0 ], edge[1 ]);
159
160
}
160
161
161
- return sameRoot (source, destination);
162
+ return isSameRoot (source, destination);
162
163
}
163
164
164
165
private void unite (int x , int y ) {
165
166
int rootX = findRoot(x);
166
167
int rootY = findRoot(y);
167
168
168
- parent [rootY] = rootX; // Error-prone point 1
169
+ parents [rootY] = rootX; // Error-prone point 1
169
170
}
170
171
171
172
private int findRoot (int x ) {
172
- if (x == parent[x]) {
173
+ var parent = parents[x];
174
+
175
+ if (x == parent) {
173
176
return x;
174
177
}
175
178
176
- parent[x] = findRoot(parent[x]); // Error-prone point 2
179
+ var root = findRoot(parent); // Error-prone point 2
180
+
181
+ parents[x] = root; // Error-prone point 3
177
182
178
- return parent[x] ;
183
+ return root ;
179
184
}
180
185
181
- private boolean sameRoot (int x , int y ) {
186
+ private boolean isSameRoot (int x , int y ) {
182
187
return findRoot(x) == findRoot(y);
183
188
}
184
189
}
@@ -190,77 +195,85 @@ class Solution {
190
195
public:
191
196
bool validPath(int n, vector<vector<int >>& edges, int source, int destination) {
192
197
for (auto i = 0; i < n; i++) {
193
- parent .push_back(i);
198
+ parents .push_back(i);
194
199
}
195
200
196
201
for (auto& edge : edges) {
197
202
unite(edge[0], edge[1]);
198
203
}
199
204
200
- return sameRoot (source, destination);
205
+ return isSameRoot (source, destination);
201
206
}
202
207
203
208
private:
204
- vector<int > parent ;
209
+ vector<int > parents ;
205
210
206
211
void unite (int x, int y) {
207
212
int root_x = findRoot(x);
208
213
int root_y = findRoot(y);
209
214
210
- parent [root_y] = root_x; // Error-prone point 1
215
+ parents [root_y] = root_x; // Error-prone point 1
211
216
}
212
217
213
218
int findRoot(int x) {
214
- if (x == parent[x]) {
219
+ auto parent = parents[x];
220
+
221
+ if (x == parent) {
215
222
return x;
216
223
}
217
224
218
- parent[x] = findRoot(parent[x]); // Error-prone point 2
225
+ auto root = findRoot(parent); // Error-prone point 2
226
+
227
+ parents[x] = root; // Error-prone point 3
219
228
220
- return parent[x] ;
229
+ return root ;
221
230
}
222
231
223
- bool sameRoot (int x, int y) {
232
+ bool isSameRoot (int x, int y) {
224
233
return findRoot(x) == findRoot(y);
225
234
}
226
235
};
227
236
```
228
237
229
238
## JavaScript
230
239
```javascript
231
- let parent
240
+ let parents
232
241
233
242
var validPath = function (n, edges, source, destination) {
234
- parent = []
243
+ parents = []
235
244
for (let i = 0; i < n; i++) {
236
- parent .push(i)
245
+ parents .push(i)
237
246
}
238
247
239
248
for (const [a, b] of edges) {
240
249
unite(a, b)
241
250
}
242
251
243
- return sameRoot (source, destination)
252
+ return isSameRoot (source, destination)
244
253
};
245
254
246
255
function unite(x, y) {
247
256
rootX = findRoot(x)
248
257
rootY = findRoot(y)
249
258
250
- parent [rootY] = rootX // Error-prone point 1
259
+ parents [rootY] = rootX // Error-prone point 1
251
260
}
252
261
253
262
function findRoot(x) {
254
- if (x == parent[x]) {
263
+ const parent = parents[x]
264
+
265
+ if (x == parent) {
255
266
return x
256
267
}
257
268
258
- parent[x] = findRoot(parent[x]) // Error-prone point 2
269
+ const root = findRoot(parent) // Error-prone point 2
270
+
271
+ parents[x] = root // Error-prone point 3
259
272
260
- return parent[x]
273
+ return root
261
274
}
262
275
263
- function sameRoot (x, y) {
276
+ function isSameRoot (x, y) {
264
277
return findRoot(x) == findRoot(y)
265
278
}
266
279
```
@@ -269,42 +282,46 @@ function sameRoot(x, y) {
269
282
``` c#
270
283
public class Solution
271
284
{
272
- int [] parent ;
285
+ int [] parents ;
273
286
274
287
public bool ValidPath (int n , int [][] edges , int source , int destination )
275
288
{
276
- parent = new int [n ];
289
+ parents = new int [n ];
277
290
278
291
for (int i = 0 ; i < n ; i ++ )
279
- parent [i ] = i ;
292
+ parents [i ] = i ;
280
293
281
294
foreach (int [] edge in edges )
282
295
{
283
296
unite (edge [0 ], edge [1 ]);
284
297
}
285
298
286
- return sameRoot (source , destination );
299
+ return isSameRoot (source , destination );
287
300
}
288
301
289
302
void unite (int x , int y )
290
303
{
291
304
int rootX = findRoot (x );
292
305
int rootY = findRoot (y );
293
306
294
- parent [rootY ] = rootX ; // Error-prone point 1
307
+ parents [rootY ] = rootX ; // Error-prone point 1
295
308
}
296
309
297
310
int findRoot (int x )
298
311
{
299
- if (x == parent [x ])
312
+ int parent = parents [x ];
313
+
314
+ if (x == parent )
300
315
return x ;
301
316
302
- parent [x ] = findRoot (parent [x ]); // Error-prone point 2
317
+ int root = findRoot (parent ); // Error-prone point 2
318
+
319
+ parents [x ] = root ; // Error-prone point 3
303
320
304
- return parent [ x ] ;
321
+ return root ;
305
322
}
306
323
307
- bool sameRoot (int x , int y )
324
+ bool isSameRoot (int x , int y )
308
325
{
309
326
return findRoot (x ) == findRoot (y );
310
327
}
@@ -313,74 +330,81 @@ public class Solution
313
330
314
331
## Go
315
332
``` go
316
- var parent []int
333
+ var parents []int
317
334
318
335
func validPath (n int , edges [][]int , source int , destination int ) bool {
319
- parent = make ([]int , n)
336
+ parents = make ([]int , n)
320
337
for i := 0 ; i < n; i++ {
321
- parent [i] = i
338
+ parents [i] = i
322
339
}
323
340
324
341
for _ , edge := range edges {
325
342
unite (edge[0 ], edge[1 ])
326
343
}
327
344
328
- return sameRoot (source, destination)
345
+ return isSameRoot (source, destination)
329
346
}
330
347
331
348
func unite (x , y int ) {
332
349
rootX := findRoot (x)
333
350
rootY := findRoot (y)
334
351
335
- parent [rootY] = rootX // Error-prone point 1
352
+ parents [rootY] = rootX // Error-prone point 1
336
353
}
337
354
338
355
func findRoot (x int ) int {
339
- if x == parent[x] {
356
+ parent := parents[x];
357
+
358
+ if x == parent {
340
359
return x
341
360
}
342
361
343
- parent[x] = findRoot (parent[x]) // Error-prone point 2
362
+ root := findRoot (parent) // Error-prone point 2
363
+
364
+ parents[x] = root // Error-prone point 3
344
365
345
- return parent[x]
366
+ return root
346
367
}
347
368
348
- func sameRoot (x , y int ) bool {
369
+ func isSameRoot (x , y int ) bool {
349
370
return findRoot (x) == findRoot (y)
350
371
}
351
372
```
352
373
353
374
## Ruby
354
375
``` ruby
355
376
def valid_path (n , edges , source , destination )
356
- @parent = []
357
- (0 ...n).each { |i | @parent << i }
377
+ @parents = (0 ...n).to_a
358
378
359
379
edges.each do |edge |
360
380
unite(edge[0 ], edge[1 ])
361
381
end
362
382
363
- same_root (source, destination)
383
+ is_same_root (source, destination)
364
384
end
365
385
366
386
def unite (x , y )
367
387
root_x = find_root(x)
368
388
root_y = find_root(y)
369
389
370
- @parent [root_y] = root_x # Error-prone point 1
390
+ @parents [root_y] = root_x # Error-prone point 1
371
391
end
372
392
373
393
def find_root (x )
374
- if x == @parent [x]
394
+ parent = @parents [x]
395
+
396
+ if x == parent
375
397
return x
376
398
end
377
399
378
- @parent [x] = find_root(@parent [x]) # Error-prone point 2
400
+ root = find_root(parent) # Error-prone point 2
401
+
402
+ @parents [x] = root # Error-prone point 3
379
403
380
- @parent [x]
404
+ root
381
405
end
382
406
383
- def same_root (x , y )
407
+ def is_same_root (x , y )
384
408
find_root(x) == find_root(y)
385
409
end
386
410
```
0 commit comments