Skip to content

Commit 3cd5d63

Browse files
committed
1971-find-if-path-exists-in-graph-2.md Reworded.
1 parent 6d5cac2 commit 3cd5d63

File tree

2 files changed

+54
-57
lines changed

2 files changed

+54
-57
lines changed

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

+52-52
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ Explanation: There is no path from vertex 0 to vertex 5.
4040
Please see [1971. Find if Path Exists in Graph ('Breadth-First Search' Solution)](1971-find-if-path-exists-in-graph.md).
4141

4242
## Intuition
43-
The island problem can be abstracted into a **graph theory** problem. This is an **undirected graph**:
44-
45-
![](../../images/graph_undirected_1.svg)
46-
47-
And this graph may have multiple **connected components**. Initially, we start from `source` vertex which belongs to one of the `connected components`.
43+
This graph may have multiple **connected components**.
4844

4945
![](../../images/graph_undirected_2.png)
5046

47+
- Initially, we start from `source` vertex which belongs to one of the `connected components`.
5148
- We need to find if there is a path from `source` to `destination`. This question is equivalent to determine if `source` and `destination` vertices belong to the same `connected component`.
5249
- A `tree` is a type of `graph`. If two nodes are in the same tree, then return `true`. So we need a method `in_same_tree(node1, node2)` to return a boolean value.
5350
- We are given `edges` data and need to divide them into multiple groups, each group can be abstracted into a **tree**.
@@ -73,10 +70,10 @@ And this graph may have multiple **connected components**. Initially, we start f
7370
```python
7471
class Solution:
7572
def __init__(self):
76-
self.father = None
73+
self.fathers = None
7774

7875
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
79-
self.father = list(range(n))
76+
self.fathers = list(range(n))
8077

8178
for x, y in edges:
8279
self.unite(x, y)
@@ -87,22 +84,25 @@ class Solution:
8784
root_x = self.find_root(x)
8885
root_y = self.find_root(y)
8986

90-
self.father[root_y] = root_x # Error-prone point 1
87+
self.fathers[root_y] = root_x # Error-prone point 1
9188

9289
def find_root(self, x):
93-
if x == self.father[x]:
90+
if x == self.fathers[x]:
9491
return x
9592

96-
self.father[x] = self.find_root(self.father[x]) # Error-prone point 2
93+
self.fathers[x] = self.find_root(self.fathers[x]) # Error-prone point 2
9794

98-
return self.father[x]
95+
return self.fathers[x]
9996

10097
def same_root(self, x, y):
10198
return self.find_root(x) == self.find_root(y)
10299
```
103100

104101
### Another UnionFind algorithm (using a map and an array of set)
105-
This solution is slower than the `Standard UnionFind algorithm`, but it is straightforward.
102+
* This solution is slower than the `standard UnionFind algorithm`, but it is straightforward.
103+
* The applicability of this solution is not as wide as the `standard UnionFind algorithm` because data in a set don't have association.
104+
The `standard UnionFind algorithm` has a `fathers` array with association of nodes.
105+
106106
```python
107107
class Solution:
108108
def __init__(self):
@@ -145,13 +145,13 @@ class Solution:
145145
## Java
146146
```java
147147
class Solution {
148-
private int[] father;
148+
private int[] fathers;
149149

150150
public boolean validPath(int n, int[][] edges, int source, int destination) {
151-
father = new int[n];
151+
fathers = new int[n];
152152

153153
for (var i = 0; i < n; i++) {
154-
father[i] = i;
154+
fathers[i] = i;
155155
}
156156

157157
for (var edge : edges) {
@@ -165,17 +165,17 @@ class Solution {
165165
int rootX = findRoot(x);
166166
int rootY = findRoot(y);
167167

168-
father[rootY] = rootX; // Error-prone point 1
168+
fathers[rootY] = rootX; // Error-prone point 1
169169
}
170170

171171
private int findRoot(int x) {
172-
if (x == father[x]) {
172+
if (x == fathers[x]) {
173173
return x;
174174
}
175175

176-
father[x] = findRoot(father[x]); // Error-prone point 2
176+
fathers[x] = findRoot(fathers[x]); // Error-prone point 2
177177

178-
return father[x];
178+
return fathers[x];
179179
}
180180

181181
private boolean sameRoot(int x, int y) {
@@ -188,23 +188,23 @@ class Solution {
188188
```cpp
189189
class Solution {
190190
private:
191-
vector<int> father;
191+
vector<int> fathers;
192192

193193
void unite(int x, int y) {
194194
int root_x = findRoot(x);
195195
int root_y = findRoot(y);
196196

197-
father[root_y] = root_x; // Error-prone point 1
197+
fathers[root_y] = root_x; // Error-prone point 1
198198
}
199199

200200
int findRoot(int x) {
201-
if (x == father[x]) {
201+
if (x == fathers[x]) {
202202
return x;
203203
}
204204

205-
father[x] = findRoot(father[x]); // Error-prone point 2
205+
fathers[x] = findRoot(fathers[x]); // Error-prone point 2
206206

207-
return father[x];
207+
return fathers[x];
208208
}
209209

210210
bool sameRoot(int x, int y) {
@@ -214,7 +214,7 @@ private:
214214
public:
215215
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
216216
for (auto i = 0; i < n; i++) {
217-
father.push_back(i);
217+
fathers.push_back(i);
218218
}
219219

220220
for (auto& edge : edges) {
@@ -228,12 +228,12 @@ public:
228228
229229
## JavaScript
230230
```javascript
231-
let father
231+
let fathers
232232
233233
var validPath = function (n, edges, source, destination) {
234-
father = []
234+
fathers = []
235235
for (let i = 0; i < n; i++) {
236-
father.push(i)
236+
fathers.push(i)
237237
}
238238
239239
for (let [a, b] of edges) {
@@ -247,17 +247,17 @@ function unite(x, y) {
247247
rootX = findRoot(x)
248248
rootY = findRoot(y)
249249
250-
father[rootY] = rootX // Error-prone point 1
250+
fathers[rootY] = rootX // Error-prone point 1
251251
}
252252
253253
function findRoot(x) {
254-
if (x == father[x]) {
254+
if (x == fathers[x]) {
255255
return x
256256
}
257257
258-
father[x] = findRoot(father[x]) // Error-prone point 2
258+
fathers[x] = findRoot(fathers[x]) // Error-prone point 2
259259
260-
return father[x]
260+
return fathers[x]
261261
}
262262
263263
function sameRoot(x, y) {
@@ -269,14 +269,14 @@ function sameRoot(x, y) {
269269
```c#
270270
public class Solution
271271
{
272-
int[] father;
272+
int[] fathers;
273273

274274
public bool ValidPath(int n, int[][] edges, int source, int destination)
275275
{
276-
father = new int[n];
276+
fathers = new int[n];
277277

278278
for (int i = 0; i < n; i++)
279-
father[i] = i;
279+
fathers[i] = i;
280280

281281
foreach (int[] edge in edges)
282282
{
@@ -291,17 +291,17 @@ public class Solution
291291
int rootX = findRoot(x);
292292
int rootY = findRoot(y);
293293

294-
father[rootY] = rootX; // Error-prone point 1
294+
fathers[rootY] = rootX; // Error-prone point 1
295295
}
296296

297297
int findRoot(int x)
298298
{
299-
if (x == father[x])
299+
if (x == fathers[x])
300300
return x;
301301

302-
father[x] = findRoot(father[x]); // Error-prone point 2
302+
fathers[x] = findRoot(fathers[x]); // Error-prone point 2
303303
304-
return father[x];
304+
return fathers[x];
305305
}
306306

307307
bool sameRoot(int x, int y)
@@ -313,12 +313,12 @@ public class Solution
313313

314314
## Go
315315
```go
316-
var father []int
316+
var fathers []int
317317

318318
func validPath(n int, edges [][]int, source int, destination int) bool {
319-
father = make([]int, n)
319+
fathers = make([]int, n)
320320
for i := 0; i < n; i++ {
321-
father[i] = i
321+
fathers[i] = i
322322
}
323323

324324
for _, edge := range edges {
@@ -332,17 +332,17 @@ func unite(x, y int) {
332332
rootX := findRoot(x)
333333
rootY := findRoot(y)
334334

335-
father[rootY] = rootX // Error-prone point 1
335+
fathers[rootY] = rootX // Error-prone point 1
336336
}
337337

338338
func findRoot(x int) int {
339-
if x == father[x] {
339+
if x == fathers[x] {
340340
return x
341341
}
342342

343-
father[x] = findRoot(father[x]) // Error-prone point 2
343+
fathers[x] = findRoot(fathers[x]) // Error-prone point 2
344344

345-
return father[x]
345+
return fathers[x]
346346
}
347347

348348
func sameRoot(x, y int) bool {
@@ -353,8 +353,8 @@ func sameRoot(x, y int) bool {
353353
## Ruby
354354
```ruby
355355
def valid_path(n, edges, source, destination)
356-
@father = []
357-
(0...n).each { |i| @father << i }
356+
@fathers = []
357+
(0...n).each { |i| @fathers << i }
358358

359359
edges.each do |edge|
360360
unite(edge[0], edge[1])
@@ -367,17 +367,17 @@ def unite(x, y)
367367
root_x = find_root(x)
368368
root_y = find_root(y)
369369

370-
@father[root_y] = root_x # Error-prone point 1
370+
@fathers[root_y] = root_x # Error-prone point 1
371371
end
372372

373373
def find_root(x)
374-
if x == @father[x]
374+
if x == @fathers[x]
375375
return x
376376
end
377377

378-
@father[x] = find_root(@father[x]) # Error-prone point 2
378+
@fathers[x] = find_root(@fathers[x]) # Error-prone point 2
379379

380-
@father[x]
380+
@fathers[x]
381381
end
382382

383383
def same_root(x, y)

solutions/1001-2000/1971-find-if-path-exists-in-graph.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ Explanation: There is no path from vertex 0 to vertex 5.
4040
Please see [1971. Find if Path Exists in Graph (UnionFind Solution)](1971-find-if-path-exists-in-graph-2.md).
4141

4242
## Intuition
43-
The island problem can be abstracted into a **graph theory** problem. This is an **undirected graph**:
44-
45-
![](../../images/graph_undirected_1.svg)
46-
47-
And this graph may have multiple **connected components**. Initially, we start from `source` vertex which belongs to one of the `connected components`.
43+
This graph may have multiple **connected components**.
4844

4945
![](../../images/graph_undirected_2.png)
5046

47+
Initially, we start from `source` vertex which belongs to one of the `connected components`.
5148
We need to find if there is a path from `source` to `destination`. This question is equivalent to determine if `source` and `destination` vertices belong to the same `connected component`.
5249

5350
### Breadth-First Search

0 commit comments

Comments
 (0)