Algorithm Search⌘K

Proofs: Brent's cycle detection algorithm

These are the human proofs of Brent's cycle detection algorithm, at full length.

Correctness of Brent's cycle detection algorithm full

This proof shows: correctness.

Let x0x_0 be the start value, xi+1=f(xi)x_{i+1} = f(x_i), and let μ≥0\mu \ge 0 and λ≥1\lambda \ge 1 be the tail length and the cycle length. We use one fact about eventually periodic sequences.

Fact. For 0≤i<j0 \le i < j, xi=xjx_i = x_j exactly when i≥μi \ge \mu and λ\lambda divides j−ij - i.

Proof of the fact. By the definition of μ\mu and λ\lambda, xμ+λ=xμx_{\mu + \lambda} = x_\mu, so xi+λ=xix_{i + \lambda} = x_i for all i≥μi \ge \mu by applying ff repeatedly, and then xi=xjx_i = x_j when i≥μi \ge \mu and λ∣j−i\lambda \mid j - i. Conversely, let xi=xjx_i = x_j with i<ji < j. Then xix_i appears again, so i≥μi \ge \mu by the minimality of μ\mu. Write j−i=qλ+rj - i = q\lambda + r with 0≤r<λ0 \le r < \lambda. Since i≥μi \ge \mu, xi=xi+qλx_i = x_{i + q\lambda}, so xi+qλ=xi+qλ+rx_{i + q\lambda} = x_{i + q\lambda + r}. Suppose r>0r > 0. Let s≥0s \ge 0 be such that m=i+qλ+sm = i + q\lambda + s satisfies m≡μ(modλ)m \equiv \mu \pmod{\lambda}. Applying ff ss times gives xm=xm+rx_m = x_{m + r}. Since m≥μm \ge \mu and λ\lambda divides m−μm - \mu, xm=xμx_m = x_\mu and xm+r=xμ+rx_{m + r} = x_{\mu + r}. So xμ=xμ+rx_\mu = x_{\mu + r} with 0<r<λ0 < r < \lambda, which contradicts the minimality of λ\lambda. So r=0r = 0. □\square

The first part (lines L1 to L6). Number the rounds k=0,1,2,…k = 0, 1, 2, \ldots, where round kk is the time during which power=2k\text{power} = 2^k. We claim that during round kk, at each test of line L3, t=x2k−1t = x_{2^k - 1}, h=x2k−1+lamh = x_{2^k - 1 + \text{lam}} with 1≤lam≤2k1 \le \text{lam} \le 2^k, where lam is the counter of the pseudocode.

At the first test, after lines L1 and L2, k=0k = 0, t=x0=x20−1t = x_0 = x_{2^0 - 1}, h=x1h = x_1 and lam=1\text{lam} = 1, so the claim holds. Suppose that it holds at a test in round kk and the test finds t≠ht \ne h. If lam<2k\text{lam} < 2^k, line L6 moves hh one step and adds 1 to lam, so the claim holds in round kk with lam+1≤2k\text{lam} + 1 \le 2^k. If lam=2k\text{lam} = 2^k, then h=x2k+1−1h = x_{2^{k+1} - 1}; line L5 sets t←h=x2k+1−1t \leftarrow h = x_{2^{k+1} - 1}, power←2k+1\text{power} \leftarrow 2^{k+1} and lam←0\text{lam} \leftarrow 0, and line L6 sets h=x2k+1h = x_{2^{k+1}} and lam=1\text{lam} = 1. The claim holds in round k+1k + 1.

By the fact, the test of line L3 in round kk finds t=ht = h exactly when 2k−1≥μ2^k - 1 \ge \mu and λ\lambda divides lam. Let KK be the least kk with 2k−1≥μ2^k - 1 \ge \mu and 2k≥λ2^k \ge \lambda. In a round k<Kk < K, either 2k−1<μ2^k - 1 < \mu, and no test succeeds, or 2k<λ2^k < \lambda, and no lam in 1,…,2k1, \ldots, 2^k is a multiple of λ\lambda, so again no test succeeds. Each such round runs to lam=2k\text{lam} = 2^k and passes to round k+1k + 1. In round KK, lam takes the values 1,2,…1, 2, \ldots and the first multiple of λ\lambda is λ\lambda itself, which is at most 2K2^K. So the loop stops in round KK with lam=λ\text{lam} = \lambda. In particular the loop terminates.

The second part (lines L7 to L10). After lines L7 and L8, t=x0t = x_0 and h=xλh = x_\lambda. Each pass of line L10 moves both one step and adds 1 to μ\mu's counter, so before the test with counter value mm, t=xmt = x_m and h=xm+λh = x_{m + \lambda}. By the fact, xm=xm+λx_m = x_{m + \lambda} exactly when m≥μm \ge \mu. So the loop stops at the first mm with m≥μm \ge \mu, which is m=μm = \mu. Line L11 returns (λ,μ)(\lambda, \mu).

Evaluation count. Lines L2 and L6 evaluate ff once each time. At the end of the first part h=x2K−1+λh = x_{2^K - 1 + \lambda}, and hh has moved one step per evaluation from x0x_0, so the first part uses 2K−1+λ2^K - 1 + \lambda evaluations. By the minimality of KK, either K=0K = 0 or 2K−1<max⁡(μ+1,λ)2^{K-1} < \max(\mu + 1, \lambda), so 2K<2max⁡(μ+1,λ)2^K < 2\max(\mu + 1, \lambda) in both cases, since max⁡(μ+1,λ)≥1\max(\mu + 1, \lambda) \ge 1. The second part uses λ\lambda evaluations in line L8 and 2μ2\mu in line L10. The total is less than 2max⁡(μ+1,λ)+2λ+2μ2\max(\mu + 1, \lambda) + 2\lambda + 2\mu. ■\blacksquare

Back to Brent's cycle detection algorithm