 Today, we will see some more examples of regular languages. Recall that a regular language is one which is accepted by a DFA. That is, if you want to show a language is regular, one of the ways of doing it would be provided DFA to accept that language. Now, suppose I have an alphabet sigma and let us say I have a language L which is made up of strings from this alphabet. From L, I can define another language which is called the complement of L, which is defined as this denoted as like this and essentially all those strings in sigma star such that x does not belong to L. So, another way of looking at this would be all those strings in sigma star. I am just rewriting it in the same in another manner. So, this is the set of, so this is your sigma star and L was here. So, what is L complement? L complement is all the strings which are outside of this L. So, we can see that L complement is equal to sigma star minus L minus here is the set difference and as you know some people write it as this. So, now what I am, what is very simple to see that if you give a DFA for L, then you almost immediately have a DFA for L complement. So, let me give the idea through this example that we have taken last time. You recall that this DFA accepts the language the set of all strings in which the substring 0 1 1 0 occurs somewhere in the string. Now, what is the complement of this language? So, this is the language. So, what is the complement of the language accepted by M? It is by definition x in 0 1 star such that 0 1 1 0 does not occur as a substring in L in x. Now, notice a string here will take the machine string in this language will take this machine M from its initial state to its final state. Now, a string which is not in this language where will this such a string will take the machine to it will take the machine to either this state or this state or this state or this state, but not to this state. Therefore, if I am interested in accepting only those strings which do not have 0 1 1 0 as substring, then such strings will take this machine from the initial state to one of these first four states, but never to this last state. And therefore, if I simply do this that means what I am doing here is I am switching the final states to non-final states and vice versa that means see this was the old I took the old DFA and in that DFA I am switching the set of final states with the set of non-final states. So, this is my new machine M 1, I obtain from the old machine just by switching the set of final states with the set of non-final states. And now do you see that the language accepted by M 1 is precisely this language let me write it clearly language accepted by this new machine M 1 is the complement of the language accepted by the old machine. So, therefore, once more what is the reason that since in the old machine a string takes the machine to from the from its initial state to its final state if and only if 0 1 1 0 occurs as a substring the complement set is consisting I mean consists of all the strings where 0 1 1 0 does not occur as a substring. So, such strings clearly would take the old machine from its initial state to one of the non-final states, but if we are interested in accepting only those strings where 0 1 1 0 does not occur as a substring then all I do is to take make this flip make the switch between the initial states and the sorry the set of final states and the set of non-final states. So, in other words by. So, I can write the what I am trying to say in a sentence like this if M accepts L then M 1 obtained by obtained from M by switching or interchanging the set of final states with the set of non-final states will accept L complement. If M accepts L then M 1 obtained from M by interchanging the set of final states with the set of non-final states will accept the complement language L full stop. So, therefore, it is quite easy to get the complement language if it if the original language was regular. Now, we can think of some other operations on sets and thereby get new languages from old languages for example, consider union. So, I have two languages L 1 L 2 both are over the alphabet sigma and let me consider this new language which is nothing but the union of L 1 union L 2 and now suppose that L 1 and L 2 are regular. So, L 1 and L 2 are regular and new language L which is the union of these two languages L 1 and L 2 what I would like to show then suppose that L 1 and L 2 are regular then L is also regular. So, in order to prove this first let us understand intuitively what we would like to do. Since L 1 and L 2 are regular L 1 and L 2 are regular there are two D F A's say M 1 and M 2 accepting these two languages respectively. So, this is by definition since L 1 and L 2 are regular by definition we will have two D F A's M 1 and M 2 accepting these two languages and let me say that M 1 has set of states as Q 1 of course, its input alphabet is sigma because it accepts L 1 which is over the alphabet sigma and its transition function is delta 1 and let us say its initial state is Q 0 1 and its final states that set is F 1 and similarly for M 2 let me write Q 2 sigma delta 2 Q 0 2 and F 2. Now, we have these two machines and we would like to accept all those strings which are in this language that means if a string is either in L 1 or in L 2 or in both I should accept that string. My goal is to build a D F A which will accept any string which is accepted either by M 1 or by M 2. Now, you may think of that that can be done in this manner that let us say the input string is X we run machine M 1 on X check whether it is going to its one of its final states that is F 1 and then run M 2 on X and check whether the machine M 2 also goes to or whether it goes to its final states and either of these two machines M 1 or M 2 goes to one of its final states then the string X should be accepted that is clear. But you see then we are going over if this is what we try to do then we would like to go over the string X twice once with M 1 and the other time with M 2. Now, that is not possible if I want to design a D F A because I can scan my input only once from left to right I scan the input and my answer should be ready. So, another possibility would be instead of trying to do it serially which is what I had told you may be what I can do is think of running both the machines parallelly and check if either of the machines goes to its one of its final states and in that case we come up with an answer. Now of course, how do we do this? Now conceptually we can do so by a machine M imagine the machine M has Q which is. So, imagine a machine D F A M which has its set of states as the cross product of the two sets of states of the two old machines M 1 and M 2. So, another way of saying what Q is? Q is P Q such that P is an element of Q 1 and Q is an element of Q 2 remember that Q 1 is the set of states of M 1 Q 2 is the set of states of M 2. So, my idea is something like this the D F A I am trying to design will keep in its finite head the information simultaneously about both the machines. It will remember where the first machine would be after scanning a portion of the input and same time it will remember where the machine M 2 will be after scanning the same part of the input. If this is my goal in that case what I can do is I define the delta of the machine M as follows. Now remember the delta the state transition function takes a state of the machine. Now I am trying to give the delta for M, but M states are pairs of something they are pairs and there of the form P Q. So, suppose it is in a state which is remember this is one state and this one state is a state of the machine M and that one state as it happens here is a pair. First component is a state of the first machine M 1 the second component is the state of the machine M 2. So, this pair which is a state now in this state the machine M C is a symbol A. Now you would like to keep this as invariant that after scanning the symbol A the machine M should remember where M 1 would be and where M 2 would be in the two components. In that case it is fairly clear that the first part of the state of M should be see again this is again a pair and this is the first component of the pair and the second component of the pair. And this is a pair in which the first component is a state of machine M 1 the second component is a state of the machine M 2. And what we are saying or trying to do is something like this that suppose this is the input x and at some point machine M 1 in is in state P machine M 2 is in state Q. In other words what I am trying to say that suppose x was given to the machine M 1 this part of the x would have taken M 1 to P and the same part would have taken M 2 to Q. And therefore, this part would have taken M the new machine I am trying to design in the state P Q. And now the next symbol is A since after scanning this part M 1 was in P M 1 now would be after scanning the symbol M 1 would be in delta 1 P A and M 2 would be in delta 2 Q A. So, therefore, this together will be the state in which this new machine M will be after scanning this part followed by the symbol A. So, if you if you if you do this if you define the delta of the new machine M in this manner. So, my new machine M remember has Q sigma delta Q 0 and F Q you have already said what is the set of states for M is this Q 1 cross Q 2 sigma is of course, the alphabet delta is as defined here. Now, what should be Q 0 remember Q 0 is a state of M and therefore, it is a pair it is a pair in which the first part is from machine M 1 state and the second part of the pair is a state of machine M 2. And this is what initially machine M should be and if my goal is to keep track of through the machine M simultaneously the two states in which M 1 and M 2 should be after scanning something. So, after scanning no string in other words after scanning epsilon or in other words after beginning Q 0 therefore, should be remember that Q 0 1 was the initial state of M 1 and Q 0 2 was the initial state of M 2. This manner what I have what I have done is I have made sure initially the machine M is in a state which is a pair in which the first part is the initial state of the machine M 1 and the second part is the initial state of machine M 2. And we have maintained through the definition of delta the invariant what is the invariant what can I assert if this is the way I define my M. So, I have defined all the way of all these four components it says I mean I can assert it that for all x delta hat of Q 0 1 Q 0 2 which is of course, the initial state of the machine M. So, what we are saying is for all x if the machine M on scanning x goes to the state which is a pair P 1 P 2. This will be the case if and only if delta hat 1 of Q 0 x is P 1 and delta hat of for the machine 2 Q 0 2 x is P 2. This follows from our definition it is not very difficult to see at all. And if you want to prove it I want to prove this if I wish to prove this statement rigorously then I prove it by induction structural induction on the length of x. And once you accept this which is not very difficult to see then if I define for machine M. The final state set as set of final states F as either. So, it is a pair in which either P is in F 1 F 1 is the set of final states of machine M 1 or Q is in F 2. So, now I have completely specified the machine M in this manner. And now you look at this and you should be able to see that the language accepted by M is the language L 1 union L 2. Remember L 1 was the language which was accepted by M 1. So, L M 1 by definition was L 1 and L M 2 was L 2. We define the machine M in this manner and we have managed to keep this. And therefore, this new machine M defined as this is going to accept L 1 union L 2. Why? Very quickly because again this is a set equality. So, let us suppose take a string from this side. So, a string in L 1 union L 2 means that that string is in either in L 1 or in L 2. So, without loss of generality suppose that string x is in L 1. Can you see that this string x? So, let us say x is in L 1 and I want to prove that then x is in language accepted by M y. Suppose x is in L 1 that means delta 1 hat of Q 0 x is in some state P 1 and since x is in L 1 P 1 is one of the final states of M 1. So, this machine M on x would go to P 1 P 2 in which the first part is a final state of M 1 and that state is going to be a final state of M because this is the definition of set of final states of M. And therefore, that string x is accepted and in a manner similar you can prove that if a string is here that means if a string is accepted by the machine M then it has to belong to either L 1 or L 2 or perhaps both. Therefore, we have proved that if we have two languages L 1 and L 2 both are regular then their union will also be a regular language. Now, using very similar idea in fact the definition of the new machine will not change much. So, suppose I was I wanted to prove that L 1 and L 2 are regular to show now that L 1 intersection L 2 is also regular. This again can be proved in a manner very similarly I start with two D F A's one for L 1 one for L 2 exactly like that let us say M 1 is for L 1 M 2 is for L 2 then I take a new machine M whose states keep track of the two states one of M 1 and one of M 2 the other of M 2. And therefore, its states are the set of pairs exactly like this as we had defined before. Here remember that Q was Q 1 cross Q 2 and now we just change this or the set of final states. Definition into and the same invariant remains true. So, new machine I am designing it is the same as the union machine the one machine I designed for the union for accepting the union of the two languages. Only thing I changed was the definition of F and now you should be able to show that this new machine M precisely accepts the intersection of L 1 and L 2. And the proof will be something similar that now consider a string x which is here that means it is both in L 1 as well as in L 2. So, x is in L 1 and x is in L 2 and I would like to show that x is accepted by L M. And on this x the machine M will go to a state which is a pair P Q since x is accepted by M 1 P has to be a final state of M 1 P has to be a final state of M 1. And since x is accepted by M 2 Q has to be a final state of M 2 and therefore, this pair P Q is such that P is a final state of M 1 Q is a final state of M 2, but precisely such states are the final states of my machine M. And therefore, the string x will be accepted by this machine. So, anything here L 1 intersection L 2 once I changed only thing I changed in the old definition union definition to the is the definition of F. And what I get is a machine which accepts the intersection of the two languages L 1 and L 2. Here Q here and now let me define its initial state and its set of final states as follows. We will say that Q 0 is the pair Q 0 1 Q 0 2 and F is all P R such that either P is in F 1 or R is in F 2. Then once I define Q 0 the initial state of M as this and the set of final states as those tuples in which at least one of the components is one of the final states of the previous machine M 1 M 2. Then it is not difficult to see that the language accepted by M is L 1 union L 2. Actually this set of final states as those tuples in which at least one of the components is one of the final states of the previous machine M 1 M 2. Then it is not difficult to see that the language accepted by M is L 1 union L 2. Actually this statement if you prove it rigorously then immediately that follows and proving rigorously the statement is also not difficult because you will just use the properties of delta 1 and delta 2 the two transition functions of the two machines. Therefore what I have is that if you give me two D F A's then in a very simple construction I can create another D F A M which is going to accept the language which is the union of the two languages of the two D F A's. Therefore what we have done is to define show this that if L 1 and L 2 are regular then you take the two D F A's which accept not the two D F A's any D F A for L 1 take a D F A for L 1 take a D F A for L 2 and then use this construction and then argue that the new machine that you have obtained you will accept L 1 union L 2. Now what about intersection? So we have seen a number of facts about regular languages. One is that if L is regular then complement of L is also regular. The other facts we just proved that if L 1 and L 2 are regular then so are the two languages obtained by taking union and the intersection. Besides this we have also seen a couple of very simple examples of regular languages like binary strings in which we have even number of zeros also we took the example that binary strings in which the number of zeros as well as number of ones both these numbers are even and it is fairly simple to prove that every finite language is regular. What do we mean by finite language? A language is finite if it has finitely many strings in it very similar to the definition of finite set you say a set is finite if it has finitely many strings and similarly a language is finite if it has finitely many members in it example would be free. So this language L it has three exactly three strings 0 1 0 0 1 and 1 0 so it has three strings in it and that is a finite number therefore this L is finite. You can prove this without difficulty to fix your idea first maybe you can look at this example itself and how would you design a DFA to accept this language L and then you can generalize that argument that given any arbitrary finite language you can have a DFA to accept that language. So we have a number of languages now we understand which are going to be finite which are going to be regular before we proceed to something else another topic let us take a few more interesting examples of examples of regular languages. So let us take this suppose I have this language L 1 which are strings over 0 1 and 2 and what I want is to define all those x's to be in L 1 if and only if the sum of digits in x is 2 mod 3. So let us take some more examples. String 2 1 0 1 0 1 2 is this in L 1 well so what is the sum of its digits 2 1 so 3 4 5 7 so sum of its digits is same 7 is mod 3 it is not 2 mod 3 and therefore this is not in L 1. On the other hand just the other one I mean I just put a 1 in front 1 2 1 0 1 0 1 2 the sum of digits now is going to be 3 1 4 5 6 8 so this is of course 8 is 2 mod 3 so therefore this is in the language L 1 and I would like to design a DFA for this language. So through these 1 or 2 examples now I am trying to fix once for all our idea about how to go ahead and design DFA. By now you understand that if the DFA tries to remember the sum of the digits it has seen so far of the scanning the prefix of an input string then that strategy is not going to work why so what is the strategy I have in mind the wrong strategy in any case suppose the string x is given to you and remember that idea of a design of a DFA is as we are scanning this string x from left to right we keep some information in the head. And someone says that let us go ahead and the information that we keep in the finite head of the machine is that the we keep in finite state head that is the mind of the machine that is one of the states of the machine is going to keep track of the sum of the digits seen so far. So you have seen all these digits suppose you try to remember in the finite head of the machine this sum now that is a wrong strategy why because this sum can be arbitrarily large think of a string which is going from here to a very far ahead then that sum of the digits in that string is going to be a huge number if you take put some more symbols after that the number is going to become larger and larger. So that sum cannot be kept in the finite head of the of any DFA because DFA can any DFA however many states it may have it can store only finite amount of information. Now how do you solve this problem well the right strategy would be do not keep the sum but keep the sum modulo 3 keep in finite head sum of digits seen so far modulo if that you do at any given time what this is this sum can be modulo 3 there are only 3 possibilities either the sum is 0 some modulo 3 is 0 or 1 or 2 right these are the only 3 possibilities. So let us keep designate 3 states so this state is the sum is 0 mod 3 some is 1 mod 3 and some is 2 mod 3 let us see we are here the sum is 0 mod 3 now a 1 comes where should you go to so let us see some of the digits seen so far is n which is since you are in this state then that sum is 0 mod 3 now a 1 came so some string x was there and up to this the sum was n and now a 1 came therefore the sum up to this is going to be n plus 1. So if n is 0 mod 3 then clearly n plus 1 is 1 mod 3 because n is 0 mod 3 0 plus 1 is 1 mod 3 right so it is easy to see on 1 from here you will go to this state where the sum is 1 mod 3 on 0 where will you go it is not difficult to see by similar argument that will remain in this state and if a digit 2 comes remember that string here consist of these 3 symbols so if a if 2 comes you will come here from this state if a 1 0 comes you will remain here if a 1 comes you will come to this state if a 2 comes when you are here you will go back to go to this state and in this state so here you are here that means what you have seen so far those digits add up to a number which is 2 mod 3 and then a 0 came where are we then is 0 mod 3. So if a 0 came then of course you will remain here because the new sum is also going to be the old sum and therefore if the old sum was 2 mod 3 the new sum is also going to be 2 mod 3 if a 1 comes then you will be here and if a 2 if 2 comes then you are going to be so this was so this almost specifies the language except the DFA except 2 things have given the transition function and I have given the set of states but we have not said what is the initial state initially which state you should be in remember when you start you have seen an empty string the sum of the digits there there are no digits and therefore the sum is 0 so you should be here which is the 0 mod 3 and you want to accept only those strings which take the machine where the sum of the digits is 2 mod 3 such strings will take the machine to this state and therefore we make this accepting. So therefore this machine will accept this language. Now let us take this particular example a little more involved by defining another language L 2 now this time the strings are binary strings and we will see this the number binary representation is x that number is so what what we mean by this is something like this we are talking of strings which are over 0 1 so these are binary strings so let us say I have 0 1 0 1 1 0 I can see such a string to be representing a number whose binary representation is this string and what is that number you know that so you take this is 1 multiplied by 0 so 1 multiplied by 0 plus so 1 multiplied by 2 4 8 16 32 so basically we have a bit 1 here here and here so 16 plus 4 20 plus 2 22 so therefore we can say that this string represents the number 22 is just the binary representation of this thing is of course usually this but if you put a 0 in front the number does not change this is it. So things are similar in that case what is we expect that we should be able to have a DFA and indeed we will be able to have a DFA and again what we can do is to keep track of not the number represented by the string you have seen so far but that number in modulo 3 that information is what we have we will keep in the finite head so therefore the machine for L 2 again will have three states let me say it in this way what we are trying we have a input is this some number some bit string supposing this is the bit string what we are trying to do is that suppose I have seen up to this point the DFA has seen up to this point then it should be after scanning up to this point the machine the DFA should be in a state which captures the information as to what is that information it should capture this is the string it has seen so far therefore it should capture the information as to modulo 3 what is the number represented so far. So if this is the bit string that you have seen so far then what is that number this number is going to be 1 2 plus 2 3 then this is 8 this is 16 16 plus 8 24 plus 3 25 so if you see this if your DFA has seen scanned up to this point then it should record that this string is what is modulo 3 what is modulo 3 what is modulo 3 of the string seen so far this is of course 1. So then it is do the problem here for this language again I will have three states the initial I have seen an initial prefix of the string input string and that string that prefix spells out number and that number is 0 mod 3 if I want what I wish to do is do that such a string should take the machine to this state if the number seen so far is 1 mod 3 then it should take the machine to this state and if the number is 2 mod 3 then I would like the machine to come here. So that way now we are clear what the three states should be the what should be the transition. So let me take one example I mean one of these states now they can be there are two transitions right transition on 0 and transition on 1 correct. So suppose some string has taken the machine to this state and now a 0 comes if this x represented the number a the binary string here represented the number n then the binary string x 0 represents let me write it clearly if x represents n then x 0 represents what 2 n the number 2 n clear is not it supposing you have some string and you put whatever is the number here if you put a 0 in front that means you are taking multiplying the number by 2 that is you know from our understanding of binary representations of number. So what I know was which is I would like to know which is the state the machine should be after scanning a 0 from here if it started if the machine was already here the machine is in this state that means it has scanned a string x whose which is the binary representation of a number let us say n which is 2 mod 3 n is 2 mod 3. And now when the 0 came then the number became 2 n right this n is 2 mod 3. So this is 2 into 2 n is you can replace n by 2 because we are going modulo 3 n is 2 mod 3. So this is 4 which is of course 1 in case of mod 3 right. So if a 0 comes I should come to this state if a 0 came and we are in this state we should be here and if a 1 comes if so again argue the same way. So basically you had seen x and now a 1 came and now suppose that x represented the number n which took the machine to this state and therefore since it is here this n is 2 mod 3. So this is about x what is the number represented by the string x 1 x 1 represents if you think just recall your x 1. Understanding of binary representations of number so if x represents n x followed by 1 represents 2 n plus 1 right n was 2 mod 3. So this is 4 plus 1 mod 3 which is 5 mod 3 which is of course 2 mod 3 right 5 mod 3 is 2 this is we are only we are going modulo 3 that is why I am writing this equivalence rather than equality and so if a 1 came I should remain here. This way you can fill up the other two states which we leave it as an exercise again as before the initial state was this. We can complete the rest of the TFA transition table in arguing in a manner like this I have done it for this state we can do the rest of the states ourselves. Now one point I want to make is this that you see that this is the it is not the case that DFA can do modulo arithmetic in fact it cannot even you know it cannot divide that and things like that. And yet we manage to remember that this will be the accepting state once we complete this diagram fully and this will accept we have a we can design a DFA in this manner which will accept this language L 2 how did we manage to do that. So one way people say that look the machine itself what it whatever it is doing is very simple or a state it sees a symbol it just goes there or goes to some other thing other state or the other there are only three states. And yet it manages to do something which is looks like that it is required to do some arithmetic. But the knowledge about modular arithmetic you are putting it yourselves as a designer in through your these transition diagrams remember that for the machine it is very simple if it is in this state if a 1 comes it should come back here if a 0 comes it should go there. In order to figure what should be as a designer you needed to know all this modular arithmetic some of the simple facts about modular arithmetic. And thereby you manage to put the two transitions for this state and similarly you can do for the rest of the states other two states. So sometimes we put it this way the knowledge of modular arithmetic required for this problem has been hardwired into this DFA. So while designing DFA when we of the lesson that I am trying to impart is that when we see such a thing we just do not go and say oh can a DFA do modular arithmetic only then such a language can be accepted by DFA no no we do not do that. DFA can only do very simple things like basically it is in a state and on a symbol it will make a transition. But it is for us to figure out what the transition should be in order to capture the essence of the modular arithmetic as we have done here.