Topological Sort - All Paths From Source to Target (DAG)
Examine the following buggy iterative DFS code for finding all paths from source to target in a DAG:
def allPathsSourceTarget(graph):
target = len(graph) - 1
res = []
stack = [(0, [0])]
while stack:
node, path = stack.pop()
if node == target:
res.append(path) # Bug here
else:
for nei in graph[node]:
stack.append((nei, path + [nei]))
return resWhat is the bug in this code?