4.6.5 · D4Theory of Computation

Exercises — Regular expressions — equivalence with finite automata

3,565 words16 min readBack to topic

Before we start, a one-screen glossary so no symbol appears unexplained.


Level 1 — Recognition (read the notation)

L1.1

Problem. Which of these strings are in ? Test each: a, b, ba, bb, .

Recall Solution

The pattern says: any number of 's and 's, then a final . So a string is in the language exactly when it is non-empty and ends in .

  • a — ends in ✓ (the part matches zero letters).
  • b — ends in ✗.
  • ba — ends in ✓.
  • bb — ends in ✗.
  • — has no final letter at all ✗ (the pattern forces at least one ). In the language: a, ba.

L1.2

Problem. For each regex, say whether (the empty string) belongs to its language: (i) , (ii) , (iii) , (iv) .

Recall Solution

Ask: "can this pattern match with zero letters read?"

  • (i) — zero copies of is allowed ⇒ contains ✓.
  • (ii) — forces exactly the two letters a then b ⇒ no ✗.
  • (iii) — the right branch is ⇒ ✓.
  • (iv) — star always allows the zero-copy case, and zero copies . So even though matches nothing, ⇒ ✓.

L1.3

Problem. Match each Thompson gadget picture to its regex. Look at the figure below: three side-by-side machines, each with a start state (left, arrow coming in) and an accept state (right, double circle).

Figure — Regular expressions — equivalence with finite automata
Figure s01 — Three base Thompson gadgets. Left (blue): a single edge labeled joins start to accept. Middle (green): a single -edge (free hop) joins start to accept. Right (red): start and accept with no path between them — nothing can cross.

Recall Solution
  • Left gadget: a single edge labeled from start to accept ⇒ regex .
  • Middle gadget: a single -edge from start to accept ⇒ regex (matches empty string only).
  • Right gadget: start and accept with no path between them ⇒ regex (matches nothing). The key reading skill: a letter edge demands that letter be read; an -edge is a free hop; no edge means dead.

Level 2 — Application (run the recipe)

L2.1 (Thompson: build)

Problem. Using Thompson's construction, count the exact number of states in the NFA for (base gadgets + fresh union states). Follow the figure below.

Figure — Regular expressions — equivalence with finite automata
Figure s02 — Thompson union gadget for . A fresh orange start -branches up into the blue -gadget (states 1→2 via ) and down into the green -gadget (states 3→4 via ); both gadget accepts -jump into a fresh orange accept . Count the circles: states.

Recall Solution

Base gadget for = 2 states (its own start + accept). Base gadget for = 2 states. Union adds a fresh start and a fresh accept = 2 more. Total states. Why fresh states: they guarantee one clean entry and one clean exit so induction stays correct (see the parent's steel-man).

L2.2 (Thompson: build)

Problem. How many states does Thompson's construction produce for ? (base gadget for + fresh star states)

Recall Solution

Gadget for = 2 states. Star adds a fresh start and fresh accept = 2 more. Total states, wired as: new start new accept (zero copies), new start -gadget start, -gadget accept back to -gadget start (repeat) and new accept (stop).

L2.3 (State elimination: read off)

Problem. A GNFA (a machine whose edges may carry whole regexes — see the glossary) has states with edges , (self-loop), . Eliminate state and give the final regex .

Recall Solution

Apply the ripping rule stated in the formula box above, with , , : Read the four labels off the picture:

  • (no direct edge yet),
  • , self-loop , . Substitute: new label . Now simplify in two tiny steps, each with a reason:
  1. — union with the empty language adds nothing, so the alternative disappears, leaving .
  2. — a leading vanishes under concatenation because gluing "the empty string, then " is just (prepending zero letters changes nothing). is the identity for concatenation, exactly like is for addition. Answer: — "any number of 's, then a single ." Check: b ✓, aab ✓, ba ✗.

L2.4 (State elimination: two states)

Problem. DFA: states , start , accept ; edges , , , . Find a regex. (Hint: normalize, then eliminate state .)

Recall Solution

Normalize (see the glossary box): add a fresh start with , and a fresh accept with (state is the old accept). Now has no incoming edge and has no outgoing edge. Eliminate state with the ripping rule, taking and the only surviving neighbours , (every route into and out of touches state ). Read 's four labels off the machine:

  • (edge ),
  • (the self-loop ),
  • therefore — "spin on 's -loop zero or more times,"
  • (edge ). The ripping rule contributes a new edge . State already had a self-loop (edge ). Combining the old loop with the new detour by , state 's self-loop becomes . The surviving skeleton is now ; eliminating gives (leading/trailing vanish under concatenation as in L2.3). Answer: — strings over with an even count of 's. Check: bb ✓, aa ✓, aba? that's 2 's ✓, a ✗.

Level 3 — Analysis (say why)

L3.1

Problem. Explain, using the ripping rule, why eliminating a state with no self-loop contributes (no star). What plays the role of ?

Recall Solution

With no self-loop, . Then . Concatenating changes nothing: . So the star term is still there — it just collapses to because you can spin on exactly zero times and no more. This is why the general rule needs no special case for self-loop-free states.

L3.2

Problem. Two students convert the same 3-state FA and get and . One insists the order of elimination "broke" the answer. Who is right and why?

Recall Solution

Neither answer is broken; both describe the same language. Union is commutative on languages: . Different elimination orders reroute paths differently, producing regexes that look different but denote the identical string set — like vs . Verify by testing strings, never by comparing regex text.

L3.3

Problem. In Thompson's star gadget, why must the new accept state be separate from 's accept? Give a concrete language that would be corrupted if you merged them.

Recall Solution

The invariant "no edge leaves the accept state" is what lets you safely concatenate this gadget onto the next one. If 's accept doubled as the star's accept, the loop-back edge (accept start) would be an edge leaving an accept state. When you then concatenate, a later machine could re-enter mid-loop and accept strings that are not clean repetitions. Concrete corruption: for , a leaked path could accept aba (start a repetition of ab, then stop after the extra a) — but aba is not in . Fresh accept + fresh start block this.


Level 4 — Synthesis (build something new)

L4.1

Problem. Write a regex over for "every string that contains at least one ." Then describe the smallest NFA idea.

Recall Solution

"Anything, then a forced , then anything": . NFA idea: a start state looping on ; an -edge to a second state; the second state loops on and is accepting. The single -edge is the "at least one " commitment. Check: b ✗, bab ✓, aaa ✓.

L4.2

Problem. Write a regex over for "strings of even length." Justify why the length is always even.

Recall Solution

Group letters in pairs: each pair is one of , i.e. . Repeat zero or more pairs: Every repetition adds exactly 2 letters, and zero repetitions gives length — all even. Check: ✓, ab ✓, abba ✓, aba ✗.

L4.3

Problem. Build (by hand) the Thompson NFA for and count total states and total -edges.

Recall Solution

Gadget = 2 states, edge . Gadget = 2 states, edge . Concatenation wires -gadget's accept -gadget's start; the -accept stops being accepting, the -accept becomes the final accept. No fresh states are added for concatenation. States . -edges added (the single glue edge).


Level 5 — Mastery (prove / decide)

L5.1

Problem. Prove that (all strings over ), i.e. the star of the whole alphabet is every string.

Recall Solution

We show the two languages contain each other, so they are equal.

Step 1 — (the easy direction). Every letter that can produce is either or , both in . A string matched by the star is a finite glue of such letters, hence a finite row of letters from — that is the definition of an element of . So every matched string is in .

Step 2 — (the direction with the "why"). We prove by induction on the length of a string that every string of length over is matched.

  • Base case : the only length- string is . The star allows zero copies of , which produces exactly . ✓
  • Inductive step: assume every string of length is matched (the induction hypothesis). Take any string of length . Split off its last letter: where has length and . By the hypothesis, is matched by some number of copies of . The single letter is matched by one more copy of (that gadget matches either or ). Concatenating gives copies, which the star permits. So is matched. ✓ By induction the claim holds for all , so every string over is in .

Conclusion. Both containments hold, therefore . The heart of it: the star's zero-copy case delivers , and each extra copy appends exactly one arbitrary letter, so we can build up any string one letter at a time.

L5.2

Problem. Decide (with reasoning) whether the language "strings of 's whose length is a perfect square" is regular. If not, name the tool that proves it.

Recall Solution

Not regular. Kleene's theorem says regular = expressible by a regex = accepted by a finite automaton. A finite machine has only finitely many states, so the gaps between accepted lengths (, gaps ) grow without bound, which a finite-state device cannot track. The formal tool is the Pumping Lemma for Regular Languages: any long enough accepted string can be pumped to , landing between consecutive squares, so it is accepted when it shouldn't be — contradiction. Hence no regex and no FA exist for it.

L5.3

Problem. Use closure properties to show: if is regular, then the language of strings in that also end in is regular. Then describe the regex/automaton construction that realizes it.

Recall Solution

"Ends in " is the language , which is itself regular (it has the obvious regex ). The requested language is the intersection : strings that are in and end in . By Regular Languages — closure properties, regular languages are closed under intersection. The construction is the product automaton: if accepts (states ) and accepts (states ), build the machine on state pairs that runs both machines in lockstep on the same input, accepting only when both components are in an accept state. This new DFA accepts exactly , so is regular. Regex view: if , the wanted language equals ; you may compute a regex for it by converting each side to a DFA, taking the product, and reading a regex back off with state elimination. Check on : the result is = "any string ending in ", e.g. ba ✓, bb ✗.

L5.4 (capstone)

Problem. Round-trip check. Take the FA of L2.4, whose regex we found to be . Argue that Thompson's construction on this regex, followed by Subset Construction — NFA to DFA equivalence, yields a DFA accepting the same language, closing the loop of Kleene's theorem.

Recall Solution

Direction 2 (state elimination) turned the DFA into the regex — "even number of 's." Direction 1 (Thompson) turns back into an -NFA with by construction (each gadget preserves its sub-language, proven by induction in the parent note). Subset construction converts to a DFA with . Chaining the three equalities: Both containments of Kleene's theorem are exercised here, so we have provably returned to the same language — this is the equivalence in action, see also Lexical analysis and grep — applications.


Recall One-line self-quiz (reveal to check)

equals which language? ::: — the star's zero-copy case survives even when the base matches nothing. Why fresh states in union/star but not concatenation? ::: Union/star create branching/looping that needs one clean entry+exit; concatenation just glues accept→start with one . Which tool proves NON-regularity? ::: The Pumping Lemma for Regular Languages (never proves regularity).