Why does Floyd’s Cycle detection algorithm work?

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 ff that maps a finite set of values SS to itself and given an initial value x0x_0 in SS, the iterated sequence of function values eventually repeats itself, forming a cycle.

The sequence of iterated function values can be represented as:

                  x0, x1=f(x0), x2=f(x1) . . . , xj=f(xj1) , . . .\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space x_0, \space x_1=f(x_0),\space x_2=f(x_1) \space. \space.\space.\space, \space x_j =f(x_{j-1})\space,\space.\space. \space.

The sequence above will eventually lead to the same value appearing twice. In other words, there will exist distinct indices ii and jj such that xi=xjx_i = x_j. Once this cycle is detected, the sequence will continue to repeat the same values periodically, starting from xix_i and ending at xj1x_{j-1}. The goal of cycle detection is to identify these indices ii and jj, where xix_i represents the starting point and xj1x_{j-1} represents the intersection point of the cycle, based on the function ff and the initial value x0x_0.

Let’s look at the following illustration to better understand cycle detection.

canvasAnimation-image
1 of 11

Detection algorithm

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.

Recap

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.

1 of 12

Why does the algorithm always work?

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.

G 2 2 8 8 2->8 1 1 8->1 5 5 1->5 3 3 5->3 6 6 3->6 9 9 6->9 7 7 7->8 9->7
A graphical representation of the array having a cycle

In the diagram above:

  • 77 is the intersection point where the hare and tortoise pointers meet.

  • 88 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:

dhare=2dtortoised _{\text{hare}} = 2d_{\text tortoise} ——— (1)(1)

Here, dd 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:

An array having a cycle
An array having a cycle

In the diagram above:

  • Green represents the starting point of the cycle.

  • Blue represents the intersection point.

  • Yellow represents the start of the array.

  • FF represents the steps taken from the start of the array to the starting point of the cycle.

  • aa represents the steps taken from the starting point to the intersection point.

  • CC 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 FF steps from the start of the array to the starting point of the cycle and then takes aa 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:

dtortoise=F+a d _{\text tortoise} = F + a\space——— (2)\space(2)

In the time it takes the tortoise pointer to travel F+aF+a 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 FF 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 aa 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:

dhare=F+C+a d _{\text hare} = F + C + a\space——— (3)\space(3)

Recall eq. (1):(1):

dhare=2dtortoised _{\text hare} = 2d_{\text tortoise} ——— (1)(1)

If we substitute the equivalent expression of dtortoised_{\text tortoise} given in eq. (2)(2) and the equivalent expression of dhared_{\text hare} given in eq. (3)(3) into eq. (1)(1), we get:

F+C+a=2(F+a)F+C+a = 2(F+a)

Let’s simplify this equation:

F+C+a=2F+2aF+C+a = 2F+2a

C=F+aC = F+a

Therefore, the distance from the start of the array to the intersection point, F+aF+a, equals CC.

We can also re-arrange this equality as follows:

F=CaF=C-a

Let’s recap our diagram again.

Recap of an array having a cycle
Recap of an array having a cycle

As we can see, CaC-a 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 FF is longer than the length of the cycle?

Copyright ©2024 Educative, Inc. All rights reserved