Sub topic 6
Closure properties: what union, intersection and complement preserve
This chapter assumes you know the definitions of recursive (decidable) and recursively enumerable (RE, semi-decidable) languages. It also assumes you can trace a deterministic Turing machine (TM) and have seen proof by contradiction. A class of languages is closed under an operation if applying the operation to members of the class always gives a language that is still in the class. GATE questions often ask for exactly this, so it pays to know the table and the reason behind each entry.
The two classes in one paragraph
A language L is recursive if some TM halts on every input and accepts exactly the strings in L. A language L is RE if some TM accepts exactly the strings in L, but on strings outside L it may reject or loop forever. Every recursive language is RE. The converse fails: the language of the halting problem is RE but not recursive.
The whole chapter rests on one asymmetry. A decider always gives an answer. A recognizer gives "yes" in finite time when the answer is yes, and may stay silent when the answer is no.
The closure table
| Operation | Recursive | RE |
|---|---|---|
| Union | Closed | Closed |
| Intersection | Closed | Closed |
| Complement | Closed | **Not closed** |
| Concatenation | Closed | Closed |
| Kleene star | Closed | Closed |
| Difference L1 − L2 | Closed | **Not closed** |
| Reversal | Closed | Closed |
| Intersection with a regular language | Closed | Closed |
Complement is the entry that separates the two classes. Most other entries follow by simulating machines one after another or side by side.
Recursive languages
Complement
Let M decide L. Build M' that runs M and swaps the answer: if M accepts, M' rejects, and if M rejects, M' accepts. This works because M always halts. Whenever M' runs, it ends in finite time with the opposite verdict, so M' decides the complement of L.
Union and intersection
Let M1 decide L1 and M2 decide L2. On input w, run M1 on w, then run M2 on w. Both runs halt.
- For the union, accept if either run accepted.
- For the intersection, accept only if both runs accepted.
Running them one after the other is enough because neither run can hang.
Concatenation and star
For concatenation L1·L2, a decider tries every split w = xy, with |w| + 1 possible splits. For each split it runs M1 on x and M2 on y, and it accepts if some split gets two acceptances. There are finitely many splits and each run halts, so the decider halts. Kleene star works the same way, by checking every way to cut w into pieces. A common approach is to compute, for each substring, whether it belongs to L*, working from shorter substrings to longer ones.
RE languages
Union needs interleaving
Let M1 and M2 recognize L1 and L2. If we run M1 to completion first, it may loop on w even though M2 would accept w. So we run both machines in alternation, one step of M1, then one step of M2, and so on. We accept as soon as either one accepts. If w is in L1 or L2, one of them accepts after finitely many steps, and so does the combined machine. If w is in neither, the combined machine never accepts, which is allowed for a recognizer. This alternating execution is called dovetailing.
Intersection
Run M1 on w. If it accepts, run M2 on w and accept if that accepts too. If w is in both languages, both runs finish. If w is missing from either one, the combined machine never accepts, which is again acceptable.
Concatenation and star
A nondeterministic TM guesses the split point, runs M1 on the first part and M2 on the second, and accepts if both accept. Nondeterministic and deterministic machines recognize the same languages, so RE is closed under concatenation. Star is similar: guess the cut points and check each piece.
Complement fails
The proof uses a theorem and then a contradiction.
Theorem. If both L and its complement are RE, then L is recursive.
Proof. Let M1 recognize L and M2 recognize the complement. On input w, dovetail the two machines. Every string is either in L or in its complement, so exactly one machine will eventually accept w. If M1 accepts, accept. If M2 accepts, reject. This machine halts on every input and decides L.
Now suppose RE were closed under complement. Take the halting language, which is RE but not recursive. Its complement would also be RE, and the theorem would make the halting language recursive. That is a contradiction. So RE is not closed under complement.
Difference
The difference L1 − L2 equals L1 ∩ complement(L2). Taking L1 to be the set of all strings, which is regular and therefore RE, gives the complement of L2. Since RE is not closed under complement, it is not closed under difference either.
A runnable illustration of dovetailing
The code below models "machines" as Python generators. Each next call performs one step and yields None while running or True when the machine accepts. A machine that never accepts yields None forever. The function either is the union recognizer.
def accepts_if_even(n):
# halts: accepts even numbers after n steps, loops on odd numbers
def gen():
for _ in range(n):
yield None
if n % 2 == 0:
yield True
else:
while True:
yield None
return gen()
def either(m1, m2, max_steps=1000):
# dovetail: one step of each machine per round
for _ in range(max_steps):
if next(m1) is True:
return True
if next(m2) is True:
return True
return None # no verdict within the budget
# m1 loops forever, m2 accepts after 5 steps
print(either(accepts_if_even(7), accepts_if_even(4))) # True
# both loop: a real recognizer would run forever
print(either(accepts_if_even(7), accepts_if_even(9))) # NoneRunning accepts_if_even(7) to completion first would hang. Alternating steps lets the second machine answer. The None result stands for "we gave up", which a real recognizer never does.
What the verdicts look like
For a language L, the pair (L, complement of L) can only fall into three situations.
| Situation | L is | Complement of L is |
|---|---|---|
| Both RE | Recursive | Recursive |
| L RE only | RE, not recursive | Not RE |
| Neither RE | Not RE | Not RE |
The fourth combination, where L is not RE but its complement is RE, is the mirror of the second row. A language that is not RE can still have an RE complement, which is the case for the complement of the halting language.
Common errors
- "Recursive implies the complement is RE only." If L is recursive, the complement is recursive, so it is also RE.
- "Complement of an RE language is RE." This holds only when the language is also recursive.
- Reading "closed" as "preserves undecidability". Closure says nothing about hard languages in isolation. Two undecidable languages can have a decidable union or intersection. For example, take the halting language H and its complement: their union is the set of all strings, which is decidable, and their intersection is empty, which is decidable.
- Forgetting the empty-language case. The empty language is recursive, so every closure statement must hold for it.
- Running machines in sequence for RE union. This breaks on looping inputs. Use dovetailing.
GATE-style reasoning
Use these steps on a statement of the form "Is class X closed under operation Y?"
- Identify whether the class is recursive or RE.
- If the operation is complement or difference, answer "closed" for recursive and "not closed" for RE.
- For union, intersection, concatenation and star, answer "closed" for both classes.
- If a statement mixes classes, such as "L1 is RE and L2 is recursive, so L1 − L2 is...", rewrite it as an intersection with a complement. The complement of L2 is recursive, so L1 ∩ complement(L2) is an intersection of an RE and a recursive language. That intersection is RE.
Exercise
L1 is recursive and L2 is RE but not recursive. For each of L1 ∪ L2, L1 ∩ L2 and L2 − L1, say whether the result must be recursive, must be RE, or neither. Then say whether the complement of L2 is RE.
Show answer
L1 ∪ L2 and L1 ∩ L2 are RE, because RE is closed under both. They need not be recursive. For example, with L1 empty, the union equals L2, which is not recursive. L2 − L1 equals L2 ∩ complement(L1). The complement of L1 is recursive, so the result is an intersection of two RE languages and is RE. The complement of L2 is not RE: if it were, L2 and its complement would both be RE, and L2 would be recursive, contradicting the assumption.
Exercise
A student claims: "Let L be RE. Then M accepts L, and swapping accept and reject states in M gives a machine for the complement." Explain precisely why this fails, using an input on which M loops.
Show answer
Swapping the accept and reject states only changes the verdict on inputs where M halts. On an input w outside L where M loops forever, the swapped machine also loops forever, so it never accepts w. Yet w belongs to the complement and must be accepted. The construction therefore does not recognize the complement. It would work only if M were a decider that halts on every input.