21/40
Part 4 of 7 · A park with no cyclesLearn
A cycle breaks the order, the recursion, and sometimes the idea of a best route
Add a lift that carries riders from falls back to top, ("falls", "top", 1), and the park has a cycle:
Three things go wrong.
- No order exists.
best[top]now depends onbest[falls], which depends onbest[top]. Kahn's order comes out short, here empty, sincetopwaits onfalls. Andbest_from_startquietly returnsNone. - The recursion never bottoms out.
down("top")asksdown("birch"), which works down throughclifftodown("falls"). That asksdown("top")again before its first call has returned, so nothing is in the cache yet. It raisesRecursionError. - There may be no best at all. The loop adds 13 points every lap, so a route that may repeat junctions has no highest score. Forbid repeats and you get the longest simple path, which in general graphs is NP-complete. The booksite's chapter on intractability lists it among its sample of NP-complete problems.