16/32
Part 4 of 7 · Depth firstLearn and do

On a long chain the recursion can pass Python's default limit of 1,000 frames

The depth-first version is recursive, and on a long chain that matters.

The call stack at its deepest, on a chain of 999 tasks
t0t1t2…t998
listed t0 first999 calls deep
t998
listed t998 first1 call deep

Listed first task first, each visit calls the next before it finishes. Listed last task first, each visit finds the next task DONE already.

That depth is the catch in Python: the default recursion limit is 1,000 frames, as in the grid lesson.

From a script, a chain of 998 tasks worked, and 999 raised RecursionError: maximum recursion depth exceeded. The other two frames went to the script itself and the call to dfs_order. Listed last task first, the same chain went through.

You don't choose how inputs arrive, so plan for the deep case. sys.setrecursionlimit(200_000) let a chain of 100,000 tasks through, but the docs warn that a limit set too high can lead to a crash. Kahn's keeps its pending work in a deque, and has no depth at all.

The editor holds deepest_call and a chain of 5 tasks. Finish its visit so it records how many calls are open at the deepest point.