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:
- The code is an even binary number: a nonempty word over that ends in .
- 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 for an even number (even binary number): state (start: no digit read yet, or the last digit is ) and state (the last digit is , so the number is even; accepting). From every state, the digit leads to and the digit to .
- The validator for the length: state (an even number of digits read; start, accepting) and state (an odd number of digits read). Every digit swaps the two states. The states of the second validator are written with the letter , to tell them apart from the states of , and the subscript is the remainder of the number of digits on division by .
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 and then , 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 and a state of .
There are pairs. We start at the pair of start states , and every digit moves both coordinates, each coordinate by its own validator.
The code is valid when the run ends in : both validators accept.
In the table, the arrow marks the start state and the star the accepting state:
| State | on 0 | on 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 and on in the table.
Which code is valid, and which rule fails for the second code?
Answer
, so is valid. On the run is : the number is even (first coordinate ), but it has three digits (second coordinate ).
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 and . In the product the states are the pairs, and a step on a letter moves each coordinate by its own automaton:
The start state is the pair . 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 , and this is the validator we needed:
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 where is the state where ends on its own and the state where ends on its own (the proof is in the product’s run on a whole word).
So says whether accepts and whether accepts.
Question 2. Fill in the truth table: for which of the four cases should the pair be accepting, if we want the intersection (), the union () or the difference ()?
| yes | yes | ? | ? | ? |
| yes | no | ? | ? | ? |
| no | yes | ? | ? | ? |
| no | no | ? | ? | ? |
Hint
Ask of each row: is the word in , in ? 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 want | Set of accepting states of the product |
|---|---|
The last row is the symmetric difference: the words that belong to exactly one of the two languages.
Example. Over let accept words with an even number of ‘s (states by the remainder; swaps them, keeps them) and words whose number of ‘s is divisible by (states by the remainder; adds one modulo , keeps the state).
The product has six states, and each letter moves only one coordinate:
| State | on | on |
|---|---|---|
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 ; for the union it is ; for the difference it is .
Question 3. Is in ? Follow its run in the table.
Worked answer
, which is the accepting state of the intersection. Directly:
two ‘s (even) and three ‘s (divisible by ).
The product’s run on a whole word
The product’s table defines one step. To know that the field’s validator 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 , and for the two automata and .
Think: the product starts at the pair and reads a word .
Which pair does it end in? State a claim using and .
The claim
Lemma. For every pair of states and every word :
In particular, from the start pair the product ends in the pair of states where and each end on their own.
Question 4. Prove the lemma by induction on the word , following the definition of on the last letter.
Here we write a nonempty word as , with and .
Hint
In the step, which definition breaks up ? After applying it, where can you use the induction hypothesis, and which definition handles the last step on ?
Proof
The claim : for every and every , .
The induction is on the length of .
Base, . By the first case of the definition of , .
By the same definition in and in , and .
Both sides equal .
Induction hypothesis: holds for the word .
Step, :
- The second case of the definition of in the product.
- The induction hypothesis .
- The definition of in the product, on the pair and the letter .
- The second case of the definitions of and .
Conclusion: implies for every letter , and holds; hence holds for every word .
What the product accepts. Denote by the product of and with the set of accepting states (the field’s validator is a special case of it).
For every word :
(1) The definition of the language of an automaton; (2) the lemma; (3) the definition of the Cartesian product; (4) the definitions of and .
Every step is an “if and only if”, so the chain proves both inclusions:
. For the field’s validator this says that accepts exactly the codes that satisfy both rules.
Closure. If and are regular languages, there are DFAs and with and , and their product with recognizes .
Hence 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 the product of and with the set of accepting states .
Write the chain of equivalences for , and conclude that the regular languages are closed under union as well.
At which step does the reasoning change?
Hint
The pair always belongs to . When does it belong to , and when to ?
Answer
Steps (1) and (2) do not change. In step (3): a pair belongs to if and only if , and to if and only if ; so it belongs to their union if and only if or .
Step (4) gives or , that is, . 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 (states , start , accepting ) so that it accepts exactly the words that 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: instead of . 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 :
all the words over that are not an even binary number.
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 we keep the states, the transitions and the start state, and replace the set of accepting states by its complement:
The reason: every word has exactly one run, and the state belongs to .
So if and only if , if and only if , if and only if .
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 (states , start , accepting ), a student changed the set of accepting states to and also moved the start state to .
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 . 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 , 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 that have an even number of ‘s and do not contain .
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 , and what must it remember?
Answer
Two conditions. An even number of ‘s: states , accepting .
Contains : states (start, or the last letter is ), (the last letter is ) and ( found; an accepting trap state).
For the negation we complement: the accepting states are .
The product has states, and the accepting ones are .
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 be the DFA over that accepts words containing the letter (state , start: no yet; and state , accepting: an has appeared. From the letter leads to and keeps the state; is a trap state) and the DFA that accepts words ending in (states , start, and , accepting; from every state leads to and to ).
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 and ends in ”, and for the language “contains or ends in ”.
(c) Run the product on . 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 accepts and whether accepts.
Answer
(a) The start state is :
| State | on | on |
|---|---|---|
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” ; for “or” .
(c) . The word is rejected for the “and” language (it does not end in ) and accepted for the “or” language (it contains ).
Practice
1. Let be the DFA over with states , start state and set of accepting states , whose transitions are , , , , and (it accepts the words that contain ).
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 ; the language is the words with no , namely with .
2. Over let be the DFA that accepts words starting with (state , start; state , accepting: the input started with ; and state , a rejecting trap state. From the digit leads to and the digit to ; and are traps) and the DFA that accepts words whose number of ‘s is divisible by (states by the remainder; start and accepting state ; adds one modulo and keeps the state).
In the product automaton of and (states are pairs, start state ), write the set of accepting states for 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 comes first in the difference, and it is the second coordinate of the pair.
How many states of are not accepting?
Answer
has the set of accepting states : accepts, and is in one of its two non-accepting states.
The symmetric difference has . For example, the empty word ends in :
it does not start with , and it has zero ‘s.
3. In the tool below, in machine A, build a DFA over for the words that end with .
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 : machine A rejects it and machine B accepts it.
For every word both machines end in the same state, which is accepting in exactly one of them, so exactly one of them accepts .
Hence the language of B is the complement of the language of A.
Back to Designing a Deterministic Finite Automaton, or see the glossary.