knowledge

Theory of Computation: Lecture 1

Strings and languages, deterministic finite automata, state meanings, the state-characterization recipe for correctness proofs, and the first closure constructions.

Strings and Languages

A finite automaton decides a yes/no question about a string while remembering only one of finitely many things at a time. Before the machines come the objects they read and classify.

Alphabet (Σ\Sigma). A finite, nonempty set of symbols. {0,1}\{0,1\} and {a,b}\{a,b\} are the usual alphabets here; any finite set works.

String. A finite sequence of symbols from Σ\Sigma. The length of a string ww is written w|w|, and the empty string ϵ\epsilon is the unique string of length 00.

String universe (Σ\Sigma^*). The set of all finite strings over Σ\Sigma, including ϵ\epsilon. Every string in Σ\Sigma^* has finite length, but Σ\Sigma^* itself is infinite whenever Σ\Sigma is nonempty.

Language. Any set of strings, that is, any subset LΣL\subseteq\Sigma^*.

A language is a yes/no problem in set form: "does ww have property PP" becomes "is ww in the set of strings with property PP", so solving the problem means deciding membership.

Finite Automata

Finite automaton. A machine with finitely many states. It reads the input one symbol at a time, left to right, and stores only its current state; when the input ends, the current state alone determines the answer.

  • Alphabet. The symbols the machine reads, fixed by the problem.
  • States. The possible memory values. Their number is fixed and independent of the input length.
  • Transition function. For every state and symbol, the rule giving the next state. In a diagram, the labeled arrows.
  • Start state. The state before any symbol is read, marked by an unlabeled incoming arrow.
  • Accepting states. The states that answer yes when the input ends there, drawn as double circles.

The machine never looks back: everything it will ever use about the symbols read so far must already be packed into the current state.

Example 1. Even number of M symbols.

For strings over Σ={M,W}\Sigma=\{M,W\}, accept exactly the strings in which M appears an even number of times. Counting the M symbols would need unbounded memory, but the answer depends only on the count's parity, and parity is one bit.

MMWWEVENODD
DFA for strings with an even number of M symbols.
Current stateRead MRead W
EVENODDEVEN
ODDEVENODD

Reading M flips parity. Reading W preserves parity. Starting in EVEN treats the empty prefix as even, and EVEN is accepting, so ϵ\epsilon is accepted.

Deterministic Finite Automata

Deterministic finite automaton (DFA). A finite automaton whose transition rule gives exactly one next state for every state-symbol pair. A DFA is drawn as a state diagram or written as a 5-tuple; these are the same machine in two forms:

M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F)
SymbolRole
QQfinite set of states
Σ\Sigmaalphabet
δ:Q×ΣQ\delta:Q\times\Sigma\to Qtransition function
q0Qq_0\in Qstart state
FQF\subseteq Qaccepting states

The tuple settles what a picture can leave unclear. The transition function is total, so every state has exactly one outgoing arrow per symbol, never zero and never two. The accepting set may be empty; with F=F=\varnothing the machine accepts no string at all.

Extended transition function. The same letter δ\delta is reused on whole strings: δ(q,w)\delta(q,w) is the state reached from qq after reading all of ww, one symbol at a time.

δ(q,ϵ)=q,δ(q,xσ)=δ(δ(q,x),σ)\delta(q,\epsilon)=q, \qquad \delta(q,x\sigma)=\delta(\delta(q,x),\sigma)

Here xσx\sigma means the string xx followed by the single symbol σ\sigma.

Read the recursion as "strip the last symbol": to process xσx\sigma, first process xx, then take one step on σ\sigma. Every induction over strings in what follows unwinds exactly this recursion.

Acceptance. A DFA accepts a string ww when its run on ww ends in an accepting state, that is, when δ(q0,w)F\delta(q_0,w)\in F.

  • Run. The sequence of states visited while reading the input, starting at q0q_0.
  • Empty input. On ϵ\epsilon no transition is taken, so the run starts and ends at q0q_0, and ϵ\epsilon is accepted exactly when q0Fq_0\in F.

Tracing the parity machine of Example 1 on two inputs shows acceptance as a pure table computation:

Input readstartMWWMWMM
StateEOOOEEOE

The final state is E, so MWWMWMM is accepted.

Input readstartWMMWWM
StateEEOEEEO

The final state is O, so WMMWWM is rejected.

Language of a machine. The language of a DFA MM, written L(M)L(M), is the set of all strings MM accepts.

L(M)={wΣδ(q0,w)F}L(M)=\{w\in\Sigma^* \mid \delta(q_0,w)\in F\}

Recognize. MM recognizes a language AA when L(M)=AL(M)=A. Every DFA recognizes exactly one language; a machine that accepts no strings still recognizes one language, namely \varnothing.

Reading a DFA

State meaning. The property of the input read so far that holds exactly when the machine is in that state. To read an unfamiliar DFA, trace a few short strings and name what each state remembers; the language then falls out of the accepting states. Give every state a meaning, not only the accepting ones.

Example 2. Strings ending in 1.

0101q1q2
A DFA for strings ending in 1.

State q1q_1. No symbol has been read, or the last symbol read was 0.

State q2q_2. The last symbol read was 1.

Only q2q_2 is accepting, so:

L={ww ends in 1}L=\{w \mid w \text{ ends in }1\}

Accepted: 11, 0101, 101101. Rejected: ϵ\epsilon, 00, 100100.

Example 3. Same transitions, different accepting state.

0101q1q2
The same transitions, but q1 is accepting.

The state meanings are unchanged. Now q1q_1 is accepting, so:

L={ϵ}{ww ends in 0}L=\{\epsilon\}\cup\{w \mid w \text{ ends in }0\}

Changing FF can change the language even when the transitions stay the same.

Accepted: ϵ\epsilon, 00, 110110. Rejected: 11, 0101, 101101.

Example 4. At least one 1, then an even number of 0s.

01100,1q1q2q3
A DFA that tracks whether a 1 has appeared and the parity of later zeros.

State q1q_1. No 1 has been seen.

State q2q_2. At least one 1 has been seen, and the number of 0 symbols after the last 1 is even.

State q3q_3. At least one 1 has been seen, and the number of 0 symbols after the last 1 is odd.

Only q2q_2 is accepting.

Therefore:

L={ww contains at least one 1 and has an even number of 0s after the last 1}L=\{w \mid w \text{ contains at least one }1\text{ and has an even number of }0\text{s after the last }1\}

Accepted: 11, 101101, 100100, 0010000100. Rejected: ϵ\epsilon, 00, 1010, 00100010.

Formal description. A formal description instantiates the tuple M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with concrete sets and a complete transition table. For the machine of Example 4:

Q={q1,q2,q3},Σ={0,1},F={q2}Q=\{q_1,q_2,q_3\}, \quad \Sigma=\{0,1\}, \quad F=\{q_2\}

The start state is q1q_1, and δ\delta is the table on the right. Diagram and description carry exactly the same information.

State01
q1q_1q1q_1q2q_2
q2q_2q3q_3q2q_2
q3q_3q2q_2q2q_2

Proving a DFA Correct

A DFA is correct when L(M)L(M) equals the intended language LL. That is two inclusions at once:

  • Soundness. Every string the machine accepts belongs to LL.
  • Completeness. Every string of LL is accepted.

Both directions follow from one stronger statement that pins down where every string lands, and that statement is proved by induction.

Induction on strings. To prove a claim for all strings: prove it for ϵ\epsilon, then prove that it survives appending one symbol.

  • Base case. Prove the claim for w=ϵw=\epsilon.
  • Induction hypothesis. Assume the claim for an arbitrary string xx.
  • Inductive step. Prove the claim for xσx\sigma, for every symbol σΣ\sigma\in\Sigma.

This is sound because every string is built from ϵ\epsilon by finitely many appends, and it fits DFAs perfectly because the extended transition function is defined by the same last-symbol recursion.

State characterization. For each state qiq_i of a DFA, the set of strings that drive the machine from the start state to qiq_i:

Ti={wΣδ(q0,w)=qi}T_i=\{w\in\Sigma^*\mid \delta(q_0,w)=q_i\}

The sets T0,,TnT_0,\dots,T_n partition Σ\Sigma^*, since every string lands in exactly one state, and L(M)L(M) is the union of the TiT_i whose states are accepting. A characterization is the state meanings made formal.

Recipe: proving L(M)=LL(M)=L.

  1. Characterize every state. Write a precise description of TiT_i for each state qiq_i, accepting or not.
  2. Match the language. Check that LL is exactly the union of the TiT_i for accepting qiq_i.
  3. Claim. For all strings ww: δ(q0,w)=qi\delta(q_0,w)=q_i iff wTiw\in T_i, simultaneously for every ii.
  4. Base case. Check that ϵ\epsilon satisfies the description of the start state and no other.
  5. Inductive step. Assume the claim for xx and prove it for xσx\sigma, by cases over the state after xx and the symbol σ\sigma. Each case is one application of δ(q0,xσ)=δ(δ(q0,x),σ)\delta(q_0,x\sigma)=\delta(\delta(q_0,x),\sigma).
  6. Conclude. The claim plus step 2 give L(M)=LL(M)=L.

Example 5. Even number of a symbols, proved correct.

Let Σ={a,b}\Sigma=\{a,b\} and L={wcount(w,a) is even}L=\{w\mid \mathrm{count}(w,a)\text{ is even}\}, where count(w,a)\mathrm{count}(w,a) is the number of a symbols in ww.

aabbq0q1
A DFA for strings with an even number of a symbols.

The characterizations are:

T0={wcount(w,a) even}T_0=\{w\mid \mathrm{count}(w,a)\text{ even}\}T1={wcount(w,a) odd}T_1=\{w\mid \mathrm{count}(w,a)\text{ odd}\}

Only q0q_0 is accepting, and L=T0L=T_0, so steps 1 and 2 of the recipe are done.

1. Claim. For every string ww, the DFA reaches q0q_0 iff count(w,a)\mathrm{count}(w,a) is even, and reaches q1q_1 iff count(w,a)\mathrm{count}(w,a) is odd.

2. Base case. For w=ϵw=\epsilon, no symbol is read, so δ(q0,ϵ)=q0\delta(q_0,\epsilon)=q_0. Also count(ϵ,a)=0\mathrm{count}(\epsilon,a)=0, which is even.

3. Inductive step. Assume the claim holds for xx. Appending one symbol gives exactly four cases:

State after xxSymbol σ\sigmaState after xσx\sigmaCount effect
q0q_0bbq0q_0even stays even
q0q_0aaq1q_1even becomes odd
q1q_1bbq1q_1odd stays odd
q1q_1aaq0q_0odd becomes even

4. Conclusion. The transition table and the count update agree in every case, so the claim holds for all strings. Since q0q_0 is the only accepting state, the DFA accepts exactly the strings with an even number of a symbols.

Weak claim. A claim that identifies the language but does not pin down every state. It can be true yet unprovable by direct induction, because the induction step needs to know which state the run is in after xx, not only whether xx is currently accepted.

Example 6. At least two a symbols: weak versus strong claims.

Let Σ={a,b}\Sigma=\{a,b\}. The following DFA accepts once it has seen two a symbols and then stays accepting.

aabba,bq0q1q2
A DFA for strings with at least two a symbols.

The language claim is:

L(M)={wcount(w,a)2}L(M)=\{w\mid \mathrm{count}(w,a)\ge 2\}

A tempting proof target is:

δ(q0,w)=q2    count(w,a)2\delta(q_0,w)=q_2 \iff \mathrm{count}(w,a)\ge 2

This target is true, but it is too weak for a direct induction.

The weak hypothesis only says: if δ(q0,x)q2\delta(q_0,x)\ne q_2, then count(x,a)<2\mathrm{count}(x,a)<2. It cannot rule out the combination δ(q0,x)=q0\delta(q_0,x)=q_0 with count(x,a)=1\mathrm{count}(x,a)=1. The real machine never produces that combination, but the induction does not know it, and on it the step breaks: xaxa would have two a symbols while the machine has only reached q1q_1. The claim must distinguish count=0\mathrm{count}=0 from count=1\mathrm{count}=1, because reading a from q0q_0 gives q1q_1 while reading a from q1q_1 gives q2q_2.

The stronger claim names all three state meanings:

δ(q0,w)={q0if count(w,a)=0,q1if count(w,a)=1,q2if count(w,a)2.\delta(q_0,w)= \begin{cases} q_0 & \text{if } \mathrm{count}(w,a)=0,\\ q_1 & \text{if } \mathrm{count}(w,a)=1,\\ q_2 & \text{if } \mathrm{count}(w,a)\ge 2. \end{cases}

When an induction stalls, do not weaken the goal; strengthen it. The right claim names, for every state, exactly which strings lead there.

1. Claim. For every string ww, the reached state is the one named by the piecewise characterization.

2. Base case. For w=ϵw=\epsilon, no symbol is read, so δ(q0,ϵ)=q0\delta(q_0,\epsilon)=q_0, and count(ϵ,a)=0\mathrm{count}(\epsilon,a)=0.

3. Inductive step. Assume the stronger claim holds for xx. The transition function and the count update agree in all six possible cases:

State after xxCount from the hypothesisSymbol σ\sigmaState after xσx\sigmaCount after xσx\sigma
q0q_000bbq0q_000
q0q_000aaq1q_111
q1q_111bbq1q_111
q1q_111aaq2q_222
q2q_22\ge 2bbq2q_22\ge 2
q2q_22\ge 2aaq2q_23\ge 3

4. Conclusion. The stronger claim is preserved after appending either symbol, so it holds for every string. Since q2q_2 is the only accepting state, the DFA accepts exactly the strings with at least two a symbols.

Regular Languages

Regular language. A language LΣL\subseteq\Sigma^* is regular if some DFA recognizes it, that is, if L=L(M)L=L(M) for a DFA MM.

Regularity means finite-state recognizability. The machine may read strings of any length, but it must classify them using only finitely many states.

The language of strings over {0,1}\{0,1\} ending in 1 is regular. The language {0n1nn0}\{0^n1^n\mid n\ge 0\} is not regular, because it needs unbounded counting.

Whether every language is regular, and how to prove that a specific one is not, is the next big question; first come the operations that build new regular languages from old ones.

Operations on Languages

Closure. A class of languages is closed under an operation if applying the operation to languages in the class always produces another language in the class.

Closure results give a second way to prove regularity. The first is direct: design a DFA and prove it correct with the recipe. The second is algebraic: write the language as a combination of known regular languages under operations that preserve regularity, and no new machine or induction is needed.

Complement. The complement of LL over alphabet Σ\Sigma is the set of all strings in Σ\Sigma^* that are not in LL.

L={wΣwL}\overline{L}=\{w\in\Sigma^*\mid w\notin L\}

If M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) recognizes LL, then M=(Q,Σ,δ,q0,QF)M'=(Q,\Sigma,\delta,q_0,Q\setminus F) recognizes L\overline{L}. The construction keeps the same states, start state, and transitions, so every run ends in the same state as before; only the verdict is flipped. Regular languages are therefore closed under complement.

Example 7. Complement of strings ending in 1.

0101NY
L: strings ending in 1.
0101NY
Complement: empty or ending in 0.

The arrows are identical. Only the accepting states change.

Union. The union of two languages contains the strings that belong to at least one of them.

L1L2={wΣwL1 or wL2}L_1\cup L_2=\{w\in\Sigma^*\mid w\in L_1\text{ or }w\in L_2\}

Intersection. The intersection of two languages contains the strings that belong to both of them.

L1L2={wΣwL1 and wL2}L_1\cap L_2=\{w\in\Sigma^*\mid w\in L_1\text{ and }w\in L_2\}

Product construction. The construction that recognizes both: run the two DFAs in parallel by letting one state record a pair of states, one from each machine. For DFAs M1=(Q1,Σ,δ1,s1,F1)M_1=(Q_1,\Sigma,\delta_1,s_1,F_1) and M2=(Q2,Σ,δ2,s2,F2)M_2=(Q_2,\Sigma,\delta_2,s_2,F_2):

PartProduct DFA
StatesQ1×Q2Q_1\times Q_2
Start state(s1,s2)(s_1,s_2)
Transitionδ((r,t),a)=(δ1(r,a),δ2(t,a))\delta((r,t),a)=(\delta_1(r,a),\delta_2(t,a))
Accepting, union{(r,t)rF1 or tF2}\{(r,t)\mid r\in F_1\text{ or }t\in F_2\}
Accepting, intersection{(r,t)rF1 and tF2}\{(r,t)\mid r\in F_1\text{ and }t\in F_2\}

The construction lists every pair, whether or not the pair is reachable, so a product DFA is often larger than necessary but never wrong. If the alphabets differ, first extend both machines to the common alphabet Σ=Σ1Σ2\Sigma=\Sigma_1\cup\Sigma_2 by adding a rejecting dead state for the new symbols.

Two finite memories running side by side are still a finite memory: the pair of current states is all the product needs to remember.

Example 8. Product states for union and intersection.

Let AA be the language of strings containing at least one 1. Let BB be the language of strings with an even number of 0 symbols.

010,1NY
A: strings containing at least one 1.
0011EO
B: strings with an even number of 0s.

In the product machine, the first letter tracks AA and the second letter tracks BB:

Product stateMeaning
NEno 1 seen, even number of 0s
NOno 1 seen, odd number of 0s
YEat least one 1 seen, even number of 0s
YOat least one 1 seen, odd number of 0s

For union, accept NE, YE, and YO, because at least one component accepts.

01010101NENOYEYO
A union B: accept if the A component accepts or the B component accepts.

For intersection, use the same graph but accept only YE, because both components accept only there.

01010101NENOYEYO
A intersection B: accept only if the A component and B component both accept.

The string 1 is in the intersection, since it contains a 1 and has zero 0s. The string 10 is in the union but not the intersection, since it contains a 1 but has an odd number of 0s.

Concatenation. The concatenation of two languages contains the strings obtained by taking one string from the first language followed by one string from the second.

L1L2={xyxL1, yL2}L_1\circ L_2=\{xy\mid x\in L_1,\ y\in L_2\}

It is also written L1L2L_1L_2. A string ww is in L1L2L_1\circ L_2 if it has some split w=xyw=xy with xL1x\in L_1 and yL2y\in L_2; the split point is not marked in the input and not fixed in advance.

Example 9. Concatenating finite languages.

Let L1={0,11}L_1=\{0,11\} and L2={1}L_2=\{1\}. Then L1L2={01,111}L_1\circ L_2=\{01,111\}.

xL1x\in L_1yL2y\in L_2xyL1L2xy\in L_1\circ L_2
00110101
111111111111

The split is the whole difficulty. A DFA reading ww left to right never knows whether the first part is finished, and a deterministic run cannot try all splits at once. Regular languages are closed under concatenation, but the clean automaton construction needs a machine that is allowed to guess.