Why does Floyd’s Cycle detection algorithm work?

Key takeaways:

  • Cycle detection involves identifying repeated values in iterative sequences that form a loop.
  • Floyd’s Algorithm, also known as the Tortoise and Hare method, detects cycles using two pointers moving at different speeds.
  • The intersection point of the cycle is where the faster-moving hare meets the slower-moving tortoise.
  • The starting point of the cycle is determined by resetting one pointer to the beginning and moving both pointers at the same speed.
  • The algorithm is mathematically proven by showing that the cycle length equals the sum of distances traveled by the pointers.
  • Floyd’s Algorithm is efficient, with a time complexity of 𝑂(𝑛)𝑂(𝑛) and a space complexity of O(1)O(1).
  • It reliably detects cycles, even if the starting distance exceeds the cycle length, due to the periodic nature of traversal.
  • The algorithm is widely applied in linked lists, iterative sequences, and preventing infinite loops.

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

Cycle detection is a crucial concept in computer science for identifying loops in iterative processes, ensuring efficient and error-free operations in data structures and algorithms.

How Floyd’s cycle detection algorithm works

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 Floyd’s cycle detection algorithm is always effective

Now let’s see 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?

Floyd’s Algorithm is highly efficient, with O(n) time and O(1) space complexity, making it ideal for detecting cycles in linked lists, iterative functions, and preventing infinite loops.

Conclusion

Cycle detection is a fundamental problem in computer science, with applications ranging from data structure validation to algorithm optimization. Floyd’s Cycle Detection Algorithm, with its simplicity and efficiency, provides a reliable method to detect and locate cycles using minimal computational resources. By taking the advantage of two pointers moving at different speeds, it ensures accurate identification of cycles and their starting points. This makes it a cornerstone tool in solving problems involving iterative sequences and preventing infinite loops.

Further readings to enhance your algorithmic skills

To explore more algorithms like Floyd’s Cycle Detection and patterns such as Two Pointers and Sliding Window, check out our comprehensive Interview Prep Series. This series dives deep into problem-solving strategies and is available in six popular programming languages: Python, C++, C#, Java, JavaScript, and Go. Perfect for honing your skills and excelling in coding interviews!

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the intuition behind Floyd cycle detection?

The intuition is that two pointers moving at different speeds in a sequence will meet if a cycle exists. Once they meet, resetting one pointer to the start and moving both at the same speed helps locate the cycle’s starting point.


What is the turtle hare algorithm?

The turtle hare algorithm, also called Floyd’s Cycle Detection Algorithm, uses two pointers: the tortoise moves one step at a time, and the hare moves two steps. If a cycle exists, the hare eventually catches up with the tortoise.


What is Floyd's tortoise and hare algorithm on an array?

Applied to an array, the algorithm identifies cycles in sequences generated by iterating through indices or values. It detects where the values start repeating and locates the cycle’s starting index.


What is the difference between Floyd and Warshall algorithm?

Floyd’s Algorithm: Detects cycles in sequences or linked data structures like arrays or linked lists.

Warshall’s Algorithm: Solves the all-pairs shortest path problem in weighted graphs, focusing on finding the shortest path between every pair of vertices.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved