 Recall, we had defined an alphabet to be a set of symbols, a finite set of symbols and as an example, we could have taken 0 or 1 or a b c d through z or any other set of symbols finite set of symbols that you choose to have. Then we also defined sigma star to be, this was defined to be the set of all eight strings over sigma. So, in case our sigma is just these two 0 and 1, then sigma star for this sigma star is the set of all finite binary strings. And another notion that we defined last time was the notion of a language. We said a language L over the alphabet sigma is a subset of sigma star. And throughout this course, one theme that will continue is that, we will consider various languages and see how we can solve the membership problem of these languages. Recall, if I have a language L, then the membership problem for L is given a string x to decide if x is in L. And today, we will take a very simple, very intuitive way of solving these problems for a class of languages. And we will do that. In fact, all these membership problems that we will be solving will have, we will define some machines or grammars and these terms will be clear as we go by. So, we will define something and we will show that this machine or this grammar will be able to, if you use that, then you will be able to solve the membership problem of this language or that language. And as I said today, our topic is finite state machines. Machines, when we talk of machines, of course, we do not really mean machines like lathe machines or drilling machine. But machine in the sense, things we do mechanistically. And you will see that these are also kinds of very specified kinds of algorithms. And best will be, when we go through some example machines, then we will be clear what we mean by machines possibly. Incidentally, these are also called, which we are going to study today, finite state automata. The word automata is the plural of the word automaton. This is singular and plural form is automata. Basically, they mean what we mean machines in our context. So, let me introduce the notion of finite state automata or finite state machines through an example .I will introduce the notion of finite state machines or finite state automata through a very simple example. So, our example is going to be a language over this sigma, which is just 0 and 1. And let me say the language is the set of, let me write it in this way. All strings x, which are in 0, 1 star, immediately then I know I am talking of finite binary strings, such that x has even number of zeros. So, this language is very clear that for example, if I take this string, this is in this language, reason it has 1, 2, 3, 4 zeros. If I just take a string of 1s, this is also in the same language L, because the number of zeros in this is 0, which is an even number. So, this is in L. On the other hand, if I take something like 1, 0, 1, 1, 0, 1, 1, 0, 0, this has 1, 2, 3, 4 and then suppose I put another 0, this is not in the language, because this has 1, 2, 3, 4, 5 zeros, which language is the language we are discussing. Of course, you will say that this is a very trivial problem, I will just count the number of zeros and if that count happens to be even, then I will say that string is in the language, otherwise it is not. Yes, you could do it that way. However, we will restrict ourselves, so that we motivate us towards this notion of finite state automata. I saying that look, the way you have to do this, make this decision, solve the membership question of this problem, this language is this, that when the string is given to you, you will start scanning from the left end. So, let me write it down. So, start scanning the string from its left end, go with the string one symbol at a time and when you come to the end of the string, when the string is over, when you are the scanning process, as you are going from left end and you are going down, down, down, when you go beyond the string, that means when you scan the entire string, your answer should be ready, to be ready with the answer as soon as the string is entirely scanned. So, think of the string, what we are saying is, you will start from the left end, from the left end. So, right in the beginning, you will see a 0 and then you will come to this symbol, then this symbol, then this symbol and so on. By the time you finish scanning the string, entire string, your answer should be ready. And answer about what? Answering that whether this string belongs to the language L, that means whether it has even number of 0s or not, thereby solving the membership problem for this language. If you think a little bit, the way to do this would be, one way to do this would be satisfying these constraints, that what at any given time after scanning some prefix of the string, I will remember whether the number of 0s I have seen so far is even or odd. So, in the beginning you have, when you started, you have not seen anything. So, imagine you are just about to start scanning the string from the left end. So, therefore, you have not seen any symbol. So, at that time, how many 0s have you seen? You have seen 0 number of 0s. So, then that time, your state of the mind is that I have seen 0 0s or even number of 0s. Then, when you see the first symbol here, in this case it happens to be a 0. So, then when you finish scanning this symbol, your state of the mind will be that I have seen so far and odd number of 0s. Then, you will scan. So, let me write this. In the beginning, you had seen even number of 0s. That will be true for beginning of any string. When I say beginning, that means, when I have not seen anything. Of course, that means, I have seen the empty string which has 0 symbols. Therefore, it has even number of 0s. So, I will see initially the state of my mind as I am doing this is that I have seen even number of 0s. When I see this 0, then I say that look now, I have seen odd number of 0s. Then, I see a 1. My state of mind does not change because I have seen for this first part, this 0 and 1. I have seen odd number of 0s. Then, again another 1 comes. Even now, I have seen only odd number of 0s. Then, when I scan this 1, I say that my state of mind, if you allow me to say it that way, my state of mind is that now, that I have seen even number of 0s. Again, my state of mind does not change. I have seen even number of 0s. I have seen even number of 0s. I have seen even number of 0s. Now, when I finish scanning this, I have seen odd number of 0s. Now, when I come here, I have seen even number of 0s. So, as and when I finish scanning this entire string, I know that number of 0s in this string was even. On the other hand, supposing my string was 1 0 1 1 0 0 1. Again, if we do it that way, in the beginning, I have seen nothing. So, even number of 0s I have seen. Even number of 0s I have seen. Here, when I finish scanning this symbol, odd number of 0s, odd number of 0s. After this, even number of 0s, odd number of 0s, odd number of 0s. So, it is possible to satisfy these constraints and be able to decide whether or not the string, which was given to me as even number of 0s or odd number of 0s. So, let me see what we are doing through a picture. For a diagram, let me say that my mind had two states. So, far as this problem is concerned, in this state, I have seen even 0s. Here, I have seen odd 0s. And how do I go from one state to another? Because you see, a symbol came. And when I after scanning that symbol, I may depending on whether it was 0 or 1, I may change the state of my mind. So, for example, in case a 1 comes, I am here. That is, I have seen even number of 0s and a 1 comes. Should I change my state? No, because even then, I have seen only an even number of 0s. So, what this means is, suppose I have scanned so far, I have scanned the number prefix of the string, which has even number of 0s. And then, I scan another symbol, which is a 1. Still clearly, I have seen only even number of 0s. So, therefore, I come back to the same state. And you will see this, if I use in this manner. If a 0 comes, then if I had seen so far even number of 0s, I will come here. My state of mind will record that I have seen odd number of 0s. And here, if I see a 1, I will remain in the same state of mind. And if I see a 0 here, then of course, previously I had seen so far odd number of 0s, 1 more 0 came. Therefore, I have seen even number of 0s. So, I move back and forth from state to state, just only two states in my mind to do this problem. Suffice is that we keep just this two states of mind. And as and when a symbol comes, I may or may not change my state depending on what the symbol was. Now, you see in the beginning, I said that when this process of scanning the input string starts, where am I? Which of these two states? I will be here, because I had not seen anything. So, therefore, I had seen 0 0s. So, I am in this state and so this state is where this whole process begins. So, let me identify this state in this manner and arrow coming from nowhere. In this picture, it means in the beginning, initially we are here in this state. And when I end, I will say the string is in the language. Remember the language was the set of all strings, which has even number of 0s. If after finishing scanning the string, if I happen to be in this particular state. So, the state in which or the set of states in which if I am in, after scanning the entire string, if those states are such that they signal to me that the string is in the language, such states also need to be marked spatially like as in this case. And we will just put distinguish such states by making a double circle. You will agree that whatever it is, this picture tells me the way of deciding whether a binary string has even number of 0s or not. But in this manner, when it is starting my scanning from the left end going symbol by symbol. And by the time I end scanning the entire string, I have my decision. So, let us just see the components of the kinds of things that we have seen in this very simple example. We had these two things and I have been telling that these are my states of mind for this problem. So, we have some states. So, component one of this picture is a set of states. And how many states? Well, it does not matter in this case there are two, but such a set I will if I will make use the notation capital Q that will denote the set of states in pictures like this. Then what did we have? In this picture if you see, I had these 0s and 1s and this marked here 0, 0, 1, 1. They are what? They are the symbols of my alphabet. So, of course, we will see many more examples of this kinds of diagrams, but even for this diagram it is clear that what I put next to an arrow is a symbol of my alphabet. And you see if you are in some state then clearly any of those symbols from your alphabet can be the next symbol. We are in this state a 0 may come or a 1 may come. So, here we have this alphabet which is the input alphabet and as usual we will call it capital sigma. Then very important thing is the rule for going from state to state. What do we do? I mean what is this idea? That we are in some state a symbol comes like we are in this state the symbol 0 came then we move to this state. So, this is called state transition. Now, whatever is in this picture I can think of that I have a mapping delta. This is called transition function and this transition function what is it doing? What is it telling me that if I am in some state and a symbol comes which is the next symbol of the input then which is the state I will go to. So, this is a mapping whose domain is the product of the set of states and the set of symbols and the range is the set of states. Now, let me name these states this way. So, let me say this is this state what I had written here is for my own understanding, but in general I will give some names like q 0 q 1. So, my two states in this example q was capital q was q 0 and q 1 and your delta will look like this something like you can imagine the delta is a mapping which is taking the present state and the symbol seen and here I have next state right this is your. So, if the present state is q 0 and if I see 0 then next state is q 1 if the present state is q 0 if I see a 1 I remain in the same state q 0 if the present state q state is q 1 I see a 0 I go to the state q 0 and if I am in the state q 1 and the symbol is 1 and then we go to the state we remain in the same state right this completely specifies the transition function as we have used in this example. Now, let us just generalize this we will see a finite state automaton f s a and f s a m is in this manner what are the components it has q sigma delta, but you know there are some other things in this diagram which so far I have not put down what are the other two things one is where I started from. So, I here in this diagram this arrow coming from nowhere represented that state I was initially. So, my fourth component is initial state and the final component is of that in this picture like in this case this state q 0 was specially marked to signal that if I end here that means the string is in the language. So, they can be a number of states which are like that all those states so let me write this here set of states ending at which means string in language. So, there are five really if you look at this that there are all these five components were there are nothing more the set of states the input alphabet sigma a transition function which I specified diagrammatically here delta. And then an initial state if we call it q 0 and this set of states ending at which means string is in l there is a set of states is a name for this kind of set states they are the set of final states or accepting states. So, let me write these components set of states alphabet input alphabet this is the state transition function this is the initial state is a one initial state is not a set it is just one unique state where you start and this is the set of final states or accepting states. See these set of states ending at which means string is in l this f let me call it as accepting or final states one important thing to say an f s a there is this word finite is there what does this finite mean this means very importantly q is a finite state finite set in other words this machine may have may be one million one million states you know not this particular one, but in general, but the number of that number of states total number of states is something finite. So, in other words this is what I am trying to say is and f s a or finite state automaton m is something which has these five components a set of states an input alphabet state transition function the initial state and the set of accepting states. And you can see some of the relationships that we have we have already mentioned them one is that of course, we say that q cardinality of q is finite. So, q is a finite set set of course, is also finite is a finite set of symbols. Now, what was delta delta was a map from q cross sigma to q and see that q is finite sigma is finite. So, this map can be specified by a table like this excuse me if this had hundred states of states and q had there are hundred states there are ten symbols then how many entries would I have in this table this can take hundred values this can take ten values. So, total number of entries in this will be thousand for every states symbol pair I should tell what is the next state is. So, delta is a mapping and therefore, you can see this is also something very finite and depending on the size of q and sigma the size of delta is there I mean that it is thereby determined and q 0 the initial state is an element of q and f the accepting or the final set of final states is a subset of q. So, now all this may at this point of time seem very artificial because a very trivial problem that we did and from there we are generalizing. But, let us see a few more examples to fix this idea of finite state machines. So, my next language that I will consider for which I will provide gain another finite state automaton is slight general generalization of this same problem. So, my next language is L 1 which is also a language over the binary alphabet. So, this is set of all strings over the binary alphabet. So, this is set of all strings so, immediately I am saying it once more that immediately I know an x is a finite binary string and now x has even number of zeros and has even number of let me rub this out we should remember what we are trying to do we are trying to give a finite state automaton which will solve the decision problem of this set L 1 and I specify a finite state automaton by providing some machine m which has this five components q sigma delta q 0 and f and the problem at hand is can I give a machine m which will accept or I am using a word which will I will define a little later. But, basically what I am trying to say the same thing that I want to give a finite state automaton such that this when this automaton finishes scanning this remember the or game is that whatever string is given we start from the left end supposing this string start from the left end scan symbol by symbol and then when I come here my decision is already made decision in this case whether or not the string that I have seen so far seen when I have seen it entirely whether it has even number of zeros as well as even number of words and we would like to do it in the same style. So, let us think if we again go back to our old picture that for solving this problem. So, our problem is we have this language L 1 and we would like to define to define an f s a finite state automaton. Which will be such that when that automaton finishes scanning the string which is given to it any string which is given to it as an input this automaton will be in a state which will signal to me whether the string has even number of zeros and even number of ones. So, we kind of extend our old idea let us think that what are the so called the so called states that I need to be able to do this. So, again the same idea will generalize will say that look supposing I have a string like 0 1 1 0 1 1 0 0 1 0. So, if I have come here that is I have seen a prefix of the string I will remember whether I have seen even number of zeros and even number of ones or even number of zeros and odd number of ones. So, let us see what are the possibilities supposing I am here what are the possibilities that could be either I have seen so far even zeros even ones even zeros. Zeros odd ones odd zeros odd ones and odd zeros even ones you will agree that any binary string will fall in one of these and what more you see for example, here this string has even number of zeros and even number of ones and now I see the next zero then what happens it means immediately I have seen an odd number of zeros when I finish scanning this particular symbol I have seen an odd number of zeros and since number of ones did not change I have seen odd number of zeros and even number of ones. So, we can actually do this way that let me have these four states even zeros even ones and odd zeros even ones odd zeros odd ones and odd zeros even ones odd zeros odd ones and here even let us say odd zeros odd ones so what is the one which is left even zeros even ones odd zeros even ones odd zeros odd ones even zeros and odd ones even zeros odd ones. So, here a zero comes we are in this state of mind a zero comes then what happens clearly this is the situation similar is the situation I had scanned the string up to this I have seen even number of zeros and a zero came so clearly now if a zero comes the number of ones do not change. So, still well have seen only even number of ones, but now because of these zero total number of zeros I have seen is odd and you can see if from here if we have seen odd number of zeros and even number of ones and another zero came here then I would have my state of mind would be that I would have seen even number of zeros even number of ones and what about here if I see a zero of course we have taken care of and here if I see a one what happens even number of zeros the number of zeros will remain zeros, but if I see a one that means number of ones I would have seen will be odd. So, even zeros and odd ones is the state I will go to if I see a one here from here if I see a one what happens I will go here clearly right now in this state this means so far I have seen odd number of zeros and odd number of ones if I see a zero then I will come here see I have seen so far odd number of zeros and odd number of ones one more zero came. So, therefore, I would have seen now even number of zeros and odd number of ones and if I see a one in the state of mind for whatever you call it then I will go to this state see odd number of zeros odd number of ones one more one came. So, odd number of zeros even number of ones and now you can complete the picture even number of zeros odd number of ones if a zero came then I will go to odd number of zeros and even odd number of ones and here if a so this is a zero and this is this arrow will be there because if there are even number of zeros and odd number of ones and one came and then I would be in seen even number of zeros and even number of ones. So, of these I have specified so this has four states sigma is of course zero and one these diagram clearly tells me what delta is the transition function but what about the initial state clearly when I have initially I have seen the empty string which has even number of zeros and even number of ones. So, this is the initial state which are the states in which if I am in at the end of the scan and the string is in the language it has even number of zeros and even number of ones you can see this is the only state which should be marked like that. So, for this example I have now again I will rub these things out because this was for my convenience I will say this is q zero this is the state q one this is let me say q two small q two and this is q three. So, the machine for l one m for l one one machine they may be many others, but at least this one has q is q zero q one q two q three delta is you know you can work you can work you can just translate this picture into a table like the one I did before for example, here again I will have now this let me let me give you couple of lines of this if the present state is q zero present input symbol is zero then my next state is going to be q one. So, in this manner you can complete the rest of the table and how many entries this table will have q has four states sigma has two symbols. So, for each pair of state and symbol I need a line here. So, it will have totally eight lines and this is delta you can complete this and remember what was q zero the initial state q zero is the initial state for this machine and this set f consists again only of the state q zero. Now, before we take up other examples let me formalize a little bit more. So, this I told you what an FSA will define will define the these FSA is to be it has five components, but this business about when to say the string is in the language that was kind of left somewhat vague. So, let me make that a little more formal let me specified in a manner which is totally unambiguous. So, remember what delta was delta was a mapping from q cross sigma to q, but you see actually I can extend this mapping to define a one which if I am in some state and then a string comes instead of a symbol after scanning that string from that state starting from that state which state I shall be. So, let me put it this way delta hat is the function which tells the state the machine the order of the function. Automaton will be after scanning the string some string x starting from the state formally what is delta hat what is the domain and range delta hat is a function which starting from a some state if you give a string. Now, what is the notation for any string or set of all strings finite strings of course these strings are over the alphabet sigma. So, this is so this means it and a string. So, I am given a state I have given a string and what is the state the automaton will be. So, delta hat the domain is state string pair and the range is going to be a state. So, for example, in this supposing I am in q 1 and the string that came is 1 1 0 1 which is the state I am going to be in oh I did not write. So, q 1 then 1 came. So, I am here then another 1 came I am here then a 0 came remember I will take this edge then a 1 came then I am here. So, actually this picture tells me even that information starting from any state given any string where the machine or where the automaton will be after scanning that string starting from this state. You can see this and you can do it for any string, but can we do it formally can we define this function delta hat given that function delta and that is not too difficult. So, I want to define this function for all strings which is an infinite set all finite strings over sigma. So, let me this is an example of an inductive definition. So, inductive definitions you have seen other examples that we start from base case and then say that suppose I have the definition for something what happens when I give get the next bigger one bigger object. So, here the objects are strings and what is the smallest string which is epsilon. So, can we answer this that suppose I am in the state q and the string is epsilon, epsilon is of course, has no symbols. So, therefore the machine will remain in this state. So, therefore it is should be q and now this is the base of the inductive definition and the now should be inductive part of the definition. In case of string I have some string already for which I suppose I know the answer and then a symbol came a. So, x is a string a is a symbol. So, I would like to define delta hat starting from q x a what is this? Well let me say inductively of course, I know delta hat of q on x. So, this means what the state in which the machine will be starting from q and when the string is x and now a symbol has come another symbol and what is that symbol which is a. So, do you see that this is going to be delta hat q x a is equal to delta of now this is a state and this is not difficult to see what what I am saying is really what this says that if you know the state the machine is going to be after seeing the string x then that state when the symbol a came from that state is the state where that machine will be is the state where the machine will be on this entire string x a. So, this is not difficult, but the reason for doing this is that now I have it is convenient that I have extended delta which was delta was a mapping from q cross sigma to q and now I have extended it to q cross sigma star. Now, I do not need this what I am trying to define is now that given any automaton like that q sigma delta q 0 f. I can define a language which is like this l m to be set of all strings from this alphabet such that delta hat q 0 x is an element of this f using this delta hat we are defining a language which is associated with this machine m. Let me just write it now like this l of l in bracket m which is this and the idea is that this l is the language and this is the accepted by this machine m. So, you see given any machine using this I can using the concept that we have just defined I can associate a language with that machine which is the set of strings accepted by m. Now, what we are going to do from now on for some time when we are discussing this that you will give me a language and my job will be to find the machine m. So, let us say you have given me a language l 1. So, you can see this is a legitimate problem to find f s a finite state automaton m 1 such that language accepted by this machine m 1 is this language l 1 that you give me. So, in that sense m 1 is going to be a description of the language that you have given me m 1 is going to be a very simple algorithm to decide the membership question for the language l 1. We will continue this in the next lecture.