Here is this graph algorithm that finds a path between two nodes in a DAG.
from typing import *
def find_path(graph: List[List[int]], start: int, end: int) -> List[int]:
path = []
def dfs(node):
path.append(node)
if node == end: return True
for neighbor in graph[node]:
if dfs(neighbor): return True
path.pop()
return False
dfs(start)
return path
I was wondering if this code could be turned into an iterative DFS.
Here is a sample input:
graph = [[1,2],[3],[3],[]]
start = 0
end = 3
find_path(graph, start, end)