-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
35 lines (24 loc) · 945 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import defaultdict
from typing import List
class Solution:
def validPath(self, n: int, edges: List[List[int]], start: int, finish: int) -> bool:
graph = self.get_adjacency_list(edges)
visited = [None] * n
self.dfs(graph, start, visited)
return visited[finish] == 1
def dfs(self, graph, vertex, visited):
visited[vertex] = 1
neighbors = graph[vertex]
for neighbor in neighbors:
if not visited[neighbor]:
self.dfs(graph, neighbor, visited)
def get_adjacency_list(self, edges):
graph = defaultdict(list)
for edge_1, edge_2 in edges:
graph[edge_1].append(edge_2)
graph[edge_2].append(edge_1)
return graph
if __name__ == "__main__":
s = Solution()
# print(s.get_adjacency_list([[0, 1], [1, 2], [2, 0]]))
print(s.validPath(3, [[0, 1], [1, 2], [2, 0]], 0, 2)) # True