How Brent’s cycle search resets on powers of two

Short answer: Brent’s cycle algorithm finds a cycle in a sequence x, f(x), f(f(x)), … with constant memory. A hare walks alone. Whenever the distance since the last reset is a power of two, the tortoise jumps to the hare and the search length doubles. When the hare lands on the tortoise, the steps taken since that reset are a multiple of the cycle length, and a second walk separates the tail from the cycle. The schedule usually evaluates f fewer times than Floyd’s fixed two-speed walk.


What the problem actually asks

Floyd’s tortoise and hare is correct and easy to prove, and it also evaluates f about three times per step of the tortoise. Brent asked for a schedule that spends those evaluations more carefully. The hare explores a window whose length is 1, then 2, then 4, and so on. The tortoise stands still during a window and teleports to the hare when the window is exhausted. Inside a window the algorithm stores one distance, not the whole history.

The first time the hare, moving one step at a time from the tortoise’s current post, lands on a state the tortoise holds, both pointers are on the cycle and the distance walked is a multiple of the cycle length. A second phase, the same lockstep idea Floyd uses, turns that multiple into the cycle start.


What the algorithm guarantees

If the sequence from the start eventually repeats, the search stops. The value λ returned by the first phase is the cycle length. The second phase returns μ, the length of the tail before the cycle. Time is O(μ + λ) function evaluations, with a smaller leading constant than Floyd’s algorithm on the usual analysis. Extra memory is a constant number of states and counters.

The sequence still has to be a function: one successor per state. A directed graph with branching is a depth-first search. A terminal state with no successor has to be defined, or the hare cannot move. Brent’s schedule does not change Pollard’s rho by itself. Rho can use either cycle finder around its polynomial walk.


The working steps

  1. Set the tortoise to the start, the hare to f of the start, the current power to 1, and the steps in this window to 1.
  2. While the tortoise and the hare differ, if the window is full then move the tortoise to the hare, double the power, and clear the window count. Then advance the hare once and increment the window count.
  3. When they meet, the window count is the cycle length λ.
  4. Put both pointers at the start. Advance one of them λ steps. Then advance both together until they meet. The number of lockstep steps is the tail length μ, and the meeting state is the cycle entrance.
  5. Store each state you still need. The algorithm keeps only the two pointers, so a caller who wants the whole cycle walks λ steps from the entrance.

Constant memory belongs to a single successor function. Floyd’s algorithm is the same problem with a different schedule. A general graph cycle is a back edge in a depth-first search. Brent’s search is the power-of-two cycle finder.


Approaches that look simpler and fail

Approach Why it fails when the window count is reported as the tail
Treat the first meeting distance as the index of the entrance That distance is the cycle length. The entrance comes from the second walk, which advances one pointer λ steps ahead of the other.
Double the window without moving the tortoise The hare then compares against a stale state that may lie off the cycle. The reset places the tortoise on the hare’s current state before the longer search.
Store every state and call the extra memory constant A set of seen states detects a repeat and uses memory proportional to μ + λ. Brent’s bound is a handful of variables.
Run the same two pointers on a node with several outgoing edges The next state is no longer a function. The search does not choose which edge to follow.

A fixed two-speed walk is Floyd’s algorithm. A factored integer from a polynomial iteration is Pollard’s rho. Brent’s algorithm is the power-of-two cycle search.


A working example

This Python fragment lets the hare walk alone and doubles the window whenever the window fills, moving the tortoise up to the hare at each reset. On the same rho shape as Floyd’s example it prints tail 3 and cycle 3.

succ = [1, 2, 3, 4, 5, 3]
def f(x):
    return succ[x]

power = lam = 1
tortoise, hare = 0, f(0)
while tortoise != hare:
    if power == lam:
        tortoise = hare
        power *= 2
        lam = 0
    hare = f(hare)
    lam += 1
tortoise = hare = 0
for _ in range(lam):
    hare = f(hare)
mu = 0
while tortoise != hare:
    tortoise, hare = f(tortoise), f(hare)
    mu += 1
assert (lam, mu) == (3, 3)
print(mu, lam)

Where it is used

Brent’s cycle finder is the restart used inside practical Pollard rho factoring: the tortoise waits at powers of two while the hare walks. Floyd’s paired walk is the older cycle test. Either one is there to find the loop, not to sort.


When you can drop this algorithm

Drop Brent’s schedule when a shorter proof matters more than the evaluation count. Floyd’s walk is enough. Keep Brent when each evaluation of f is expensive and the sequence is still a function.


Why this stays published

We publish this so a search for Brent’s cycle algorithm returns the power-of-two reset and the second walk that separates the tail from the cycle. Brent’s algorithm finds a cycle length by walking a hare across doubling windows and finds the entrance with a second lockstep walk, using constant memory.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *