Algorithm Search⌘K

Proofs: L* algorithm

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

L* learns the minimal DFA with at most n equivalence queries full

This proof shows: correctness, termination, complexity.

Let L⊆Σ∗L \subseteq \Sigma^* be the target, and let nn be the number of states of its minimal complete DFA, which is also the number of classes of the Myhill-Nerode relation ≡L\equiv_L (x≡Lyx \equiv_L y when xz∈L⇔yz∈Lxz \in L \Leftrightarrow yz \in L for all zz). DFAs are complete and have no ε\varepsilon-moves. For a DFA MM with transition function δ\delta we use δ∗(q,xy)=δ∗(δ∗(q,x),y)\delta^*(q, xy) = \delta^*(\delta^*(q, x), y). A DFA MM agrees with the table (S,E,T)(S, E, T) if for all s∈S∪SΣs \in S \cup S\Sigma and e∈Ee \in E, MM accepts sese if and only if T(se)=1T(se) = 1. The target's minimal DFA agrees with every table that L* builds, since TT holds true membership answers.

Throughout, SS is prefix-closed and EE is suffix-closed, and ε∈S\varepsilon \in S, ε∈E\varepsilon \in E. This holds after line L1, and lines L4, L6 and L10 keep it: line L4 adds aeae with e∈Ee \in E, whose proper suffixes are suffixes of ee and so in EE; line L6 adds sasa with s∈Ss \in S; line L10 adds all prefixes of cc. Let k(S,E)k(S, E) be the number of distinct rows row(s)\mathrm{row}(s) with s∈Ss \in S.

Part 1: the hypothesis agrees with the table

Let the table be closed and consistent, and let H=H(S,E,T)H = H(S, E, T) with transition function δ\delta, start q0=row(ε)q_0 = \mathrm{row}(\varepsilon) and accepting set FF. As noted on the page, δ\delta and FF are well defined.

Lemma 1. For every s∈S∪SΣs \in S \cup S\Sigma, δ∗(q0,s)=row(s)\delta^*(q_0, s) = \mathrm{row}(s).

Proof. By induction on ∣s∣|s|. For s=εs = \varepsilon it is the definition of q0q_0. Let s=s′as = s'a with a∈Σa \in \Sigma. If s∈Ss \in S, then s′∈Ss' \in S since SS is prefix-closed. If s∈SΣs \in S\Sigma, then s′s' is the element of SS that was extended. In both cases s′∈Ss' \in S, so by the induction hypothesis δ∗(q0,s′a)=δ(row(s′),a)=row(s′a)\delta^*(q_0, s'a) = \delta(\mathrm{row}(s'), a) = \mathrm{row}(s'a) by the definition of δ\delta. □\square

Lemma 2. For every s∈S∪SΣs \in S \cup S\Sigma and e∈Ee \in E, δ∗(q0,se)∈F\delta^*(q_0, se) \in F if and only if T(se)=1T(se) = 1.

Proof. By induction on ∣e∣|e|. If e=εe = \varepsilon: by Lemma 1, δ∗(q0,s)=row(s)\delta^*(q_0, s) = \mathrm{row}(s). By closedness (or trivially if s∈Ss \in S) there is s′∈Ss' \in S with row(s′)=row(s)\mathrm{row}(s') = \mathrm{row}(s), so row(s)∈F\mathrm{row}(s) \in F if and only if T(s′)=1T(s') = 1, and T(s′)=T(s)T(s') = T(s) because the two rows agree on the column ε\varepsilon.

If e=ae′e = ae' with a∈Σa \in \Sigma: then e′∈Ee' \in E since EE is suffix-closed. Pick s′∈Ss' \in S with row(s′)=row(s)\mathrm{row}(s') = \mathrm{row}(s). Then

δ∗(q0,sae′)=δ∗(δ(row(s′),a),e′)=δ∗(row(s′a),e′)=δ∗(q0,s′ae′),\delta^*(q_0, s a e') = \delta^*(\delta(\mathrm{row}(s'), a), e') = \delta^*(\mathrm{row}(s'a), e') = \delta^*(q_0, s'ae'),

using Lemma 1 for ss and for s′a∈SΣs'a \in S\Sigma. By the induction hypothesis for s′as'a and e′e', this state is in FF if and only if T(s′ae′)=1T(s'ae') = 1. Finally T(s′ae′)=T(sae′)T(s'ae') = T(sae'), because row(s′)=row(s)\mathrm{row}(s') = \mathrm{row}(s) and ae′=e∈Eae' = e \in E. □\square

So HH agrees with the table.

Part 2: uniqueness of the hypothesis

Lemma 3. Let the table be closed and consistent, let k=k(S,E)k = k(S, E), and let M=(Q′,Σ,δ′,q0′,F′)M = (Q', \Sigma, \delta', q_0', F') be a DFA that agrees with the table and has at most kk states. Then MM is isomorphic to H(S,E,T)H(S, E, T).

Proof. For q∈Q′q \in Q' let ρ(q):E→{0,1}\rho(q) : E \to \{0, 1\} map ee to 1 if and only if δ′∗(q,e)∈F′\delta'^*(q, e) \in F'. Because MM agrees with the table, ρ(δ′∗(q0′,s))=row(s)\rho(\delta'^*(q_0', s)) = \mathrm{row}(s) for all s∈S∪SΣs \in S \cup S\Sigma. The strings of SS have kk distinct rows, so the states δ′∗(q0′,s)\delta'^*(q_0', s) with s∈Ss \in S have at least kk distinct images under ρ\rho, and so are at least kk distinct states. As ∣Q′∣≤k|Q'| \le k, these are all the states of MM, ∣Q′∣=k|Q'| = k, and ρ\rho maps Q′Q' bijectively onto the set of rows of SS. Hence a state of MM is determined by its ρ\rho value.

Define φ(row(s))=δ′∗(q0′,s)\varphi(\mathrm{row}(s)) = \delta'^*(q_0', s) for s∈Ss \in S. It is well defined: if row(s1)=row(s2)\mathrm{row}(s_1) = \mathrm{row}(s_2), the two states have the same ρ\rho value and so are equal. It is a bijection from the states of HH onto Q′Q' by the previous paragraph. It maps start to start, since φ(row(ε))=q0′\varphi(\mathrm{row}(\varepsilon)) = q_0'. It preserves transitions: for s∈Ss \in S and a∈Σa \in \Sigma, closedness gives s′′∈Ss'' \in S with row(s′′)=row(sa)\mathrm{row}(s'') = \mathrm{row}(sa), and the states δ′(φ(row(s)),a)=δ′∗(q0′,sa)\delta'(\varphi(\mathrm{row}(s)), a) = \delta'^*(q_0', sa) and φ(row(sa))=δ′∗(q0′,s′′)\varphi(\mathrm{row}(sa)) = \delta'^*(q_0', s'') have the same ρ\rho value row(sa)\mathrm{row}(sa), so they are equal. It preserves acceptance: φ(row(s))∈F′\varphi(\mathrm{row}(s)) \in F' if and only if T(s)=1T(s) = 1 (agreement with e=εe = \varepsilon), if and only if row(s)∈F\mathrm{row}(s) \in F. □\square

Part 3: the number of rows

Lemma 4. k(S,E)≤nk(S, E) \le n at all times, and kk never decreases.

Proof. If row(s1)≠row(s2)\mathrm{row}(s_1) \neq \mathrm{row}(s_2), some e∈Ee \in E has T(s1e)≠T(s2e)T(s_1 e) \neq T(s_2 e), so s1̸≡Ls2s_1 \not\equiv_L s_2. So the access strings with distinct rows lie in distinct classes of ≡L\equiv_L, of which there are nn. Adding a string to SS adds a row and changes no other row. Adding a column to EE extends every row by one entry, so rows that differed still differ. Hence kk never decreases. □\square

Lemma 5. Each execution of line L4 or line L6 increases kk by at least one.

Proof. Line L4 adds aeae where row(s1)=row(s2)\mathrm{row}(s_1) = \mathrm{row}(s_2) and T(s1ae)≠T(s2ae)T(s_1 ae) \neq T(s_2 ae); after the addition the rows of s1s_1 and s2s_2 differ in the new column, and by Lemma 4 no other pair of distinct rows merges, so kk grows. Line L6 adds sasa, whose row differs from every row of SS; the new column set is the same, so kk grows by one. □\square

Part 4: counterexamples and termination

Lemma 6. Let HH be a hypothesis with kk states that receives the counterexample cc, and let H′H' be the next hypothesis. Then H′H' has more than kk states.

Proof. Let (S,E,T)(S, E, T) be the table that gave HH and (S′,E′,T′)(S', E', T') the table that gives H′H'. Then S⊆S′S \subseteq S', E⊆E′E \subseteq E', and T′T' extends TT. H′H' agrees with (S′,E′,T′)(S', E', T') by Part 1, and so it also agrees with (S,E,T)(S, E, T), whose strings are among those of the new table. The number of states of H′H' is k(S′,E′)≥k(S,E)=kk(S', E') \ge k(S, E) = k by Lemma 4. Suppose that it is exactly kk. Then H′H' agrees with (S,E,T)(S, E, T) and has at most kk states, so it is isomorphic to HH by Lemma 3, and L(H′)=L(H)L(H') = L(H). But c∈S′c \in S' after line L10 and ε∈E′\varepsilon \in E', so H′H' accepts cc if and only if T′(c)=1T'(c) = 1, that is, if and only if c∈Lc \in L; and HH accepts cc if and only if c∉Lc \notin L. This contradicts L(H′)=L(H)L(H') = L(H). □\square

Theorem. L* stops after at most nn equivalence queries and at most n−1n - 1 executions of lines L4 and L6 in all, and returns the minimal DFA of LL.

Proof. By Lemmas 4 and 5, kk starts at 1 and is at most nn, and each execution of L4 or L6 increases it, so there are at most n−1n - 1 such executions in the whole run. Between two consecutive equivalence queries, the loop therefore makes finitely many passes, each of which either adds a column, adds a row, or reaches line L7 with a closed and consistent table. The hypotheses have k≤nk \le n states by Lemma 4, and by Lemma 6 their sizes strictly increase. Since the first has at least one state, there are at most nn hypotheses, so at most nn equivalence queries, and the last one is answered "yes" (the loop cannot run forever without asking another one, by the bound on L4 and L6). The returned HH satisfies L(H)=LL(H) = L and has at most nn states, so it has exactly nn states and, by the uniqueness of the minimal DFA up to isomorphism, it is the minimal DFA. □\square

Part 5: cost

Let mm be the length of the longest counterexample. By Lemma 4 and Lemma 5, ∣E∣≤1+(n−1)=n|E| \le 1 + (n - 1) = n. SS gains at most n−1n - 1 strings at line L6 and at most mm new strings at each of at most n−1n - 1 executions of line L10 (the prefix ε\varepsilon is already present), so ∣S∣≤n+m(n−1)|S| \le n + m(n - 1). The table has ∣S∪SΣ∣⋅∣E∣≤(σ+1)(n+m(n−1))n|S \cup S\Sigma| \cdot |E| \le (\sigma + 1)(n + m(n - 1)) n entries, and each membership query fills one new entry, so this bounds the number of membership queries.

The loop runs at most (n−1)+(n−1)+n≤3n(n - 1) + (n - 1) + n \le 3n times. In each pass, the consistency check, the closedness check, the construction of the hypothesis and the filling of new entries read each entry of the table a constant number of times, which is O(σmn2)O(\sigma m n^2) entries, and each read hashes a string of length at most m+2nm + 2n. So a pass takes O(σmn2(m+n))O(\sigma m n^2 (m + n)) time, and the whole run O(σmn3(m+n))O(\sigma m n^3 (m + n)) time besides the teacher's work. The table stores O(σmn2)O(\sigma m n^2) strings of length O(m+n)O(m + n), which is O(σmn2(m+n))O(\sigma m n^2 (m + n)) space. ■\blacksquare

Back to L* algorithm