Floyd's Cycle Detection
a.k.a "tortoise and the hare algorithm"
Last updated
a.k.a "tortoise and the hare algorithm"
Last updated
Use a slow
pointer and fast
pointer to traverse over a graph/linkedlist, where slow
moves by 1 whereas fast
moves by 2 every round. Eventually, if there is a cycle in the graph/linkedlist, fast
and slow
will meet at some point, indicating that a cycle exists.
Think of them as two runners on a track: Since the fast runner runs exactly twice as fast as the slow runner, if there exists a cycle, the two runners will inevitably meet at some moment within the cycle.
Floyd's Cycle Detection can find more information about the cyclic graph:
To find the cycle length, first find the meeting point where fast
and slow
pointer collides. Then the cycle length can be found by running the slow
pointer from that meeting point until it reaches back to itself.
After the first collision/meet point of Floyd's Cycle Detection, we set one of the pointers back to the head of the whole list. Then, running both pointers at the same speed will cause them to meet again. That meeting point is exactly the entry point of the cycle. This image illustrates it well: