Skip to content

Commit fc51fb8

Browse files
committed
1971-find-if-path-exists-in-graph.md Added depth-first search in Python.
1 parent dea9b51 commit fc51fb8

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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-
This graph may have multiple **connected components**.
43+
This graph may have multiple **connected components**.
4444

4545
![](../../images/graph_undirected_2.png)
4646

@@ -68,6 +68,7 @@ We need to find if there is a path from `source` to `destination`. This question
6868
* Space: `O(n)`.
6969

7070
## Python
71+
### Breadth-first search
7172
```python
7273
class Solution:
7374
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
@@ -93,6 +94,44 @@ class Solution:
9394
return False
9495
```
9596

97+
### Depth-first search
98+
```python
99+
class Solution:
100+
def __init__(self):
101+
self.visited = set()
102+
self.found = False
103+
104+
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
105+
if source == destination:
106+
return True
107+
108+
self.destination = destination
109+
self.vertex_to_vertices = defaultdict(list)
110+
111+
for vertex1, vertex2 in edges:
112+
self.vertex_to_vertices[vertex1].append(vertex2)
113+
self.vertex_to_vertices[vertex2].append(vertex1)
114+
115+
self.depth_first_search(source)
116+
117+
return self.found
118+
119+
def depth_first_search(self, vertex_):
120+
if self.found:
121+
return
122+
123+
for vertex in self.vertex_to_vertices[vertex_]:
124+
if vertex == self.destination:
125+
self.found = True
126+
return
127+
128+
if vertex in self.visited:
129+
continue
130+
131+
self.visited.add(vertex)
132+
self.depth_first_search(vertex)
133+
```
134+
96135
## Java
97136
```java
98137
// Welcome to create a PR to complete the code of this language, thanks!

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

+39
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ We need to find if there is a path from `source` to `destination`. This question
6868
* Space: `O(n)`.
6969

7070
## Python
71+
### Breadth-first search
7172
```python
7273
class Solution:
7374
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
@@ -93,6 +94,44 @@ class Solution:
9394
return False
9495
```
9596

97+
### Depth-first search
98+
```python
99+
class Solution:
100+
def __init__(self):
101+
self.visited = set()
102+
self.found = False
103+
104+
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
105+
if source == destination:
106+
return True
107+
108+
self.destination = destination
109+
self.vertex_to_vertices = defaultdict(list)
110+
111+
for vertex1, vertex2 in edges:
112+
self.vertex_to_vertices[vertex1].append(vertex2)
113+
self.vertex_to_vertices[vertex2].append(vertex1)
114+
115+
self.depth_first_search(source)
116+
117+
return self.found
118+
119+
def depth_first_search(self, vertex_):
120+
if self.found:
121+
return
122+
123+
for vertex in self.vertex_to_vertices[vertex_]:
124+
if vertex == self.destination:
125+
self.found = True
126+
return
127+
128+
if vertex in self.visited:
129+
continue
130+
131+
self.visited.add(vertex)
132+
self.depth_first_search(vertex)
133+
```
134+
96135
## Java
97136
```java
98137
// Welcome to create a PR to complete the code of this language, thanks!

0 commit comments

Comments
 (0)