algorithmgraph-theoryprimescycle# Brent's cycle detection algorithm

Can anyone please help me out with Brent's cycle detection algorithm . I m not understanding exactly why "search for the smallest power of two 2^i that is larger than both λ and μ" ? How powers of 2 are playing role in the detection of cycle . Is it somehow related to Floyd's cycle detection algorithm ? I m unable to get it from the basics . Please help !

Solution

This method uses increasing steps (1, 2, 4, 8...) to get inside the loop as soon as possible. When P = 2^k becomes larger than both λ and μ, then tortoise (T) is in the loop, and hare (H) makes no more than P steps to meet (standing) tortoise again. It seems that some simulation would be useful. Let's we have list 0-1-2-3-4-5-6-7-3

```
P=1 T=0 H=0; H makes 1 step and doesn't meet T
P=2 T=1 H=1; H makes 2 steps and doesn't meet T
P=4 T=3 H=3; H makes 4 steps and doesn't meet T
P=8 T=7 H=7; H makes 5 steps and meets T !!!!!
```

Notice: With P=4 T is inside the loop, but hare doesn't go through the whole cycle (P >= μ but P < λ )

So we have found μ<8 and λ=5. Then we want to find μ (loop entry point)

```
T=0 H=0; H makes 5 steps; H=5
while T <> H
move both
T=1 H=6
T=2 H=7
T=3 H=3 !!!!!!!
```

We've just found μ=3

- Difference between back tracking and dynamic programming
- How can we optimize this algorithm from O(N ** 2) to better complexity in order to pass performance test?
- How do I predict the required size of a Base32 Decode output?
- Reversing AND Bitwise
- Why does my binary search need an extra comparison? log2(N)+1
- How to build a trie for finding exact phonetic matches, sorted globally by weight, and paginated? (Building off this example)
- What is the Time Complexity and Space Complexity of extending a string according to a rule?
- Skyscraper puzzle algorithm
- Check if all elements in a list are equal
- Bitwise Interval Arithmetic
- fast algorithm for drawing filled circles?
- How to find distance from the latitude and longitude of two locations?
- Determine if two rectangles overlap each other?
- Randomly Splitting a Graph according to Conditions
- Maximize distance while pushing crates
- Free a binary tree without recursion
- How can I estimate number of nodes in given tree structure?
- Explanation of class Definition for Binary Trees in leetcode
- Procedural Generation of 2D Rooms
- Is there an algorithm to find the closest element to X in an unsorted array in Ω(logN)?
- Advanced Java idiom for 2+ classes implementing a generic interface
- Is there any algorithm in c# to singularize - pluralize a word?
- Number of Common sub sequences of two strings
- Trying to solve problem 19 on Euler Project
- is a "non-decreasing" sequence "increasing"?
- Is it possible to get the original value of a number, after several multiplications **with overflow**?
- Algorithm to determine the highest and lowest possible finishing position of a team in a league
- Algorithm to calculate the number of divisors of a given number
- Rolling or sliding window iterator?
- best way to pick a random subset from a collection?