Lesson

Combining Automata: Complement and Product

A form field with two validity rules: building the product and the complement of deterministic finite automata, proving that the product runs both in parallel, and combining simple validators for and, or and negation.

Last updated:


In this lesson we learn to build the product automaton of two deterministic finite automata (DFAs) and to choose its set of accepting states for the intersection, the union, the difference and the symmetric difference; to build a complement automaton, and why the construction needs a transition for every state and every letter; to prove by induction that the product runs both automata in parallel, and hence that the regular languages are closed under intersection, union and complement; and to break a specification of and, or and negation into simple conditions and combine their automata.
We practise the combination on validators that check several validity rules.

One field, two rules

A sign-up form has a field for a binary code, and the system reads the code in pairs of digits.
The field’s specification sets two validity rules:

  1. The code is an even binary number: a nonempty word over {0,1}\{0,1\} that ends in 00⁠.
  2. The code has an even number of digits.

The code is checked by a validator: a component that reads the characters one at a time, as they are typed, and keeps only a state, like every DFA.
We already have a validator for each rule on its own:

  • The validator AA for an even number (even binary number): state q¬evenq_{\neg\mathrm{even}} (start: no digit read yet, or the last digit is 11⁠) and state qevenq_{\mathrm{even}} (the last digit is 00⁠, so the number is even; accepting). From every state, the digit 00 leads to qevenq_{\mathrm{even}} and the digit 11 to q¬evenq_{\neg\mathrm{even}}⁠.
  • The validator BB for the length: state p0p_0 (an even number of digits read; start, accepting) and state p1p_1 (an odd number of digits read). Every digit swaps the two states. The states of the second validator are written with the letter pp⁠, to tell them apart from the states of AA⁠, and the subscript is the remainder of the number of digits on division by 22⁠.

AA

q¬evenqeven1010

BB

p0p10,10,1
The two validators of the field: AA checks that the code is an even binary number, and BB checks that the number of digits is even.

Transitions of the diagram A:

  • δ(q_¬even, 0) = q_even
  • δ(q_¬even, 1) = q_¬even
  • δ(q_even, 0) = q_even
  • δ(q_even, 1) = q_¬even

Transitions of the diagram B:

  • δ(p_0, 0) = p_1
  • δ(p_0, 1) = p_1
  • δ(p_1, 0) = p_0
  • δ(p_1, 1) = p_0

What goes wrong without combining them? If we run AA and then BB⁠, we must store the input and read it twice, while the validator reads each character once and does not store the input.
If we design a new automaton from scratch, we lose the checking already done on each validator, and every other combination of the rules (one rule or the other, one rule without the other) means designing and checking again.

Think: how do we build one DFA that reads each digit once and checks both rules?
What must it remember, how many states will it have, and what does each digit do?
Plan in words before you open anything below.

Hint

What does each of the two validators remember after each digit?
Can we remember both things together?

Answer

It remembers the state of each validator, that is, a pair: a state of AA and a state of BB⁠.
There are 2⋅2=42\cdot2=4 pairs. We start at the pair of start states (q¬even,p0)(q_{\neg\mathrm{even}},p_0)⁠, and every digit moves both coordinates, each coordinate by its own validator.
The code is valid when the run ends in (qeven,p0)(q_{\mathrm{even}},p_0)⁠: both validators accept.
In the table, the arrow marks the start state and the star the accepting state:

Stateon 0on 1
→(q¬even,p0)\to(q_{\neg\mathrm{even}},p_0)(qeven,p1)(q_{\mathrm{even}},p_1)(q¬even,p1)(q_{\neg\mathrm{even}},p_1)
(q¬even,p1)(q_{\neg\mathrm{even}},p_1)(qeven,p0)(q_{\mathrm{even}},p_0)(q¬even,p0)(q_{\neg\mathrm{even}},p_0)
∗ (qeven,p0){*}\,(q_{\mathrm{even}},p_0)(qeven,p1)(q_{\mathrm{even}},p_1)(q¬even,p1)(q_{\neg\mathrm{even}},p_1)
(qeven,p1)(q_{\mathrm{even}},p_1)(qeven,p0)(q_{\mathrm{even}},p_0)(q¬even,p0)(q_{\neg\mathrm{even}},p_0)
(q¬even,p0)(q¬even,p1)(qeven,p1)(qeven,p0)01010101
The product automaton of the field. In the upper row the first coordinate is q¬evenq_{\neg\mathrm{even}} and in the lower row qevenq_{\mathrm{even}}⁠: the digit 11 always leads to the upper row and the digit 00 to the lower one, and every digit swaps the second coordinate between p0p_0 and p1p_1⁠.

Transitions of the diagram:

  • δ((q_¬even,p_0), 0) = (q_even,p_1)
  • δ((q_¬even,p_0), 1) = (q_¬even,p_1)
  • δ((q_¬even,p_1), 0) = (q_even,p_0)
  • δ((q_¬even,p_1), 1) = (q_¬even,p_0)
  • δ((q_even,p_1), 0) = (q_even,p_0)
  • δ((q_even,p_1), 1) = (q_¬even,p_0)
  • δ((q_even,p_0), 0) = (q_even,p_1)
  • δ((q_even,p_0), 1) = (q_¬even,p_1)

Question 1. Follow the runs on 10101010 and on 110110 in the table.
Which code is valid, and which rule fails for the second code?

Answer

(q¬even,p0)→1(q¬even,p1)→0(qeven,p0)→1(q¬even,p1)→0(qeven,p0)(q_{\neg\mathrm{even}},p_0)\xrightarrow{1}(q_{\neg\mathrm{even}},p_1)\xrightarrow{0}(q_{\mathrm{even}},p_0)\xrightarrow{1}(q_{\neg\mathrm{even}},p_1)\xrightarrow{0}(q_{\mathrm{even}},p_0)⁠, so 10101010 is valid. On 110110 the run is (q¬even,p0)→1(q¬even,p1)→1(q¬even,p0)→0(qeven,p1)(q_{\neg\mathrm{even}},p_0)\xrightarrow{1}(q_{\neg\mathrm{even}},p_1)\xrightarrow{1}(q_{\neg\mathrm{even}},p_0)\xrightarrow{0}(q_{\mathrm{even}},p_1)⁠: the number is even (first coordinate qevenq_{\mathrm{even}}⁠), but it has three digits (second coordinate p1p_1⁠).

Product automaton

The same construction works for any two DFAs over the same alphabet:
the new automaton runs both in parallel on the same input and keeps both current states as a pair.
This is the product automaton. Let A=(QA,Σ,δA,q¬even,FA)A=(Q_A,\Sigma,\delta_A,q_{\neg\mathrm{even}},F_A) and B=(QB,Σ,δB,p0,FB)B=(Q_B,\Sigma,\delta_B,p_0,F_B)⁠. In the product the states are the pairs, and a step on a letter x∈Σx\in\Sigma moves each coordinate by its own automaton:

Q=QA×QBδ((q,p),x)=(δA(q,x), δB(p,x))\begin{aligned} Q&=Q_A\times Q_B\\ \delta\bigl((q,p),x\bigr)&=\bigl(\delta_A(q,x),\ \delta_B(p,x)\bigr) \end{aligned}

The start state is the pair (q¬even,p0)(q_{\neg\mathrm{even}},p_0)⁠. The five-tuple still lacks one component:
the set of accepting states. For the code field we wanted both validators to accept, so the set of accepting states is {(qeven,p0)}\{(q_{\mathrm{even}},p_0)\}⁠, and this is the validator we needed:

M=({q¬even,qeven}×{p0,p1}, {0,1}, δ, (q¬even,p0), {(qeven,p0)})M=\bigl(\{q_{\neg\mathrm{even}},q_{\mathrm{even}}\}\times\{p_0,p_1\},\ \{0,1\},\ \delta,\ (q_{\neg\mathrm{even}},p_0),\ \{(q_{\mathrm{even}},p_0)\}\bigr)

For another combination of the rules we choose another set of accepting states.

Each coordinate of the pair moves exactly as its own automaton does, so after reading a word the product is in the pair (q,p)(q,p) where qq is the state where AA ends on its own and pp the state where BB ends on its own (the proof is in the product’s run on a whole word).
So qq says whether AA accepts and pp whether BB accepts.

Question 2. Fill in the truth table: for which of the four cases should the pair be accepting, if we want the intersection (∩\cap⁠), the union (∪\cup⁠) or the difference (−-⁠)?

qqpp∩\cap∪\cup−-
yesyes???
yesno???
noyes???
nono???
Hint

Ask of each row: is the word in L(A)L(A)⁠, in L(B)L(B)⁠? Then apply the operation to those two yes/no answers, as in a truth table for “and”, “or”, and “and not”.

Answer: the sets of accepting states, written as sets of pairs

The intersection takes only the first row (yes, yes); the union the first three rows; the difference only the second row (yes, no).
As sets of pairs:

Language we wantSet of accepting states FF of the product
L(A)∩L(B)L(A)\cap L(B)FA×FBF_A\times F_B
L(A)∪L(B)L(A)\cup L(B)(FA×QB)∪(QA×FB)\begin{gathered}(F_A\times Q_B)\cup{}\\(Q_A\times F_B)\end{gathered}
L(A)−L(B)L(A)-L(B)FA×(QB−FB)F_A\times(Q_B-F_B)
L(A) △ L(B)L(A)\,\triangle\,L(B)(FA×(QB−FB))∪((QA−FA)×FB)\begin{gathered}\bigl(F_A\times(Q_B-F_B)\bigr)\cup{}\\\bigl((Q_A-F_A)\times F_B\bigr)\end{gathered}

The last row is the symmetric difference: the words that belong to exactly one of the two languages.

Example. Over {a,b}\{a,b\} let AA accept words with an even number of aa‘s (states q0,q1q_0,q_1 by the remainder; aa swaps them, bb keeps them) and BB words whose number of bb‘s is divisible by 33 (states p0,p1,p2p_0,p_1,p_2 by the remainder; bb adds one modulo 33⁠, aa keeps the state).
The product has six states, and each letter moves only one coordinate:

Stateon aaon bb
(q0,p0)(q_0,p_0)(q1,p0)(q_1,p_0)(q0,p1)(q_0,p_1)
(q0,p1)(q_0,p_1)(q1,p1)(q_1,p_1)(q0,p2)(q_0,p_2)
(q0,p2)(q_0,p_2)(q1,p2)(q_1,p_2)(q0,p0)(q_0,p_0)
(q1,p0)(q_1,p_0)(q0,p0)(q_0,p_0)(q1,p1)(q_1,p_1)
(q1,p1)(q_1,p_1)(q0,p1)(q_0,p_1)(q1,p2)(q_1,p_2)
(q1,p2)(q_1,p_2)(q0,p2)(q_0,p_2)(q1,p0)(q_1,p_0)
(q0,p0)(q0,p1)(q0,p2)(q1,p0)(q1,p1)(q1,p2)bbbbbbaaaaaa
The product as a grid of pairs: the row is the state of AA and the column the state of BB⁠. The letter aa moves between the rows and the letter bb between the columns. The double circle marks the accepting state of the intersection.

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_1,p_0)
  • δ((q_0,p_0), b) = (q_0,p_1)
  • δ((q_0,p_1), a) = (q_1,p_1)
  • δ((q_0,p_1), b) = (q_0,p_2)
  • δ((q_0,p_2), a) = (q_1,p_2)
  • δ((q_0,p_2), b) = (q_0,p_0)
  • δ((q_1,p_0), a) = (q_0,p_0)
  • δ((q_1,p_0), b) = (q_1,p_1)
  • δ((q_1,p_1), a) = (q_0,p_1)
  • δ((q_1,p_1), b) = (q_1,p_2)
  • δ((q_1,p_2), a) = (q_0,p_2)
  • δ((q_1,p_2), b) = (q_1,p_0)

For the intersection the set of accepting states is {(q0,p0)}\{(q_0,p_0)\}⁠; for the union it is {(q0,p0),(q0,p1),(q0,p2),(q1,p0)}\{(q_0,p_0),(q_0,p_1),(q_0,p_2),(q_1,p_0)\}⁠; for the difference L(A)−L(B)L(A)-L(B) it is {(q0,p1),(q0,p2)}\{(q_0,p_1),(q_0,p_2)\}⁠.

Question 3. Is aabbbaabbb in L(A)∩L(B)L(A)\cap L(B)⁠? Follow its run in the table.

Worked answer

(q0,p0)→a(q1,p0)→a(q0,p0)→b(q0,p1)→b(q0,p2)→b(q0,p0)(q_0,p_0)\xrightarrow{a}(q_1,p_0)\xrightarrow{a}(q_0,p_0)\xrightarrow{b}(q_0,p_1)\xrightarrow{b}(q_0,p_2)\xrightarrow{b}(q_0,p_0)⁠, which is the accepting state of the intersection. Directly:
two aa‘s (even) and three bb‘s (divisible by 33⁠).

The product’s run on a whole word

The product’s table defines one step. To know that the field’s validator MM accepts exactly the valid codes, we need to know which pair the product is in after reading a whole word.
For this we use the function that returns the state after reading a whole word (the formal definition):
for the product we denote it δ∗\delta^*⁠, and for the two automata δA∗\delta_A^* and δB∗\delta_B^*⁠.

Think: the product starts at the pair (q,p)(q,p) and reads a word ww⁠.
Which pair does it end in? State a claim using δA∗\delta_A^* and δB∗\delta_B^*⁠.

The claim

Lemma. For every pair of states (q,p)∈QA×QB(q,p)\in Q_A\times Q_B and every word w∈Σ∗w\in\Sigma^*⁠:

δ∗((q,p),w)=(δA∗(q,w), δB∗(p,w))\delta^*\bigl((q,p),w\bigr)=\bigl(\delta_A^*(q,w),\ \delta_B^*(p,w)\bigr)

In particular, from the start pair (q0,p0)(q_0,p_0) the product ends in the pair of states where AA and BB each end on their own.

Question 4. Prove the lemma by induction on the word ww⁠, following the definition of δ∗\delta^* on the last letter.
Here we write a nonempty word as w=uxw=ux⁠, with u∈Σ∗u\in\Sigma^* and x∈Σx\in\Sigma⁠.

Hint

In the step, which definition breaks up δ∗((q,p),ux)\delta^*\bigl((q,p),ux\bigr)⁠? After applying it, where can you use the induction hypothesis, and which definition handles the last step on xx⁠?

Proof

The claim P(w)P(w)⁠: for every q∈QAq\in Q_A and every p∈QBp\in Q_B⁠, δ∗((q,p),w)=(δA∗(q,w),δB∗(p,w))\delta^*\bigl((q,p),w\bigr)=\bigl(\delta_A^*(q,w),\delta_B^*(p,w)\bigr)⁠.
The induction is on the length of ww⁠.

Base, w=εw=\varepsilon⁠. By the first case of the definition of δ∗\delta^*⁠, δ∗((q,p),ε)=(q,p)\delta^*\bigl((q,p),\varepsilon\bigr)=(q,p)⁠.
By the same definition in AA and in BB⁠, δA∗(q,ε)=q\delta_A^*(q,\varepsilon)=q and δB∗(p,ε)=p\delta_B^*(p,\varepsilon)=p⁠.
Both sides equal (q,p)(q,p)⁠.

Induction hypothesis: P(u)P(u) holds for the word uu⁠.

Step, w=uxw=ux⁠:

δ∗((q,p),ux)=(1)δ(δ∗((q,p),u), x)=(2)δ((δA∗(q,u), δB∗(p,u)), x)=(3)(δA(δA∗(q,u),x), δB(δB∗(p,u),x))=(4)(δA∗(q,ux), δB∗(p,ux))\begin{aligned} \delta^*\bigl((q,p),ux\bigr) &\overset{(1)}{=}\delta\bigl(\delta^*((q,p),u),\,x\bigr)\\ &\overset{(2)}{=}\delta\bigl((\delta_A^*(q,u),\,\delta_B^*(p,u)),\,x\bigr)\\ &\overset{(3)}{=}\bigl(\delta_A(\delta_A^*(q,u),x),\ \delta_B(\delta_B^*(p,u),x)\bigr)\\ &\overset{(4)}{=}\bigl(\delta_A^*(q,ux),\ \delta_B^*(p,ux)\bigr) \end{aligned}
  1. The second case of the definition of δ∗\delta^* in the product.
  2. The induction hypothesis P(u)P(u)⁠.
  3. The definition of δ\delta in the product, on the pair (δA∗(q,u),δB∗(p,u))\bigl(\delta_A^*(q,u),\delta_B^*(p,u)\bigr) and the letter xx⁠.
  4. The second case of the definitions of δA∗\delta_A^* and δB∗\delta_B^*⁠.

Conclusion: P(u)P(u) implies P(ux)P(ux) for every letter xx⁠, and P(ε)P(\varepsilon) holds; hence P(w)P(w) holds for every word w∈Σ∗w\in\Sigma^*⁠.

What the product accepts. Denote by MM the product of AA and BB with the set of accepting states FA×FBF_A\times F_B (the field’s validator is a special case of it).
For every word ww⁠:

w∈L(M)  ⟺  (1)δ∗((q0,p0),w)∈FA×FB  ⟺  (2)(δA∗(q0,w), δB∗(p0,w))∈FA×FB  ⟺  (3)δA∗(q0,w)∈FA ∧ δB∗(p0,w)∈FB  ⟺  (4)w∈L(A) ∧ w∈L(B)\begin{aligned} w\in L(M) &\overset{(1)}{\iff}\delta^*\bigl((q_0,p_0),w\bigr)\in F_A\times F_B\\ &\overset{(2)}{\iff}\bigl(\delta_A^*(q_0,w),\ \delta_B^*(p_0,w)\bigr)\in F_A\times F_B\\ &\overset{(3)}{\iff}\delta_A^*(q_0,w)\in F_A\ \wedge\ \delta_B^*(p_0,w)\in F_B\\ &\overset{(4)}{\iff}w\in L(A)\ \wedge\ w\in L(B) \end{aligned}

(1) The definition of the language of an automaton; (2) the lemma; (3) the definition of the Cartesian product; (4) the definitions of L(A)L(A) and L(B)L(B)⁠.
Every step is an “if and only if”, so the chain proves both inclusions:
L(M)=L(A)∩L(B)L(M)=L(A)\cap L(B)⁠. For the field’s validator this says that MM accepts exactly the codes that satisfy both rules.

Closure. If L1L_1 and L2L_2 are regular languages, there are DFAs AA and BB with L(A)=L1L(A)=L_1 and L(B)=L2L(B)=L_2⁠, and their product with FA×FBF_A\times F_B recognizes L1∩L2L_1\cap L_2⁠.
Hence L1∩L2L_1\cap L_2 is regular too. We say that the regular languages are closed under intersection:
the operation on two regular languages gives a regular language.

Question 5. Denote by M′M' the product of AA and BB with the set of accepting states (FA×QB)∪(QA×FB)(F_A\times Q_B)\cup(Q_A\times F_B)⁠.
Write the chain of equivalences for L(M′)L(M')⁠, and conclude that the regular languages are closed under union as well.
At which step does the reasoning change?

Hint

The pair (δA∗(q0,w),δB∗(p0,w))\bigl(\delta_A^*(q_0,w),\delta_B^*(p_0,w)\bigr) always belongs to QA×QBQ_A\times Q_B⁠. When does it belong to FA×QBF_A\times Q_B⁠, and when to QA×FBQ_A\times F_B⁠?

Answer

Steps (1) and (2) do not change. In step (3): a pair (q′,p′)∈QA×QB(q',p')\in Q_A\times Q_B belongs to FA×QBF_A\times Q_B if and only if q′∈FAq'\in F_A⁠, and to QA×FBQ_A\times F_B if and only if p′∈FBp'\in F_B⁠; so it belongs to their union if and only if q′∈FAq'\in F_A or p′∈FBp'\in F_B⁠.
Step (4) gives w∈L(A)w\in L(A) or w∈L(B)w\in L(B)⁠, that is, L(M′)=L(A)∪L(B)L(M')=L(A)\cup L(B)⁠. Hence, as for the intersection, the union of two regular languages is a regular language.

Complement automaton

A validity rule is sometimes phrased as a ban: the code does not satisfy some condition.
When we already have a validator for the condition itself, we can build from it a validator for the ban.

Think: what do we change in the even-number validator AA (states q0,qevenq_0,q_{\mathrm{even}}⁠, start q0q_0⁠, accepting qevenq_{\mathrm{even}}⁠) so that it accepts exactly the words that AA rejects?

Hint

Does the run on a word, that is, the state where it ends, depend on the set of accepting states?

Answer

Only the set of accepting states: {q¬even}\{q_{\neg\mathrm{even}}\} instead of {qeven}\{q_{\mathrm{even}}\}⁠. The run does not depend on the set of accepting states, so every word ends in the same state as before, and the verdict on it flips.
The new automaton accepts the empty word and the words that end in 11⁠:
all the words over {0,1}\{0,1\} that are not an even binary number.

AA

q¬evenqeven1010

ACA^C

q¬evenqeven1010
AA and its complement ACA^C⁠: the same states, the same arrows and the same start state; only the double circle moved from qevenq_{\mathrm{even}} to q¬evenq_{\neg\mathrm{even}}⁠.

Transitions of the diagram A:

  • δ(q_¬even, 0) = q_even
  • δ(q_¬even, 1) = q_¬even
  • δ(q_even, 0) = q_even
  • δ(q_even, 1) = q_¬even

Transitions of the diagram A^C:

  • δ(q_¬even, 0) = q_even
  • δ(q_¬even, 1) = q_¬even
  • δ(q_even, 0) = q_even
  • δ(q_even, 1) = q_¬even

Complement automaton. In the same way, for every DFA A=(Q,Σ,δ,s0,F)A=(Q,\Sigma,\delta,s_0,F) we keep the states, the transitions and the start state, and replace the set of accepting states by its complement:

AC=(Q,Σ,δ,s0,Q−F)L(AC)=Σ∗−L(A)\begin{aligned} A^C&=(Q,\Sigma,\delta,s_0,Q-F)\\ L(A^C)&=\Sigma^*-L(A) \end{aligned}

The reason: every word ww has exactly one run, and the state δ∗(s0,w)\delta^*(s_0,w) belongs to QQ⁠.
So w∈L(AC)w\in L(A^C) if and only if δ∗(s0,w)∈Q−F\delta^*(s_0,w)\in Q-F⁠, if and only if δ∗(s0,w)∉F\delta^*(s_0,w)\notin F⁠, if and only if w∉L(A)w\notin L(A)⁠.
This holds for a DFA, which has a transition for every state and every letter, as in the definition.
Hence the complement of a regular language is a regular language too.

Connection to later courses: complement in a machine that does not always stop

Swapping the verdicts works here because a run of a DFA always ends, in an accepting or a non-accepting state.
In a machine whose run may never end, such as a Turing machine (a machine with an unbounded memory tape), a word whose run does not end is not accepted even after the swap, so the swap alone does not give the complement.

Question 6. A common mistake is to change more than the set of accepting states.
To build a complement of the even-number validator AA (states q0,qevenq_0,q_{\mathrm{even}}⁠, start q0q_0⁠, accepting qevenq_{\mathrm{even}}⁠), a student changed the set of accepting states to {q0}\{q_0\} and also moved the start state to qevenq_{\mathrm{even}}⁠.
Find a word on which the student’s automaton is wrong.

Hint

Check the shortest words, beginning with the empty word. Is each of them an even binary number, and does the student’s automaton accept it?

Answer

The empty word ε\varepsilon⁠. It is not an even binary number, so it belongs to the complement.
In the student’s automaton its run stays at the start state qevenq_{\mathrm{even}}⁠, which is not accepting, and it is rejected.
The start state decides where every run begins; changing it changes runs, not only the verdict at their end.
On every nonempty word the student’s automaton is right, because the last digit alone decides the state; so only a test of the empty word reveals the mistake.

Combine simple machines

Think first: a validator accepts the words over {a,b}\{a,b\} that have an even number of aa‘s and do not contain bbbb⁠.
Break the rule into simple conditions and plan in words: for which conditions do we build a DFA, what do we complement, how many states will the product have, and which of them accept?

Hint

“Do not contain” is the negation of “contain”. How many states does a DFA need to accept the words that contain bbbb⁠, and what must it remember?

Answer

Two conditions. An even number of aa‘s: states q0,q1q_0,q_1⁠, accepting q0q_0⁠.
Contains bbbb: states p0p_0 (start, or the last letter is aa⁠), pbp_b (the last letter is bb⁠) and pbbp_{bb} (bbbb found; an accepting trap state).
For the negation we complement: the accepting states are {p0,pb}\{p_0,p_b\}⁠.
The product has 2⋅3=62\cdot3=6 states, and the accepting ones are {q0}×{p0,pb}={(q0,p0),(q0,pb)}\{q_0\}\times\{p_0,p_b\}=\{(q_0,p_0),(q_0,p_b)\}⁠.

This is the sixth strategy: combine simple machines (the first five are in the summary of designing a deterministic finite automaton).
When the specification is a conjunction, a disjunction or a negation of simple conditions, build a DFA for each condition, complement what is phrased as a negation, and combine them in a product with the set of accepting states of the operation.
The machine remembers the state of each simple machine, and its number of states is the product of their numbers of states.

Question 7. Let AA be the DFA over {a,b}\{a,b\} that accepts words containing the letter aa (state q0q_0⁠, start: no aa yet; and state qaq_a⁠, accepting: an aa has appeared. From q0q_0 the letter aa leads to qaq_a and bb keeps the state; qaq_a is a trap state) and BB the DFA that accepts words ending in bb (states p0p_0⁠, start, and pbp_b⁠, accepting; from every state aa leads to p0p_0 and bb to pbp_b⁠).

AA

q0qabaa,b

BB

p0pbabab
The automaton AA (contains aa⁠) and the automaton BB (ends in bb⁠).

Transitions of the diagram A:

  • δ(q_0, a) = q_a
  • δ(q_0, b) = q_0
  • δ(q_a, a) = q_a
  • δ(q_a, b) = q_a

Transitions of the diagram B:

  • δ(p_0, a) = p_0
  • δ(p_0, b) = p_b
  • δ(p_b, a) = p_0
  • δ(p_b, b) = p_b

(a) Build the transition table of the product.

(b) Write the set of accepting states for the language “contains aa and ends in bb”, and for the language “contains aa or ends in bb”.

(c) Run the product on bbabba⁠. Does the word belong to each of the two languages?

Hint

In each row of the table, move each coordinate by its own automaton only.
In (b), ask of each pair whether AA accepts and whether BB accepts.

Answer

(a) The start state is (q0,p0)(q_0,p_0)⁠:

Stateon aaon bb
→(q0,p0)\to(q_0,p_0)(qa,p0)(q_a,p_0)(q0,pb)(q_0,p_b)
(q0,pb)(q_0,p_b)(qa,p0)(q_a,p_0)(q0,pb)(q_0,p_b)
(qa,p0)(q_a,p_0)(qa,p0)(q_a,p_0)(qa,pb)(q_a,p_b)
(qa,pb)(q_a,p_b)(qa,p0)(q_a,p_0)(qa,pb)(q_a,p_b)
(q0,p0)(q0,pb)(qa,p0)(qa,pb)abababab
The product of AA and BB⁠, with the accepting state of "and".

Transitions of the diagram:

  • δ((q_0,p_0), a) = (q_a,p_0)
  • δ((q_0,p_0), b) = (q_0,p_b)
  • δ((q_0,p_b), a) = (q_a,p_0)
  • δ((q_0,p_b), b) = (q_0,p_b)
  • δ((q_a,p_0), a) = (q_a,p_0)
  • δ((q_a,p_0), b) = (q_a,p_b)
  • δ((q_a,p_b), a) = (q_a,p_0)
  • δ((q_a,p_b), b) = (q_a,p_b)

(b) For “and” {(qa,pb)}\{(q_a,p_b)\}⁠; for “or” {(q0,pb),(qa,p0),(qa,pb)}\{(q_0,p_b),(q_a,p_0),(q_a,p_b)\}⁠.

(c) (q0,p0)→b(q0,pb)→b(q0,pb)→a(qa,p0)(q_0,p_0)\xrightarrow{b}(q_0,p_b)\xrightarrow{b}(q_0,p_b)\xrightarrow{a}(q_a,p_0)⁠. The word is rejected for the “and” language (it does not end in bb⁠) and accepted for the “or” language (it contains aa⁠).

Practice

1. Let AA be the DFA over {a,b}\{a,b\} with states q0,qa,qabq_0,q_a,q_{ab}⁠, start state q0q_0 and set of accepting states {qab}\{q_{ab}\}⁠, whose transitions are δ(q0,a)=qa\delta(q_0,a)=q_a⁠, δ(q0,b)=q0\delta(q_0,b)=q_0⁠, δ(qa,a)=qa\delta(q_a,a)=q_a⁠, δ(qa,b)=qab\delta(q_a,b)=q_{ab}⁠, and δ(qab,a)=δ(qab,b)=qab\delta(q_{ab},a)=\delta(q_{ab},b)=q_{ab} (it accepts the words that contain abab⁠).
Build the automaton of the complement and describe its language.

Hint

Keep the states and transitions. Which states were accepting in the original, and which should accept now?

Answer

The set of accepting states is {q0,qa}\{q_0,q_a\}⁠; the language is the words with no abab⁠, namely biajb^ia^j with i,j≥0i,j\geq0⁠.

2. Over {0,1}\{0,1\} let AA be the DFA that accepts words starting with 00 (state q0q_0⁠, start; state qokq_{\mathrm{ok}}⁠, accepting: the input started with 00⁠; and state qtrapq_{\mathrm{trap}}⁠, a rejecting trap state. From q0q_0 the digit 00 leads to qokq_{\mathrm{ok}} and the digit 11 to qtrapq_{\mathrm{trap}}⁠; qokq_{\mathrm{ok}} and qtrapq_{\mathrm{trap}} are traps) and BB the DFA that accepts words whose number of 11‘s is divisible by 33 (states p0,p1,p2p_0,p_1,p_2 by the remainder; start and accepting state p0p_0⁠; 11 adds one modulo 33 and 00 keeps the state).
In the product automaton of AA and BB (states are pairs, start state (q0,p0)(q_0,p_0)⁠), write the set of accepting states for L(B)−L(A)L(B)-L(A) and for the symmetric difference (the words that belong to exactly one of the two languages).

Hint

Which coordinate must accept and which must not? Here BB comes first in the difference, and it is the second coordinate of the pair.
How many states of AA are not accepting?

Answer

L(B)−L(A)L(B)-L(A) has the set of accepting states {(q0,p0),(qtrap,p0)}\{(q_0,p_0),(q_{\mathrm{trap}},p_0)\}⁠: BB accepts, and AA is in one of its two non-accepting states.
The symmetric difference has {(qok,p1),(qok,p2),(q0,p0),(qtrap,p0)}\{(q_{\mathrm{ok}},p_1),(q_{\mathrm{ok}},p_2),(q_0,p_0),(q_{\mathrm{trap}},p_0)\}⁠. For example, the empty word ends in (q0,p0)(q_0,p_0)⁠:
it does not start with 00⁠, and it has zero 11‘s.

3. In the tool below, in machine A, build a DFA over {a,b}\{a,b\} for the words that end with abab⁠.
In machine B, build the DFA with the same states and arrows in which the accepting and the non-accepting states are swapped.
Choose “the other machine” in “Check against” and press “Check”.
The tool returns a shortest counterexample, as on the page Does My Automaton Do What I Meant?.
On which word do they differ? What is the relation between the language of B and the language of A, and why?

The tool needs JavaScript. Solve the question on paper instead.

Hint

Run three short words on both machines and note which machine accepts each.
In which state does each run end, and which of the two machines treats that state as accepting?

Answer

The shortest counterexample is the empty word ε\varepsilon⁠: machine A rejects it and machine B accepts it.
For every word ww both machines end in the same state, which is accepting in exactly one of them, so exactly one of them accepts ww⁠.
Hence the language of B is the complement of the language of A.

Back to Designing a Deterministic Finite Automaton, or see the glossary.