Proofs: Subsequence automaton
These are the human proofs of Subsequence automaton, at full length.
The subsequence automaton accepts exactly the subsequences of the text full
This proof shows: correctness.
We use the pseudocode of the page. Let . For write . For a word and , write if is a subsequence of . Let be the least with , or NIL if ; then .
Lemma 1 (the table). After BUILD, for all and all letters , , or NIL if the set is empty.
Proof. Induction on from down. For the set is empty, and line L1 sets NIL. Let and assume the claim for row . Lines L3 and L4 make row equal to row except at , where the entry is . For , the least with is . For , a with has , so , and the least one is by the hypothesis.
Lemma 2 (greedy step). Let be a word with NIL, and let be a letter. Then , where NIL on the right means that .
Proof. If NIL, then and with , so and . Conversely, let with an embedding whose last position is $j' $ and holds . The other positions embed in , so , that is, , and . By Lemma 1, is defined and at most . So when is defined, and if $\mathrm{nxt}[q][c] = $ NIL, no such exists and .
Theorem 1. CONTAINS() returns true if and only if . Each time line L8 completes with NIL after the letters , .
Proof. Induction on . For , line L6 sets . The step from to is Lemma 2. If line L9 returns false after , then this prefix is not a subsequence of by Lemma 2, and so neither is . If the loop ends, NIL, so , and line L10 returns true.
Lemma 3 (the words read from a state). For each state , the words that the automaton can read from state without a NIL move are exactly the subsequences of , and each such word has exactly one path.
Proof. The automaton is deterministic, so each word has at most one path from . A path from that reads visits states with by Lemma 1, so the word is a subsequence of . Conversely, Lemma 2 applied to the text , with the start state in place of 0 (the proof is the same, with positions restricted to ), shows that a subsequence of is read from without a NIL move.
Theorem 2. COUNT-DISTINCT returns the number of distinct subsequences of , the empty one included.
Proof. Let be the number of distinct subsequences of , the empty one included. By Lemma 3, is the number of paths that start at (the path of length 0 reads ). Each non-empty path starts with a move to some NIL and continues with a path from there, and the first letter and the rest of the path determine it. So over the letters with a move. All moves go to larger states, so line L11, which runs from down to 0, uses only values already computed; . By induction , and line L12 returns .
Theorem 3 (minimality). Every deterministic automaton, with partial or total transitions, that accepts exactly the subsequences of has at least states that are not dead, so the subsequence automaton has the least number of states.
Proof. By Lemma 3, the language accepted from state is . For , itself is in and has length , while every word of has length at most . So the languages are distinct and non-empty. The words , for , lead state 0 of our automaton to state , by Theorem 1 (the leftmost embedding of a prefix of is the prefix itself). In any deterministic automaton for the same language, the states reached by two of these words must accept different languages: for , the word is accepted after , but has length and is not a subsequence of . So these words reach different states, and none of them is dead.
Cost. BUILD copies rows of entries: . CONTAINS does at most iterations with one lookup each. COUNT-DISTINCT reads each of the entries once.