Algorithm Search⌘K

Proofs: Kleene's theorem

These are the human proofs of Kleene's theorem, at full length.

Proof of Kleene's theorem full

This proof shows: statement.

We prove 1⇒2⇒3⇒11 \Rightarrow 2 \Rightarrow 3 \Rightarrow 1 for a fixed finite alphabet Σ\Sigma.

Paths in an epsilon-NFA. Let N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F) with δ:Q×(Σ∪{ε})→2Q\delta : Q \times (\Sigma \cup \{\varepsilon\}) \to 2^Q. A path from pp to rr is a sequence p=p0,p1,…,pm=rp = p_0, p_1, \ldots, p_m = r with labels x1,…,xm∈Σ∪{ε}x_1, \ldots, x_m \in \Sigma \cup \{\varepsilon\} such that pt∈δ(pt−1,xt)p_t \in \delta(p_{t-1}, x_t) for each tt. Its label is the string x1x2⋯xmx_1 x_2 \cdots x_m, in which each ε\varepsilon adds nothing. We write p⇝wrp \overset{w}{\leadsto} r when some path from pp to rr has the label ww; the path with m=0m = 0 gives p⇝εpp \overset{\varepsilon}{\leadsto} p. The language is L(N)={w:q0⇝wf for some f∈F}L(N) = \{ w : q_0 \overset{w}{\leadsto} f \text{ for some } f \in F \}. The epsilon-NFA page shows that this is the same as the definition by the extended transition function.

Step 1: from expressions to epsilon-NFAs

The size ∣E∣|E| of an expression is its number of occurrences of ∅\emptyset, ε\varepsilon, symbols and operators.

Lemma 1. For each regular expression EE there is an epsilon-NFA NEN_E with L(NE)=L(E)L(N_E) = L(E) and at most 2∣E∣2|E| states, which has the shape: (a) one accepting state ff, different from the start state ss; (b) no transition enters ss; (c) no transition leaves ff.

Proof. By structural induction on EE. In each case the automata of the subexpressions have disjoint sets of states, and we keep all their transitions.

Base cases. For ∅\emptyset: states s,fs, f and no transition. For ε\varepsilon: the one transition f∈δ(s,ε)f \in \delta(s, \varepsilon). For a∈Σa \in \Sigma: the one transition f∈δ(s,a)f \in \delta(s, a). In each case the only paths from ss to ff are the ones with zero transitions (none, since s≠fs \ne f) or the one transition, so the languages are ∅\emptyset, {ε}\{\varepsilon\} and {a}\{a\}. Each has 2 states and the shape.

Union E=E1+E2E = E_1 + E_2. Let N1N_1 and N2N_2 have the start states s1,s2s_1, s_2 and the accepting states f1,f2f_1, f_2. Add new states s,fs, f and the ε\varepsilon-transitions s→s1s \to s_1, s→s2s \to s_2, f1→ff_1 \to f, f2→ff_2 \to f. A path from ss to ff starts with s→s1s \to s_1 or s→s2s \to s_2; say s→s1s \to s_1. After it, the path uses transitions of N1N_1 until it takes a transition that is not in N1N_1. The only such transition from a state of N1N_1 is f1→ff_1 \to f, because no transition leads from N1N_1 to N2N_2 and none enters ss. So the path is s→s1s \to s_1, then a path of N1N_1 from s1s_1 to f1f_1, then f1→ff_1 \to f, and its label is in L(N1)=L(E1)L(N_1) = L(E_1). In the same way a path through s2s_2 has its label in L(E2)L(E_2). Conversely each such combination is a path. So L(NE)=L(E1)∪L(E2)L(N_E) = L(E_1) \cup L(E_2). The new transitions leave ss and enter ff, so the shape holds. The number of states is n1+n2+2≤2∣E1∣+2∣E2∣+2=2∣E∣n_1 + n_2 + 2 \le 2|E_1| + 2|E_2| + 2 = 2|E|.

Concatenation E=E1E2E = E_1 E_2. Add the ε\varepsilon-transition f1→s2f_1 \to s_2; the start state is s1s_1 and the accepting state is f2f_2. A path from s1s_1 to f2f_2 must reach a state of N2N_2, and the only transition from a state of N1N_1 to a state of N2N_2 is f1→s2f_1 \to s_2. No transition leads back from N2N_2 to N1N_1. So the path is a path of N1N_1 from s1s_1 to f1f_1, then f1→s2f_1 \to s_2, then a path of N2N_2 from s2s_2 to f2f_2, and its label is xyxy with x∈L(E1)x \in L(E_1) and y∈L(E2)y \in L(E_2). Conversely each such xx and yy give a path. So L(NE)=L(E1)L(E2)L(N_E) = L(E_1)L(E_2). No transition enters s1s_1 (the new one enters s2s_2) and none leaves f2f_2. The number of states is n1+n2≤2∣E∣n_1 + n_2 \le 2|E|.

Star E=E1∗E = E_1^*. Add new states s,fs, f and the ε\varepsilon-transitions s→s1s \to s_1, s→fs \to f, f1→s1f_1 \to s_1, f1→ff_1 \to f. A path from ss to ff is either s→fs \to f, with label ε\varepsilon, or starts with s→s1s \to s_1. In the second case, cut the path at each use of a new transition. Between two cuts the path uses only transitions of N1N_1, starts at s1s_1 (each new transition that the path takes before the end enters s1s_1), and ends at a state that has a new outgoing transition, which is f1f_1. So the path is s→s1s \to s_1, then k≥1k \ge 1 paths of N1N_1 from s1s_1 to f1f_1 joined by f1→s1f_1 \to s_1, then f1→ff_1 \to f. Its label is x1⋯xkx_1 \cdots x_k with each xi∈L(E1)x_i \in L(E_1). Conversely ε\varepsilon and each such product are labels of paths. So L(NE)={ε}∪⋃k≥1L(E1)k=L(E1)∗L(N_E) = \{\varepsilon\} \cup \bigcup_{k \ge 1} L(E_1)^k = L(E_1)^*. The new transitions enter s1s_1 and ff and leave ss and f1f_1, so the shape holds. The number of states is n1+2≤2∣E∣n_1 + 2 \le 2|E|. □\square

The expression (E)(E) with redundant brackets needs no case. This proves 1⇒21 \Rightarrow 2.

Step 2: from epsilon-NFAs to DFAs

For S⊆QS \subseteq Q let Cl(S)={r:p⇝εr for some p∈S}\mathrm{Cl}(S) = \{ r : p \overset{\varepsilon}{\leadsto} r \text{ for some } p \in S \}, the epsilon-closure of SS. Define the DFA D=(2Q,Σ,Δ,S0,FD)D = (2^Q, \Sigma, \Delta, S_0, F_D) with S0=Cl({q0})S_0 = \mathrm{Cl}(\{q_0\}), Δ(S,a)=Cl(⋃p∈Sδ(p,a))\Delta(S, a) = \mathrm{Cl}\big(\bigcup_{p \in S} \delta(p, a)\big) and FD={S:S∩F≠∅}F_D = \{ S : S \cap F \neq \emptyset \}. It is complete, since Δ\Delta is defined for each SS and aa, including S=∅S = \emptyset.

Lemma 2. For each w∈Σ∗w \in \Sigma^*, Δ∗(S0,w)=R(w)\Delta^*(S_0, w) = R(w), where R(w)={r:q0⇝wr}R(w) = \{ r : q_0 \overset{w}{\leadsto} r \}.

Proof. By induction on ∣w∣|w|. For w=εw = \varepsilon, R(ε)=Cl({q0})=S0=Δ∗(S0,ε)R(\varepsilon) = \mathrm{Cl}(\{q_0\}) = S_0 = \Delta^*(S_0, \varepsilon). For w=vaw = va with a∈Σa \in \Sigma, take a path from q0q_0 to rr with the label vava. Exactly ∣va∣|va| of its transitions carry a symbol, and the last of them carries aa. Cut the path before and after this transition. The first part is a path from q0q_0 to some pp with label vv, the transition goes from pp to some p′∈δ(p,a)p' \in \delta(p, a), and the last part is a path from p′p' to rr with label ε\varepsilon. So r∈R(va)r \in R(va) if and only if there are p∈R(v)p \in R(v) and p′∈δ(p,a)p' \in \delta(p, a) with p′⇝εrp' \overset{\varepsilon}{\leadsto} r, that is, if and only if r∈Δ(R(v),a)r \in \Delta(R(v), a). By the induction hypothesis R(v)=Δ∗(S0,v)R(v) = \Delta^*(S_0, v), so R(va)=Δ(Δ∗(S0,v),a)=Δ∗(S0,va)R(va) = \Delta(\Delta^*(S_0, v), a) = \Delta^*(S_0, va). □\square

So w∈L(D)w \in L(D) if and only if R(w)∩F≠∅R(w) \cap F \neq \emptyset, if and only if w∈L(N)w \in L(N). The DFA has 2∣Q∣2^{|Q|} states; keeping only the subsets reachable from S0S_0 does not change the language. This proves 2⇒32 \Rightarrow 3.

Step 3: from DFAs to expressions

Let M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) be a complete DFA with Q={1,…,n}Q = \{1, \ldots, n\}. For a string w=a1⋯amw = a_1 \cdots a_m and a state ii, the run from ii is r0=ir_0 = i, rt=δ(rt−1,at)r_t = \delta(r_{t-1}, a_t). Its inner states are r1,…,rm−1r_1, \ldots, r_{m-1}; the first and the last state do not count. For 0≤k≤n0 \le k \le n define

Rij(k)={w∈Σ∗:δ∗(i,w)=j and every inner state of the run of w from i is at most k}.R^{(k)}_{ij} = \{ w \in \Sigma^* : \delta^*(i, w) = j \text{ and every inner state of the run of } w \text{ from } i \text{ is at most } k \}.

Lemma 3. For all i,ji, j and kk, the set Rij(k)R^{(k)}_{ij} is the language of a regular expression Eij(k)E^{(k)}_{ij}.

Proof. By induction on kk.

Base k=0k = 0. No state is at most 0, so the run has no inner state, and m≤1m \le 1. With m=0m = 0 the string is ε\varepsilon, and it lies in the set exactly when i=ji = j. With m=1m = 1 the string is a symbol aa with δ(i,a)=j\delta(i, a) = j. So Rij(0)R^{(0)}_{ij} is the finite set {a∈Σ:δ(i,a)=j}\{ a \in \Sigma : \delta(i, a) = j \}, plus ε\varepsilon when i=ji = j. Its expression is the sum of these symbols (and ε\varepsilon), or ∅\emptyset when the set is empty.

Step k≥1k \ge 1. We show

Rij(k)=Rij(k−1)∪Rik(k−1)(Rkk(k−1))∗Rkj(k−1).R^{(k)}_{ij} = R^{(k-1)}_{ij} \cup R^{(k-1)}_{ik} \big(R^{(k-1)}_{kk}\big)^* R^{(k-1)}_{kj}.

Let w∈Rij(k)w \in R^{(k)}_{ij}. If no inner state of its run equals kk, then each inner state is at most k−1k - 1, and w∈Rij(k−1)w \in R^{(k-1)}_{ij}. Otherwise let t1<t2<⋯<tpt_1 < t_2 < \cdots < t_p be the inner positions with rt=kr_t = k, where p≥1p \ge 1. Cut ww at these positions: w=xy1⋯yp−1zw = x y_1 \cdots y_{p-1} z, where x=a1⋯at1x = a_1 \cdots a_{t_1}, yl=atl+1⋯atl+1y_l = a_{t_l + 1} \cdots a_{t_{l+1}} and z=atp+1⋯amz = a_{t_p + 1} \cdots a_m. The piece xx leads from ii to kk, and its inner states are inner states of ww before t1t_1, so they are at most kk and not equal to kk: x∈Rik(k−1)x \in R^{(k-1)}_{ik}. In the same way each yl∈Rkk(k−1)y_l \in R^{(k-1)}_{kk} and z∈Rkj(k−1)z \in R^{(k-1)}_{kj}. So ww is in the right side.

Conversely, the set Rij(k−1)R^{(k-1)}_{ij} is a subset of Rij(k)R^{(k)}_{ij}. Let w=xy1⋯yqzw = x y_1 \cdots y_q z with x∈Rik(k−1)x \in R^{(k-1)}_{ik}, each yl∈Rkk(k−1)y_l \in R^{(k-1)}_{kk} and z∈Rkj(k−1)z \in R^{(k-1)}_{kj}, where q≥0q \ge 0. By the lemma δ∗(i,uv)=δ∗(δ∗(i,u),v)\delta^*(i, uv) = \delta^*(\delta^*(i, u), v) of the DFA page, the run of ww from ii is the run of xx, which ends in kk, followed by the runs of the yly_l from kk to kk and the run of zz from kk to jj. So δ∗(i,w)=j\delta^*(i, w) = j. Each inner state of ww is an inner state of a piece, which is at most k−1k - 1, or a joint between two pieces, which is kk. So w∈Rij(k)w \in R^{(k)}_{ij}.

By the induction hypothesis the four sets on the right have expressions, so Eij(k)=Eij(k−1)+Eik(k−1)(Ekk(k−1))∗Ekj(k−1)E^{(k)}_{ij} = E^{(k-1)}_{ij} + E^{(k-1)}_{ik} \big(E^{(k-1)}_{kk}\big)^* E^{(k-1)}_{kj} is an expression for Rij(k)R^{(k)}_{ij}. □\square

Each inner state is at most nn, so Rij(n)={w:δ∗(i,w)=j}R^{(n)}_{ij} = \{ w : \delta^*(i, w) = j \}. Hence

L(M)=⋃j∈FRq0j(n),L(M) = \bigcup_{j \in F} R^{(n)}_{q_0 j},

and the sum of the expressions Eq0j(n)E^{(n)}_{q_0 j} over j∈Fj \in F, or ∅\emptyset if F=∅F = \emptyset, is an expression for L(M)L(M). This proves 3⇒13 \Rightarrow 1 and the theorem. ■\blacksquare

Back to Kleene's theorem