Tarjan's cycle detection algorithm implemented in Python