Only an ACTIVE task means a cycle, so the search needs three states
Run the depth-first version on the broken pipeline, the one with a cycle.
try: dfs_order(tasks, broken)except ValueError as error: print("ValueError:", error)
Predict
dfs_order runs on the broken pipeline, where compile also needs test. Which task does the error name?
Pick one to see what it prints.
Each task is in one of three states:
NEW: not visited yet.
ACTIVE: its visit has started and not finished. It's on the path you're following right now.
DONE: finished.
Reaching an ACTIVE task means you followed arrows out of it and came back around. That's a cycle. On the broken pipeline:
The ACTIVE tasks, the current path, on the broken pipeline
fetch
visit fetch
fetchcompile
visit compile
fetchcompiletest
visit test
fetchcompiletestdeploy
visit deployit finishes: DONE
fetchcompiletest
test reaches compileACTIVE: a cycle
Reaching a DONE task is fine. It finished earlier, and you've found a second route to it, not a loop. On the clean pipeline, package reaches deploy after test already finished it.
Each task is visited once and each pair looked at once, so it's O(V+E) time, the same as Kahn's. Its memory is the state dict plus the call stack, and the stack can grow as deep as the longest chain.
The editor holds find_cycle, the three-state search with a path list of the ACTIVE tasks. Fill in the one line that returns the cycle.
Pick an answer above to see the rest
Instructions
1
Fill in the line in find_cycle(tasks, needs) that runs when nxt is ACTIVE: return the part of path from nxt to the end.