Algorithm Search⌘K

Proofs: DC3 algorithm

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

DC-v sorts the sample by recursion and all suffixes by one comparison through the sample full

This proof shows: correctness.

We use the pseudocode and notation of the page. Let tt have length nn, with t[p]=0t[p] = 0 for p≥np \ge n and t[p]≥1t[p] \ge 1 for p<np < n. Write TiT_i for the suffix t[i…]t[i \ldots], read with its trailing zeros, and Ti<TjT_i < T_j for the lexicographic order; for i≠ji \ne j, Ti≠TjT_i \ne T_j, since TiT_i has its first zero at offset n−in - i. The proof is by induction on nn: we assume that DC returns the suffix array of every shorter string, and the base case n≤2vn \le 2v sorts directly.

Lemma 1 (the first difference of two sample suffixes). Let i≠ji \ne j be in SS, and let qq be the least q≥0q \ge 0 with t[i+q]≠t[j+q]t[i + q] \ne t[j + q]. If ii is in class dd and i′i' is the last position of class dd in SS, then i+q<i′+vi + q < i' + v.

Proof. The position i′i' is the largest p≤np \le n with p≡dp \equiv d, so i′>n−vi' > n - v and i′+v−1≥ni' + v - 1 \ge n. The first zero of TiT_i is at offset n−in - i, and TjT_j has a nonzero letter there unless n−i≥n−jn - i \ge n - j; in the same way TiT_i has a nonzero letter at offset n−jn - j unless n−j≥n−in - j \ge n - i. Since i≠ji \ne j, the two first zeros are at different offsets, so q≤min⁡(n−i,n−j)≤n−iq \le \min(n - i, n - j) \le n - i, and i+q≤n≤i′+v−1i + q \le n \le i' + v - 1. □\square

Lemma 2 (the sample ranks). After line L5, for i,j∈Si, j \in S, rank[i]<rank[j]rank[i] < rank[j] if and only if Ti<TjT_i < T_j, and the ranks are 1,…,∣S∣1, \ldots, \lvert S \rvert.

Proof. Line L2 gives equal names to equal vv-tuples and orders the names like the tuples. If all names differ, the order of the names is the order of the first vv letters, which decides each comparison, so the names are the ranks. Otherwise let WW be the string of names of line L4, and let σ(i)\sigma(i) be the place of ii in WW. For ii in class dd, the letters of WW from σ(i)\sigma(i) up to the end of the block of class dd are the names of i,i+v,…,i′i, i + v, \ldots, i', which are the names of the consecutive vv-blocks of TiT_i up to offset i′−i+v−1i' - i + v - 1. Compare TiT_i and TjT_j, with qq as in Lemma 1, and let p=⌊q/v⌋p = \lfloor q / v \rfloor. The blocks 0,…,p−10, \ldots, p - 1 of TiT_i and TjT_j are equal, so they have the same names, and block pp differs at its letter q−pvq - pv, so the names of block pp differ in the same direction as t[i+q]t[i + q] and t[j+q]t[j + q]. By Lemma 1 applied to ii and to jj, block pp of each suffix is still inside the block of its class in WW. So the suffixes of WW at σ(i)\sigma(i) and σ(j)\sigma(j) first differ at offset pp, in the same direction as TiT_i and TjT_j. WW is shorter than tt when n>2vn > 2v, since ∣S∣≤∣D∣⌈(n+1)/v⌉<n\lvert S \rvert \le \lvert D \rvert \lceil (n + 1)/v \rceil < n, so by induction line L4 returns its suffix array, and line L5 ranks SS in the order of the TiT_i. □\square

Set rank[p]=0rank[p] = 0 for p>np > n. This is consistent with the order: such a pp is never compared with a rank unless the letters before it already include a zero at a place where the other suffix has a zero too, which we treat in Lemma 3.

Lemma 3 (the comparison). Let i,j<ni, j < n, i≠ji \ne j, and let ℓ<v\ell < v be such that i+ℓi + \ell and j+ℓj + \ell are in classes of DD. Then Ti<TjT_i < T_j if and only if (t[i…i+ℓ−1],rank[i+ℓ])<(t[j…j+ℓ−1],rank[j+ℓ])(t[i \ldots i + \ell - 1], rank[i + \ell]) < (t[j \ldots j + \ell - 1], rank[j + \ell]) in lexicographic order.

Proof. TiT_i is t[i…i+ℓ−1]t[i \ldots i + \ell - 1] followed by Ti+ℓT_{i + \ell}, and the same for jj. If the first ℓ\ell letters differ, both sides are decided by them. If they are equal, then neither contains a zero: a zero at offset r<ℓr < \ell in both would put both first zeros at the same offset, so n−i=n−jn - i = n - j. Hence i+ℓ≤ni + \ell \le n and j+ℓ≤nj + \ell \le n, both are in SS, i+ℓ≠j+ℓi + \ell \ne j + \ell, and the comparison of TiT_i and TjT_j is the comparison of Ti+ℓT_{i + \ell} and Tj+ℓT_{j + \ell}, which by Lemma 2 is that of their ranks. □\square

Line L6. For a position ii of class cc, i+ℓci + \ell_c is in a class of DD, so by Lemma 3 with jj in the same class and ℓ=ℓc\ell = \ell_c, sorting by the key (t[i…i+ℓc−1],rank[i+ℓc])(t[i \ldots i + \ell_c - 1], rank[i + \ell_c]) sorts the class in the order of the suffixes. The radix sort is correct, and the keys of distinct positions differ by Lemma 3, so the order is strict.

Lines L7 and L8. For classes aa and bb, DD is a difference cover, so there are x,y∈Dx, y \in D with x−y≡a−b(modv)x - y \equiv a - b \pmod v. With ℓ=(x−a) mod v\ell = (x - a) \bmod v, (a+ℓ) mod v=x(a + \ell) \bmod v = x and (b+ℓ) mod v=(b+x−a) mod v=y(b + \ell) \bmod v = (b + x - a) \bmod v = y, both in DD, so the least such ℓ\ell exists and off[a][b]off[a][b] is defined. By Lemma 3, the comparison of line L8 is the order of the suffixes, for any two positions. Merging lists that are sorted in this order, with this comparison, gives all positions 0,…,n−10, \ldots, n - 1 in the order of their suffixes, which is the suffix array. ■\blacksquare

Back to DC3 algorithm