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:

One lap: 3 + 4 + 5 + 1 = 13 points, and the lap can repeat
345the lift, 1topashclifffalls

Three things go wrong.

  • No order exists. best[top] now depends on best[falls], which depends on best[top]. Kahn's order comes out short, here empty, since top waits on falls. And best_from_start quietly returns None.
  • The recursion never bottoms out. down("top") asks down("birch"), which works down through cliff to down("falls"). That asks down("top") again before its first call has returned, so nothing is in the cache yet. It raises RecursionError.
  • 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.