Algorithm Search⌘K

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 T=t1⋯tnT = t_1 \cdots t_n. For 0≤i≤n0 \le i \le n write T>i=ti+1⋯tnT_{>i} = t_{i+1} \cdots t_n. For a word PP and 0≤q≤n0 \le q \le n, write P⪯qTP \preceq_q T if PP is a subsequence of t1⋯tqt_1 \cdots t_q. Let first(P)\mathrm{first}(P) be the least qq with P⪯qTP \preceq_q T, or NIL if P⪯̸TP \not\preceq T; then first(ε)=0\mathrm{first}(\varepsilon) = 0.

Lemma 1 (the table). After BUILD, for all 0≤i≤n0 \le i \le n and all letters cc, nxt[i][c]=min⁡{j:i<j≤n, tj=c}\mathrm{nxt}[i][c] = \min\{ j : i < j \le n,\ t_j = c \}, or NIL if the set is empty.

Proof. Induction on ii from nn down. For i=ni = n the set is empty, and line L1 sets NIL. Let i≤ni \le n and assume the claim for row ii. Lines L3 and L4 make row i−1i - 1 equal to row ii except at tit_i, where the entry is ii. For c=tic = t_i, the least j>i−1j > i - 1 with tj=ct_j = c is ii. For c≠tic \ne t_i, a j>i−1j > i - 1 with tj=ct_j = c has j≠ij \ne i, so j>ij > i, and the least one is nxt[i][c]\mathrm{nxt}[i][c] by the hypothesis. □\square

Lemma 2 (greedy step). Let PP be a word with q=first(P)≠q = \mathrm{first}(P) \ne NIL, and let cc be a letter. Then first(Pc)=nxt[q][c]\mathrm{first}(Pc) = \mathrm{nxt}[q][c], where NIL on the right means that Pc⪯̸TPc \not\preceq T.

Proof. If j=nxt[q][c]≠j = \mathrm{nxt}[q][c] \ne NIL, then P⪯qTP \preceq_q T and tj=ct_j = c with j>qj > q, so Pc⪯jTPc \preceq_j T and first(Pc)≤j\mathrm{first}(Pc) \le j. Conversely, let Pc⪯j′TPc \preceq_{j'} T with an embedding whose last position is $j' $ and holds cc. The other positions embed PP in t1⋯tj′−1t_1 \cdots t_{j' - 1}, so q=first(P)≤j′−1q = \mathrm{first}(P) \le j' - 1, that is, j′>qj' > q, and tj′=ct_{j'} = c. By Lemma 1, nxt[q][c]\mathrm{nxt}[q][c] is defined and at most j′j'. So first(Pc)≥j\mathrm{first}(Pc) \ge j when jj is defined, and if $\mathrm{nxt}[q][c] = $ NIL, no such j′j' exists and Pc⪯̸TPc \not\preceq T. □\square

Theorem 1. CONTAINS(PP) returns true if and only if P⪯TP \preceq T. Each time line L8 completes with q≠q \ne NIL after the letters p1⋯prp_1 \cdots p_r, q=first(p1⋯pr)q = \mathrm{first}(p_1 \cdots p_r).

Proof. Induction on rr. For r=0r = 0, line L6 sets q=0=first(ε)q = 0 = \mathrm{first}(\varepsilon). The step from rr to r+1r + 1 is Lemma 2. If line L9 returns false after p1⋯pr+1p_1 \cdots p_{r+1}, then this prefix is not a subsequence of TT by Lemma 2, and so neither is PP. If the loop ends, first(P)≠\mathrm{first}(P) \ne NIL, so P⪯TP \preceq T, and line L10 returns true. □\square

Lemma 3 (the words read from a state). For each state ii, the words ww that the automaton can read from state ii without a NIL move are exactly the subsequences of T>iT_{>i}, and each such word has exactly one path.

Proof. The automaton is deterministic, so each word has at most one path from ii. A path from ii that reads c1⋯crc_1 \cdots c_r visits states i<j1<⋯<jri < j_1 < \cdots < j_r with tjs=cst_{j_s} = c_s by Lemma 1, so the word is a subsequence of T>iT_{>i}. Conversely, Lemma 2 applied to the text TT, with the start state ii in place of 0 (the proof is the same, with positions restricted to (i,n](i, n]), shows that a subsequence of T>iT_{>i} is read from ii without a NIL move. □\square

Theorem 2. COUNT-DISTINCT returns the number of distinct subsequences of TT, the empty one included.

Proof. Let N(i)N(i) be the number of distinct subsequences of T>iT_{>i}, the empty one included. By Lemma 3, N(i)N(i) is the number of paths that start at ii (the path of length 0 reads ε\varepsilon). Each non-empty path starts with a move to some nxt[i][c]≠\mathrm{nxt}[i][c] \ne NIL and continues with a path from there, and the first letter cc and the rest of the path determine it. So N(i)=1+∑cN(nxt[i][c])N(i) = 1 + \sum_{c} N(\mathrm{nxt}[i][c]) over the letters with a move. All moves go to larger states, so line L11, which runs from nn down to 0, uses only values already computed; cnt[n]=1=N(n)\mathrm{cnt}[n] = 1 = N(n). By induction cnt[i]=N(i)\mathrm{cnt}[i] = N(i), and line L12 returns N(0)N(0). □\square

Theorem 3 (minimality). Every deterministic automaton, with partial or total transitions, that accepts exactly the subsequences of TT has at least n+1n + 1 states that are not dead, so the subsequence automaton has the least number of states.

Proof. By Lemma 3, the language accepted from state ii is Li={w:w⪯T>i}L_i = \{ w : w \preceq T_{>i} \}. For i<i′i < i', T>iT_{>i} itself is in LiL_i and has length n−i>n−i′n - i > n - i', while every word of Li′L_{i'} has length at most n−i′n - i'. So the n+1n + 1 languages L0,…,LnL_0, \ldots, L_n are distinct and non-empty. The words t1⋯tit_1 \cdots t_i, for 0≤i≤n0 \le i \le n, lead state 0 of our automaton to state ii, by Theorem 1 (the leftmost embedding of a prefix of TT 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 i<i′i < i', the word T>iT_{>i} is accepted after t1⋯tit_1 \cdots t_i, but t1⋯ti′T>it_1 \cdots t_{i'} T_{>i} has length n+i′−i>nn + i' - i > n and is not a subsequence of TT. So these n+1n + 1 words reach n+1n + 1 different states, and none of them is dead. □\square

Cost. BUILD copies nn rows of σ\sigma entries: Θ(nσ)\Theta(n \sigma). CONTAINS does at most kk iterations with one lookup each. COUNT-DISTINCT reads each of the (n+1)σ(n + 1) \sigma entries once.

Back to Subsequence automaton