In computer science, cycle detection or cycle finding is the algorithmic problem of finding a cycle in a sequence of iterated function values.

For any function that maps a finite set to itself, and any initial value in , the sequence of iterated function values

:<math> x_0,\ x_1=f(x_0),\ x_2=f(x_1),\ \dots,\ x_i=f(x_{i-1}),\ \dots</math>

must eventually use the same value twice: there must be some pair of distinct indices and such that . Once this happens, the sequence must continue periodically, by repeating the same sequence of values from to . Cycle detection is the problem of finding and , given and .

Several algorithms are known for finding cycles quickly and with little memory. Robert W. Floyd's tortoise and hare algorithm moves two pointers at different speeds through the sequence of values until they both point to equal values. Alternatively, Brent's algorithm is based on the idea of exponential search. Both Floyd's and Brent's algorithms use only a constant number of memory cells, and take a number of function evaluations that is proportional to the distance from the start of the sequence to the first repetition. Several other algorithms trade off larger amounts of memory for fewer function evaluations.

The applications of cycle detection include testing the quality of pseudorandom number generators and cryptographic hash functions, computational number theory algorithms, detection of infinite loops in computer programs and periodic configurations in cellular automata, automated shape analysis of linked list data structures, and detection of deadlocks for transactions management in DBMS.

Example

thumb|upright=1.5|This function defines the cycles {4} and {1, 6, 3}.

The figure shows a function that maps the set to itself. If one starts from and repeatedly applies , one sees the sequence of values

:

The cycle in this value sequence is .

Definitions

Let be any finite set, be any endofunction from to itself, and be any element of . For any , let . Let be the smallest index such that the value reappears infinitely often within the sequence of values , and let (the loop length) be the smallest positive integer such that . The cycle detection problem is the task of finding and .

One can view the same problem graph-theoretically, by constructing a functional graph (that is, a directed graph in which each vertex has a single outgoing edge) the vertices of which are the elements of and the edges of which map an element to the corresponding function value, as shown in the figure. The set of vertices reachable from starting vertex form a subgraph with a shape resembling the Greek letter rho (): a path of length from to a cycle of vertices.

Practical cycle-detection algorithms do not find and exactly. They usually find lower and upper bounds for the start of the cycle, and a more detailed search of the range must be performed if the exact value of is needed. Also, most algorithms do not guarantee to find directly, but may find some multiple . (Continuing the search for an additional steps, where is the smallest prime divisor of , will either find the true or prove that .)

Computer representation

Except in toy examples like the above, will not be specified as a table of values. Such a table implies space complexity, and if that is permissible, building a predecessor array (associative array mapping to ) while iterating f will detect the first repeated value when it is visited the second time, at which point the value in the predecessor array is and the current index is +. Rather, a cycle detection algorithm is given a black box for generating the sequence , and the task is to find and using very little memory.

The black box might consist of an implementation of the recurrence function , but it might also store additional internal state to make the computation more efficient. Although must be true in principle, this might be expensive to compute directly; the function could be defined in terms of the discrete logarithm of or some other difficult-to-compute property which can only be practically computed in terms of additional information. In such cases, the number of black boxes required becomes a figure of merit distinguishing the algorithms.

A second reason to use one of these algorithms is that they are pointer algorithms which do no operations on elements of other than testing for equality. An associative array implementation requires computing a hash function on the elements of , or ordering them. But cycle detection can be applied in cases where neither of these are possible.

The classic example is Pollard's rho algorithm for integer factorization, which searches for a factor of a given number by looking for values and which are equal modulo without knowing in advance.<!--In fact, it searches modulo all possible factors simultaneously, a detail not stated explicitly to simplify the example.--> This is done by computing the greatest common divisor of the difference with a known multiple of , namely . If the gcd is non-trivial (neither 1 nor ), then the value is a proper factor of , as desired. However, the algorithm does not appear in Floyd's published work, and this may be a misattribution: Floyd describes algorithms for listing all simple cycles in a directed graph in a 1967 paper, but this paper does not describe the cycle-finding problem in functional graphs that is the subject of this article. In fact, Knuth's statement (in 1969), attributing it to Floyd, without citation, is the first known appearance in print, and it thus may be a folk theorem, not attributable to a single individual.

The key insight in the algorithm is as follows. If there is a cycle, then, for any integers and , , where is the length of the loop to be found, is the index of the first element of the cycle, and is a whole integer representing the number of loops. Based on this, it can then be shown that for some if and only if (if in the cycle, then there exists some such that , which implies that ; and if there are some and such that , then and ). Thus, the algorithm only needs to check for repeated values of this special form, one twice as far from the start of the sequence as the other, to find a period of a repetition that is a multiple of . Once is found, the algorithm retraces the sequence from its start to find the first repeated value in the sequence, using the fact that divides and therefore that . Finally, once the value of is known it is trivial to find the length of the shortest repeating cycle, by searching for the first position for which .

The algorithm thus maintains two pointers into the given sequence, one (the tortoise) at , and the other (the hare) at . At each step of the algorithm, it increases by one, moving the tortoise one step forward and the hare two steps forward in the sequence, and then compares the sequence values at these two pointers. The smallest value of for which the tortoise and hare point to equal values is the desired value .

The following Python code shows how this idea may be implemented as an algorithm.

<syntaxhighlight lang="python">

def floyd(f, x0) -> (int, int):

"""Floyd's cycle detection algorithm."""

  1. Main phase of algorithm: finding a repetition x_i = x_2i.
  2. The hare moves twice as quickly as the tortoise and
  3. the distance between them increases by 1 at each step.
  4. Eventually they will both be inside the cycle and then,
  5. at some point, the distance between them will be
  6. divisible by the period λ.

tortoise = f(x0) # f(x0) is the element/node next to x0.

hare = f(f(x0))

while tortoise != hare:

tortoise = f(tortoise)

hare = f(f(hare))

  1. At this point the tortoise position, ν, which is also equal
  2. to the distance between hare and tortoise, is divisible by
  3. the period λ. So hare moving in cycle one step at a time,
  4. and tortoise (reset to x0) moving towards the cycle, will
  5. intersect at the beginning of the cycle. Because the
  6. distance between them is constant at 2ν, a multiple of λ,
  7. they will agree as soon as the tortoise reaches index μ.
  1. Find the position μ of first repetition.

mu = 0

tortoise = x0

while tortoise != hare:

tortoise = f(tortoise)

hare = f(hare) # Hare and tortoise move at same speed

mu += 1

  1. Find the length of the shortest cycle starting from x_μ
  2. The hare moves one step at a time while tortoise is still.
  3. lam is incremented until λ is found.

lam = 1

hare = f(tortoise)

while tortoise != hare:

hare = f(hare)

lam += 1

return lam, mu

</syntaxhighlight>

This code only accesses the sequence by storing and copying pointers, function evaluations, and equality tests; therefore, it qualifies as a pointer algorithm. The algorithm uses operations of these types, and storage space.

Brent's algorithm

Richard P. Brent described an alternative cycle detection algorithm that, like the tortoise and hare algorithm, requires only two pointers into the sequence. However, it is based on a different principle: searching for the smallest power of two that is larger than both and . For , the algorithm compares with each subsequent sequence value up to the next power of two, stopping when it finds a match. It has two advantages compared to the tortoise and hare algorithm: it finds the correct length of the cycle directly, rather than needing to search for it in a subsequent stage, and its steps involve only one evaluation of the function rather than three.

The following Python code shows how this technique works in more detail.

<syntaxhighlight lang="python">

def brent(f, x0) -> (int, int):

"""Brent's cycle detection algorithm."""

  1. main phase: search successive powers of two

power = lam = 1

tortoise = x0

hare = f(x0) # f(x0) is the element/node next to x0.

  1. this assumes there is a cycle; otherwise this loop won't terminate

while tortoise != hare:

if power == lam: # time to start a new power of two?

tortoise = hare

power *= 2

lam = 0

hare = f(hare)

lam += 1

  1. Find the position of the first repetition of length λ

tortoise = hare = x0

for i in range(lam):

hare = f(hare)

  1. The distance between the hare and tortoise is now λ.
  1. Next, the hare and tortoise move at same speed until they agree

mu = 0

while tortoise != hare:

tortoise = f(tortoise)

hare = f(hare)

mu += 1

return lam, mu

</syntaxhighlight>

Like the tortoise and hare algorithm, this is a pointer algorithm that uses tests and function evaluations and storage space. It is not difficult to show that the number of function evaluations can never be higher than for Floyd's algorithm. Brent claims that, on average, his cycle finding algorithm runs around 36% more quickly than Floyd's and that it speeds up the Pollard rho algorithm by around 24%. He also performs an average case analysis for a randomized version of the algorithm in which the sequence of indices traced by the slower of the two pointers is not the powers of two themselves, but rather a randomized multiple of the powers of two. Although his main intended application was in integer factorization algorithms, Brent also discusses applications in testing pseudorandom number generators. finds the period <math>\lambda</math>, and the lower and upper bound of the starting point, <math>\mu_l</math> and <math>\mu_u</math>, of the first cycle. The difference between the lower and upper bound is of the same order as the period, i.e. <math>\mu_l + \lambda \approx \mu_h</math>.

The algorithm maintains an array of tortoises <math>T_j</math>. For each <math>x_i</math>:

  • For each <math>0 \le j \le \log_2 i,</math> compare <math>x_i</math> to <math>T_j</math>.
  • If <math>x_i = T_j</math>, a cycle has been detected, of length <math>\lambda = (i - 2^j) \bmod 2^{j+1} + 1.</math>
  • If no match is found, set <math>T_k \leftarrow x_i</math>, where <math>k</math> is the number of trailing zeros in the binary representation of <math>i+1</math>. I.e. the greatest power of 2 which divides <math>i+1</math>.

If it is inconvenient to vary the number of comparisons as <math>i</math> increases, you may initialize all of the <math>T_j = x_0</math>, but must then return <math>\lambda = i</math> if <math>x_i = T_j</math> while <math>i < 2^j</math>.

Advantages

The main features of Gosper's algorithm are that it is economical in space, very economical in evaluations of the generator function, and always finds the exact cycle length (never a multiple). The cost is a large number of equality comparisons. It could be roughly described as a concurrent version of Brent's algorithm. While Brent's algorithm uses a single tortoise, repositioned every time the hare passes a power of two, Gosper's algorithm uses several tortoises (several previous values are saved), which are roughly exponentially spaced. According to the note in HAKMEM item 132, we survey these techniques briefly.

  • Brent
  • Sedgewick, Szymanski, and Yao provide a method that uses memory cells and requires in the worst case only <math>(\lambda+\mu)(1+cM^{-1/2})</math> function evaluations, for some constant , which they show to be optimal. The technique involves maintaining a numerical parameter , storing in a table only those positions in the sequence that are multiples of , and clearing the table and doubling whenever too many values have been stored.
  • Several authors have described distinguished point methods that store function values in a table based on a criterion involving the values, rather than (as in the method of Sedgewick et al.) based on their positions. For instance, values equal to zero modulo some value might be stored. More simply, Nivasch

Applications

Cycle detection has been used in many applications.

  • Determining the cycle length of a pseudorandom number generator is one measure of its strength. This is the application cited by Knuth in describing Floyd's method. and his related kangaroo algorithm for the discrete logarithm problem.
  • In cryptographic applications, the ability to find two distinct values x<sub>μ&minus;1</sub> and x<sub>λ+μ&minus;1</sub> mapped by some cryptographic function ƒ to the same value x<sub>μ</sub> may indicate a weakness in ƒ. For instance, Quisquater and Delescaille also use cycle detection algorithms to attack DES. The technique may also be used to find a collision in a cryptographic hash function.
  • Cycle detection may be helpful as a way of discovering infinite loops in certain types of computer programs.
  • Periodic configurations in cellular automaton simulations may be found by applying cycle detection algorithms to the sequence of automaton states. In Common Lisp, the S-expression printer, under control of the <code>*print-circle*</code> variable, detects circular list structure and prints it compactly.
  • Teske