Theory of Computation · Week 1 · Lesson 0005 of 7
Regular expressions and automata
A regular expression describes a language through choices, consecutive pieces and repetitions.
Translate between a machine and an expression, and justify whether distinct match lengths can grow without bound.
1Warm-up
2Read the expression from the inside out
A regular expression describes a language. The expression ∅ describes the empty language, ε describes {ε}, and a symbol a describes {a}. If P and Q are expressions, then P+Q describes union, PQ describes concatenation, and P* describes star. In an expression, + is union when placed between alternatives. Parentheses group terms. Otherwise star binds first, then concatenation, then union.
A string matches an expression when it belongs to the language described. To test concatenation, try splitting the string into compatible pieces. For a union, one matching alternative suffices. For a star, split into zero or more matching pieces.
For (0+1)*01(0+1)*, test 10010. Positions 3 and 4 give 01. The split is 10 · 01 · 0, and both outer pieces match (0+1)*. By contrast, 1111 has no 01 and fails. The expression with middle term (01+10) matches exactly the binary words containing both bits, because any change of bit gives a consecutive 01 or 10.
To construct a DFA or NFA from an expression, use the single-symbol machine for each symbol, the one-state accepting machine with no moves for ε, and the one-state nonaccepting machine with no moves for ∅, treating these base machines as NFAs. Combine them using the union, concatenation and star constructions. For 01+ε, the 01 branch reads exactly 0 then 1 while the ε branch accepts with zero symbols.
Sipser, Introduction to the Theory of Computation, §1.3.
3Read paths as expressions
Write a symbol for each edge, a star for any number of loop traversals, and a union for alternative routes. An accepting path may stop at any accepting state once the input has been consumed.
Stopping at q₀ contributes ε. If the first symbol is a, the route to q₁ reads aa*, and taking the edge to q₂ gives aa*ba*. If the first symbol is b, the corresponding terms are bb* and bb*ab*. The whole expression is ε+aa*+aa*ba*+bb*+bb*ab*, equivalently a*+aa*ba*+b*+bb*ab*.
A leading star permits zero copies. Replacing aa* by a* before a switch can introduce new words. In the displayed machine, baa goes to q₃ then q₄ and cannot read the final a, while a*ba* generates baa.
State elimination replaces an intermediate state with direct edges labeled by regular expressions. It gives a systematic method when there are many paths. Add a fresh start with an ε-arrow to the old start, and a fresh accepting finish reached by ε from every old accepting state. Keep only the fresh finish accepting. Merge parallel labels by union and use ∅ where there is no edge. On removing k, update each surviving label Rij to Rij + Rik(Rkk)*Rkj. After eliminating all other states, read the label from the new start to the new finish.
In the displayed fragment, eliminating k changes the s-to-f label to 1+01*0. The direct route contributes 1. The other route contributes 0 for entry, 1* for the loop, and 0 for exit. As a second calculation, if the direct label is ∅, entry a, loop ∅, and exit b, the update is ∅+a(∅)*b = ab, since (∅)* describes {ε}.
4Count distinct strings
Different choices may give the same string. The union 0+(0+0) still generates one word. Concatenating {0, 00} with itself gives only 00, 000 and 0000 because 000 has two factorizations. To count words of a specified length, restrict to that length and remove duplicates. For (0+1)*00, length three permits exactly 000 and 100.
The star of a language containing a nonempty string u is infinite. For each k ≥ 0, repeating u gives length k|u|. These lengths are distinct. If the language has no nonempty string, its star is {ε}.
For (P+R)*Q + PQ, first check whether Q generates any string. If it generates none, both terms generate none. If Q generates some v and P+R generates a nonempty u, the first term generates ukv for every k ≥ 0. The lengths k|u|+|v| grow without bound. For example, with P=0, R=ε and Q=1, we obtain 1, 01, 001, and so on.
If P+R generates no nonempty word, then (P+R)* describes {ε}. The entire expression then describes exactly the language of Q because PQ contributes either that same language or the empty set. With P=R=ε and Q=0, the expression generates just 0. The counts p=q=r=1 occur in both this example and the infinite one, so the counts alone cannot decide.
5Recap
- Use union for alternatives, concatenation for consecutive pieces, and star for repeats.
- State elimination adds each route through the removed state to the surviving direct route.
- An infinite family follows when matched words have arbitrarily large lengths.
6Check yourself
7Cheat card
- Precedence
- Star, concatenation, union.
- Paths
- Label every mandatory edge and allow zero or more traversals of each loop.
- Eliminate k
- New direct label = old direct + entry · loop* · exit.
- Memory hook
- One cut proves a match. One growing family proves infinitude.