In computer science, cycle detection, or cycle finding, is a fundamental problem that revolves around finding cycles in a sequence of iterated function values. When working with a function that maps a finite set of values to itself and given an initial value in , the iterated sequence of function values eventually repeats itself, forming a cycle.
The sequence of iterated function values can be represented as:
The sequence above will eventually lead to the same value appearing twice. In other words, there will exist distinct indices and such that . Once this cycle is detected, the sequence will continue to repeat the same values periodically, starting from and ending at . The goal of cycle detection is to identify these indices and , where represents the starting point and represents the intersection point of the cycle, based on the function and the initial value .
Let’s look at the following illustration to better understand cycle detection.
Floyd’s Cycle algorithm is one of the algorithms used to detect whether a linked list or array representation of the data has a cycle in it and what is the starting point of that cycle. It is also known as the tortoise and hare algorithm.
In this algorithm, the two pointers, tortoise and hare, traverses the data structure. The tortoise pointer moves one step at a time, while the hare pointer moves twice as fast as the tortoise pointer. Due to moving twice as fast as the tortoise pointer, the hare pointer catches up to the tortoise pointer at a certain point if a cycle exists. This point is known as the intersection point of the cycle.
Note: The point from where we enter the cycle is known as the starting point of the cycle. It can be found by traversing the array one more time. Now, move the hare pointer from the intersection point and the tortoise pointer from the start of the data structure but at the same speed.
Let’s look at a visual representation of detecting a cycle, finding the intersection point, and the starting point of the cycle in an array.
Now, let’s delve into and try to understand why Floyd’s cycle detection algorithm is always able to detect the cycle and find its starting point.
Let’s look at the example again, having a scenario where a cycle is present in the array.
In the diagram above:
is the intersection point where the hare and tortoise pointers meet.
is the starting point of the cycle.
The hare pointer is traversing two times faster than the tortoise pointer. This can be represented by the following equation:
———
Here, represents the number of elements traversed.
Let’s look at the following diagram to see the steps taken by the tortoise and hare pointers from the start of the array to the intersection point:
In the diagram above:
Green represents the starting point of the cycle.
Blue represents the intersection point.
Yellow represents the start of the array.
represents the steps taken from the start of the array to the starting point of the cycle.
represents the steps taken from the starting point to the intersection point.
represents the cycle length, in terms of the number of steps taken to go once around the cycle.
With this setup in mind, let’s see the distance traveled by the tortoise and hare pointers.
The tortoise pointer travels steps from the start of the array to the starting point of the cycle and then takes steps from the starting point to the intersection point of the cycle, that is, the point where both pointers meet. So, we can express the distance traveled by the tortoise pointer in the form of this equation:
———
In the time it takes the tortoise pointer to travel steps, the hare pointer, since it’s traveling twice as fast as the tortoise pointer, will have also traveled around the cycle at least once. So, we can say the hare pointer, first, travels steps from the start of the array to the starting point of the cycle, then travels at least a cycle, and at the end travels steps from the starting point to the intersection point of the cycle. Now, we can express the distance traveled by the hare pointer as the following equation:
———
Recall eq.
———
If we substitute the equivalent expression of given in eq. and the equivalent expression of given in eq. into eq. , we get:
Let’s simplify this equation:
Therefore, the distance from the start of the array to the intersection point, , equals .
We can also re-arrange this equality as follows:
Let’s recap our diagram again.
As we can see, is, in fact, the distance from the intersection point back to the starting point of the cycle. This illustrates why, when we move hare pointer forward, starting at the intersection point, and the tortoise pointer from the start of the array, the point where they meet again is the starting point of the cycle.
That is why Floyd’s Cycle detection is always able to locate the starting point of the cycle.
Time to ponder: What if is longer than the length of the cycle?