 Today, we discuss closure properties of regular languages. Let us understand what do we mean by this phrase closure properties. Remember that the class of regular languages by that what we mean is all regular languages, no the set of all regular languages that we have been that is the one we have been referring to as the class of regular languages. And now, so this each element of this class is a language and it is regular l 1, l 2, l 3 and so on. Now, take some operation for example, union you take two languages here l 1 and another language let us say l 2. And when you take the union of these two languages you get another language. So, you can think of this way that union is a binary operation on the set of languages for any class of course. Now, as it happens and we note this fact and we will see that once more that the union of two regular languages is regular. So, let me write it this way if l 1 and l 2 are regular then so is l 1 union l 2. So, what is happening we have this binary operation in this case union we take two languages any two languages from this class and define the new language obtained by taking the union of these two languages. And what do I find that this new language the result of that binary operation is still in the in this class. Because since l 1 l 2 is regular it will occur somewhere in this class itself. So, in other words the operation binary operation union which would give me a language by taking any two languages from here. So, what we are saying once more is that union is an operation a binary operation defined on this class which when performed on any two languages in the class the new language that we obtain still belongs to the same old class. So, in that sense the operation of union does not give me any new language from this class, but the new language formed by taking the union of two languages is inside this. So, another way of saying this what people say that this class is closed the class is closed under union. Because you are not getting any new language when you perform this operation of union. Now, there are many possible operations in particular we will see a few which are binary as well as we will see a few which are unary. And we will show that under all those operation the we never managed to get any extra language by performing that operation on this class. So, therefore, the class is closed under union. So, let us we started with union. So, let us see this the class of regular languages is closed under and here I will provide a list of the operations list of some not not totally exhausted, but a number of operations under which our class is closed. First is union now what is the proof for that fact that the class regular the class of all regular languages is closed under union. Actually, we have seen this proof earlier let me remind you what we did was suppose I have two D F A's M 1 and M 2 and M 1 is Q 1 sigma delta 1 Q 0 and F 1 and M 2 is Q 2 same alphabet sigma delta 2 Q 0 let me put a one here and a subscript and F 2 and one of the classes that we had right in the beginning we had considered taking the product of these two sets of states. So, let me do it here sometimes the new machine M is called the product automation taking the product of these two D F A's. So, this has a set of states and that set is the cross product of the two D F A set of states Q 1 cross Q 2 and what is the idea for this product automaton let me write it here idea is. So, this is the product product we did not use this term, but let me use this now and that we can define in this following manner. The idea is this machine M will keep track of what happens to both the machines M 1 and M 2 at the same time and it does in a very straight forward simple way in its state it keeps track of the two states of the two machine and now when a symbol comes suppose I define this delta in this way delta now see the this state set of states is now pairs right. So, each state here is a pair. So, let me say Q 1 Q 2 is not it a state here is an element of Q and each element here is a pair. So, I wrote like that Q 1 Q 2 and where the first of the pair is a state of M 1 and the second is a state of M 2 right and now suppose I define the this delta in this way which we had seen earlier. This is supposed to give me delta is supposed to give me a state of this machine in the product automaton and suppose we define it as delta 1 of Q 1 A and delta 2 of Q 2 A. So, delta 1 of Q 1 A gives me the state the machine M 1 will be in when it sees a symbol A in state Q 1 and similarly delta 2 Q 2 A defines the state in which the machine M 2 will be when it scans the symbol A on state Q 2. So, you see what is happening you can see that suppose after scanning a part of the string x the two machine the machine M 1 was in Q 1 and the machine Q 2 M 2 was in Q 2 and the product automaton our goal of the product automaton is that it is keeping track in its head. That means in its set of states the what is happening with the two individual machines M 1 and M 2. So, since machine M 1 was in state Q 1 and the machine M 2 was in state Q 2 and now let us say this symbol comes then at this point clearly this will be the state in which the machine M will be the product machine. So, if you define Q and sigma this way Q and delta this way sigma is of course the same sigma as both M 1 and M 2 they have then Q 0 is of course very simple the initial state is what initially the two machines are M 1 and M 2. So, delta 0 the state Q 0 is the pair Q 0 1 and Q 0 2 the index the suffix 1 meant this is the initial state of machine M 1 and the suffix 2 meant that is the initial state of machine M 2. So, initially the product machine will be in this state and now it is very easy to see as we had argued earlier that the machine M on scanning a string x we can say delta hat of for this machine. So, let us say Q 0 and 2 on scanning x clearly it is going to be easy to see delta hat of Q 0 x this is 1 and this is delta hat 2 of Q 0. I am saying the same thing now in terms of the symbol delta hat and what we are saying is that the machine M on seeing x it is going to be in a state whose first component is the state in which the machine M 1 would have been on seeing the string x and whose second component is the state in which the machine M 2 would be on scanning the same string x. So, then it was easy in fact that is what we did for if I now define f as p 1 p 2 such that p 1 is in f 1 this picture or 2 in f 2 remember that f 1 is the set of final states of the machine M 1 f 2 is the set of final states of the machine M 2 and now what does it mean? If this product machine is in one of these states f that means either the machine M 1 has reached one of its final states or the machine M 2 has reached one of its final states. So, that means therefore any string which takes the machine M 1 to a final state or takes the machine M 2 to one of the final states of of course, M 2 then all such strings will be accepted by this machine M. So, therefore clearly the language accepted by this machine M is language accepted by M 1 union language accepted by. So, if you take any two regular languages of course, there will be two D F A corresponding to these two regular languages do this construction to obtain the product machine define the set of final states of this machine in this manner and you have another D F A which accepts the union of the two languages. So, therefore, we can see the class of regular languages is closed under union, but let me give you the same proof more simply now using the notion of regular expression. What do we want to prove that L 1 L 2 be regular union L 2 now that we know the notion of regular expressions and we also know the fact that for every regular regular language there is a regular expression which denotes that regular language can use that fact to prove this much more simply than what we did there. So, let me give you the proof using regular language regular expressions since L 1 L 2 are regular I can claim the existence of these two regular expressions let R 1 and R 2 be two regular expressions such that language denoted by R 1 is L 1 why can I why can I say this can say this because L 1 is regular and we have proved that for every regular language there is a regular expression which denotes that regular language language denoted by the regular expression R 2 is L 2 then by the very definition of regular expressions clearly then the regular expression R 1 plus R 2 by definition L 1 union that is the very definition of the operator plus for regular expressions. Therefore, you see this is a simpler proof for that, but that construction is useful when I want to show some other closure properties in fact the very next closure property that you would like to consider will be intersection. So, here what you would like to prove that if we are given two regular languages L 1 and L 2 their intersection L 1 intersection L 2 is also regular suppose we manage to prove this then clearly I can say that the class of regular languages is closed also under intersection. Now as it happens this kind of proof through regular expressions will not work as simple as that because we do not have any regular expression operation corresponding to inter section. However, this product automaton idea will work what is the class of what is the set of strings which are there in this set L 1 intersection L 2 these are precisely those strings which are both in L 1 as well as in L 2 by definition of intersection. So, what would such strings do to the machines M 1 and M 2 each such string which is both in the language accepted that by the machine M 1 as well as in the language accepted by M 2 all such strings will take the product automaton to a pair of states where both the states are such that P 1 is in F 1 and P 2 is in F 2 that is all I need to do in the definition that is all the change I need from that union definition of the product automaton to the obtaining a product automaton for the intersection of the languages this delta definition remains same q 0 definition remains same and now because of this change this is going to be the language accepted by this product automaton where the set of final states is defined in this manner just change the previous or to add that machine M is going to accept the intersection of the language. So, you see that product automaton became useful in this case and what about you know there are other Boolean operations. So, let me take another one which is complementation now what does it mean that I take a language L the complement recall was defined to be sigma star minus this is an unary operation as opposed to union and intersection which were binary operation on this class of regular languages. Here I take any language I perform this operation of complementation I get a new language and we say that this class is closed under complementation if whenever minus L whenever L is regular its complement is also regular in fact that is the case and how do we know that and again this is a fact something we have seen before if I take a DFA for L I swap the final states the accepting states with the non accepting states the resulting DFA will accept precisely the complement of L we have seen that. So, let me therefore see the class of regular languages is also closed under complementation now there is a binary operation which is something obtained by this notion of difference of two sets and that is set difference if you take L 1 and L 2 two languages which are of course, sets of strings. So, L 1 minus L 2 by definition is the set of all strings such that x is in L 1 and x is not in L again it is the case the that the class of regular languages is closed under set difference which is a binary operation and that is something which is again immediately obvious from this product machine construction again we just need to change the set of final states and now you will say what P 1 is in F 1 and P 2 is in F 2. So, all those strings x which takes the first machine that is the machine for the language L 1 to one of its final states, but does not take the second machine to one of its final states that means the second machine does not accept the string, but the first machine accepts that means if the first machine was for L 1 and the second machine was for L 2. Therefore, this machine will accept all strings which are in L 1, but not in L 2. Actually now that we had done these we could have said it more simply this result without going through the notion of product automaton because you see this another way of saying this would be what L 1 intersection L 2 complement is not this which are the strings which are here all those strings which are in L 1 as well as in L 2 complement that means all those strings which are in L 1, but not in L 2. However, since we have seen that regular languages are closed under complementation. So, if I take a regular language L 2 I do the complementation I get a new language, but that is also regular. So, as we are saying that suppose L 1 and L 2 are regular let me give that argument once more then we have already proved that L 2 complement is also regular and that follows from the fact that the class of regular languages is closed under complementation. So, since L 2 is regular L 2 complement is regular therefore, L 1 intersection L 2 complement is also regular why because in the same thing that I have two regular languages L 1 and L 2 complement and I am taking their intersection and regular languages are closed under intersection. So, therefore, the new language I get is also regular. Therefore, the conclusion is that class of regular languages is closed under set difference once more because I take any two regular languages I consider their difference L 1 minus L 2 and that will be regular because of this argument. See all these operations union intersection complementation set difference these operations are defined on sets. However, there are certain operations which are defined because these are sets of strings. In fact, we know a language is a set of strings and each such string in the set will be finite strings and you have seen some of these two most important concatenation and the cleanies star or cleaning closure will remind ourselves of this definition once more that is we say that we have two languages L 1 and L 2 two languages. Remember what was the definition of the language obtained by concatenating these two languages by definition it was the set of all strings x y such that x is in L 1 and y is in L 2. In other words the concatenation is a binary operation on class of languages obtained by the new languages obtained by taking a string from L 1 taking another string from another string or one string from L 2 put doing the concatenation. And these are the kinds of strings which will be there in the language which is the concatenation of L 1 L 2. Now, can we see immediately that the class of regular languages is closed under concatenation the binary operation concatenation yes indeed because again just think of regular expressions. Since to prove this that the class of regular languages is closed under concatenation again we appeal to regular expressions that will give us the simplest proof to what we will say that suppose L 1 and L 2 are regular then there are regular expressions R 1 R 2 such that the language denoted by R 1 is L 1 and the language denoted by R 2 is L 2 we know that because again the idea is for every regular language there is a regular expression which denotes that regular language. And what is the regular expression R 1 R 2 we know this that this regular expression will denote the language obtained by concatenating the two languages L R 1 language denoted by R 1 and language denoted by R 2 again this is the definition of concatenation for regular expression. So, since I have a regular expression for the language L R 1 L R 2 concatenated therefore, I claim that the regular languages the class of regular languages is closed also under concatenation same with cleanly closure that proof will be very similar only thing then will appeal to the fact that if R 1 is a regular expression then by definition. So, is R 1 star such that the regular the regular language denoted by this is the closure cleanly closure of the language L R 1 where L R 1 is the language denoted by this. So, if I take any regular expression R 1 and I take star of that of course that gives me the language which is the closure of the language denoted by R 1. So, therefore, again the class of regular languages is closed under cleanly closure there is one more example of closure that we had seen earlier which is the operation of reverser. You can see this that this operation is a binary operation on the class of regular languages this is also a binary operation on the class of regular languages. This is an unary operation on the class of regular languages take one language complement it you get another language this is a binary operation this is a binary operation you concatenate two languages and this is again a unary operation you take a language L and then you talk of it is star. So, that is a unary operation this is again going to be a unary operation and let me define that once more. So, L is a language over the alphabet sigma then what is L R reversal of L will take this way is the set of all strings x in sigma star such that when you reverse the string x R on reversing x you get x R that is in the language L. We had seen this, but let us remind one ourselves once more by taking a very simple example supposing L is just two strings 0 0 and 1 1 0 if we make it just very simple language with two strings. So, what we are saying then L R would be we are saying that you take a string from here reverse it you write the string the other way round instead of left to write you write it from right to left for the first string of course, you will get the same string back on reversal for the second string you will get 0 1 1 and this constitutes these two strings will constitute the language which is the reversal of the language L like to remind you what we did to show that if L is regular then so will be the reversal language L R briefly what we did was consider the DFA M for L we are recalling what we did to show that if L is regular then so will be L R we said that look consider a DFA for L call it M and there what you do very briefly I am trying to recall what we do is reverse all arrows in the transition diagram of M that is one thing that you do second thing that will do is maybe a picture would be better. So, this was Q 0 in the of M and let us see we had number of final states the second thing that we do is add a new state right and there are epsilon transitions to the old final states from which epsilon transitions defined to each old final state they are now of course, no longer final states. So, they are just ordinary states only final states will be the old initial state and then this new machine M dash we claim would have accepted the language which is the reversal of the language accepted by the old machine M. Now, that was a proof with that we had given then and it was more as an illustration of non-determinism remember non-determinism came about in two ways because you had epsilon transition as well as when you reversed the directions of the arrows in transition diagram from the same state on the same symbol now they could be more than one states that one could go to. So, we would get a NFA in general and that NFA it is not difficult to see would have accepted the reversal language, but like some of the proofs before now that we know regular expressions this proof also can be made much simpler using the notion of regular expression. So, let me provide the proof now again we will see that since a language L is regular you see this is what I am trying to show that if the language L is regular then it is reversal L R will also be regular. Now, this proof will be a little different from the previous ones because we did not have a direct operation for reversal as we had for concatenation as well as for linear start with our regular expression notation. What we can do is that we say that for every regular expression let me claim this write it properly let R be a regular expression then this notation is a little funny because R on top of I am writing R. So, let me make this instead of R let me make it a let a be a regular expression then a R the reversal of the regular expression now this is something I am going to define will accept a L of a but I have not yet defined what I mean by given a regular expression what is its reversal this I will do inductive. I want to now provide the definition of a regular expression reversal and these kinds of proofs we have seen earlier that I want to claim something here I am trying to define something over all regular expressions and we will do that inductively the way regular expressions themselves were defined inductive. So, remember the base cases base cases where that I had the regular expressions epsilon a and now intuitively what we are trying to do is that if we have a regular expression for the language I am trying to define the regular expression for the reversal of that language. So, I have here epsilon this regular expression stands for the language. So, let me write it language denoted by this regular expression is of course this now what is the reversal language obtained from here take the empty string you reverse it you will get back epsilon right. So, clearly let me define reversal of this is nothing but E r. So, this is a definition remember let me write it put it this way. So, that we know that this is a definition we are defining the reversal of epsilon to be to be itself and the same kind of reasoning will be true for these two also reversal of the regular expression a is the regular expression a itself by definition and the reversal of the regular expression pi is again by definition is going to be. So, what I have done that I have defined for the base cases of regular expression definition what the reversal means now the inductive step we call that we build regular expressions also inductively there after the base cases I had three ways of getting new regular expressions out of all regular expressions. So, we said that if r let me now call it again a 1 and a 2 a 1 and a 2 are regular expressions right then I know a 2 here then I know that there are three separate ways I can build bigger regular expressions out of a 1 and a 2 and they were a 1 and a 1 plus a 2 concatenation a 1 a 2 and let us say a 1 stop. So, this is of course, a unary and this is these two are by. Now, let us see inductively we have defined a 1 r and a 2 r these are already defined then I defined a 1 r and a 2 r these are the reversal of this to be you take the regular expression a 1 take its reversal and now put a 2 take its reversal and similarly in this case and this is again a definition. So, the definition for the reversal of the concatenation language is going to be what and I will come to the reason why we are defining it this way a 2 reversal concatenated with a 1 reversal and a the reversal of the clinic closure is going to be we are defining it as take a 1 and its reversal and then the closure. Now, the claim that we have is see what we have done so far we have a and now we are defining something called reversal of the regular expression and what will finally claim that this regular expression will denote the language which is the reversal of the language denoted by the regular expression a and we will very clearly see this once we understand the motivation behind defining the reversal this way for regular expressions for the base case I have already given you the idea this is what I am trying to ensure. Now, let us take this case suppose I have a 1 and a 2 to regular expression then inductively we have let us assume language denoted by reversal of a 1 the regular expression a 1 is take the language denoted by a 1 its reversal now this is no longer a definition this is a claim because inductively we are making this a claim assume this and similarly l a 2 r is the language obtained by reversing the language denoted by a 2. So, let us say a is the language a 1 plus a 2 this is the language not the language the regular expression a 1 is a regular expression a 2 is a regular expression I am just denoting by this this regular expression. So, what is the language because this denotes the language which is denoted by this regular expression and what is its reversal by definition it is the set of all strings right which whose reversals will be denoted by the will be either in let me write it this way set of all strings x such that x r is in l a 1 or x r is in l a 2 remember this simple a capital A is the regular expression a 1 plus a 2. So, do you see what we are doing here inductively the language denoted by a 1 r is the reversal of the language l a 1 and the language denoted by a 2 r is the reversal of the language l a 2. So, if I just take the plus operation for this two regular expressions then clearly I will have this because by definition the language denoted by this regular expressions will be the set of all strings which are either is in this regular expression in the language denoted by this regular expression or denoted by this regular expression right. So, therefore, the reversal we defined in this manner and now you know inductively I can claim that if a 1 and a 2 are regular expression then the this regular expression a 1 reversal and a 2 reversal will indeed will be the denoted language let me let me write it l right denotes the reversal of what we are saying is if you take the two regular expressions a 1 r and a 2 r simply put a plus between them the new regular expression that you get that will generate all strings which are either the reversal of a string in a 1 the language denoted by a 1 or of a string denoted by a 2. So, therefore, what I am claiming now is that if I have two regular expressions a 1 and a 2 then their reversals their individual reversals if I now just put this in the then how did I define this for this you know it was defined this way and this is going to be the language which is the reversal of the union of the two languages denoted by a 1 and a 2 right. So, you see this is a simple simpler of this example 3 take this case concatenation what we did was that by definition I I had a regular expression a 1 a 2 then its reversal I defined it as take the reversal of the expression which is the regular expression of the second one a 2 reversal followed by the regular expression reversal of a 1. Now, again it is not difficult to see that if you inductively assume that a 1 r denotes the set of all strings which are in the language which constitute the language reversal of a l a 1. So, assume that is language a 2 a 1 r language denoted by this regular expression is language sorry this is a language. So, I need to take reversal here reversal operation is on the language when I do this I am getting a syntactic I am referring to the syntactic expression which we defined in this manner. Now, if this holds then a 1. So, let me claim this a 2 r a 1 r the language denoted by this is indeed the language obtained by a 1 a 2 l of a 1 l of a 2 and the language denoted by concatenate them and now take the reversal is this is also fairly easy to see see what is happening is when you take the concatenation of two languages and then you reverse that language that means initially you will get a string which is in the reversal of a 2 and next that is what you are getting here a 2 r a 2 r inductively will generate all strings which are in the language this followed by you have reversed it. So, now on the reversal finally, the last part will be a string in the language denoted by a 1 and therefore, this will be the situation. So, similarly for clinic closure and therefore, what I am trying to say is that using regular expression we can also prove the fact that class of regular languages is closed under reversal. So, what we have done in today's lecture is that first of all we defined an important notion called closure of a class and then we considered the class of regular languages and proved in fact most of the results were already known by us as examples of DFA NFA etcetera that the class of regular languages is closed under union intersection complementation set difference concatenation clinic closure as well as closed under reversal. And we have also found another interesting thing in this process that the fact that the class of regular languages can be captured by regular expressions that helped us in proving some of these results in a manner which is different or more simple. So, that will be the case always that since for the same class I have different ways of looking at either as regular expressions or as language or as you know or through automata. So, sometimes automata will give us easier proof sometimes regular expressions will give easier proofs for this. We will see some reason why other than of course the fact that these are nice things to know why closure properties are useful.