4.6.6 · D3Theory of Computation

Worked examples — Regular languages — closed under union, intersection, complement, concatenation, Kleene star

3,901 words18 min readBack to topic

Before anything: a DFA (deterministic finite automaton) is a robot that reads a string one symbol at a time, sits in exactly one state at each moment, and at the end says YES (accept) or NO (reject). An NFA (nondeterministic finite automaton) is the same robot but it may split into several guesses at once, and it says YES if any guess ends happy. We will lean on both. See Deterministic Finite Automata (DFA) and Nondeterministic Finite Automata (NFA) if either word is fuzzy — this page assumes only that a machine reads symbols and says YES/NO.

We now clear every box. Alphabet is or as stated; means length of , means "count of 0s in ."


The scenario matrix

Every closure problem lives in one of these boxes. The columns are what you must do; the rows are what the problem looks like.

# Case class What the problem hands you Right tool Example
C1 AND of two conditions "words satisfying both X and Y" Product Construction (intersection accept set) (a)
C2 OR of two conditions "words satisfying X or Y" Product (union accept set) or -NFA (b)
C3 NOT a condition "words that are not X" Determinize then flip accept states (c)
C4 Join / one-after-another "an X-part followed by a Y-part" -glue concatenation (d)
C5 Repeat any number of times "zero or more copies of X" -glue Kleene star (e)
C6 Degenerate / empty inputs , , , zero copies check the / empty edge case (f)
C7 Prove NON-regular via closure "show L is not regular" intersect with a regular set, hit Pumping Lemma for Regular Languages (g)
C8 Real-world word problem disguised as a story translate to a language, then C1–C5 (h)
C9 Exam twist complement of an intersection, De Morgan trap De Morgan's Laws + right order of ops (i)

(a) — Box C1: AND of two conditions

Forecast: guess now — how many states does the product machine have, and does get accepted?

Steps.

  1. Build the DFA for . Two states: (seen an even count of 0s so far, start & accept) and (odd). Reading a flips ; reading a stays put. Why this step? Parity of 0s only depends on how many 0s we've seen mod 2 — two states is all the memory you need.

  2. Build the DFA for . Two states: (last symbol was not 1 / nothing yet, start) and (last symbol was 1, accept). Reading goes to ; reading goes to . Why this step? "Ends in 1" only cares about the very last symbol, so remember just that.

  3. Form the product. States are pairs with , states. Start . Transition runs both coordinates on the same input symbol. Accept set . Why this step? Product Construction tracks both invariants at once inside one combined state, so nothing is lost. AND ⇒ accept only when both coordinates are happy.

  4. Run . Track this is exactly the trajectory drawn in Figure 1 below: start . End state — first coordinate is , not accept. Rejected. Why this step? has one 0 (odd) so it fails ; the product correctly rejects.

Figure — Regular languages — closed under union, intersection, complement, concatenation, Kleene star

In Figure 1, note the four combined states arranged as a grid: the horizontal axis is the parity coordinate (/) and the vertical axis is the last-symbol coordinate (/). The single amber double-circle at top-right is the only accept state — read a (cyan arrow) to move right along the last-symbol axis, read a (white arrow) to flip parity.

Verify: (odd) so , hence ✓. : two 0s (even) ∈ , ends in 1 ∈ → accepted. : zero 0s is even (∈ ) but does not end in 1 → rejected. All three match the machine's verdicts.


(b) — Box C2: OR of two conditions

Forecast: a language defined by OR is usually huge — guess which few short strings are the only rejects.

Steps.

  1. Build the DFA for (""). Three states counting length capped at 2: , start (length 0), (length 1) (length , and self-loops on every symbol). Accept set . Why this step? Length only needs to be tracked up to the threshold 2, then it saturates — three states suffice.

  2. Build the DFA for ("contains an "). Two states: , start (no yet), reading goes to (seen an , self-loop forever), reading stays. Accept set . Why this step? "Contains an " is a one-way switch: once flipped, it never flips back — two states.

  3. Form the product ( states) and use the union accept set Now every is concrete (from steps 1–2). Why this step? A pair should accept when at least one coordinate is final — that is exactly "either machine is happy," which is OR.

  4. Note the complement is easier to reason about: by De Morgan's Laws. A string is rejected iff and has no . In product terms that is a state with first coordinate and second — none of which lie in . Why this step? Rejects are rare, so describing the rejected set directly is faster than listing accepts.

  5. Enumerate rejects: strings with length 0 or 1 and no . Length 0: . Length 1 with no : . That's it. Why this step? leaves only lengths 0 and 1; "no " over forces every symbol to be .

Verify: rejected set , size . Check : length 2 (∈) → accepted ✓. Check : length 1 (∉) but contains (∈) → accepted ✓. Check : length 1, no → rejected ✓.


(c) — Box C3: NOT a condition (the NFA trap)

Forecast: if you swap accept states, do you get — and does it matter whether you started from an NFA or a DFA?

Steps.

  1. The natural machine is an NFA that guesses where the substring begins. Concretely has states : start self-loops on both and (waiting), guesses "the starts here," confirms it, and (the only accept) self-loops on both symbols. It is nondeterministic because from on an there are two moves: stay at or jump to . Why this step? We must actually see the NFA to understand why swapping its accepts is illegal.
  1. Determinize by tracking the set of NFA states reachable so far. Start set =: . On : =: (stay + guess). On from : . From on : , , giving =: (contains accept ). From on : . Once appears it self-loops (still contains and ). So the reachable subsets are exactly three: , , , with the accept (any subset containing ). Why this step? Subset Construction (NFA to DFA) gives a DFA with "exactly one state per string," the guarantee that makes flipping valid; here the three surviving subsets become the three DFA states.

  2. Flip: 's accept set — accepting exactly the strings that never complete an . Why this step? In a total DFA, "not in " is literally "not accepted by " — that is the definition of complement.

  3. Describe in words: strings with no substring = all 's first then all 's, i.e. matching . Why this step? Once an appears it must never be followed by , forcing the shape.

Figure — Regular languages — closed under union, intersection, complement, concatenation, Kleene star

Figure 2 draws the determinized 3-state DFA. The amber double-circles are the accept states for (after the flip); (cyan, the "saw " trap) is where accepts. Follow the cyan then arrows to reach ; the white self-loop labels remind you which symbol keeps you waiting.

Verify: ? It matches and contains no ✓. ? It contains → in , so not in ✓. ? empty string has no → in ✓.


(d) — Box C4: Join / one-after-another

Forecast: where does the NFA hand control from the -machine to the -machine when reading ?

Steps.

  1. Write . Each factor is regular (single symbol under star). Why this step? Decomposing into known-regular pieces means Regular Expressions closure does the proof — no machine drawn from scratch.

  2. Build NFA for (a single state looping on , both start and accept) and for (a single state looping on ). Glue: add an -transition from every accept state of to the start of ; new start start of ; new accepts accepts of . Why this step? A word splits into an -prefix and a -suffix but we don't know where — the -jump lets the NFA guess the split point.

  3. Trace . Stay in reading ; take the -jump into (guessing the split is here); read in ; end in an accept state. Why this step? The correct split is "after the 2 's" — the NFA's guess lands exactly there and accepts.

Verify: since it is with ✓. ? It's then — no way to write as (an after a ) → rejected. In the glued NFA there is no path reading before entering from without already having left ; so is rejected ✓.


(e) — Box C5: Repeat any number of times

Forecast: which of those four strings are in ? Guess before reading on.

Steps.

  1. Build a small NFA for : with the only accept. Why this step? We need a base machine to loop; is a 3-state chain.

  2. Add a fresh start-and-accept state with . Add from every old accept () back to . New accept set . Why this step? The fresh gives (the , i.e. , copy) without wrongly marking accepting. If instead you just marked the old start accepting, then any word that merely passes through mid-run (via the loop-back) would be wrongly accepted even without completing a whole copy of . Looping restarts a new copy of .

  3. Test each string. : sits in (accept). : (accept). : after first at , loop , read second to (accept). : ends mid-copy at after the loop — not accepting. Why this step? contains exactly whole-number repetitions of ; is copies, so it must be rejected.

Figure — Regular languages — closed under union, intersection, complement, concatenation, Kleene star

Figure 3 shows the star construction: the amber on the far left is the fresh start-and-accept state, its amber -arrow feeds into , and the curved amber -arrow from back to is the loop that "restarts a new copy of ." Note is the only state reachable by the empty string, which is why is accepted.

Verify: membership: (k=0) ✓, (k=1) ✓, (k=2) ✓, (odd length, cannot be ) ✓. Length rule: any has even length; is odd → rejected ✓.


(f) — Box C6: Degenerate & empty inputs

Forecast: is empty? Is it ? Pick before reading.

Steps.

  1. . Using powers, with always. With , every term is empty, but still contributes . So . Why this step? Star always includes the empty string (the , zero-copies term), even when the base language is empty. This is the whole point of the fresh accept state .

  2. . Concatenating with itself any number of times still gives : . Every power is , so the union . So . Why this step? No new words can ever appear because gluing empty strings produces only the empty string.

  3. . Concatenation . There is no , so no pair exists, and the resulting set is empty → . Why this step? Concatenation needs a prefix from the first set; if that set is empty, no product string can form. ( annihilates under concatenation, just like multiplying by 0.)

Verify: has size ✓. has size ✓. has size ✓. Contrast: (identity), so and behave oppositely under concatenation.


(g) — Box C7: Prove NON-regularity via closure

Forecast: why is it easier to prove non-regular than itself?

Steps.

  1. Suppose, for contradiction, were regular. Why this step? Closure arguments transfer non-regularity backward: we assume regular and derive an impossible consequence.

  2. is regular (a regular expression). Regular languages are closed under intersection (Product Construction), so would be regular. Why this step? Intersecting with the "clean shape" strips away messy interleavings and isolates the hard core.

  3. Compute the intersection: strings that are all 's then all 's and have equal counts . Why this step? The shape forces leading 's and trailing 's; equality forces .

  4. Apply the pumping lemma to : it is a standard non-regular language (pumping the pumped -block breaks the count balance). Contradiction with step 2. Why this step? We reduced our unknown language to a known non-regular one; the contradiction kills the assumption.

Figure — Regular languages — closed under union, intersection, complement, concatenation, Kleene star

Figure 4 pictures the argument as two overlapping circles: the cyan circle is (), the white circle is the regular language , and their amber overlap is exactly . The caption traces the logic — if the whole cyan set were regular, its intersection with the white set would be too, yet the pumping lemma forbids the amber overlap from being regular.

Conclusion: is not regular.

Verify: intersection identity — sample : (∈) and matches → in intersection, and it equals ✓. Sample : (∈) but does not match (a then an ) → excluded from the intersection, confirming the intersection is exactly ✓. Since is non-regular by the pumping lemma, step 2's " is regular" is impossible, so the assumption " is regular" fails ✓.


(h) — Box C8: Real-world word problem

Forecast: which two closure operations does this story secretly ask for?

Steps.

  1. Translate to languages over . Condition X: total value . Condition Y: sequence ends in . Why this step? A story becomes solvable only once it is a formal language; "vends" = "accepted."

  2. X is regular: track the running total mod 25 — only values matter, a 5-state DFA. Reading adds 5 mod 25; reading adds 10 mod 25; accept when total . Why this step? Money grows unboundedly, but mod 25 it lives in a finite set — finite memory, so regular.

  3. Y is regular: a 2-state DFA remembering the last symbol (accept iff last was ). Why this step? Only the final coin matters, so one bit of memory suffices.

  4. The vending set is — regular by intersection closure (Box C1), built as the Product Construction of the 5-state and 2-state DFAs (up to states). Why this step? "Multiple of 25 and ends in dime" is an AND of two regular conditions.

Verify: test = ¢, not vended (fails X). Test = ¢, but ends in not vended (fails Y). Test = ¢, and ends in vended ✓. So the language is regular and non-empty.


(i) — Box C9: The exam twist (De Morgan trap)

Forecast: is "flip the intersection's accept set" the same as ?

Steps.

  1. Correct construction A: build the product DFA (which is total and deterministic), give it the intersection accept set , then flip to . Why this step? Because the product is already a DFA (exactly one state per string), flipping its accept set is valid complement — no NFA hazard here (contrast the [!mistake] in Box C3).

  2. Correct construction B (the check): De Morgan's Laws give . Complement each DFA (flip its own accepts), then take the union via product with accept set . Why this step? This double-derivation confirms both routes land on the same language — a favourite exam sanity check.

  3. Spot the trap: the two accept sets must agree. = "not (both final)" = "at least one non-final" = , which is exactly the union-of-complements accept set. They match. Why this step? If a student instead flipped each coordinate to (both non-final), that is — the wrong language. The De Morgan structure is the guardrail.

Verify (concrete counts): let , ; , . Product has states. (size 1). accept set states. Union-of-complements set : pairs with first or second = all minus the single = states ✓. The wrong "both non-final" set has size — clearly a different language.


Recall Quick self-test

Which box: "words with an even number of 's that also start with "? ::: C1 (AND → product / intersection) Is empty? ::: No — it is ; star always contributes the (k=0) copy. To complement an NFA, what must you do first? ::: Determinize it via subset construction, then flip accept states. Why glue with -transitions for concatenation? ::: The NFA must guess the unknown split point between the -part and the -part. How does closure prove non-regularity? ::: Assume regular, intersect with a regular language to isolate a known non-regular language, hit a pumping-lemma contradiction.

Parent: Regular Languages — Closure Properties