Proof of Kleene's theorem full
This proof shows: statement.
We prove 1⇒2⇒3⇒1 for a fixed finite alphabet Σ.
Paths in an epsilon-NFA. Let N=(Q,Σ,δ,q0,F) with δ:Q×(Σ∪{ε})→2Q. A path from p to r is a sequence p=p0,p1,…,pm=r with labels x1,…,xm∈Σ∪{ε} such that pt∈δ(pt−1,xt) for each t. Its label is the string x1x2⋯xm, in which each ε adds nothing. We write p⇝wr when some path from p to r has the label w; the path with m=0 gives p⇝εp. The language is L(N)={w:q0⇝wf for some f∈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∣ of an expression is its number of occurrences of ∅, ε, symbols and operators.
Lemma 1. For each regular expression E there is an epsilon-NFA NE with L(NE)=L(E) and at most 2∣E∣ states, which has the shape: (a) one accepting state f, different from the start state s; (b) no transition enters s; (c) no transition leaves f.
Proof. By structural induction on E. In each case the automata of the subexpressions have disjoint sets of states, and we keep all their transitions.
Base cases. For ∅: states s,f and no transition. For ε: the one transition f∈δ(s,ε). For a∈Σ: the one transition f∈δ(s,a). In each case the only paths from s to f are the ones with zero transitions (none, since s=f) or the one transition, so the languages are ∅, {ε} and {a}. Each has 2 states and the shape.
Union E=E1+E2. Let N1 and N2 have the start states s1,s2 and the accepting states f1,f2. Add new states s,f and the ε-transitions s→s1, s→s2, f1→f, f2→f. A path from s to f starts with s→s1 or s→s2; say s→s1. After it, the path uses transitions of N1 until it takes a transition that is not in N1. The only such transition from a state of N1 is f1→f, because no transition leads from N1 to N2 and none enters s. So the path is s→s1, then a path of N1 from s1 to f1, then f1→f, and its label is in L(N1)=L(E1). In the same way a path through s2 has its label in L(E2). Conversely each such combination is a path. So L(NE)=L(E1)∪L(E2). The new transitions leave s and enter f, so the shape holds. The number of states is n1+n2+2≤2∣E1∣+2∣E2∣+2=2∣E∣.
Concatenation E=E1E2. Add the ε-transition f1→s2; the start state is s1 and the accepting state is f2. A path from s1 to f2 must reach a state of N2, and the only transition from a state of N1 to a state of N2 is f1→s2. No transition leads back from N2 to N1. So the path is a path of N1 from s1 to f1, then f1→s2, then a path of N2 from s2 to f2, and its label is xy with x∈L(E1) and y∈L(E2). Conversely each such x and y give a path. So L(NE)=L(E1)L(E2). No transition enters s1 (the new one enters s2) and none leaves f2. The number of states is n1+n2≤2∣E∣.
Star E=E1∗. Add new states s,f and the ε-transitions s→s1, s→f, f1→s1, f1→f. A path from s to f is either s→f, with label ε, or starts with s→s1. In the second case, cut the path at each use of a new transition. Between two cuts the path uses only transitions of N1, starts at s1 (each new transition that the path takes before the end enters s1), and ends at a state that has a new outgoing transition, which is f1. So the path is s→s1, then k≥1 paths of N1 from s1 to f1 joined by f1→s1, then f1→f. Its label is x1⋯xk with each xi∈L(E1). Conversely ε and each such product are labels of paths. So L(NE)={ε}∪⋃k≥1L(E1)k=L(E1)∗. The new transitions enter s1 and f and leave s and f1, so the shape holds. The number of states is n1+2≤2∣E∣. □
The expression (E) with redundant brackets needs no case. This proves 1⇒2.
Step 2: from epsilon-NFAs to DFAs
For S⊆Q let Cl(S)={r:p⇝εr for some p∈S}, the epsilon-closure of S. Define the DFA D=(2Q,Σ,Δ,S0,FD) with S0=Cl({q0}), Δ(S,a)=Cl(⋃p∈Sδ(p,a)) and FD={S:S∩F=∅}. It is complete, since Δ is defined for each S and a, including S=∅.
Lemma 2. For each w∈Σ∗, Δ∗(S0,w)=R(w), where R(w)={r:q0⇝wr}.
Proof. By induction on ∣w∣. For w=ε, R(ε)=Cl({q0})=S0=Δ∗(S0,ε). For w=va with a∈Σ, take a path from q0 to r with the label va. Exactly ∣va∣ of its transitions carry a symbol, and the last of them carries a. Cut the path before and after this transition. The first part is a path from q0 to some p with label v, the transition goes from p to some p′∈δ(p,a), and the last part is a path from p′ to r with label ε. So r∈R(va) if and only if there are p∈R(v) and p′∈δ(p,a) with p′⇝εr, that is, if and only if r∈Δ(R(v),a). By the induction hypothesis R(v)=Δ∗(S0,v), so R(va)=Δ(Δ∗(S0,v),a)=Δ∗(S0,va). □
So w∈L(D) if and only if R(w)∩F=∅, if and only if w∈L(N). The DFA has 2∣Q∣ states; keeping only the subsets reachable from S0 does not change the language. This proves 2⇒3.
Step 3: from DFAs to expressions
Let M=(Q,Σ,δ,q0,F) be a complete DFA with Q={1,…,n}. For a string w=a1⋯am and a state i, the run from i is r0=i, rt=δ(rt−1,at). Its inner states are r1,…,rm−1; the first and the last state do not count. For 0≤k≤n define
Rij(k)={w∈Σ∗:δ∗(i,w)=j and every inner state of the run of w from i is at most k}. Lemma 3. For all i,j and k, the set Rij(k) is the language of a regular expression Eij(k).
Proof. By induction on k.
Base k=0. No state is at most 0, so the run has no inner state, and m≤1. With m=0 the string is ε, and it lies in the set exactly when i=j. With m=1 the string is a symbol a with δ(i,a)=j. So Rij(0) is the finite set {a∈Σ:δ(i,a)=j}, plus ε when i=j. Its expression is the sum of these symbols (and ε), or ∅ when the set is empty.
Step k≥1. We show
Rij(k)=Rij(k−1)∪Rik(k−1)(Rkk(k−1))∗Rkj(k−1). Let w∈Rij(k). If no inner state of its run equals k, then each inner state is at most k−1, and w∈Rij(k−1). Otherwise let t1<t2<⋯<tp be the inner positions with rt=k, where p≥1. Cut w at these positions: w=xy1⋯yp−1z, where x=a1⋯at1, yl=atl+1⋯atl+1 and z=atp+1⋯am. The piece x leads from i to k, and its inner states are inner states of w before t1, so they are at most k and not equal to k: x∈Rik(k−1). In the same way each yl∈Rkk(k−1) and z∈Rkj(k−1). So w is in the right side.
Conversely, the set Rij(k−1) is a subset of Rij(k). Let w=xy1⋯yqz with x∈Rik(k−1), each yl∈Rkk(k−1) and z∈Rkj(k−1), where q≥0. By the lemma δ∗(i,uv)=δ∗(δ∗(i,u),v) of the DFA page, the run of w from i is the run of x, which ends in k, followed by the runs of the yl from k to k and the run of z from k to j. So δ∗(i,w)=j. Each inner state of w is an inner state of a piece, which is at most k−1, or a joint between two pieces, which is k. So w∈Rij(k).
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) is an expression for Rij(k). □
Each inner state is at most n, so Rij(n)={w:δ∗(i,w)=j}. Hence
L(M)=j∈F⋃Rq0j(n), and the sum of the expressions Eq0j(n) over j∈F, or ∅ if F=∅, is an expression for L(M). This proves 3⇒1 and the theorem. ■