 Hello friends, today we will see one more sequential searching algorithm. So, at the end of this session students will be able to find out all the text positions where the pattern occurs using BDM algorithm. So, what is BDM algorithm? It is backward DAWG matching algorithm or deterministic acyclic word graph algorithm. Now, the word backward means we are processing from right to left in window position. We have already seen it in BM algorithm also. Now, this algorithm is based on suffix automata. So, what is suffix automata? Automata which accepts all the suffixes of the given pattern or a string. Now, consider the example here. The string is AB, AB, A. Now, there are five characters. So, we are getting five suffixes. A string itself is a suffix, longest suffix. Then we are having this. So, AB, AB, A is a suffix. Then B, AB, A. A, BA, BA and A. So, we are getting here five suffixes. So, we need to find the automata which will accept all the suffixes. So, how one can start? One can find the non-deterministic automata for the given pattern. Convert that non-deterministic automata to deterministic one. Now, in this automata, each path from initial node to any other internal node will represent a substrate. Whereas path from initial node to any accepting node will represent one of the suffixes for the given pattern. So, let us see with example. So, again consider the same pattern AB, A, BA. So, this is the automata which accepts all the suffixes of this pattern. So, if you look at this particular node A122 or the path from 1 to 2, it accepts A. So, this is one of the suffixes. Second is BA. So, we have to start with 1, 3, 2, which is accepting BA. Third one is A, BA. So, we will be starting with 1, 2, 4, 5, which is accepting ABA, then BA, BA. So, again 1, 3, 2, 4, 5, and the last is going to be ABA, BA. So, in this way, we are reaching to one of the accepting state and we are finding the suffix. Now, consider the path 1, 2, 4. Now, 4 is an internal which is not an accepting state. So, AB is a substring of a given suffix. So, this is what the suffix automata which accepts all the suffixes for the pattern ABA, BA. Now, consider the other pattern CAA, where there are three characters and three suffixes CAA, AA, and A. So, at this moment, pause the video and try to find out the suffix automata which will accept suffixes of this given pattern. So, you can directly draw the automata since the string is short or you can find out the non-deterministic automata and try to convert it into deterministic one. So, here is an answer. So, here 1, 2, 3, 4 will accept CAA as a suffix, 1, 5, 4 will accept AA, and at the end 1, 5 will accept A. So, this automata accepts all the suffixes of the pattern CAA. So, once you have understood that how to draw a suffix automata, we can go for this medium algorithm. Now, in this medium algorithm, we have to find the suffix automata for the reverse pattern. Remember that we are processing from right to left. So, automata will be fine for the reverse string of the pattern. So, p raise to r is nothing but a reverse pattern. So, algorithm as we have already said that algorithm will search backwards inside the text for the substring of the pattern using automata. Now, whenever we will find an accepting state, that means we have found a suffix. So, we have to remember that particular position where we have found a suffix. And in that way, we will store this latest prefix that we have already recognized, because prefix will be for a pattern which is same as suffix for the reverse pattern. So, when we can say that match is found when the complete window is trade. So, there will be two cases. Either we have completed the, we have read the particular window, or we have reached to a state where there is no transition. So, in either of the case, window is need to be shifted with the longest prefix which we have already stored. So, let us see with example how the medium algorithm works. So, we have already, we have taken the pattern a, a, b, b, a, a, b. And the reverse string is b, a, a, b, b, a, a. So, this is a non-deterministic automata or with null transition. Any, at any stage we can reach with a null transition, we can accept the suffix for that particular string. So, consider the string a, a, b, b, a, a, which is a suffix. So, we can start with i. And then we can move from 1 to 7, accepting this suffix a, a, b, b, a, a, a. So, we have found first non-deterministic automata. Now, we will be converting it into the deterministic one. So, we have not gone through the complete process, but this is the resultant one, deterministic automata for the reverse pattern. Now, how we will start? Of course, we will start with the first position in the text. But what we have already discussed is that it will be the, we are going to process the characters from right to left. So, here this window, which is written in blue color, represents the text window which we are processing. And this is the first character that we are going to process. So, what will be this? The current character is a, which is at the mth position. And what is m? Is nothing but the length of our pattern. So, in this case, the length of the pattern is 7. So, we have again, we have not yet occurred any of the suffix for the window. So, we have written m as in last as in 7. And the text position is also always at 1. Remember that though we are processing from right to left, the pattern will occur from this left to right only. So, we are at this initial stage. So, we will be processing the character a. So, after processing, we will be reaching to the state number 2367. So, once we have processed a, we have reached to this 2367. So, character is present or the transition is present. So, just decrement the value and go to the next character. So, j is 6. Now, since this is accepting state, it means that a is going to be the suffix. So, we are storing that position as in last. So, last will become now 6. So, again process this character a. So, for this transition, it will be moving to the state number 37. So, for this a, we have moved to the state number 37. And now j has been decremented to 5. So, current character will become now b. So, again after processing this, we have reached to the state number 4. And again the position is 4, where the current character is a. So, from a, we need to process now the character a. So, now for this state number 4, there is no transition for this state a. So, while moving, we have already in this previous, here we have seen that 37 is also an accepting state. So, last has been changed to 5. So, here we have got that there is no transition for the state number 4 for the input a. So, in this case, we have to move this pattern. So, where we will move this? Now, the last position that we have remembered is 5. It means that this b a is the longest suffix that we have already found, okay? So, if you look at this, it is going to be b, sorry, it is going to be a, a, b that we have already processed because we are processing from right to left. So, a, a and b that we have already found. So, what will be the next text position? It will be the text position plus the last occurrence that we have found. So, the next text position will be 6. So, we are moving this window to the right by this many positions. So, now we are coming to this. So, next seven characters from this position will be processed, okay? So, this will be the new setup. Again, we have to start from the right. So, this is the new window. So, again j will be at 7. Last occurrence will be at 7. Text position is 6 and the current character is going to be b. So, at this moment pause the video and try to find out the transition and see that again, are we finding the pattern or not? So, the first character is b. We have processed this b and we have reached to the state number 145 and then we have decremented this j by 1. Now, since this is not an accepting state, the last position is not changed. Again, the next character we have processed is a and we have reached to the state number 26. Again, this is not an accepting state. So, we have reached to the state number. We have not changed this last position. So, process the next character which is nothing but a and we have reached to the state number 37. And now the next character is going to be b. So, here the character is b that we are already processed and we have reached to the state number four and just decremented the value of j. So, in this manner, we have reached to the state number five. Now, in this process, when this 37 is an accepting state, when we have got it, the last value is changed to five because this is an accepting state. So, after processing this, we have reached to the start of the window and then for a, we have reached to the accepting state number seven. Also, we have reached to this start of the window. We have completely read that window or the characters in the window. It means that we can say that pattern is found at the position of six. So, I hope you have understood the process. We have started with the text position one. We have processed one character, one by one character in that whenever we have got the accepting state, we have stored that position as we have found the prefix for the pattern and suffix for the window. And we have completed till either we are finding the pattern or we have to make the transition. So, worst case complexity of this medium algorithm is O of mn. Whereas, average case complexity is O of n log m divided by m. Thank you.