-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_paths_from_source_to_target.py
57 lines (48 loc) · 1.65 KB
/
all_paths_from_source_to_target.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
50
51
52
53
54
55
56
57
'''
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.
The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).
'''
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = []
partialResult = []
def findPath(node):
if node == len(graph)-1:
result.append(partialResult.copy())
return
for i in graph[node]:
partialResult.append(i)
findPath(i)
partialResult.pop()
partialResult.append(0)
findPath(0)
return result
-----------------------------------------------------------------------------------
#dfs
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = []
stack = [(0, [0])]
target = len(graph) - 1
while stack:
cur,route = stack.pop()
if cur == target:
result.append(route)
else:
for node in graph[cur]:
stack.append((node, route + [node]))
return result
----------------------------------------------------------
#bfs
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = []
dq = deque([(0, [0])])
target = len(graph) - 1
while dq:
cur,route = dq.popleft()
if cur == target:
result.append(route)
else:
for node in graph[cur]:
dq.append((node, route + [node]))
return result