 we can use Myhill-Nerode theorem to prove that some language is not regular. So far we have used the pumping lemma either the usual one or the generalized one to prove some language L to be non regular, but now that we know Myhill-Nerode theorem this gives us another method to prove a language not regular. For example, consider this language 0 N, 1 N we know that this language is not regular we prove its non regularity by the pumping lemma. Now, the way we use Myhill-Nerode theorem to prove non regularity is this, recall we have one thing that Myhill-Nerode theorem says that L is regular if and only if R L is of finite. In other words the equivalence relation R L for language L if you consider that then if the number of equivalence classes is infinite then the language is not regular and if the number of equivalence classes is finite then the language is regular. In this case it is fairly easy to show that R L for this language will have infinitely many equivalence classes. Let us consider sets of strings like this E k is 0 N, 1 N minus k and of course, now take two strings from two different E k's. So, let us say E k 1 and E k 2 I claim that a string. So, let us write it here let us say x is in E k 1 and y is in E k 2 and k 1 is different from k 2. So, consider this string x 1 k 1. So, remember what is x x will have some N number of 0's and some number of 1's which is less than by k 1 from the number of 0's and if you add this one you know k 1 once after x in clearly this will have equal number of 0's and 1's therefore, x 1 to the power k 1 this string will be in the language. Whereas, if you take y from E k 2 the deficiency number of 1's it is less by is k 2 and k 2 is different from k 1. So, this will not be in the language. So, therefore, from here we find that x and y was any string from E k 1 and any string from E k 2 must in different equivalence classes. Then it is not difficult to see now since we can have infinitely many k's this means that we will have infinitely many sets like this such that if you take two different such sets and you take two strings one from here one from here they cannot be in the same equivalence class they must be in different equivalence classes and therefore, number of equivalence classes of R L for this language must be infinite. Therefore, this proves that the language L is not regular. So, this is a direct application of Myhill-Narodith theorem to show a language to be not regular. Let us now consider this topic of minimization of states of DFA's. So, what is the problem? We have a DFA m we would like to find out find a DFA m dash such that both m and m dash they accept the same language and of all DFA's accepting that language m dash has the minimum number of m. Now, the first thing we need to appreciate that even for this problem Myhill-Narodith theorem tells us a way of going about let us see how. So, L is let us say L is a language and m is a DFA to accept it. Now, you recall the definition of R m what was R m we said starting from this machine definition m we said that two strings x and y are related by this if and only if x and y take m from its initial state to the same state. Now, we can write it in or the symbols that we are familiar with suppose m is q sigma delta q 0 and f then how many equivalence classes R m will have R m will have exactly so many equivalence classes and one for each state and we remember that corresponding to state q let us say small q the equivalence class may let me may be I can call it as a q is the set of all strings x in sigma star such that delta hat q 0 x is q. So, what it means is sigma star is partition into equivalence classes and each equivalence class corresponds to one of the states of the machine m. Now, what is the best machine for this language L now it is not difficult to see if we go back to Myhill-Narodith theorem the any machine so for the language L a machine a DFA let us say m 1 has minimum number of states a DFA m 1 has minimum number of states the of course, this DFA has to accept the language L if and only if the equivalence relation corresponding to this machine R m 1 is exactly equal to the equivalence relation R m. In other words what we are trying to say is this that x for all x y in sigma star x R m 1 y this holds if and only if x y is so because remember that any machine to accept L gives rise to of course, an equivalence class R m and what we have proved in Myhill-Narodith theorem that that R m is what we do we call it is so let me let me get a picture. So, let us say this is sigma star and let us say force we are just taking an example R L is partition sigma star let us say in these many equivalence classes and remember that the language L has to be one of these or a number of this union of some of these equivalence classes. So, for the purpose of illustration let us write let us say that this particular equivalence class the strings in this D constitute the language L this is my L and of course, the other strings are not in the language L now if we provide any machine any DFA to accept L Myhill-Narodith theorem says that the corresponding R m for so let us say m to accept L has to be a refinement of R L that means what if you go back to the definition of the refinement of two equivalence relations the R m is an equivalence relation R L is another equivalence relation and we said that R m is a refinement of R L if the equivalence classes of R L are such that they are basically obtained by breaking one or more equivalence classes of R L actually I should have said 0 or more because if m is your best machine then R m equivalence classes will be same as R L equivalence classes. So, in other words if the machine m does not have the minimum number of states to accept the language then it means that R m will essentially partition some of the equivalence classes will break up some of the equivalence classes. So, let me show that situation in this manner let us say by this colored thing what we mean is now previously R L had 1, 2, 3, 4 you know 1, 2, 3, 4 equivalence classes. Now, this particular equivalence class of R L is partitioned into two and this is one equivalence class of R m this is another equivalence class of R m and similarly here may be more things are broken up but this picture will suffice to make a point and this is the general understanding of refinement R m is a proper refinement of R L that means that it breaks up some of the old equivalence classes of R L for its equivalence classes. So, what would minimization mean minimization would mean that right now I have two states recall that for every state of the machine m we had an equivalence class in R m if it is a proper refinement R m is a proper refinement of R L that would mean what two or more equivalence classes corresponding to therefore, two or more different states of the machine m all of these actually fall in the same equivalence class of R L right. So, for example, here that may be this particular equivalence class corresponded to state q 1 and this particular this particular equivalence class corresponded to the state q 2 and what is their relation their relation is that the equivalence class in R m corresponding to the state q 1 and the state q 2 both these classes are subsets of the same equivalence class of R L. So, if there is a way of combining these two states then of course, it would mean that this line would go this pink line would go and will have we will have improved in terms of the number of states in the D F A accepting the same line. So, this is why we are saying that for the language L a D F A m 1 to accept L has minimum number of states if and only the equivalence classes corresponding to that machine R m 1 they are exactly the same equivalence classes of R L if not it would mean the R m 1 is a proper refinement of R L and therefore, this kind of possibility of combining two equivalence classes its two of its equivalence classes into one is there and thereby the possibility of reducing the number of states is there. So, now we can see what is our problem for minimization we are we have been given a particular machine m we have been given a D F A m can I find two states repeatedly in this machine m such that the equivalence classes corresponding to these two states equivalence classes for R m corresponding to these two states both of these equivalence classes they are parts of the same equivalence class of R L how do I identify such pairs of states the question we are asking is for this machine m let us say m is again as before q sigma delta q 0 F the question we are asking is can we identify q 1 q 2 in q such that the equivalence classes corresponding to q 1 q 2 subsets same equivalence class. Now, it might appear a little confusing because we are simultaneously talking about two different equivalence relation when I say can we identify q 1 q 2 in q such that the equivalence classes corresponding to q 1 q 2 now here we are talking of the real equivalence relation R m. So, let me write it clearly such that in R m the equivalence classes corresponding to q 1 and q 2 are both subsets of the same equivalence class of R L. Now, so then you can see then this is then you can see the picture. So, this is q 1 this is q 2 something like this and therefore, at least intuitively we see that we can do something to merge these two states and thereby will improve in number of states. Now, what is easy in fact what is more direct and equivalent of course, to identify pairs of states which are not in other words let us look at this what we are saying is that q 1 q 2 another way of saying this that we are trying to identify this pair q 1 q 2 is distinguishable this is the term used to mean that the equivalence class in R m corresponding to q 1 and the equivalence class corresponding to q 2 in R m these two equivalence classes are not subsets of the same equivalence class of R L. So, if the equivalence classes in R m of q 1 and q 2 fall in different equivalence class of this is the definition of this term distinguishable that I say a pair of states is distinguishable if this is this holds. Now, this holds when that we know see what do we know when will to so let us say now that a q 1 is the equivalence class corresponding to q 1 in R m of course, and a q is the equivalence class corresponding to q 2 in. So, when are they not in the same equivalence class of R L. So, the situation is this is q 1 and may be not q 2 here, but if you take something like this this was your q 2 then this equivalence class and this is your a q 1 and this is your a q 2 and there of course, in two different equivalence classes of R L and such pairs of states are easy to see or easy to figure out at least we have a definition because they are in two different equivalence classes of R L. So, a string from here let us say what can I say about suppose that x is in a q 1 and y is in a q 2. Now, suppose that x is not in the same it is not related to y by the equivalence relation R L that means, the equivalence class in which x is there and in which y is there these two classes all in different equivalence classes of R L and that is what is was this question. So, that is how to answer and what would what do I know about a q 1 and a q 2 a q 1 are all those strings which take m from q 0 to the state q 1 and a q 2 are all those strings which take the machine m from q 0 the initial state to the state q 2. Now, let us write this in terms of the machine behavior. So, let us summarize what we are saying this is a machine m q 1 q 2 are two states in the machine and equivalence class a q 1 is an equivalence class corresponding to the state q 1 for the equivalence relation R m and that by definition is this we had seen the definition of R m is the set of all strings which take the machine from its initial state to q 1 and similarly for q 2. Now, what we are saying that these two states are distinguishable if and only if these two equivalence classes are in two different equivalence classes of R L and how can I figure that out. Now, look at it this way that suppose there is a z some strings a such that delta hat exactly one of delta hat q 1 z and delta hat of q 2 z. Remember these two will be two states what we are saying that suppose there is some string z such that exactly one of these two states is in F and basically that means the other is not other is not in F consider this situation. So, there is some string z such that delta hat of q 1 z let us say it is one of the final states and delta hat of q 2 z is not a final state. But what does it mean let us say string x took the machine as we said suppose from q 0 to q 1 and now consider this z. So, now let us say to suppose x this string x is in a q 1 and y is in a q 2. Suppose this is the situation then now look at these two strings x z and y z. So, of course x is in the equivalence class a q 1 and now you applied this string z as a concatenated string to x on x right and similarly you did for y z and now what you would what do you find that totally if you take this string x z x took the machine m from q 0 to q 1 and then from q 1 z took it to let us say final state and whereas y took the machine from its initial state to q 2 and then the same z took the machine to a non final state. That means what that means this string will be in the language and this string will not be in the language. That means what the two strings x and y is not related by the relation equivalence relation R L and therefore of course it means that a q 1 and a q 2 are in two different equivalence classes of R L and that is why this is the situation in which we can say q 1 and q 2 this pair of states is distinguishable and coming back to what we were saying earlier it means that there is no way we can merge these two states and hope to get DFA which will be accepting the same language. However, you see that this is now at least it seems that given a pair of states look at this situation what we are saying that suppose there is a string z in sigma star that exactly one of these two states is a final state. This is something we can test as we shall see by means of an algorithm and thereby we will be able to figure out all pairs of distinguishable states and from what I know from Eil-Nerode theorem that such pairs of distinguishable states if q 1 and q 2 are distinguishable that they cannot go into the same equivalence class of R L and my the best DFA or the DFA that will correspond to whose R M will correspond to R L that will be by somehow combining those states which are not distinguishable. So, let us now spend time to find out how we can test this condition. So, we have motivated ourselves towards this definition of distinguishability for a pair of states what we are saying is that a pair of states is distinguishable if you can find a string z such that exactly one of delta hat q 1 z this is one state and this is the other state going from q 2 to z using z exactly one of these states is in F. In such a case if you can find this condition to be true for a pair of states we will see that pair of states is distinguishable and there is a systematic way of finding all pairs of distinguishable states first of all one thing is very clear that if one of q 1 q 2 is in F and the other is not q 1 q 2 is distinguishable why so because in that case in this situation. So, what is the situation let us say q 1 is in F one of the final states and q 2 is not F then this pair is distinguishable that is because consider z to be epsilon the empty string delta hat q 1 z q 1 epsilon is q 1 which is a final state whereas delta hat of q 2 z z is epsilon this will then be when z is epsilon it will be q 2 and q 2 is not final state in that case of course this pair is distinguishable. So, that is how our algorithm would start to find out the set of all possible pairs of distinguishable all possible distinguishable pairs of states I am outline of algorithm to find all pairs of distinguishable states. So, the initialization will start from this observation. So, let us see what is our the desired output, output should be the set of all the set s let us say all pairs. So, this is the set that I would like to construct given a machine M. So, initialization for s will build s iteratively initialization is from this observation that s is all p q one of p or q in F the other not. So, I have first approximation to my set of all distinguishable states. Now, let us see how we can starting from this initialization how we can augment this set iteratively. So, this is the iteration part consider r 1 r 2 with a pair of states which is not already in s and we will add r 1 r 2 in s. So, we will add we will augment our set s by this new pair if for some a in sigma delta r 1 a and delta r 2 a this was in s. In other words what we are iteration is consider all pairs r 1 r 2 which are not already there in s this pair r 1 r 2 will be in will put put it put this pair also in s provided I find some symbol a such that you know this pair it was already there in s what is the justification suppose delta r 1 a is p and delta r 2 a is q which means that p q was already in s and therefore, they are distinguishable. So, there is a string z such that delta hat of p z let us say is in one of the final states is is one of the final states and delta hat of q z is not and now consider the string a z a takes r 1 2 p and rest of the string z takes p to a final state whereas, this same string a z this same string a z what its behavior with respect to r 2 r 2 goes to q on this symbol a and from q this string z takes the machine to a state which is not final states. So, here is this string a z which is such that exactly one of this will take r 1 of the of the two states r 1 and r 2 a z is a string such that it will take the machine one to a final state from one exactly one of these states r 1 and r 2 and therefore, r 1 r 2 is distinguishable. So, this is the justification let me rub this out this is the justification part and our algorithm therefore, will mean that in the iteration part you are considering all pairs of states which are not already in s and such a pair you will add to s you know if you find some symbol a remember that number of symbols in sigma is finite. So, one after another you test this condition you have chosen a pair r 1 r 2 which is not in s consider delta r 1 a and delta r 2 a this pair ask is it already in s if it is in s then put this pair also in s and so on and finally, we must say when to stop we say that stop the process if in an iteration no new pair is added to remember the iteration is this entire thing. So, maybe you know just to emphasize this I should write consider each r 1 r 2 not in s. So, in one iteration remember in an iteration you already have a set of pairs which is in s and some of the pairs which are not in s these are the set of all pairs of states. So, in iteration in one iteration you will consider all the elements which are not in s and as you consider one pair you may find this condition to be true. So, in that case that pair will be added and so on. Now, imagine an iteration where all the pairs which are not in s you tested this condition and you could not put this any new pair that pair to s that was true for all the pairs not in s in that case we say that iteration did not add any new pair to s and in such a case will stop the algorithm. What is the time complexity of this algorithm is it is fairly easy to see that this is a polynomial time algorithm because in each iteration how many total number of pairs of states are there if there are n states there are you know n square pairs of the order n square pair actually n choose two pair and actually we should talk of unordered pair because these are if p q is distinguishable and q p is also distinguishable, but that is a detail the main thing is in each iteration we add at least one new pair. So, how many total number of iterations? So, let me write here the time complexity of the algorithm and then we will bother about the correctness time complexity. There are at most order n square iterations and what do you do in each iteration in each iteration you consider all the pairs which are not in s and they can be order n such pairs which are not in s sorry order n square such pairs not in s and you do something you do this test for each such pair. So, you can see that in each iteration n square. So, therefore, the algorithm is polynomial time, this what I said is kind of pessimistic you should be able to see that the algorithm really the way I have said as if it is order n 4, but actually it is order n cube just to make this point that it is polynomial time I made this rather lose argument, but you should be able to see this that this is actually order n cube though from what I have said it does not immediately follow you should be able to do this check that this is indeed order n cube and now let us talk about the correctness of the algorithm. So, let us see why the algorithm that we have outlined is indeed correct when is it correct if when we stop by then we would have discovered all pairs of states which are distinguishable. We end with a certain set s and if that s contains every pair of distinguishable states then of course, the algorithm is correct. So, I would like to show that is indeed true and let us prove this by contradiction. Suppose, our algorithm is not correct then suppose then they must exist then for some instance and here the instance is of course, you are given a DFA then for some instance when the algorithm ends there are pairs of states pairs of distinguishable states which are not in s that is why the algorithm is incorrect. So, incorrect means this condition must be true there must be some instance where it is working incorrectly that means it has not been able to complete the entire set of it has not been able to put all the set of distinguishable states into distinguishable pairs of states in s. Now, let s prime be the set of distinguishable states pairs of states not in s. So, these are s prime are all those pairs which ought to have been in s, but they are not in s. So, supposing these are these pairs p 1 p 2 or let us say p 1 q 1 p 2 q 2 and so on and now suppose for each one let us say we say for each p i q i each such distinguishable pair which is not in s let z i let z 1 in this case z i. So, these are strings and what are these strings they distinguish these pairs these are the shortest. So, this so let z i be the shortest length string such that exactly one of p i z i q i z i is in f. So, that means these string z i witnesses the fact that the pair p i q i is distinguishable and we are saying that z i be the shortest length string of all such pairs let of all such strings they may there will be infinitely many strings let z i be the shortest. Now, let z be the shortest of all these strings. So, in each z i is shortest witness for that corresponding pair and of all these let particular z z be the shortest of all z i and let z correspond to the pair. So, now let the string z be some a followed by let us say z dash that is the string z begins with the symbol a. Now, consider this pair of states delta p a let this be the state r 1 and let this be state r 2. So, I claim that r 1 r 2 is of course is firstly two things I claim is distinguishable r 1 r 2 is in s that means r 1 r 2 this pair this distinguishable pair would have been discovered by our algorithm. Why the answer is the reason for that is very simple because the fact that r 1 r 2 they are distinguishable that is witnessed by z prime because if you go from r 1 on z prime say let us say this from p on a goes to r 1 and r 1 on z prime goes to let us say final state and this does not. So, r 1 and r 2 will be distinguishable and a shorter string shorter than z z exist. So, in that is that would have been the pair r 1 r 2 I would have picked up and z prime would have been the z and not this z. So, this is true r 1 r 2 this pair is distinguishable of course that is easy to see and what more this pair is already in s and now comes the contradiction that consider. So, since r 1 r 2 is in s it means this pair r 1 r 2 this pair r 1 r 2 was included in s sometime in the algorithm may be in the initial phase or may be in some one of the iterations. So, now consider the iteration immediately following that when r 1 r 2 came in to s by then the pair p q would not have been in s in any case p q is never in s p q was missed by our algorithm, but when you take the pair p q and consider the symbol a then you would have got the pair r 1 r 2 which was already there in this set s and then p q would have been declared as distinguishable. So, therefore, we have a contradiction and therefore, this is not true that there is some pair of distinguishable states which our algorithm had failed to discover.