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 be a string over the alphabet , indexed from 0, and let be the set of its factors, including the empty word . For let be its set of end positions, with .
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).
- The states are the classes of the relation on ; state 0 is the class of , which contains only .
- The words of a state are the suffixes of its longest word with lengths in , and for the suffix of the longest word of of length is the longest word of .
- is defined if and only if for the words of .
- for the words of .
Property 3 is well defined: whether depends only on , since if and only if some has and . For property 4, a state created while reading is the class of the prefix , so for its words. A clone of a state holds words whose set of end positions contains of the words of , because they are suffixes of those words, so 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 is in if and only if is undefined.
Proof. The only proper factor of is , so if and only if , which by property 3 for the state of means that is undefined.
Lemma 2 (longer words). Let with letters and a word . Then if and only if there is a state such that is the shortest word of , is the longest word of , is defined and is undefined.
Proof. Every proper factor of is a factor of or of , and is closed under taking factors, so if and only if , and .
() The word is a word of , so it is in ; undefined gives by property 3; is a word of and is defined, so .
() Let be the state of ; since . By property 3, is undefined because , while the state of has a transition by because ; so is not in . By property 2, the words of are the suffixes of its longest word with lengths in . The word is one of them and , its suffix of length , is not, so . Hence , is the shortest word of , and is the suffix of the longest word of of length , which by property 2 is the longest word of . Then is defined since .
Claim. The list returned at line L10 contains each word of exactly once and nothing else, where stands for .
Proof. Lines L3 and L4 append , the word , exactly for the letters with undefined; by Lemma 1 these are the one-letter minimal absent words, each once.
Lines L5 to L9 consider each pair with and defined exactly once, and append a triple exactly when is undefined. For such a pair, line L6 sets , the length of the shortest word of by property 2, and by property 4 , so line L9 appends the word . Write with a letter; then , the suffix of of length , is the longest word of by property 2. By Lemma 2 (), . By Lemma 2 (), each word of of length at least 2 arises from some such pair . Two different pairs give different words: if then 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.
Cost. By property 3, if is defined then for the words of , hence for their suffixes in , so is defined. Thus the loop of line L7 at state runs times, where is the number of words appended at . Summed over all states this is at most the number of transitions plus , and the automaton has states and transitions. With hash maps each test takes expected time, so after the construction the scan takes expected time, and the construction takes expected time. ∎