Algorithm Search⌘K

Proofs: FKM algorithm

These are the human proofs of FKM algorithm, at full length.

FKM visits every prenecklace of length n once, in increasing order, with p = lyn(a) full

This proof shows: correctness, termination.

Notation. The alphabet is {0,…,k−1}\{0, \ldots, k-1\} with k≥1k \ge 1, ordered as integers, and words are compared lexicographically. Indices are 0-based. For a nonempty word vv, v∞v^\infty is the infinite word vvv⋯vvv\cdots, and v∞[0..m−1]v^\infty[0..m-1] is its prefix of length mm. A Lyndon word is a nonempty word strictly smaller than each of its proper nonempty suffixes; equivalently, strictly smaller than its other rotations. A word aa is a prenecklace if it is a prefix of v∞v^\infty for some Lyndon word vv. Prefixes of prenecklaces are prenecklaces. For a nonempty word aa, lyn(a)\mathrm{lyn}(a) is the length of the longest prefix of aa that is a Lyndon word; it is at least 1, since a single letter is a Lyndon word.

Lemma 1 (suffixes of a prenecklace). If aa is a prenecklace of length mm and 0≤i<m0 \le i < m, then a[i..m−1]≥a[0..m−i−1]a[i..m-1] \ge a[0..m-i-1].

Proof. Let aa be a prefix of v∞v^\infty with vv Lyndon, and let r=i mod ∣v∣r = i \bmod |v| and v′v' the rotation v[r..∣v∣−1] v[0..r−1]v[r..|v|-1]\,v[0..r-1]. The suffix of v∞v^\infty from index ii is v′∞v'^\infty. Since vv is a Lyndon word, v≤v′v \le v', and both have length ∣v∣|v|; so either v=v′v = v', or they differ inside their first ∣v∣|v| letters with vv smaller. In both cases each prefix of v∞v^\infty is at most the prefix of v′∞v'^\infty of the same length. The words a[i..m−1]a[i..m-1] and a[0..m−i−1]a[0..m-i-1] are these prefixes of length m−im - i. □\square

Lemma 2 (Duval). Let uu be a Lyndon word of length pp, let u′=u[0..r−1]u' = u[0..r-1] with 0≤r<p0 \le r < p, let e≥1e \ge 1 and let cc be a letter with c>u[r]c > u[r]. Then x=ueu′cx = u^e u' c is a Lyndon word.

Proof. Let 0<i<∣x∣=ep+r+10 < i < |x| = ep + r + 1 and write i=qp+ti = qp + t with 0≤t<p0 \le t < p. We show x[i..]>xx[i..] > x.

  • Case t=0t = 0, so 1≤q≤e1 \le q \le e. The suffix is ue−qu′cu^{e-q} u' c. It agrees with xx on its first (e−q)p+r(e-q)p + r letters, and the next letters are cc in the suffix and x[(e−q)p+r]=u[r]x[(e-q)p + r] = u[r] in xx, since (e−q)p+r<ep(e-q)p + r < ep. As c>u[r]c > u[r], the suffix is larger.
  • Case t>0t > 0 and q<eq < e. The suffix starts with u[t..p−1]u[t..p-1], a proper suffix of uu, so u[t..p−1]>uu[t..p-1] > u. A shorter word that is larger than uu is not a prefix of uu, so there is d<p−td < p - t with u[t..t+d−1]=u[0..d−1]u[t..t+d-1] = u[0..d-1] and u[t+d]>u[d]u[t+d] > u[d]. Then the suffix and xx first differ at offset dd, and the suffix is larger.
  • Case t>0t > 0 and q=eq = e. Then t≤rt \le r and the suffix is u[t..r−1] cu[t..r-1]\,c, of length r−t+1≤pr - t + 1 \le p, while xx starts with u[0..r−t]u[0..r-t]. Take dd as in the previous case. If d<r−td < r - t, the suffix is larger at offset dd. Otherwise u[t..r−1]=u[0..r−t−1]u[t..r-1] = u[0..r-t-1] and u[r]≥u[r−t]u[r] \ge u[r-t] (equality when d>r−td > r - t, strict when d=r−td = r - t), so c>u[r]≥u[r−t]c > u[r] \ge u[r-t], and the suffix is larger at offset r−tr - t. □\square

Lemma 3 (the longest Lyndon prefix of a periodic word). Let uu be a Lyndon word of length pp and m≥pm \ge p. Then lyn(u∞[0..m−1])=p\mathrm{lyn}(u^\infty[0..m-1]) = p.

Proof. The prefix uu is Lyndon. Let y=u∞[0..l−1]y = u^\infty[0..l-1] with p<l≤mp < l \le m, and write y=uevy = u^e v with e≥1e \ge 1 and vv a proper prefix of uu. If vv is nonempty, it is a proper nonempty suffix of yy and also a prefix of yy, so v<yv < y, and yy is not Lyndon. If vv is empty, then e≥2e \ge 2 and the proper suffix ue−1u^{e-1} is a prefix of yy, so again yy is not Lyndon. □\square

Lemma 4 (extension). Let aa be a nonempty prenecklace of length mm with q=lyn(a)q = \mathrm{lyn}(a), and suppose a=u∞[0..m−1]a = u^\infty[0..m-1] with u=a[0..q−1]u = a[0..q-1]. Let bb be a letter and c=a[m−q]c = a[m - q]. Then:

  • if b=cb = c, abab is a prenecklace, ab=u∞[0..m]ab = u^\infty[0..m] and lyn(ab)=q\mathrm{lyn}(ab) = q;
  • if b>cb > c, abab is a Lyndon word, so it is a prenecklace with lyn(ab)=m+1\mathrm{lyn}(ab) = m + 1 and ab=(ab)∞[0..m]ab = (ab)^\infty[0..m];
  • if b<cb < c, abab is not a prenecklace.

Proof. Write m=eq+rm = eq + r with e≥1e \ge 1 and 0≤r<q0 \le r < q; then a=ueu[0..r−1]a = u^e u[0..r-1] and c=u[r]c = u[r], because u∞u^\infty has period qq. If b=cb = c, then ab=u∞[0..m]ab = u^\infty[0..m], a prenecklace, and lyn(ab)=q\mathrm{lyn}(ab) = q by Lemma 3. If b>cb > c, abab is Lyndon by Lemma 2; a Lyndon word ww is a prefix of w∞w^\infty, and lyn(w)=∣w∣\mathrm{lyn}(w) = |w|. If b<cb < c, the suffix of abab at index eqeq is u[0..r−1] bu[0..r-1]\,b, and the prefix of abab of the same length is u[0..r−1] u[r]u[0..r-1]\,u[r], which is larger. By Lemma 1, abab is not a prenecklace. □\square

Lemma 5 (structure). Every nonempty prenecklace aa of length mm satisfies a=u∞[0..m−1]a = u^\infty[0..m-1] with u=a[0..lyn(a)−1]u = a[0..\mathrm{lyn}(a)-1].

Proof. Induction on mm. For m=1m = 1, lyn(a)=1\mathrm{lyn}(a) = 1 and u=au = a. For m≥2m \ge 2, a[0..m−2]a[0..m-2] is a prenecklace, the induction hypothesis applies to it, and since aa is a prenecklace, Lemma 4 leaves the first two cases, which both give the claim. □\square

So Lemma 4 applies to every nonempty prenecklace. In particular, for q≤nq \le n the map u↦u∞[0..n−1]u \mapsto u^\infty[0..n-1] is a bijection from the Lyndon words of length qq onto the prenecklaces of length nn with lyn=q\mathrm{lyn} = q: it lands there by Lemma 3, it is onto by Lemma 5, and uu is the prefix of length qq of its image.

Lemma 6 (successor). Let aa be a prenecklace of length nn other than (k−1)n(k-1)^n, let jj be the largest index with a[j]<k−1a[j] < k - 1, and let x=a[0..j−1] (a[j]+1)x = a[0..j-1]\,(a[j]+1). Then xx is a Lyndon word, a′=x∞[0..n−1]a' = x^\infty[0..n-1] is a prenecklace with lyn(a′)=j+1\mathrm{lyn}(a') = j + 1, and a′a' is the least prenecklace of length nn greater than aa.

Proof. If j=0j = 0, xx is one letter, a Lyndon word. If j≥1j \ge 1, let w=a[0..j−1]w = a[0..j-1], a nonempty prenecklace, and q=lyn(w)q = \mathrm{lyn}(w). Since wa[j]=a[0..j]wa[j] = a[0..j] is a prenecklace, Lemma 4 gives a[j]≥w[j−q]a[j] \ge w[j - q], so a[j]+1>w[j−q]a[j] + 1 > w[j-q], and by Lemma 4 again xx is a Lyndon word. Then a′a' is a prefix of x∞x^\infty, a prenecklace, and lyn(a′)=j+1\mathrm{lyn}(a') = j + 1 by Lemma 3, since j+1≤nj + 1 \le n.

The word a′a' is greater than aa, since they first differ at index jj. Let yy be a word of length nn with a<y<a′a < y < a'. The letters of aa after index jj are all k−1k - 1, so yy first exceeds aa at some index i≤ji \le j. If i<ji < j, then y[i]>a[i]=a′[i]y[i] > a[i] = a'[i] and y>a′y > a'; so i=ji = j, and y[j]=a[j]+1y[j] = a[j] + 1, since a larger letter would again give y>a′y > a'. Hence yy begins with xx, and since y<a′y < a', there is an index d>jd > j with y[0..d−1]=a′[0..d−1]y[0..d-1] = a'[0..d-1] and y[d]<a′[d]y[d] < a'[d]. The prenecklace y[0..d−1]=x∞[0..d−1]y[0..d-1] = x^\infty[0..d-1] has lyn=j+1\mathrm{lyn} = j + 1 by Lemma 3, and a′[d]=x∞[d]=x∞[d−j−1]=y[d−j−1]a'[d] = x^\infty[d] = x^\infty[d - j - 1] = y[d - j - 1]. By Lemma 4, y[0..d]y[0..d] is not a prenecklace, so neither is yy. □\square

The loop invariant. Each time line L3 is reached, aa is a prenecklace of length nn and p=lyn(a)p = \mathrm{lyn}(a).

Initialization. Line L1 sets a=0n=0∞[0..n−1]a = 0^n = 0^\infty[0..n-1], a prenecklace because 00 is a Lyndon word, and p=1=lyn(0n)p = 1 = \mathrm{lyn}(0^n) by Lemma 3.

Maintenance. Lines L4 and L5 stop at the largest jj with a[j]<k−1a[j] < k - 1, or at j=−1j = -1 if there is none. If j≥0j \ge 0, line L7 makes a[0..j]=xa[0..j] = x as in Lemma 6 and sets p=j+1p = j + 1, and line L8, running ii upward, sets a[i]=a[i−p]a[i] = a[i - p] for p≤i<np \le i < n, so that a=x∞[0..n−1]=a′a = x^\infty[0..n-1] = a'. By Lemma 6, a′a' is a prenecklace with lyn(a′)=j+1=p\mathrm{lyn}(a') = j + 1 = p.

Each prenecklace is visited once, in increasing order. The first word visited is 0n0^n, the least word of length nn. By Lemma 6, each later word visited is the least prenecklace greater than the one before it. So the visited words are increasing, and no prenecklace is skipped. The last one is (k−1)n(k-1)^n, a prefix of (k−1)∞(k-1)^\infty and hence a prenecklace, after which line L6 returns.

Termination. Each pass through lines L4 to L8 strictly increases aa, and there are finitely many words of length nn. Each inner loop runs at most nn times.

The tests of VISIT. Let aa be a prenecklace of length nn with p=lyn(a)p = \mathrm{lyn}(a) and u=a[0..p−1]u = a[0..p-1], so a=u∞[0..n−1]a = u^\infty[0..n-1] by Lemma 5.

  • If pp divides nn, aa is a necklace. Then a=un/pa = u^{n/p}. A rotation of aa by ii is (ur)n/p(u_r)^{n/p}, where uru_r is the rotation of uu by r=i mod pr = i \bmod p, and u≤uru \le u_r because uu is a Lyndon word. So aa is at most each of its rotations.
  • If aa is a necklace, pp divides nn. Write a=zea = z^{e} with zz primitive. Let 0<r<∣z∣0 < r < |z| and let zrz_r be the rotation of zz by rr. It differs from zz, because zz is primitive. The rotation of aa by rr is zr ez_r^{\,e}, and zr e≥ze=az_r^{\,e} \ge z^e = a; since zrz_r and zz have the same length, this forces zr≥zz_r \ge z, so zr>zz_r > z. So zz is a Lyndon word, a=z∞[0..n−1]a = z^\infty[0..n-1], and lyn(a)=∣z∣\mathrm{lyn}(a) = |z| by Lemma 3, which divides nn.
  • Lyndon words. aa is a Lyndon word if and only if its longest Lyndon prefix is aa itself, that is, p=np = n. Every Lyndon word of length nn is a prenecklace, so it is visited. ■\blacksquare

Back to FKM algorithm