Pregunta de entrevista de Amazon

Detect a loop in the given directed graph.

Respuesta de la entrevista

Anónimo

18 de may de 2012

Assumption: there is only a single transition out of every state. Have two pointers, initialized to the start state. Advance the first one one state at a time and advance the second one two states at a time. If the two pointers point to the same state there is a cycle. Note that this picks up rho situations, where you don't start in a cycle but have to travel a bit to reach one.