2018-01-13 20:55:56
The algorithm of Floyd judgment Circle (Floyd Cycle Detection algorithm), also known as the Turtle Rabbit Race algorithm (tortoise and Hare algorithm), is a finite state machine can be , an iterative function or a chain list to determine whether a ring exists , and the algorithm for the starting and length of the ring is obtained. The algorithm, according to Gartner, was invented by American scientist Robert Floyd, but the algorithm did not appear in Robert Floyd's published writings.
If a finite state machine, an iterative function, or a ring exists on a linked list, then 2 pointers at different speeds on a ring must meet at some point. At the same time, it is clear that if the 2 pointers from the same starting point (even if the starting point is not on a certain ring) at the same time start at different speeds, then a ring can be judged, and the beginning and the length of the ring where the 2 encounters are found.
First, the algorithm description
If a finite state machine, an iterative function, or a linked list exists, there must be a starting point where a ring can be reached (the starting point can also be on a ring).
In the initial state, assume that a starting node is known as node S. Now set two pointers T and H, pointing them all to S.
Next, let T and H move forward at the same time, but the speed is different:T each step forward 1, H 2 step forward . As long as both can move forward and do not meet, so keep the advance of both.
- When H is unable to advance, that is, to reach a node that has no successor , it is determined that the ring will not be encountered from S .
- Conversely, when T and H meet again , it can be determined that from the S will enter a certain ring , set it as the ring C.
If you determine that a ring exists, you can find the start and length of the ring.
Ring Length: The above algorithm has just judged the existence of the ring C, it is obvious that T and H are in the same node, set it as Node M. Obviously, only H does not move, and T pushes forward, eventually return to the node m, the number of steps to count the T-propulsion, obviously this is the length of the ring C.
ring Entrance: in order to find out the starting point of the ring C, as long as the H is still located in the node m, and T returns to the start node s, the distance between H and T is an integer multiple of the ring C length. Then, let T and H move forward, and keep the same speed: T each step forward 1, H 1 step forward. The process continues until T and H meet again, the meeting is located in the same node p, node p is the first node from the node s to reach the ring C, that is, a starting point of the ring C.
Proof of the Loop entry algorithm:
Assuming that the distance from the slow pointer to the meeting point is L, the fast pointer is 2l long and the ring length is R.
L = x + y;
2l = L + nr;
==> nr = x + y
==> x = nr-y
So at this point, the meeting point of the slow pointer to the start point, the fast pointer travel speed and the slow pointer to maintain the same, then when the slow pointer to walk x, the fast hands go nr-y, just the two at the entrance of the ring meet.
Second, pseudo-code description
1 T: = &s 2 h: = &s //The pointer T and H both point to the start node S. 3 repeat 4 t: = T->next 5 h: = H->next 6 If H is not NULL //To be aware that this judgment cannot generally be omitted 7 h: = H->next 8 until T = h or H = null 9 if h! = null //If there is a ring then ten N: = 0 Repeat //The length of the loop : = t ->next N: = n+1 until t = h : = &s //A starting point for a ring and a while t! = H- t: = t-> Next h: = H->next P: = *t
Third, the complexity of the algorithm
Time complexity: Notice that when the pointer t reaches a start node p of ring C (at this point the pointer h is clearly on the ring C), the pointer T can only walk 1 laps at a maximum. If the node s to p distance is m, and the length of the ring C is N, then the time complexity is O (m+n), which is the algorithm of linear time.
space complexity: only need to create pointer T, pointer h, save the ring length n, a beginning of the ring p. The space complexity is O (1), which is the algorithm of constant space.
Iv. Application
For finite state machines and linked lists, it is possible to determine whether a starting point will be returned to a state and node that has been accessed during the run.
For an iterative function, it is possible to determine its existence period and to find its minimum positive period .
V. Related algorithms
Although the Floyd algorithm has achieved linear time complexity and constant space complexity, the Brent algorithm will reduce the constant coefficient of time complexity, and the average consumption time is less 36%than the Floyd algorithm.
Bruent Algorithm Description:
The bruent algorithm has a moving rabbit and a stationary turtle. Here the turtle in the Rabbit marching steps to reach step_limit, will be transmitted to the rabbit's position, while the rabbit's marching steps to reset to 0, while improving the step_limit twice times the original.
Both the tortoise and the rabbit start at the top of the list. every iteration of the rabbit is one step. if it is the same position as the fixed turtle, it is obviously a loop. If the end of the list is reached, there is no loop.
Why do you want to move the turtle? If a rabbit is stuck in a loop and does not encounter a turtle, it will cycle forever, so the turtle must be transferred to the rabbit position after a certain number of steps.
Why does it take twice times each time?
bruent algorithm:
1 turtle = top 2 rabbit = Top 3 4 Steps_taken = 0 5 Step_limit = 2 6 7 forever:8 If rabbit = End:9 retu RN ' No Loop Found ' rabbit = rabbit.next11 Steps_taken + = 113 if rabbit = = turtle:15 return ' Loop F Ound ' if Steps_taken = = step_limit:18 Steps_taken = 019 step_limit *= //Teleport the TURTLE21 turtle = Rabbit
Vi. extracts of related topics
public boolean ishappy (int n) { int slow = n; int fast = n; Do { slow = calc (slow); Fast = Calc (fast); Fast = Calc (fast); } while (slow! = fast); if (slow! = 1) return false; else return true; } int calc (int n) { int sum = 0; while (n > 0) { int k = n; sum + = K*k; n/=; } return sum; }
Code Explanation: The ring is obviously impossible to exist 1, so if it falls into the ring, then it is impossible to calculate 1, otherwise the two will be equal after the operation to 1.
Floyd ring algorithm Floyd cycle Detection algorithm