Skip to content

Latest commit

 

History

History

797-AllPathsFromSourcetoTarget

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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.