Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2689bea

Browse files
committed
1971-find-if-path-exists-in-graph-2.md Perfected code.
1 parentfc51fb8 commit2689bea

File tree

2 files changed

+181
-133
lines changed

2 files changed

+181
-133
lines changed

‎en/1001-2000/1971-find-if-path-exists-in-graph-2.md

Lines changed: 88 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ This graph may have multiple **connected components**.
5454
-`UnionFind` algorithm typically has three methods:
5555
- The`unite(node1, node2)` operation can be used to merge two trees.
5656
- 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.
5858

5959
##Approach (UnionFind algorithm)
6060
1. Initially, each node is in its own group.
6161
1. Iterate`edges` data and`unite(node1, node2)`.
62-
1. Return`same_root(source, destination)`.
62+
1. Return`is_same_root(source, destination)`.
6363

6464
##Complexity
6565
* Time:`O(n)`.
@@ -69,32 +69,33 @@ This graph may have multiple **connected components**.
6969
###Standard UnionFind algorithm (recommended)
7070
```python
7171
classSolution:
72-
def__init__(self):
73-
self.parent=None
74-
7572
defvalidPath(self,n:int,edges: List[List[int]],source:int,destination:int) ->bool:
76-
self.parent=list(range(n))
73+
self.parents=list(range(n))
7774

7875
for x, yin edges:
7976
self.unite(x, y)
8077

81-
returnself.same_root(source, destination)
78+
returnself.is_same_root(source, destination)
8279

8380
defunite(self,x,y):
8481
root_x=self.find_root(x)
8582
root_y=self.find_root(y)
8683

87-
self.parent[root_y]= root_x# Error-prone point 1
84+
self.parents[root_y]= root_x# Error-prone point 1
8885

8986
deffind_root(self,x):
90-
if x==self.parent[x]:
87+
parent=self.parents[x]
88+
89+
if x== parent:
9190
return x
9291

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
9495

95-
returnself.parent[x]
96+
returnroot
9697

97-
defsame_root(self,x,y):
98+
defis_same_root(self,x,y):
9899
returnself.find_root(x)==self.find_root(y)
99100
```
100101

@@ -145,40 +146,44 @@ class Solution:
145146
##Java
146147
```java
147148
classSolution {
148-
privateint[]parent;
149+
privateint[]parents;
149150

150151
publicbooleanvalidPath(intn,int[][]edges,intsource,intdestination) {
151-
parent=newint[n];
152+
parents=newint[n];
152153

153154
for (var i=0; i< n; i++) {
154-
parent[i]= i;
155+
parents[i]= i;
155156
}
156157

157158
for (var edge: edges) {
158159
unite(edge[0], edge[1]);
159160
}
160161

161-
returnsameRoot(source, destination);
162+
returnisSameRoot(source, destination);
162163
}
163164

164165
privatevoidunite(intx,inty) {
165166
int rootX= findRoot(x);
166167
int rootY= findRoot(y);
167168

168-
parent[rootY]= rootX;// Error-prone point 1
169+
parents[rootY]= rootX;// Error-prone point 1
169170
}
170171

171172
privateintfindRoot(intx) {
172-
if (x== parent[x]) {
173+
var parent= parents[x];
174+
175+
if (x== parent) {
173176
return x;
174177
}
175178

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
177182

178-
returnparent[x];
183+
returnroot;
179184
}
180185

181-
privatebooleansameRoot(intx,inty) {
186+
privatebooleanisSameRoot(intx,inty) {
182187
return findRoot(x)== findRoot(y);
183188
}
184189
}
@@ -190,77 +195,85 @@ class Solution {
190195
public:
191196
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
192197
for (auto i = 0; i < n; i++) {
193-
parent.push_back(i);
198+
parents.push_back(i);
194199
}
195200

196201
for (auto& edge : edges) {
197202
unite(edge[0], edge[1]);
198203
}
199204

200-
returnsameRoot(source, destination);
205+
returnisSameRoot(source, destination);
201206
}
202207

203208
private:
204-
vector<int>parent;
209+
vector<int>parents;
205210

206211
voidunite(int x, int y) {
207212
int root_x = findRoot(x);
208213
int root_y = findRoot(y);
209214

210-
parent[root_y] = root_x; // Error-prone point 1
215+
parents[root_y] = root_x; // Error-prone point 1
211216
}
212217

213218
int findRoot(int x) {
214-
if (x == parent[x]) {
219+
auto parent = parents[x];
220+
221+
if (x == parent) {
215222
return x;
216223
}
217224

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
219228

220-
returnparent[x];
229+
returnroot;
221230
}
222231

223-
boolsameRoot(int x, int y) {
232+
boolisSameRoot(int x, int y) {
224233
return findRoot(x) == findRoot(y);
225234
}
226235
};
227236
```
228237
229238
## JavaScript
230239
```javascript
231-
letparent
240+
letparents
232241
233242
var validPath = function (n, edges, source, destination) {
234-
parent = []
243+
parents = []
235244
for (let i = 0; i < n; i++) {
236-
parent.push(i)
245+
parents.push(i)
237246
}
238247
239248
for (const [a, b] of edges) {
240249
unite(a, b)
241250
}
242251
243-
returnsameRoot(source, destination)
252+
returnisSameRoot(source, destination)
244253
};
245254
246255
function unite(x, y) {
247256
rootX = findRoot(x)
248257
rootY = findRoot(y)
249258
250-
parent[rootY] = rootX // Error-prone point 1
259+
parents[rootY] = rootX // Error-prone point 1
251260
}
252261
253262
function findRoot(x) {
254-
if (x == parent[x]) {
263+
const parent = parents[x]
264+
265+
if (x == parent) {
255266
return x
256267
}
257268
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
259272
260-
returnparent[x]
273+
returnroot
261274
}
262275
263-
functionsameRoot(x, y) {
276+
functionisSameRoot(x, y) {
264277
return findRoot(x) == findRoot(y)
265278
}
266279
```
@@ -269,42 +282,46 @@ function sameRoot(x, y) {
269282
```c#
270283
publicclassSolution
271284
{
272-
int[]parent;
285+
int[]parents;
273286

274287
publicboolValidPath(intn,int[][]edges,intsource,intdestination)
275288
{
276-
parent=newint[n];
289+
parents=newint[n];
277290

278291
for (inti=0;i<n;i++)
279-
parent[i]=i;
292+
parents[i]=i;
280293

281294
foreach (int[]edgeinedges)
282295
{
283296
unite(edge[0],edge[1]);
284297
}
285298

286-
returnsameRoot(source,destination);
299+
returnisSameRoot(source,destination);
287300
}
288301

289302
voidunite(intx,inty)
290303
{
291304
introotX=findRoot(x);
292305
introotY=findRoot(y);
293306

294-
parent[rootY]=rootX;// Error-prone point 1
307+
parents[rootY]=rootX;// Error-prone point 1
295308
}
296309

297310
intfindRoot(intx)
298311
{
299-
if (x==parent[x])
312+
intparent=parents[x];
313+
314+
if (x==parent)
300315
returnx;
301316

302-
parent[x]=findRoot(parent[x]);// Error-prone point 2
317+
introot=findRoot(parent);// Error-prone point 2
318+
319+
parents[x]=root;// Error-prone point 3
303320
304-
returnparent[x];
321+
returnroot;
305322
}
306323

307-
boolsameRoot(intx,inty)
324+
boolisSameRoot(intx,inty)
308325
{
309326
returnfindRoot(x)==findRoot(y);
310327
}
@@ -313,74 +330,81 @@ public class Solution
313330

314331
##Go
315332
```go
316-
varparent []int
333+
varparents []int
317334

318335
funcvalidPath(nint,edges [][]int,sourceint,destinationint)bool {
319-
parent =make([]int, n)
336+
parents =make([]int, n)
320337
fori:=0; i < n; i++ {
321-
parent[i] = i
338+
parents[i] = i
322339
}
323340

324341
for_,edge:=range edges {
325342
unite(edge[0], edge[1])
326343
}
327344

328-
returnsameRoot(source, destination)
345+
returnisSameRoot(source, destination)
329346
}
330347

331348
funcunite(x,yint) {
332349
rootX:=findRoot(x)
333350
rootY:=findRoot(y)
334351

335-
parent[rootY] = rootX// Error-prone point 1
352+
parents[rootY] = rootX// Error-prone point 1
336353
}
337354

338355
funcfindRoot(xint)int {
339-
if x == parent[x] {
356+
parent:= parents[x];
357+
358+
if x == parent {
340359
return x
341360
}
342361

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
344365

345-
returnparent[x]
366+
returnroot
346367
}
347368

348-
funcsameRoot(x,yint)bool {
369+
funcisSameRoot(x,yint)bool {
349370
returnfindRoot(x) ==findRoot(y)
350371
}
351372
```
352373

353374
##Ruby
354375
```ruby
355376
defvalid_path(n,edges,source,destination)
356-
@parent= []
357-
(0...n).each { |i|@parent<< i }
377+
@parents= (0...n).to_a
358378

359379
edges.eachdo |edge|
360380
unite(edge[0], edge[1])
361381
end
362382

363-
same_root(source, destination)
383+
is_same_root(source, destination)
364384
end
365385

366386
defunite(x,y)
367387
root_x= find_root(x)
368388
root_y= find_root(y)
369389

370-
@parent[root_y]= root_x# Error-prone point 1
390+
@parents[root_y]= root_x# Error-prone point 1
371391
end
372392

373393
deffind_root(x)
374-
if x==@parent[x]
394+
parent=@parents[x]
395+
396+
if x== parent
375397
return x
376398
end
377399

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
379403

380-
@parent[x]
404+
root
381405
end
382406

383-
defsame_root(x,y)
407+
defis_same_root(x,y)
384408
find_root(x)== find_root(y)
385409
end
386410
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp