Sub topic 10
Closure properties of regular languages
This chapter assumes you can build DFAs and NFAs, apply subset construction, and convert a regular expression to an automaton. It also assumes you know the basic set operations. Here you will see which operations keep a language regular, how each construction works, and how to use closure in GATE questions.
What closure means
A class of languages is closed under an operation if applying the operation to members of the class always gives a member of the class. For regular languages, closure under union means that if L1 and L2 are regular, then L1 ∪ L2 is regular.
Closure is useful in two directions:
- Building. To show a language is regular, write it as a combination of simpler regular languages.
- Refuting. To show a language is not regular, suppose it were regular and apply an operation that yields a language already known to be non-regular.
Closure is proved by construction. For each operation you build an automaton or regular expression for the result.
The operations at a glance
| Operation | Closed? | Standard construction | Column |
|---|---|---|---|
| Union | Yes | Regex `r1 \ | r2`, or product DFA |
| Concatenation | Yes | Join the two NFAs with ε-moves | |
| Kleene star | Yes | New start state with ε-loop back | |
| Complement | Yes | Complete the DFA, swap accepting and non-accepting states | |
| Intersection | Yes | Product construction, or De Morgan's law | |
| Difference | Yes | L1 ∩ complement of L2 | |
| Reversal | Yes | Reverse every edge, swap start and accepting roles | |
| Homomorphism | Yes | Substitute each symbol's image in the regex | |
| Inverse homomorphism | Yes | Run the DFA on the image of each input symbol | |
| Prefix, suffix, quotient by a regular language | Yes | Change which states are start or accepting | |
| Arbitrary union of infinitely many regular languages | No | Every singleton is regular, but not every language is a union of singletons |
Union, concatenation, and star
These three operations are exactly the operators of regular expressions. If r1 and r2 denote L1 and L2, then r1|r2, r1r2, and r1* denote the union, the concatenation, and the star. This proves closure immediately.
The automaton versions use ε-NFAs. Union adds a new start state with ε-edges to both old start states. Concatenation adds ε-edges from every accepting state of the first machine to the start of the second, and the first machine's accepting states stop being accepting. Star adds a new start state that is also accepting, with an ε-edge to the old start, and adds ε-edges from each old accepting state back to the old start. The new accepting start state is what makes the empty string belong to L*.
Complement
Complement is the operation most often done wrong. Take a DFA for L and swap accepting and non-accepting states. Two conditions must hold:
- The DFA must be complete. Every state needs a transition on every symbol. A missing transition is an implicit dead state, and that dead state must become accepting in the complement. Add it explicitly first.
- The machine must be deterministic. Swapping accepting states in an NFA does not complement the language. A string may have one accepting path and one rejecting path. Swapping makes the same string accepted through the other path, so the string stays in the language.
Complementing an NFA means converting to a DFA first. That conversion can cost up to 2^n states, so the complement of an n-state NFA may need exponentially many DFA states in the worst case.
The complement is taken relative to Σ. For L over {a, b}, the complement is Σ − L, and it depends on the alphabet you fix.
Intersection and the product construction
The product construction runs two DFAs in lockstep. Its states are pairs (p, q), where p is a state of the first DFA and q is a state of the second. On symbol c, the pair moves to (δ1(p, c), δ2(q, c)). The start state is (start1, start2). Only the choice of accepting pairs changes between operations:
- Intersection: both components accepting.
- Union: at least one component accepting.
- Difference L1 − L2: first accepting, second not accepting.
The product has at most n1 × n2 states. This bound is tight in general, so intersecting an n-state DFA with an m-state DFA can need n·m states even after minimization.
The code below builds the product for two DFAs over {a, b}. Machine A accepts strings with an even number of a's. Machine B accepts strings ending in b.
from collections import deque
ALPHABET = "ab"
def make_dfa(start, delta, accepting):
return {"start": start, "delta": delta, "accepting": accepting}
A = make_dfa(0, {(0, 'a'): 1, (1, 'a'): 0, (0, 'b'): 0, (1, 'b'): 1}, {0})
B = make_dfa(0, {(0, 'a'): 0, (1, 'a'): 0, (0, 'b'): 1, (1, 'b'): 1}, {1})
def product(m1, m2, combine):
start = (m1["start"], m2["start"])
seen = {start}
queue = deque([start])
delta = {}
while queue:
p, q = queue.popleft()
for c in ALPHABET:
nxt = (m1["delta"][(p, c)], m2["delta"][(q, c)])
delta[((p, q), c)] = nxt
if nxt not in seen:
seen.add(nxt)
queue.append(nxt)
accepting = {(p, q) for (p, q) in seen
if combine(p in m1["accepting"], q in m2["accepting"])}
return make_dfa(start, delta, accepting)
def complement(m):
states = {s for (s, _) in m["delta"]}
return make_dfa(m["start"], m["delta"], states - m["accepting"])
def accepts(m, w):
s = m["start"]
for ch in w:
s = m["delta"][(s, ch)]
return s in m["accepting"]
inter = product(A, B, lambda x, y: x and y)
union = product(A, B, lambda x, y: x or y)
diff = product(A, B, lambda x, y: x and not y)
print(accepts(inter, "aab")) # True: two a's, ends in b
print(accepts(inter, "ab")) # False: one a
print(accepts(union, "ab")) # True: ends in b
print(accepts(diff, "aa")) # True: even a's, not ending in b
print(accepts(complement(A), "a")) # True: odd number of a's
print(len({s for (s, _) in inter["delta"]})) # 4 reachable pair statesThe complement function is safe because both machines are complete: every state has a transition on every symbol.
Reversal, homomorphism, and quotients
Reversal. The reverse of L is the set of reversed strings, written L^R. Take an NFA for L and reverse every edge. Make the old start state the only accepting state. If there are several old accepting states, add a new start state with ε-edges to all of them. The reversed machine is an NFA even if the original was a DFA.
Homomorphism. A homomorphism h maps each symbol to a string, and it extends to whole strings symbol by symbol. For example, h(a) = 01 and h(b) = ε. The image h(L) is regular: replace each symbol in a regular expression for L with the regular expression for its image.
Inverse homomorphism. The set h⁻¹(L) contains every string w with h(w) in L. Given a DFA for L, build a DFA with the same states. On symbol c, move to the state the original DFA reaches by reading the string h(c).
Quotient and prefixes. Let L/R be the strings x for which some y in R makes xy belong to L. For regular L and regular R, L/R is regular. The construction keeps the DFA for L and marks a state as accepting if some string of R leads from it to an accepting state. The set of prefixes of L is a special case, with R = Σ*.
Using closure in proofs
Suppose you want to show L = {aⁿbⁿ : n ≥ 0} is not regular. Assume it is. The language ab is regular, but that gives no contradiction by itself, so choose a better example. Consider M = {w ∈ {a, b} : w has equally many a's and b's}. Intersect M with ab. If M were regular, then M ∩ ab* = {aⁿbⁿ} would be regular by closure under intersection. That language fails the pumping lemma, so M is not regular.
The pattern is: intersect with a regular language that isolates a known non-regular language, or apply a homomorphism, or take a complement. Then cite closure for the contradiction.
Common errors
- Treating "closed" as "if and only if". If L1 ∪ L2 is regular, you cannot conclude that L1 and L2 are regular. Take L1 = {aⁿbⁿ} and L2 = its complement. Their union is Σ*, which is regular.
- Subsets of regular languages. A subset of a regular language need not be regular. Every language over {a, b} is a subset of Σ*.
- Infinite operations. Closure covers finitely many applications. Every singleton {aⁿbⁿ} is regular, yet the infinite union of all of them is not.
- Forgetting the dead state. An incomplete DFA complemented naively accepts too little.
- Complementing an NFA directly. This gives the wrong language, as explained above.
- Wrong state counts. The product bound n·m is an upper bound on reachable states, and minimization can lower the final count. Complementing a complete DFA keeps the state count unchanged.
Exercise
Let L1 be the set of strings over {0, 1} with an even number of 1s, and let L2 be the set of strings ending in 01. Both are regular. Describe how to build a DFA for L1 − L2 using the product construction. Say which pairs are accepting and give an upper bound on the number of states, taking the minimal DFAs as 2 states for L1 and 3 states for L2.
Show answer
Run both complete DFAs in lockstep, with states (p, q), start state (start1, start2), and transitions applied to both components. Accept a pair when p is accepting in the DFA for L1 and q is not accepting in the DFA for L2. The product has at most 2 × 3 = 6 states.
Exercise
A student says: "The language L has an NFA with 5 states. To get a machine for the complement, swap accepting and non-accepting states of the NFA." Explain the error and state the correct procedure.
Show answer
Swapping accepting states works only on complete DFAs. In an NFA, a string can have both an accepting path and a rejecting path, so after the swap the string may still be accepted. The correct procedure is to convert the NFA to a DFA with subset construction, complete it with a dead state if any transition is missing, and then swap accepting and non-accepting states. The resulting DFA can have up to 2^5 = 32 states.