Floyd’s Cycle Finding Algorithm
Last Updated :
06 Sep, 2024
Floyd's cycle finding algorithm or Hare-Tortoise algorithm is a pointer algorithm that uses only two pointers, moving through the sequence at different speeds. This algorithm is used to find a loop in a linked list. It uses two pointers one moving twice as fast as the other one. The faster one is called the fast pointer and the other one is called the slow pointer.
While traversing the linked list one of these things will occur-
- The Fast pointer may reach the end (NULL) which shows that there is no loop in the linked list.
- The Fast pointer again catches the slow pointer at some time therefore a loop exists in the linked list.
Floyd’s Cycle Finding Algorithm:
The idea is to start with the two pointers slow and fast, both starting at the head of the linked list.
- While traversing the List:
- slow pointer will move one step at a time.
- fast pointer moves two steps at a time.
- If there's a cycle, the fast pointer will eventually catch up with the slow pointer within the cycle because it's moving faster.
- If there's no cycle, the fast pointer will reach the end of the list (i.e., it will become NULL).
- When the slow and fast pointers meet, a cycle or loop exists.
For more details about the working of this algorithm, Please refer to this article Detect loop or cycle in a linked list.
How does Floyd's Algorithm works?
The algorithm is to start two pointers, slow and fast from head of linked list. We move slow one node at a time and fast two nodes at a time. If there is a loop, then they will definitely meet.
This approach works because of the following facts :
- When slow pointer enters the loop, the fast pointer must be inside the loop.
- if we consider movements of slow and fast pointers, we can notice that distance between them (from slow to fast) increase by one after every iteration.
After each iteration where the slow pointer moves one step forward and the fast pointer moves two steps forward, the distance between the two pointers increases. Initially, if the slow pointer is at a distance k from a certain point in the cycle, then after one iteration, the distance between the slow and fast pointers becomes k+1. After two iterations, this distance becomes k+2 and so on. As they continue to move within the cycle, this distance will eventually equal the cycle length n. At this point, since the distance wraps around the cycle and both pointers are moving within the same cycle, they will meet.
For more details about the working & proof of this algorithm, Please refer to this article, How does Floyd's Algorithm works.
Find the Starting Node of a Cycle in a Linked List
To find the starting node of a cycle in a linked list, follow the steps below:
- Using above algorithm we can find the meeting point (if cycle exists) where the slow and fast pointers intersect inside the cycle.
- After detecting the cycle, reset one pointer (slow) to the head of the list. Keep the other pointer (fast) at the meeting point.
- Move both pointers one step at a time. The node where they meet again is the start of the cycle.
For more details about the working of this algorithm, Please refer to this article, Find the Starting Node of a Cycle in a Linked List.
Advantages :
Related articles:
Similar Reads
Brent's Cycle Detection Algorithm Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. We have discussed Floyd's algorithm to detect cycle in linked list. Brent's cycle detection algorithm is similar to floyd's algorithm as it also uses two pointer technique. But there is some
13 min read
Floyd Warshall Algorithm Given a matrix dist[][] of size n x n, where dist[i][j] represents the weight of the edge from node i to node j. If there is no direct edge, dist[i][j] is set to a large value (e.g., 10â¸) to represent infinity. The diagonal entries dist[i][i] are 0, since the distance from a node to itself is zero.
9 min read
CSES Solutions â Cycle Finding Given a directed graph with N nodes and M edges determine if there exists a negative cycle in the graph. If a negative cycle exists print "YES" and output any vertex sequence that forms the cycle; otherwise print "NO". Examples: Input: 4 5 1 2 1 2 4 1 3 1 1 4 1 -3 4 3 -2 Output: YES 1 2 4 1 Approach
11 min read
Karp's minimum mean (or average) weight cycle algorithm Given a directed and strongly connected graph with non-negative edge weights. We define the mean weight of a cycle as the summation of all the edge weights of the cycle divided by the no. of edges. Our task is to find the minimum mean weight among all the directed cycles of the graph.The input is pr
11 min read
Floyd-Warshall Algorithm in Python The Floyd-Warshall algorithm, named after its creators Robert Floyd and Stephen Warshall, is fundamental in computer science and graph theory. It is used to find the shortest paths between all pairs of nodes in a weighted graph. This algorithm is highly efficient and can handle graphs with both posi
3 min read
Fleury's Algorithm for printing Eulerian Path or Circuit Given an undirected connected graph with v nodes, and e edges, with adjacency list adj. The task is to print an Eulerian trail or circuit using Fleury's AlgorithmA graph is said to be Eulerian if it contains an Eulerian Cycle, a cycle that visits every edge exactly once and starts and ends at the sa
12 min read