-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
49 lines (40 loc) · 1.24 KB
/
sol.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import Counter, List
class Solution:
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
if n == 1:
return True
paths = self.getPathsMap(edges)
if source not in paths:
return False
visited = {}
queue = [source]
while queue:
cur = queue.pop(0)
if cur in visited:
continue
visited[cur] = True
if cur == destination:
return True
if cur not in paths:
continue
for path in paths[cur]:
queue.append(path)
return False
def getPathsMap(self, edges: List[List[int]]) -> dict:
paths = {}
for edge in edges:
fromnode, tonode = edge
if fromnode not in paths:
paths[fromnode] = {}
paths[fromnode][tonode] = True
if tonode not in paths:
paths[tonode] = {}
paths[tonode][fromnode] = True
return paths
n = 6
edges = [[0,1],[0,2],[3,5],[5,4],[4,3]]
source = 0
destination = 5
s = Solution()
res = s.validPath(n, edges, source, destination)
print(res)