Video 6-7: §6.6 The Boyer-Moore algorithm
CS 566 – Efficient Algorithms (Winter 2025-26) · unit page
· YouTube
2025-11-19, 15:47
The transcript was generated automatically by YouTube's speech recognition and is offered as is. It may contain errors, in particular concerning technical terms, names and formulas.
0:04 The first example of an idea that I would like to tease for string matching is the Boy Moore algorithm. And the question at this point is, what else do we want? We now have an optimal algorithm, which has the best possible runtime and reasonable storage space. What could actually be improved at this point? Whenever someone says it is the optimal solution, you have to ask about what.
0:37 In this case, optimal is with respect to the worst case, i.e. the worst input, which may not be the most relevant. We have already seen with the Brothforce algorithm, the Westcase input there was a very repetitive text here too. Now, the best case, if you will.
1:02 These extreme examples are often not really representative of the application. That is, there may still be something that can be improved by looking at this example. If I have a text that has nothing but A's, but a pattern that consists only of other letters, I don't need to compare much to know that I won't find any occurrences.
1:23 Of course, an algorithm can't proceed like this by looking at the text from above, but it could, for example, make the following comparisons. It could compare the last character of the petter with the one with the position in the text and accordingly here and here and here and would determine that these are four mismatches , but they cover all possible positions of the bolder in the text.
1:54 And because the character doesn't match here and the way the text is structured , no match is possible, even though I haven't even looked at most of the characters in the text.
2:07 So in algorithms, we often have such lower bounds where we say, yes, we have to have at least time to analyze the input, sometimes not. Here it is actually enough to look at a small part of the input, if we are lucky and clever about which parts we look at . So you always have to be careful with the arguments. These real lower bounds, as a small detour, so to speak, come from the fact that you have problems in which changing the input at one point changes the result, so that an algorithm that delivers the correct result actually has to look at all the details of the input . But that's exactly the point here, that's not the case. There's no way to add a different symbol here. Even if I were to put an X there, there's still no match that would somehow hide the pad in the text if there's an A here.
3:00 So if there's no X at each of the places we've checked, there's no way to lower the pad so that there's a match, even if I replace all the other characters with Xs. That's exactly the point where you can save money here.
3:19 Okay, the idea behind the Boy Mo algorithm is to somehow systematically exploit this observation, because for this extreme example, we can probably agree on that. In general, it's not exactly representative.
3:38 The first insight, as in the example, is that it might be worth checking these characters from the right, so that we first look at the last character. If that is already a mismatch, we at least have the option, as in this extreme example, to move the next character completely behind this current character, because this one comparison excludes much more than if I only had the first character.
4:07 That is the main insight, as simple as it is, and the rest is, um, cleaning up everything that we destroy by doing so. But first, um, let's say, um, if you only do that, then you are back to the Brufors algorithm.
4:22 Perhaps not surprising, if the text consists of the letter A and the lettering of the first character is different, then I compare all characters from the right. We have to prevent that.
4:40 The first rule that also prevents that is called bad character jumps. That is what always happened in our example earlier. If I recognize a character C in the text that does not match the current position in the pattern, then I know that it is located at this point in the text C. That means I can move the pattern as far to the right as I want with this C. So either, if it does not occur in the pattern at all, completely behind this position, if the character does not occur in the pattern at all, then no match can overlap with this position.
5:16 Otherwise, I can at least move it as far as the last occurrence in my text, in my pattern. Um, Cerein, so here is my C. I may have just tried to find the pattern here. Here is some other character that was not C. Maybe the last occurrence of my C here in front is the pattern. Then I can at least move it far enough so that this last occurrence of C now coincides with the C that I found in the text.
5:50 I have to take the last occurrence because I want to be conservative. I cannot skip any potential matches. So if there is another C here in front, then that's fine, but I can only move it as far as this one. That's one thing. That's the simple rule . The second rule, which tries to counteract this a little bit, is called F suffix.
6:14 If I've already matched a part of my pattern here , then I'd like this place to match again the next time I move it, because these are comparisons that I've already paid for, and I don't want to waste them . Um, I'll give you a short example of both in a moment. Um, before we get to that, the final algorithm that uses these two rules.
6:42 How to calculate them is explained below , and how they are formally defined. The main algorithm is essentially structured like the Bluce algorithm, except that we compare from the right and possibly continue playing more than one place. So we try to match characters from the text and the pattern. We do this from the right. That means if there's a match , I make my J smaller, the position of the pettern. If I've crossed out the entire pettern in this way, then I've found an occurrence. Otherwise, J is a mismatch at this point and then I push my throttle, my position in the text where the pattern could start, by a certain value, which is calculated from the better shift of the two rules.
7:34 Both give me a value for how far I can jump without jumping to a potential occurrence. And I can take the larger of the two jumps.
7:45 So far, the general structure is clear, and you still have to look at these rules in detail. And that's the part that I just want to show you a little bit using an example, which we don't require in the exam.
8:01 First, the bad character rule is relatively simple. Here is a text and pattern. I start matching on the right. Now I have an R here. That's a character that doesn't appear in my pattern at all . So I can completely bypass it at this point. Well, that was the case. There is no last occurrence. The same thing happens here again with W. I can also completely bypass it. Then I'm done here for now. Okay, here I find the pattern at the end of the text. Uh, the second case happens down here. I have my pattern Moore and in Boyer Moore I have the R here. That's a mismatch with the E, but there is an occurrence of R in Mur. That means I can only shift as far as the last occurrence in R allows me. Then I've made it match. And now I'm trying to check this gesture here. Again from the right. Of course, that's immediately a mismatch again.
8:58 That means it doesn't work. And the M is now my next bad character. That also appears at the very front. That means I can shift much, much further here So, those are the two cases. If the character doesn't occur at all or occurs somewhere, then I shift to the last, to the rightmost occurrence. That's the bad character rule.
9:34 it as the last occurrence of the character or -1 and then both the calculation and the shifting are relatively simple. Uh, we fill the table once at the beginning as -1 and then overwrite it for each character in the appropriate order with the index. Um, it's explained here. Um, it's very simple.
9:59 Um, I'd like to briefly show you the other rule. Um, just the idea here, we won't do the exact calculation. If you want to read about it, it's in the EAA book, as I called it, Design and Analysis of Algorithms. Um, we've written about it in detail there, with slightly different indices, or you can read it if you're interested in how it can actually be calculated. So here's just an example: we have a pattern and a text. We start matching and have now found the last few characters, but then we get a mismatch. Now we have to move the pattern further to the right, because there can't be an occurrence at that point. I can save myself the next comparisons. But I don't want to move these four characters that I learned from the text to a place where there would be a mismatch. I can save myself that. So I shift as far to the right as I can find these last characters here again in my pattern.
11:06 This is, so to speak, the shortest, that it is the closest occurrence before it. So there is a suffix of pattern that occurs again somewhere in the pattern. Then I take the rightmost occurrence of this suffix.
11:24 Okay, so I take the last four characters and see where else do they occur in the pattern? At this point only once, otherwise it is simply the rightmost occurrence.
11:38 There is a second case that can happen here. This is a little bit different. Here again I have found a pattern in the text in part and now a mismatch. But at this point here is good. The good suffix that I found does not occur in the pattern at all.
11:59 But that does not necessarily mean that I can shift completely past it, because there is a suffix of suffix that occurs at the beginning and I have to bring it into line, because that could be an occurrence of something.
12:13 So this second case means there is this suffix that I was able to match, not quite in the pattern, but a suffix of the suffix goes in front of the petter. It's a subtly different case. For then here, so that I can navigate this far.
12:42 these are the two cases. Um, to calculate this, you can essentially use a similar algorithm as the failure links for the Km algorithm. Um, here are the two cases written down again. Um, you essentially use the failure links on the mirrored word.
13:04 Then you can use the same algorithm, as I said, if you want to see the details. Um. Ah, cool. I even put it on the slides. But I wouldn't take it out here now, because I didn't discuss it seriously today.
13:25 So, then I take back what I said earlier . It's also in the EA book, but I also did the translation here. Um, you know, but it's not in detail.
13:38 Okay, so just as a fact, they should be able to understand this using an example and they should know the fact that it can be calculated in linear time.
13:52 What can we say about the Boyer Mo algorithm as a whole? We have these two rules, last occurrence and bad character, last occurrence and good suffix.
14:04 Uh, we can show that if the pattern does not occur in the text, the Boyamur algorithm also has essentially optimal worst-case running time. But that is actually not obvious. That is a noble analysis. The result is of course still true, but if you are wondering why that is true, don't expect it to be immediately obvious, but that is somehow a more difficult proof. Of course, we won't do that here either . Uh, this dependency on sigma comes from the calculation of bad character. Here we have dependency on sigma again, but at least only as a plus sigma. That is good. That means we haven't ruined the worst case. But unfortunately, that only applies if the pattern does not occur. For the worst case, you can construct one in which this root boss runtime essentially appears.
14:57 This is partly because we sometimes compare several things again with the good. Um, if you, um, skip things, um, it can happen that you don't take all the information you've learned about the text with you, and it's difficult to improve that. Um, I don't want to go into further detail. Um, if you compare this with a typical English text, then Boer Mur is usually significantly faster, because it doesn't remember many characters at all. Of course, that depends on the pattern and the text together, but in practice, Offby is only the choice that, for example, takes the text editor when you give up control.