Algorithm Search⌘K

Proofs: Closure properties of regular languages

These are the human proofs of Closure properties of regular languages, at full length.

Proof of the closure properties of regular languages full

This proof shows: statement.

Fix finite alphabets Σ\Sigma and Δ\Delta. Let L1=L(M1)L_1 = L(M_1) and L2=L(M2)L_2 = L(M_2) with complete DFAs Mi=(Qi,Σ,δi,qi,Fi)M_i = (Q_i, \Sigma, \delta_i, q_i, F_i), where δi:Qi×Σ→Qi\delta_i : Q_i \times \Sigma \to Q_i and there are no ε\varepsilon-moves. We use the lemma δ∗(q,xy)=δ∗(δ∗(q,x),y)\delta^*(q, xy) = \delta^*(\delta^*(q, x), y) of the DFA page, and we use Kleene's theorem: a language is regular if and only if a complete DFA, an NFA, an epsilon-NFA or a regular expression describes it. For an NFA or an epsilon-NFA we write p⇝wrp \overset{w}{\leadsto} r when some path from pp to rr has the label ww, as in the proof of Kleene's theorem.

Part 1: complement

Claim. L1‾=Σ∗∖L1\overline{L_1} = \Sigma^* \setminus L_1 is regular.

Proof. Let M′=(Q1,Σ,δ1,q1,Q1∖F1)M' = (Q_1, \Sigma, \delta_1, q_1, Q_1 \setminus F_1). The machines M1M_1 and M′M' have the same transition function, so they reach the same state δ1∗(q1,w)\delta_1^*(q_1, w) on each w∈Σ∗w \in \Sigma^*. This state is defined because δ1\delta_1 is complete. So w∈L(M′)w \in L(M') if and only if δ1∗(q1,w)∉F1\delta_1^*(q_1, w) \notin F_1, if and only if w∉L1w \notin L_1. □\square

The proof uses both determinism and completeness. Each string has exactly one run, so "the run does not end in F1F_1" is the same as "no run ends in F1F_1".

Part 2: the product and the Boolean operations

Let P=(Q1×Q2,Σ,δ,(q1,q2),F)P = (Q_1 \times Q_2, \Sigma, \delta, (q_1, q_2), F) with δ((p,r),a)=(δ1(p,a),δ2(r,a))\delta((p, r), a) = (\delta_1(p, a), \delta_2(r, a)), where FF is chosen below. This is the product construction. It is a complete DFA.

Lemma 1. For all p∈Q1p \in Q_1, r∈Q2r \in Q_2 and w∈Σ∗w \in \Sigma^*, δ∗((p,r),w)=(δ1∗(p,w),δ2∗(r,w))\delta^*((p, r), w) = (\delta_1^*(p, w), \delta_2^*(r, w)).

Proof. By induction on ∣w∣|w|. For w=εw = \varepsilon both sides are (p,r)(p, r). For w=vaw = va, by the induction hypothesis and the definition of δ\delta,

δ∗((p,r),va)=δ(δ∗((p,r),v),a)=δ((δ1∗(p,v),δ2∗(r,v)),a)=(δ1∗(p,va),δ2∗(r,va)).□\delta^*((p, r), va) = \delta(\delta^*((p, r), v), a) = \delta((\delta_1^*(p, v), \delta_2^*(r, v)), a) = (\delta_1^*(p, va), \delta_2^*(r, va)). \quad \square

Claim. L1∩L2L_1 \cap L_2, L1∪L2L_1 \cup L_2, L1∖L2L_1 \setminus L_2 and L1△L2L_1 \triangle L_2 are regular.

Proof. By Lemma 1, w∈L(P)w \in L(P) if and only if (δ1∗(q1,w),δ2∗(q2,w))∈F(\delta_1^*(q_1, w), \delta_2^*(q_2, w)) \in F. Take

  • F=F1×F2F = F_1 \times F_2: then w∈L(P)w \in L(P) if and only if w∈L1w \in L_1 and w∈L2w \in L_2;
  • F=(F1×Q2)∪(Q1×F2)F = (F_1 \times Q_2) \cup (Q_1 \times F_2): then w∈L(P)w \in L(P) if and only if w∈L1w \in L_1 or w∈L2w \in L_2;
  • F=F1×(Q2∖F2)F = F_1 \times (Q_2 \setminus F_2): then w∈L(P)w \in L(P) if and only if w∈L1w \in L_1 and w∉L2w \notin L_2;
  • F=(F1×(Q2∖F2))∪((Q1∖F1)×F2)F = (F_1 \times (Q_2 \setminus F_2)) \cup ((Q_1 \setminus F_1) \times F_2): then w∈L(P)w \in L(P) if and only if exactly one of w∈L1w \in L_1, w∈L2w \in L_2 holds. □\square

The same argument works for each Boolean function β\beta of two arguments, with F={(p,r):β([p∈F1],[r∈F2])}F = \{ (p, r) : \beta([p \in F_1], [r \in F_2]) \}. It also works for kk DFAs at once, with ∏i∣Qi∣\prod_i |Q_i| states. For the union, both DFAs must be complete: if δ1∗(q1,w)\delta_1^*(q_1, w) were undefined, the product would have no state for ww even when w∈L2w \in L_2.

Part 3: concatenation

Claim. L1L2={xy:x∈L1,y∈L2}L_1 L_2 = \{ xy : x \in L_1, y \in L_2 \} is regular.

Proof. Assume Q1∩Q2=∅Q_1 \cap Q_2 = \emptyset. Let NN be the epsilon-NFA with the states Q1∪Q2Q_1 \cup Q_2, the start state q1q_1, the accepting states F2F_2, the transitions δ1(p,a)∈δN(p,a)\delta_1(p, a) \in \delta_N(p, a) for p∈Q1p \in Q_1 and δ2(r,a)∈δN(r,a)\delta_2(r, a) \in \delta_N(r, a) for r∈Q2r \in Q_2, and one ε\varepsilon-move f→q2f \to q_2 for each f∈F1f \in F_1.

Let w∈L(N)w \in L(N), with a path from q1q_1 to some g∈F2g \in F_2. The path starts in Q1Q_1 and ends in Q2Q_2. The only transitions from a state of Q1Q_1 to a state of Q2Q_2 are the ε\varepsilon-moves f→q2f \to q_2 with f∈F1f \in F_1, and no transition leads from Q2Q_2 to Q1Q_1. So the path uses exactly one of these moves. Before it, the path follows δ1\delta_1 from q1q_1 to ff with some label xx, so δ1∗(q1,x)=f∈F1\delta_1^*(q_1, x) = f \in F_1 and x∈L1x \in L_1. After it, the path follows δ2\delta_2 from q2q_2 to gg with some label yy, so y∈L2y \in L_2. Hence w=xy∈L1L2w = xy \in L_1 L_2.

Conversely, let x∈L1x \in L_1 and y∈L2y \in L_2. The run of M1M_1 on xx is a path of NN from q1q_1 to f=δ1∗(q1,x)∈F1f = \delta_1^*(q_1, x) \in F_1. Follow it by the move f→q2f \to q_2 and by the run of M2M_2 on yy, which ends in F2F_2. This is a path with the label xyxy, so xy∈L(N)xy \in L(N). By Kleene's theorem L(N)L(N) is regular. □\square

Part 4: Kleene star

Claim. L1∗=⋃k≥0L1kL_1^* = \bigcup_{k \ge 0} L_1^k is regular.

Proof. Let s∉Q1s \notin Q_1 be a new state. Let NN be the epsilon-NFA with the states Q1∪{s}Q_1 \cup \{s\}, the start state ss, the accepting states {s}\{s\}, the transitions of M1M_1, an ε\varepsilon-move s→q1s \to q_1, and an ε\varepsilon-move f→sf \to s for each f∈F1f \in F_1.

Let w∈L(N)w \in L(N), with a path from ss to ss. Cut the path at each visit to ss. The path with zero transitions gives w=ε∈L10w = \varepsilon \in L_1^0. Otherwise each piece goes from ss to ss without visiting ss in between. The only move out of ss is s→q1s \to q_1, and the only moves into ss are f→sf \to s with f∈F1f \in F_1. So a piece is s→q1s \to q_1, then a path that follows δ1\delta_1 from q1q_1 to some f∈F1f \in F_1 with some label xjx_j, then f→sf \to s. Hence δ1∗(q1,xj)∈F1\delta_1^*(q_1, x_j) \in F_1, each xj∈L1x_j \in L_1, and w=x1⋯xk∈L1kw = x_1 \cdots x_k \in L_1^k.

Conversely, ε∈L(N)\varepsilon \in L(N) by the path with zero transitions. For x1,…,xk∈L1x_1, \ldots, x_k \in L_1 with k≥1k \ge 1, join the pieces s→q1s \to q_1, the run of M1M_1 on xjx_j, and the move from its last state, which is in F1F_1, to ss. This is a path from ss to ss with the label x1⋯xkx_1 \cdots x_k. So L(N)=L1∗L(N) = L_1^*, and it is regular by Kleene's theorem. □\square

The new state ss is necessary. If we made q1q_1 accepting and added ε\varepsilon-moves f→q1f \to q_1 instead, a path could return to q1q_1 along a transition of M1M_1 and stop there, in the middle of a word of L1L_1.

Part 5: reversal

Claim. L1R={wR:w∈L1}L_1^R = \{ w^R : w \in L_1 \} is regular, where (a1⋯am)R=am⋯a1(a_1 \cdots a_m)^R = a_m \cdots a_1.

Proof. Let NN be the epsilon-NFA with the states Q1∪{s}Q_1 \cup \{s\}, where ss is new, the start state ss, the accepting states {q1}\{q_1\}, an ε\varepsilon-move s→fs \to f for each f∈F1f \in F_1, and a transition p∈δN(r,a)p \in \delta_N(r, a) for each transition δ1(p,a)=r\delta_1(p, a) = r of M1M_1. So the arrows of M1M_1 are reversed.

A sequence r0,r1,…,rmr_0, r_1, \ldots, r_m with rt=δ1(rt−1,at)r_t = \delta_1(r_{t-1}, a_t) is a path of M1M_1 with the label a1⋯ama_1 \cdots a_m if and only if rm,rm−1,…,r0r_m, r_{m-1}, \ldots, r_0 is a path of the reversed transitions with the label am⋯a1a_m \cdots a_1. So w∈L1w \in L_1, which means that some path of M1M_1 leads from q1q_1 to some f∈F1f \in F_1 with the label ww, holds if and only if the path s→fs \to f followed by a reversed path from ff to q1q_1 with the label wRw^R exists, which means wR∈L(N)w^R \in L(N). So L(N)=L1RL(N) = L_1^R. □\square

The reversed machine is nondeterministic: a state can have several incoming transitions on one symbol in M1M_1, and F1F_1 can have several states.

Part 6: homomorphism

A homomorphism is a map h:Σ∗→Δ∗h : \Sigma^* \to \Delta^* with h(xy)=h(x)h(y)h(xy) = h(x)h(y) for all x,yx, y. It is fixed by the strings h(a)h(a) for a∈Σa \in \Sigma, since h(a1⋯am)=h(a1)⋯h(am)h(a_1 \cdots a_m) = h(a_1) \cdots h(a_m) and h(ε)=εh(\varepsilon) = \varepsilon. For a language LL, h(L)={h(w):w∈L}h(L) = \{ h(w) : w \in L \}.

Claim. If L⊆Σ∗L \subseteq \Sigma^* is regular, then h(L)⊆Δ∗h(L) \subseteq \Delta^* is regular.

Proof. By Kleene's theorem L=L(E)L = L(E) for a regular expression EE over Σ\Sigma. Let h(E)h(E) be the expression over Δ\Delta obtained by replacing each occurrence of a symbol aa with an expression for the one string h(a)h(a): the concatenation of its symbols, or ε\varepsilon if h(a)=εh(a) = \varepsilon. We show L(h(E))=h(L(E))L(h(E)) = h(L(E)) by structural induction on EE.

  • E=∅E = \emptyset: h(∅)=∅h(\emptyset) = \emptyset. E=εE = \varepsilon: h({ε})={ε}h(\{\varepsilon\}) = \{\varepsilon\}. E=aE = a: L(h(a))={h(a)}=h({a})L(h(a)) = \{h(a)\} = h(\{a\}).
  • E=E1+E2E = E_1 + E_2: h(A∪B)=h(A)∪h(B)h(A \cup B) = h(A) \cup h(B) for all languages A,BA, B, by the definition of the image.
  • E=E1E2E = E_1 E_2: h(AB)={h(xy):x∈A,y∈B}={h(x)h(y)}=h(A)h(B)h(AB) = \{ h(xy) : x \in A, y \in B \} = \{ h(x)h(y) \} = h(A)h(B).
  • E=E1∗E = E_1^*: h(Ak)=h(A)kh(A^k) = h(A)^k for each kk, by the previous case and induction on kk, and so h(A∗)=⋃kh(Ak)=⋃kh(A)k=h(A)∗h(A^*) = \bigcup_k h(A^k) = \bigcup_k h(A)^k = h(A)^*.

In each case the induction hypothesis gives the expressions for the parts. So h(L)=L(h(E))h(L) = L(h(E)) is regular. □\square

Part 7: inverse homomorphism

For L⊆Δ∗L \subseteq \Delta^*, let h−1(L)={w∈Σ∗:h(w)∈L}h^{-1}(L) = \{ w \in \Sigma^* : h(w) \in L \}.

Claim. If L⊆Δ∗L \subseteq \Delta^* is regular, then h−1(L)⊆Σ∗h^{-1}(L) \subseteq \Sigma^* is regular.

Proof. Let M=(Q,Δ,δ,q0,F)M = (Q, \Delta, \delta, q_0, F) be a complete DFA for LL. Define M′=(Q,Σ,δ′,q0,F)M' = (Q, \Sigma, \delta', q_0, F) with δ′(q,a)=δ∗(q,h(a))\delta'(q, a) = \delta^*(q, h(a)) for a∈Σa \in \Sigma. It is a complete DFA over Σ\Sigma, since δ∗\delta^* is defined for each string.

We show δ′∗(q,w)=δ∗(q,h(w))\delta'^*(q, w) = \delta^*(q, h(w)) by induction on ∣w∣|w|. For w=εw = \varepsilon both sides are qq. For w=vaw = va, by the induction hypothesis and the lemma δ∗(q,xy)=δ∗(δ∗(q,x),y)\delta^*(q, xy) = \delta^*(\delta^*(q, x), y),

δ′∗(q,va)=δ′(δ∗(q,h(v)),a)=δ∗(δ∗(q,h(v)),h(a))=δ∗(q,h(v)h(a))=δ∗(q,h(va)).\delta'^*(q, va) = \delta'(\delta^*(q, h(v)), a) = \delta^*(\delta^*(q, h(v)), h(a)) = \delta^*(q, h(v)h(a)) = \delta^*(q, h(va)).

So w∈L(M′)w \in L(M') if and only if δ∗(q0,h(w))∈F\delta^*(q_0, h(w)) \in F, if and only if h(w)∈Lh(w) \in L. □\square

Part 8: right quotient

For languages LL and KK over Σ\Sigma, let L/K={x:xy∈L for some y∈K}L / K = \{ x : xy \in L \text{ for some } y \in K \}.

Claim. If LL is regular, then L/KL / K is regular for each language KK, regular or not.

Proof. Let M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) be a complete DFA for LL, and let F′={q∈Q:δ∗(q,y)∈F for some y∈K}F' = \{ q \in Q : \delta^*(q, y) \in F \text{ for some } y \in K \}. By the lemma δ∗(q0,xy)=δ∗(δ∗(q0,x),y)\delta^*(q_0, xy) = \delta^*(\delta^*(q_0, x), y), the DFA (Q,Σ,δ,q0,F′)(Q, \Sigma, \delta, q_0, F') accepts xx if and only if some y∈Ky \in K has xy∈Lxy \in L. □\square

The set F′F' exists for every KK, but a program can compute it only when it can decide, for each qq, whether KK meets {y:δ∗(q,y)∈F}\{ y : \delta^*(q, y) \in F \}. For a regular KK this is an emptiness test of a product.

All eight parts together prove the theorem. ■\blacksquare

Back to Closure properties of regular languages