Algorithm Search⌘K

Proofs: Duval's algorithm

These are the human proofs of Duval's algorithm, at full length.

DUVAL returns the Lyndon factorization full

This proof shows: correctness, termination.

Notation. s=s[0..n−1]s = s[0..n-1] is a word over a totally ordered alphabet, with 0-based indices, and words are compared in lexicographic order, where a proper prefix is smaller. A Lyndon word is a nonempty word that is smaller than each of its proper nonempty suffixes. For a nonempty word uu, uω=uuu⋯u^\omega = uuu\cdots is the infinite power. We use two facts proved on the Chen-Fox-Lyndon theorem page: the Lyndon factorization exists and is unique, and a proper nonempty suffix of a Lyndon word uu is larger than uu and is not a prefix of uu.

Lemma 1. Let uu be a Lyndon word of length pp. For each q≥0q \ge 0, uω[q..]≥uωu^\omega[q..] \ge u^\omega, comparing the infinite words letter by letter.

Proof. Let r=q mod pr = q \bmod p. Then uω[q..]=ρωu^\omega[q..] = \rho^\omega with ρ=u[r..p−1] u[0..r−1]\rho = u[r..p-1]\,u[0..r-1], a rotation of uu. If r=0r = 0 the two words are equal. Otherwise u<ρu < \rho, because a Lyndon word is smaller than its proper rotations, and since ∣u∣=∣ρ∣|u| = |\rho| they differ inside the first pp letters, with the smaller letter in uu. So uω<ρωu^\omega < \rho^\omega. □\square

Lemma 2 (extension). Let uu be a Lyndon word of length pp, m≥1m \ge 1, u′u' a proper prefix of uu, a=u[∣u′∣]a = u[|u'|], and c>ac > a a letter. Then w=umu′cw = u^m u' c is a Lyndon word.

Proof. Let N=∣w∣=mp+∣u′∣+1N = |w| = mp + |u'| + 1 and x=umu′a=uω[0..N−1]x = u^m u' a = u^\omega[0..N-1], so ww is xx with its last letter aa replaced by cc. Take a proper suffix w[q..N−1]w[q..N-1] with 1≤q<N1 \le q < N. By Lemma 1, x[q..N−1]≥x[0..N−1−q]x[q..N-1] \ge x[0..N-1-q], these being prefixes of the same length of uω[q..]u^\omega[q..] and uωu^\omega. So either the two words are equal, or they first differ at some offset dd with x[q+d]>x[d]x[q+d] > x[d].

  • If they first differ at d<N−1−qd < N - 1 - q, then q+d<N−1q + d < N - 1 and d<N−1d < N - 1, so ww agrees with xx at both indices, and w[q..]w[q..] and ww first differ at dd with the larger letter in w[q..]w[q..].
  • Otherwise they agree on the offsets 0,…,N−2−q0, \ldots, N - 2 - q, and at the last offset N−1−qN - 1 - q we have x[N−1]=a≥x[N−1−q]x[N-1] = a \ge x[N-1-q]. Then w[q..N−1]w[q..N-1] and w[0..N−1−q]w[0..N-1-q] agree before the last offset, and at it w[N−1]=c>a≥x[N−1−q]=w[N−1−q]w[N-1] = c > a \ge x[N-1-q] = w[N-1-q].

In both cases w[q..N−1]>ww[q..N-1] > w, since the two first differ inside w[q..N−1]w[q..N-1]. So ww is smaller than each of its proper nonempty suffixes. □\square

Lemma 3 (output step). Let z=umu′yz = u^m u' y, where uu is a Lyndon word, m≥1m \ge 1, u′u' is a proper prefix of uu, and either yy is empty or yy starts with a letter c<u[∣u′∣]c < u[|u'|]. Then the Lyndon factorization of zz is mm copies of uu followed by the Lyndon factorization of u′yu' y.

Proof. If u′yu'y is empty, the claim holds, since u,…,uu, \ldots, u is nonincreasing. Otherwise let ℓ\ell be the first factor of the Lyndon factorization of u′yu' y. If ∣ℓ∣≤∣u′∣|\ell| \le |u'|, then ℓ\ell is a prefix of u′u', hence a prefix of uu, so ℓ≤u\ell \le u. If ∣ℓ∣>∣u′∣|\ell| > |u'|, then yy is nonempty and ℓ\ell starts with u′cu' c, while uu starts with u′ u[∣u′∣]u'\,u[|u'|] and c<u[∣u′∣]c < u[|u'|], so ℓ<u\ell < u. In both cases the sequence u,…,u,ℓ,…u, \ldots, u, \ell, \ldots is a nonincreasing sequence of Lyndon words with product zz, and by uniqueness it is the Lyndon factorization of zz. □\square

Outer invariant. At each test of line L2: 0≤i≤n0 \le i \le n, the list FF holds the start indices of factors ℓ1,…,ℓr\ell_1, \ldots, \ell_r with ℓ1⋯ℓr=s[0..i−1]\ell_1 \cdots \ell_r = s[0..i-1], and the Lyndon factorization of ss is ℓ1,…,ℓr\ell_1, \ldots, \ell_r followed by the Lyndon factorization of s[i..n−1]s[i..n-1]. At the first test i=0i = 0 and FF is empty, so this holds.

Inner invariant. At each test of line L4 in a round that starts at ii: i≤k<j≤ni \le k < j \le n, and with p=j−kp = j - k, the word u=s[i..i+p−1]u = s[i..i+p-1] is a Lyndon word and s[i..j−1]=umu′s[i..j-1] = u^m u' with m≥1m \ge 1 and u′u' a proper prefix of uu. Since s[i..j−1]s[i..j-1] is a prefix of uωu^\omega, the letter that continues the period is uω[j−i]=uω[j−i−p]=s[k]u^\omega[j - i] = u^\omega[j - i - p] = s[k], and s[k]=u[∣u′∣]s[k] = u[|u'|].

  • First test. Line L3 sets j=i+1j = i + 1 and k=ik = i, so p=1p = 1, u=s[i]u = s[i], m=1m = 1 and u′u' is empty.
  • Equal letters. If s[j]=s[k]s[j] = s[k], then s[i..j]=umu′s[k]s[i..j] = u^m u' s[k], and u′ s[k]u'\,s[k] is a prefix of uu. If it is a proper prefix, it is the new u′u'; if it equals uu, then mm grows by one and the new u′u' is empty. Line L5 sets k←k+1k \leftarrow k + 1 and line L6 sets j←j+1j \leftarrow j + 1, so pp is unchanged and the invariant holds.
  • Larger letter. If s[j]>s[k]=u[∣u′∣]s[j] > s[k] = u[|u'|], then s[i..j]s[i..j] is a Lyndon word by Lemma 2. Line L5 sets k←ik \leftarrow i and line L6 sets j←j+1j \leftarrow j + 1, so the new pp is j+1−ij + 1 - i, the new uu is s[i..j]s[i..j], m=1m = 1 and u′u' is empty.

Each step increases jj, and j≤nj \le n, so the inner loop stops.

The output step. When the inner loop stops, s[i..j−1]=umu′s[i..j-1] = u^m u' as above, and either j=nj = n or s[j]<s[k]=u[∣u′∣]s[j] < s[k] = u[|u'|]. By Lemma 3 with y=s[j..n−1]y = s[j..n-1], the Lyndon factorization of s[i..n−1]s[i..n-1] is mm copies of uu followed by the factorization of s[i+mp..n−1]s[i+mp..n-1]. Lines L7 to L9 append i,i+p,…i, i + p, \ldots while i≤k=j−pi \le k = j - p, that is, while i+p≤ji + p \le j. Since j−i=mp+∣u′∣j - i = mp + |u'| with ∣u′∣<p|u'| < p, they append exactly mm indices, the starts of the mm copies of uu, and leave ii at the old ii plus mpmp. So the outer invariant holds again. Since m≥1m \ge 1, each round increases ii, so the outer loop stops.

Conclusion. At the end i=ni = n, so s[i..n−1]s[i..n-1] is empty, and by the outer invariant FF holds the start indices of the Lyndon factorization of ss. ■\blacksquare

Back to Duval's algorithm