Algorithm Search⌘K

Proofs: Minimal absent words algorithm

These are the human proofs of Minimal absent words algorithm, at full length.

MINIMAL-ABSENT-WORDS reports each minimal absent word exactly once full

This proof shows: correctness.

Let y=y[0..n−1]y = y[0..n-1] be a string over the alphabet Σ\Sigma, indexed from 0, and let FF be the set of its factors, including the empty word ε\varepsilon. For x∈Fx \in F let E(x)={ j:y[j−∣x∣+1..j]=x }E(x) = \{\, j : y[j - |x| + 1..j] = x \,\} be its set of end positions, with E(ε)={−1,0,…,n−1}E(\varepsilon) = \{-1, 0, \ldots, n-1\}.

The suffix automaton. We use these properties of the automaton built at line L1, which hold for the standard online construction (Blumer and coauthors, 1985).

  1. The states are the classes of the relation E(x)=E(x′)E(x) = E(x') on FF; state 0 is the class of ε\varepsilon, which contains only ε\varepsilon.
  2. The words of a state pp are the suffixes of its longest word with lengths in (len[link[p]],len[p]](\mathrm{len}[\mathrm{link}[p]], \mathrm{len}[p]], and for p≠0p \ne 0 the suffix of the longest word of pp of length len[link[p]]\mathrm{len}[\mathrm{link}[p]] is the longest word of link[p]\mathrm{link}[p].
  3. next[p][b]\mathrm{next}[p][b] is defined if and only if xb∈Fxb \in F for the words xx of pp.
  4. end[p]∈E(x)\mathrm{end}[p] \in E(x) for the words xx of pp.

Property 3 is well defined: whether xb∈Fxb \in F depends only on E(x)E(x), since xb∈Fxb \in F if and only if some j∈E(x)j \in E(x) has j+1≤n−1j + 1 \le n - 1 and y[j+1]=by[j + 1] = b. For property 4, a state created while reading y[i]y[i] is the class of the prefix y[0..i]y[0..i], so i∈E(x)i \in E(x) for its words. A clone of a state qq holds words whose set of end positions contains EE of the words of qq, because they are suffixes of those words, so end[q]\mathrm{end}[q] is a valid end position for the clone too. The end positions of a state never lose elements as the construction reads further letters, so property 4 holds at the end.

Lemma 1 (one-letter words). A letter bb is in MAW(y)\mathrm{MAW}(y) if and only if next[0][b]\mathrm{next}[0][b] is undefined.

Proof. The only proper factor of bb is ε∈F\varepsilon \in F, so b∈MAW(y)b \in \mathrm{MAW}(y) if and only if b∉Fb \notin F, which by property 3 for the state 00 of ε\varepsilon means that next[0][b]\mathrm{next}[0][b] is undefined. □\square

Lemma 2 (longer words). Let w=a u bw = a\,u\,b with letters a,ba, b and a word uu. Then w∈MAW(y)w \in \mathrm{MAW}(y) if and only if there is a state p≠0p \ne 0 such that auau is the shortest word of pp, uu is the longest word of link[p]\mathrm{link}[p], next[link[p]][b]\mathrm{next}[\mathrm{link}[p]][b] is defined and next[p][b]\mathrm{next}[p][b] is undefined.

Proof. Every proper factor of ww is a factor of auau or of ubub, and FF is closed under taking factors, so w∈MAW(y)w \in \mathrm{MAW}(y) if and only if au∈Fau \in F, ub∈Fub \in F and aub∉Faub \notin F.

(⇐\Leftarrow) The word auau is a word of pp, so it is in FF; next[p][b]\mathrm{next}[p][b] undefined gives aub∉Faub \notin F by property 3; uu is a word of link[p]\mathrm{link}[p] and next[link[p]][b]\mathrm{next}[\mathrm{link}[p]][b] is defined, so ub∈Fub \in F.

(⇒\Rightarrow) Let pp be the state of au∈Fau \in F; p≠0p \ne 0 since au≠εau \ne \varepsilon. By property 3, next[p][b]\mathrm{next}[p][b] is undefined because aub∉Faub \notin F, while the state of uu has a transition by bb because ub∈Fub \in F; so uu is not in pp. By property 2, the words of pp are the suffixes of its longest word with lengths in (len[link[p]],len[p]](\mathrm{len}[\mathrm{link}[p]], \mathrm{len}[p]]. The word auau is one of them and uu, its suffix of length ∣au∣−1|au| - 1, is not, so ∣au∣−1≤len[link[p]]<∣au∣|au| - 1 \le \mathrm{len}[\mathrm{link}[p]] < |au|. Hence ∣au∣=len[link[p]]+1|au| = \mathrm{len}[\mathrm{link}[p]] + 1, auau is the shortest word of pp, and uu is the suffix of the longest word of pp of length len[link[p]]\mathrm{len}[\mathrm{link}[p]], which by property 2 is the longest word of link[p]\mathrm{link}[p]. Then next[link[p]][b]\mathrm{next}[\mathrm{link}[p]][b] is defined since ub∈Fub \in F. □\square

Claim. The list MM returned at line L10 contains each word of MAW(y)\mathrm{MAW}(y) exactly once and nothing else, where (i,ℓ,b)(i, \ell, b) stands for y[i..i+ℓ−1] by[i..i+\ell-1]\,b.

Proof. Lines L3 and L4 append (0,0,b)(0, 0, b), the word bb, exactly for the letters b∈Σb \in \Sigma with next[0][b]\mathrm{next}[0][b] undefined; by Lemma 1 these are the one-letter minimal absent words, each once.

Lines L5 to L9 consider each pair (p,b)(p, b) with p≠0p \ne 0 and next[link[p]][b]\mathrm{next}[\mathrm{link}[p]][b] defined exactly once, and append a triple exactly when next[p][b]\mathrm{next}[p][b] is undefined. For such a pair, line L6 sets ℓ=len[link[p]]+1\ell = \mathrm{len}[\mathrm{link}[p]] + 1, the length of the shortest word xpx_p of pp by property 2, and by property 4 xp=y[end[p]−ℓ+1..end[p]]x_p = y[\mathrm{end}[p] - \ell + 1..\mathrm{end}[p]], so line L9 appends the word xp bx_p\,b. Write xp=a ux_p = a\,u with aa a letter; then uu, the suffix of xpx_p of length len[link[p]]\mathrm{len}[\mathrm{link}[p]], is the longest word of link[p]\mathrm{link}[p] by property 2. By Lemma 2 (⇐\Leftarrow), xp b∈MAW(y)x_p\,b \in \mathrm{MAW}(y). By Lemma 2 (⇒\Rightarrow), each word of MAW(y)\mathrm{MAW}(y) of length at least 2 arises from some such pair (p,b)(p, b). Two different pairs give different words: if p≠p′p \ne p' then xp≠xp′x_p \ne x_{p'} because the states are disjoint classes, and the pair is recovered from the word as its prefix without the last letter and that letter. So each word of length at least 2 is appended exactly once. Words of length 1 and words of length at least 2 are different, which completes the proof. □\square

Cost. By property 3, if next[p][b]\mathrm{next}[p][b] is defined then xb∈Fxb \in F for the words xx of pp, hence vb∈Fvb \in F for their suffixes vv in link[p]\mathrm{link}[p], so next[link[p]][b]\mathrm{next}[\mathrm{link}[p]][b] is defined. Thus the loop of line L7 at state pp runs ∣next[p]∣+zp|\mathrm{next}[p]| + z_p times, where zpz_p is the number of words appended at pp. Summed over all states this is at most the number of transitions plus zz, and the automaton has O(n)O(n) states and transitions. With hash maps each test takes O(1)O(1) expected time, so after the construction the scan takes O(n+σ+z)O(n + \sigma + z) expected time, and the construction takes O(n)O(n) expected time. ∎

Back to Minimal absent words algorithm