 Hello friends, today we will see one more algorithm for sequential searching. So learning outcome for this session is students will be able to find all the text positions where the pattern occurs using BM algorithm that is Boyer-Mure algorithm. We have already seen brute force approach and KMP algorithm in which we are sliding our window from left to right and also we are comparing the text from left to right. Here the check inside the window proceed backwards. So we are moving from for the text from left to right only but the checking inside the window will be from right to left. So in this case you can see that we will place a window and we start comparing from the right character. Now here two heuristics will be used. One is a match heuristics. Whenever the mismatch occurs the match characters are used for shifting the window. So in the KMP we have used prefix information. Here we will be using suffix information. Occurrence heuristics the character that produce mismatch has to be aligned to the same character in the pattern. So for example here the mismatch character is C. Now we have to see that whether this C is present in the pattern. If it is present we need to align this particular pattern to the position of C. So we will see one example. So we have to see two things bad character heuristics and good suffix heuristics. Now what is the ideal case wherever the mismatch occurred if that character is not present in the pattern we can move the pattern completely. Okay now in this case you can see that this character D is a mismatch character and this D is not present in the pattern. So it's not there is no point in comparing all the remaining characters. We can safely move this pattern to the number of characters present in the we can safely move this pattern to the right side by five characters since the length of the pattern is five. So here you can see that we have moved the pattern and again we will start comparing. This is the ideal case. Now what is bad character heuristics? A mismatch character is present somewhere else in the pattern. Hence that character should be aligned to the character in the pattern. Now consider this case here we will start comparing from right to left. Now B is mismatched here and this B is present in the pattern. So this B will be aligned to this B. So obviously we need to shift our pattern to the right by two characters. Okay so in this case we have shifted let us see that this B is aligned to this B in the pattern. So this is called as bad character heuristics. Now consider this case. Now here B and B is matched A and A is also matched but here it is a mismatch. So mismatch character is A. Now just now we have seen that we have to align this particular character to the character present in the pattern. Now the last occurrence of this character is here and if you want to align this A with this A we need to shift this pattern to left and this is a negative shift because there is no character present to match and hence we need to find the maximum possible shift distance and it is called as a good suffix heuristics. Now what I mean by good suffix heuristics is that how many characters are matched. Now in this case A B is matched. Okay so we have to see that the number of characters matched are they present in the pattern. So this A B is also present in the pattern. So we need to align the number of characters matched to the pattern present over here. So how we have shifted is that this A B is which is already matched is again aligned to the A B which is present in the pattern. Now consider the second scenario. This is again A B is matched and this A is mismatched. So what we can do is that we have shifted this to align this A B. Now here A B is matched and this is a mismatched character. Now this A B is matched C is present. Okay so we can align this C to this C and the matched characters are not present in the pattern. Are you getting at this? So here we have to see that the mismatched character can we align shift safely or the suffix which is matched whether it is present in the pattern. So we have to find the maximum distance which we can shift. So out of these two which condition will give you the maximum distance that we need to apply. Now consider this scenario. Here B A B and this B A B are matched whereas the mismatch A and B. So B A B can we use this B A B for finding the shift but this B A B is not present in the pattern. But what is present? A B is present. So next suffix that is A B is present. So we need to align this particular pattern to A B not to B A B. So all such conditions we need to consider while checking the occurrences using BM algorithm. So what we need to do we have to find the bad character heuristics. It means that the character which is mismatched or which has produced mismatch is it present in the pattern and can we shift windowed safely. Second how many characters are matched or suffix? So out of this which will give you the longest shift that needs to be selected in the BM algorithm. So the first step for this BM algorithm is that calculate the last occurrence of the character. So we will call it as an last function. Now consider that last occurrence of the character means that the index of the last occurrence of C in pattern if C is present and if the character is not present it will be considered as a minus 1. Now let us look at this pattern. So A B A C A B. So the characters present in the pattern are A B and C. So star means the character which is not present. So A is present and the last occurrence of this A is 4. So here last occurrence of A 4 for B it is going to be 5 and for C it is going to be 3. So minus 1 for the characters which are not present. So first step is that calculate the last occurrences of every character. Now at this moment pause the video and try to find out the last occurrences for the given pattern. So these are the values you must have got the same values. So A the last occurrence for A is 6, last occurrence for B is 3 whereas last occurrence for C is 5. Now the second step is that finding the heuristics information. So here I is the text position which we want to shift. So M is a number of characters in the pattern or pattern length. Now J is a position where mismatch has been occurred whereas this is going to be the last occurrence of the character which has which has produced the mismatch. So out of this which is minimum that we need to take so that we will get the longer shift. So we will see this by example. So here these are the last occurrences which we have already calculated. We will be placing our window. So I will be at position of 6, J will be also at position of 6. Here we are starting with the indexing of 0. So M number of characters in the pattern. So now A and A is matched so whenever there is a match we need to decrement the pointer. So I and J has become 5 but there is a mismatch. So mismatch character is B. So what is the last occurrence of B that we have already calculated which is nothing but 3. So the mismatch position that is 5 or the last occurrence out of that which is minimum that we have to take. So 5 plus 7 minus minimum of this will give you the next position as 8. So we need to align our window to the text position of 8. So if you can look at this, this B will be aligned to this B. So again I is at position 8 and J will start at position of 6 as we are moving from right to left. Now this is again matched. Next mismatch is the same again at the position of 7. So here J is 5 and I is 7. So the mismatch character is B. Last occurrence of the character is once again 3. So the next position that we will get it as an 10. So again this B will be aligned to this B. See the heuristics or the number of characters match are only 1 and the position that we need to align is going to be 3. So out of this which is maximum that we are taking, okay, for obviously for subtraction. So the next position will be 10. So this B and this B is now aligned. Again we will go for checking. Again there is a mismatch with this particular character. So we need to shift. Here again J is 5 and I is now 9. So the next position that we are getting it as an 12. So here now A and I is at position of 12. A is at, J is at position of 6. So character is matched. This decrement is, again it is matched, decremented. So whenever there is a match we need to decrement both the positions and if there is a mismatch we need to find the next position. So here the characters are matched. So we have reached J to 0. Means all the characters are matched. It means that we have found pattern. So pattern is found at position of 6. So here we have found the first occurrence. We need to find the pattern. We need to find the next occurrences of the pattern. So we need to again shift our window to right by one position and again start the same process of BM algorithm. Thank you.