Union of regular languages
Let L1, L2 be two regular languages. We will prove that their union L = L1 ∪ L2 is also a regular language.
Construction using a deterministic automaton
The idea
We have two regular languages L1 and L2 and we want to prove that their union L1 ∪ L2 is again a regular language. Since L1 and L2 are regular languages, there are finite automata A1 and A2 that accept the languages L1 and L2, so L(A1) = L1 and L(A2) = L2. We will now construct a finite automaton A that accepts the language L1 ∪ L2, which proves that the language L1 ∪ L2 is regular.
How do we do that? We have the finite automata A1 and A2, which accept the individual languages. We can say that a string w belongs to the language L1 ∪ L2 if and only if at least one of the automata A1 or A2 accepts it.
The automaton A could work as follows: for an input w it will simulate the run of the automaton A1 on the input w. If A1 accepts the string w, then A accepts the string w too. If A1 rejects the string w, then A tries to simulate the run of A2 on the input w. If A2 accepts the string w, then A accepts the string w as well. Otherwise it rejects.
What remains is to formalise what it means for the automaton A to “simulate” the run of another automaton.
Formalisation
We have two regular languages L1 and L2, which are accepted by the finite automata
We construct a finite automaton A = ⟨Q, Σ, δ, q, F⟩ that accepts the language L = L1 ∪ L2. We will use the idea of simulating the two automata A1, A2. Imagine that the input is the string w = w1w2… wn and that we simulate the runs of the automata A1 and A2 on the string w at the same time. The initial configuration of the automaton A1 is ⟨q1, w1w2… wn⟩ and the initial configuration of A2 is ⟨q2, w1w2… wn⟩. In each automaton we now perform one computation step, which takes us to the configurations ⟨δ1(q1, w1), w2… wn⟩ and ⟨δ2(q2, w1), w2… wn⟩.
We can see that these configurations differ only in the first component; the second one, the unread part of the string, is always the same. So during the simulation we do not need to keep two configurations of two automata; one configuration of the form ⟨⟨qi, qj⟩, wl… wn⟩ is enough, where qi ∈ Q1 and qj ∈ Q2. In other words, the automaton A we are constructing will have the initial configuration ⟨⟨q1, q2⟩, w⟩. The first part of the pair ⟨q1, q2⟩ is the state the automaton A1 is currently in, and the second component is the current state of the automaton A2.
So we can write that the automaton A will have a set of states equal to Q = Q1 × Q2. It will be the Cartesian product of the states of the two previous automata. The idea that follows is that the automaton A will have the start state ⟨q1, q2⟩, and if the automaton A1 moves to the state qi on the symbol w1 and the automaton A2 moves to the state qj on the symbol w1, then the automaton A moves to the state ⟨qi, qj⟩ on the symbol w1.
We write the transition function δ like this (here we already assume that we are currently in the states qi and qj):
All that remains is a few details. For the alphabet, Σ= Σ1 ∪ Σ2. The start state is q = ⟨q1, q2⟩. And the accepting states are all pairs ⟨qi, qj⟩ such that either qi ∈ F1 or qj ∈ F2.
Illustration of the construction
Let us take two automata. The first is the automaton A1, which accepts all strings (including the empty string) in which zeros and ones alternate, that is, strings of the form 01, 0101, 010101, …
The other automaton A2 accepts strings that contain at least one zero:
The union of these languages consists of the strings that either contain a zero or have the form 01, 0101, … Now we construct a finite automaton A = ⟨Q, Σ, δ, q, F⟩ that accepts exactly this union. First we show what the states of this new automaton A will look like. It will be the Cartesian product of the states of the first and second automaton:
These are the six states of the automaton A that accepts the union L(A1) ∪ L(A2). In a diagram they would look like this:
Do not be put off by the states being made up of pairs of states; this is just to help us keep track of what is going on in the automaton. The states could just as well be called q0, …, q5 as usual. The accepting states are those that contain either the state q0 or p1, which are the accepting states of the original automata. The start state is ⟨q0, p0⟩.
Now we have to find all the transitions. We write down a table like this:
And we fill in the table step by step. First we find where the transition from the state ⟨q0, p0⟩ leads on the input 0. We find where the transition from the state q0 on the input 0 leads in the automaton A1: it leads to the state q1. In the automaton A2 the transition from p0 on zero leads to the state p1. So we write ⟨q1, p1⟩ in the table:
On the input 1 we get: for the automaton A1 we have δ1(q0, 1) = q2 and for the automaton A2 we have δ2(p0, 1) = p0. So we get the state ⟨q2, p0⟩.
We fill in the rest of the table:
And from this table we only need to draw the rest of the diagram.
We can check that the automaton works as it should. Let us try the string 0100. The automaton goes through the states
Since the state ⟨q2, p1⟩ is an accepting state, the automaton A accepts the string 0100. How would it turn out if we tried to get the automata A1 and A2 to accept the string 0100? The automaton A1 would go through these states:
The automaton ended in the state q2, which is not accepting, so the automaton A1 would not accept this string. What about the automaton A2?
The state p1 is accepting, so the automaton A2 would accept the string 0100. Notice that the automata A1 and A2 ended in the states q2 and p1, which agrees with the automaton A ending in the state ⟨q2, p1⟩.
Construction using a non-deterministic automaton
Example
We can also prove that the set of regular languages is closed under union by constructing a non-deterministic automaton, which will be much simpler.
So we have two regular languages L1, L2 and we want to prove that their union L = L1 ∪ L2 is also a regular language. Since L1, L2 are regular languages, there must be automata A1, A2 that accept these languages. That is, L(A1) = L1 and L(A2) = L2. Using these automata we construct an automaton A that accepts the language L, that is, L(A) = L.
Suppose that the finite automata A1 and A2 look like this:
and
We would construct an automaton that accepts the union of the two languages by creating a new start state and adding two epsilon transitions from it to the original start states. That is all. The automaton would look like this:
Formalisation
We have two automata A1 = ⟨Q1, Σ, δ1, q1, F1⟩ and A2 = ⟨Q2, Σ, δ2, q2, F2⟩. We construct an automaton A = ⟨Q, Σ, δ, q0, F⟩ that accepts the union of the languages accepted by the previous automata, that is, L(A) = L(A1) ∪ L(A2). Here
- Q = Q1 ∪ Q2 ∪ {q0}
- F = F1 ∪ F2
And we define the transition function δ like this:
Sources
Chapters: Closure of regular languages, Union of regular languages, Intersection of regular languages, Difference of regular languages, Complement of a regular language, Concatenation of regular languages, Kleene closure of a regular language