Topological Sort - All Paths From Source to Target (DAG)
Consider the following iterative DFS code to find all paths from source (0) to target in a DAG:
from typing import List
def allPathsSourceTarget(graph: List[List[int]]) -> List[List[int]]:
target = len(graph) - 1
res = []
stack = [(0, [0])]
while stack:
node, path = stack.pop()
if node == target:
res.append(path)
else:
for nei in graph[node]:
stack.append((nei, path + [nei]))
return res
# Input graph: [[1,2],[3],[3],[]]
print(allPathsSourceTarget([[1,2],[3],[3],[]]))What is the exact output of this code?