Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 821 Bytes

File metadata and controls

22 lines (16 loc) · 821 Bytes

All Paths From Source to Target

Problem can be found in here!

Solution: Depth-first Search

def allPathsSourceTarget(graph: List[List[int]]) -> List[List[int]]:
    def dfs(node: int, path: List[int]) -> None:
        if node == target:
            paths.append(path)

        for neighbor in graph[node]:
            dfs(neighbor, path+[neighbor])

    paths = []
    target = len(graph)-1
    dfs(0, [0])
    return paths

Time Complexity: O(V+E), Space Complexity: O(V+E), where V is the number of courses and E is the length of prerequisites.