Algorithm Search⌘K

Proofs: CAT necklace generation

These are the human proofs of CAT necklace generation, at full length.

GEN 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

The calls. We show: for each tt with 1≤t≤n1 \le t \le n, the call GEN(t,p)(t, p) is made exactly once for each prenecklace xx of length tt, at a moment when a[0..t−1]=xa[0..t-1] = x, and then p=lyn(x)p = \mathrm{lyn}(x). Moreover, while a call GEN(t,p)(t, p) and the calls below it run, the cells a[0..t−1]a[0..t-1] do not change, since the call writes only a[t]a[t] and its descendants write only higher cells.

Level 1. Line L1 sets a[0]a[0] to each letter cc in turn and calls GEN(1,1)(1, 1). Every one-letter word is a Lyndon word, hence a prenecklace with lyn=1\mathrm{lyn} = 1, and there are no other prenecklaces of length 1.

From tt to t+1t + 1, for t<nt < n. A prenecklace yy of length t+1t + 1 is xbxb, where x=y[0..t−1]x = y[0..t-1] is a prenecklace (a prefix of a prenecklace) and bb a letter. By the hypothesis, GEN(t,lyn(x))(t, \mathrm{lyn}(x)) runs exactly once with a[0..t−1]=xa[0..t-1] = x; it reads c0=a[t−p]=x[t−p]c_0 = a[t - p] = x[t - p], which stays fixed during the call. By Lemma 5, Lemma 4 applies to xx with q=pq = p: the extensions xbxb that are prenecklaces are exactly those with b≥c0b \ge c_0, with lyn(xb)=p\mathrm{lyn}(xb) = p for b=c0b = c_0 and lyn(xb)=t+1\mathrm{lyn}(xb) = t + 1 for b>c0b > c_0. Line L5 sets a[t]=c0a[t] = c_0 and calls GEN(t+1,p)(t + 1, p), and lines L6 and L7 set a[t]=ba[t] = b and call GEN(t+1,t+1)(t + 1, t + 1) for each bb from c0+1c_0 + 1 to k−1k - 1. So each prenecklace xbxb gets exactly one call, with the right value of pp, and no other word of length t+1t + 1 gets a call.

Order. The calls form a tree in which the children of a call are made with increasing letters at index tt. The words of length nn are reached in the order of a depth-first walk that takes children in increasing order, which is increasing lexicographic order: two leaves first differ at some index tt, below the call for their common prefix, and the leaf with the smaller letter at tt is under the earlier child.

Termination. Each call makes at most kk calls, and the depth is at most nn, so the tree is finite. The number of calls is 1+∑t=1nPk(t)1 + \sum_{t=1}^{n} P_k(t), where Pk(t)P_k(t) is the number of prenecklaces of length tt.

The tests of line L3. 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 e≥ze=az_r^{\,e} \ge z^e = a, and since zrz_r and zz have the same length, 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 is a prenecklace, so each Lyndon word of length nn is visited.

The number of calls. By Lemmas 3 and 5, a prenecklace of length tt with lyn=q\mathrm{lyn} = q is u∞[0..t−1]u^\infty[0..t-1] for exactly one Lyndon word uu of length qq, and each Lyndon word uu of length q≤tq \le t gives one. So Pk(t)=∑q=1tLk(q)P_k(t) = \sum_{q=1}^{t} L_k(q), where Lk(q)L_k(q) is the number of Lyndon words of length qq, and the number of calls is

1+∑t=1n∑q=1tLk(q)=1+∑q=1nLk(q) (n−q+1).1 + \sum_{t=1}^{n} \sum_{q=1}^{t} L_k(q) = 1 + \sum_{q=1}^{n} L_k(q)\,(n - q + 1).

The qq rotations of a Lyndon word of length qq are distinct words, so Lk(q)≤kq/qL_k(q) \le k^q / q. Put q=n−dq = n - d. For d≤n/2d \le n/2, 1/q≤2/n1/q \le 2/n, and these terms sum to at most 2knn∑d≥0(d+1)k−d=2knn(kk−1)2\frac{2k^n}{n} \sum_{d \ge 0} (d + 1) k^{-d} = \frac{2k^n}{n} \left(\frac{k}{k-1}\right)^2 for k≥2k \ge 2. For d>n/2d > n/2, each term is at most kn/2(n+1)k^{n/2} (n + 1), and there are at most nn of them. The number NN of necklaces of length nn is at least kn/nk^n / n, since each necklace class has at most nn words. So the number of calls is at most 2(k/(k−1))2N+1+n(n+1)kn/22(k/(k-1))^2 N + 1 + n(n+1)k^{n/2}, and n(n+1)kn/2=o(kn/n)n(n+1)k^{n/2} = o(k^n / n). Each call does O(1)O(1) work besides the calls it makes, so the total time is O(N)O(N) for k≥2k \ge 2. ■\blacksquare

Back to CAT necklace generation