Algorithm Search⌘K

Proofs: Lyndon-based runs computation

These are the human proofs of Lyndon-based runs computation, at full length.

RUNS returns exactly the runs of the string full

This proof shows: correctness, termination.

Notation. s=s[0..n−1]s = s[0..n-1] with n≥1n \ge 1 is a word over a totally ordered alphabet, with 0-based indices. For ℓ∈{0,1}\ell \in \{0, 1\}, <0<_0 is the given order on letters and <1<_1 its reverse. Each order is extended to words lexicographically, with a proper prefix smaller than the longer word. A Lyndon word for <ℓ<_\ell is a nonempty word that is strictly smaller than each of its proper nonempty suffixes. suf(k)=s[k..n−1]\mathrm{suf}(k) = s[k..n-1], with suf(n)=ε\mathrm{suf}(n) = \varepsilon. For a run we use half-open intervals here: a run is [l,r)[l, r) with smallest period pp, r−l≥2pr - l \ge 2p, and no extension with the period pp to the left or right; the algorithm reports it as the triple (l,r−1,p)(l, r - 1, p). Let Tp={t:0≤t<n−p, s[t]=s[t+p]}T_p = \{t : 0 \le t < n - p,\ s[t] = s[t+p]\}, so that s[x..y−1]s[x..y-1] has the period pp exactly when [x,y−p)⊆Tp[x, y - p) \subseteq T_p.

Lemma 1 (the Lyndon array). For 0≤i<n0 \le i < n and a fixed order, let jj be the least index j>ij > i with suf(j)<suf(i)\mathrm{suf}(j) < \mathrm{suf}(i), or j=nj = n if there is none. Then s[i..j−1]s[i..j-1] is the longest prefix of suf(i)\mathrm{suf}(i) that is a Lyndon word, so λ[i]=j−i\lambda[i] = j - i.

Proof. First, w=s[i..j−1]w = s[i..j-1] is a Lyndon word. Let i<k<ji < k < j, and compare x=s[k..j−1]x = s[k..j-1] with ww; ∣x∣<∣w∣|x| < |w|. If xx is a prefix of ww, then suf(k)=x⋅suf(j)\mathrm{suf}(k) = x \cdot \mathrm{suf}(j) and suf(i)=x⋅suf(i+j−k)\mathrm{suf}(i) = x \cdot \mathrm{suf}(i + j - k), and suf(k)>suf(i)\mathrm{suf}(k) > \mathrm{suf}(i) by the choice of jj, so suf(j)>suf(i+j−k)\mathrm{suf}(j) > \mathrm{suf}(i+j-k). But i<i+j−k<ji < i + j - k < j, so suf(i+j−k)>suf(i)>suf(j)\mathrm{suf}(i+j-k) > \mathrm{suf}(i) > \mathrm{suf}(j), a contradiction. So xx and ww differ at some offset d<∣x∣d < |x|; the letters there are those of suf(k)\mathrm{suf}(k) and suf(i)\mathrm{suf}(i) at offset dd, and suf(k)>suf(i)\mathrm{suf}(k) > \mathrm{suf}(i) gives x>wx > w.

Second, no longer prefix w′=s[i..m−1]w' = s[i..m-1] with j<m≤nj < m \le n is a Lyndon word. Its proper nonempty suffix y=s[j..m−1]y = s[j..m-1] is either a prefix of w′w', and then y<w′y < w', or differs from w′w' at an offset d<∣y∣d < |y|, where suf(j)<suf(i)\mathrm{suf}(j) < \mathrm{suf}(i) gives y<w′y < w'. □\square

Lemma 2 (rotations of a primitive word). Let ww be a primitive word of length pp. Its pp rotations are pairwise distinct, and the least of them is a Lyndon word.

Proof. If the rotation by kk, with 0<k<p0 < k < p, equals ww, then w[t]=w[(t+k) mod p]w[t] = w[(t + k) \bmod p] for every tt, hence w[t]=w[(t+mk) mod p]w[t] = w[(t + mk) \bmod p] for every integer mm. The multiples of kk modulo pp are the multiples of d=gcd⁡(k,p)<pd = \gcd(k, p) < p, so w=(w[0..d−1])p/dw = (w[0..d-1])^{p/d}, which is not primitive. So the rotations are distinct, and so are the rotations of any rotation of ww.

Let ww now be the least rotation and vv a proper nonempty suffix, w=uvw = uv. If vv is not a prefix of ww, then vv and ww differ at an offset d<∣v∣d < |v|, and so do vuvu and ww; vuvu is a rotation, so vu>wvu > w, hence v>wv > w. If vv is a proper prefix of ww, write w=vxw = vx with ∣x∣=∣u∣|x| = |u|. The rotation vuvu differs from w=vxw = vx, so vu>vxvu > vx, hence u>xu > x, and the first difference of uu and xx is at an offset below ∣u∣|u|. So the rotation xvxv of w=vxw = vx is smaller than uv=wuv = w, a contradiction. □\square

Lemma 3 (Lyndon words are unbordered and primitive). A Lyndon word ww has no nonempty proper factor that is both a prefix and a suffix, and ww is primitive.

Proof. Such a factor vv is a proper nonempty suffix and a proper prefix, so v<wv < w, which a Lyndon word excludes. If w=zkw = z^k with k≥2k \ge 2, then zk−1z^{k-1} is such a factor. □\square

Lemma 4 (the extension of lines L6 and L7). Let 0≤i<n0 \le i < n, 1≤p1 \le p, j=i+p≤nj = i + p \le n. Let aa be the length of the longest common suffix of s[0..i−1]s[0..i-1] and s[0..j−1]s[0..j-1], and bb the length of the longest common prefix of suf(i)\mathrm{suf}(i) and suf(j)\mathrm{suf}(j). Put l=i−al = i - a and r=j+br = j + b. Then [l,r−p)⊆Tp[l, r - p) \subseteq T_p, l−1∉Tpl - 1 \notin T_p when l>0l > 0, and r−p∉Tpr - p \notin T_p when r<nr < n. So s[l..r−1]s[l..r-1] has the period pp and cannot be extended to the left or right with the period pp.

Proof. By definition, t∈Tpt \in T_p for i−a≤t<ii - a \le t < i and for i≤t<i+bi \le t < i + b, which is [l,r−p)[l, r - p). By maximality of aa, either a=ia = i (so l=0l = 0) or s[i−a−1]≠s[j−a−1]s[i - a - 1] \ne s[j - a - 1], that is l−1∉Tpl - 1 \notin T_p. By maximality of bb, either j+b=nj + b = n or s[i+b]≠s[j+b]s[i + b] \ne s[j + b], that is r−p∉Tpr - p \notin T_p. □\square

Soundness. Suppose line L8 adds (l,r−1,p)(l, r - 1, p), found at ii with p=λℓ[i]p = \lambda_\ell[i], so r−l≥2pr - l \ge 2p. By Lemma 4, s[l..r−1]s[l..r-1] has the period pp and is not extendable with it, and l≤i<i+p≤rl \le i < i + p \le r. Let qq be its smallest period and suppose q<pq < p. Since r−l≥2p≥p+qr - l \ge 2p \ge p + q, the Fine and Wilf theorem makes d=gcd⁡(p,q)<pd = \gcd(p, q) < p a period of s[l..r−1]s[l..r-1]. Then the factor s[i..i+p−1]s[i..i+p-1] has the period dd, which divides its length pp, so it equals zp/dz^{p/d} with p/d≥2p/d \ge 2. But it is a Lyndon word for <ℓ<_\ell by Lemma 1, hence primitive by Lemma 3, a contradiction. So pp is the smallest period, and (l,r−1,p)(l, r - 1, p) is a run.

Completeness. Let [l,r)[l, r) be a run of ss with smallest period pp. Choose ℓ\ell: if r<nr < n, then s[r]≠s[r−p]s[r] \ne s[r - p], and let ℓ\ell be the order with s[r]<ℓs[r−p]s[r] <_\ell s[r - p]; if r=nr = n, let ℓ=0\ell = 0.

A Lyndon root. The word w=s[l..l+p−1]w = s[l..l+p-1] is primitive: if w=zkw = z^k with k≥2k \ge 2, then s[l..r−1]s[l..r-1], which has the period pp, also has the period ∣z∣<p|z| < p, against the choice of pp. Since r−l≥2pr - l \ge 2p, for l≤c<l+pl \le c < l + p the factor s[c..c+p−1]s[c..c+p-1] is the rotation of ww by c−lc - l. By Lemma 2, for the order <ℓ<_\ell, the least of them, at some cc with l≤c<l+pl \le c < l + p, is a Lyndon word. Note c+p≤l+2p−1<rc + p \le l + 2p - 1 < r.

Its Lyndon array entry. We show λℓ[c]=p\lambda_\ell[c] = p. The prefix s[c..c+p−1]s[c..c+p-1] of suf(c)\mathrm{suf}(c) is a Lyndon word. Let m>c+pm > c + p with m≤nm \le n; we show that y=s[c..m−1]y = s[c..m-1] is not a Lyndon word. If m≤rm \le r, then yy has the period p<∣y∣p < |y|, so s[c+p..m−1]s[c+p..m-1] is a nonempty proper factor that is both a prefix and a suffix of yy, which Lemma 3 excludes. If m>rm > r, then r<nr < n, and the proper suffix y′=s[c+p..m−1]y' = s[c+p..m-1] agrees with yy at the offsets t<r−c−pt < r - c - p, since c+t∈Tpc + t \in T_p, while at the offset r−c−p<∣y′∣r - c - p < |y'| it has s[r]s[r] against s[r−p]s[r - p] in yy. As s[r]<ℓs[r−p]s[r] <_\ell s[r-p], y′<yy' < y, and yy is not a Lyndon word.

It is found. So at i=ci = c the algorithm has p=λℓ[c]p = \lambda_\ell[c] and j=c+pj = c + p. Since [l,r−p)⊆Tp[l, r - p) \subseteq T_p and l≤c<r−pl \le c < r - p, while l−1∉Tpl - 1 \notin T_p (or l=0l = 0) and r−p∉Tpr - p \notin T_p (or r=nr = n) by the maximality of the run, the values of Lemma 4 are a=c−la = c - l and b=r−p−cb = r - p - c. So lines L6 and L7 give exactly ll and rr, and r−l≥2pr - l \ge 2p, so line L8 adds (l,r−1,p)(l, r - 1, p).

Termination and conclusion. The two loops have 2⋅n2 \cdot n iterations, each of constant work beyond the precomputed structures. Every triple added is a run (soundness), every run is added at least once (completeness), and RR is a set, so the algorithm returns exactly the set of runs of ss. For n=0n = 0 the loops are empty and there are no runs. ■\blacksquare

Back to Lyndon-based runs computation