✖

Difference of regular languages

Let L1, L2 be two regular languages. We will prove that their difference L = L1 ∖ L2 is also a regular language.

Idea

There is really not much to prove. For the difference of two sets, the following relation holds:

L1∖L2=L1∩L2c,

where L2c is the complement of the language L2. From the chapters on the intersection of regular languages and the complement of a regular language, we know that regular languages are closed under both of these operations. This means that we can construct an automaton that accepts the intersection of languages and an automaton that accepts the complement of a language. We can therefore use these automata to construct an automaton that accepts the language L1∩L2c, which is at the same time the language L1 ∖ L2.

Example

Let A1 be this automaton, which accepts all strings that contain an even number of 1s:

and let A2 be an automaton that accepts strings containing an odd number of 0s:

We will construct an automaton that accepts the difference of these languages. We need an automaton A2c, which will be the complement of the automaton A2. We just swap the accepting and non-accepting states:

Now we only need to construct the automaton A, which will accept the intersection of the languages L(A1) and L(A2c):

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