Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def get_all_paths_from_src(self, graph, src_node):
- # Initialise list of paths to return from src_node to tgt_node
- final_paths = []
- # Retrieve list of adjacent nodes
- adj_nodes = graph[src_node]
- # For each adjacent node an leading out of source node sn
- for adj_node in adj_nodes:
- if adj_node == len(graph) - 1:
- final_paths.append([src_node, adj_node])
- else:
- # Generate all paths to end node en from an (recursive), return list of paths
- downstream_paths = self.get_all_paths_from_src(graph, adj_node)
- # Add those paths to my final list of paths
- for downstream_path in downstream_paths:
- final_paths.append([src_node] + downstream_path)
- # Return my final list of paths
- return final_paths
- def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
- return self.get_all_paths_from_src(graph, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement