The Classical BM algorithm

Source: Internet
Author: User

The KMP match was matched from the beginning of the pattern string, and in 1977, Professor Robert S. Boyer of the University of Texas and Professor J Strother Moore invented a new string matching algorithm: Boyer-moore algorithm, or BM algorithm for short. The algorithm starts at the end of the pattern string and has a time complexity of O (N) in the worst case. In practice, the actual efficiency is higher than the KMP algorithm.

The BM algorithm defines two rules:

    • Bad character rule: when a character in a text string does not match a character in the pattern string, we call the mismatch character in the text string a bad character, where the pattern string needs to move to the right, the number of bits to move = the position of the bad character in the pattern string-the position of the bad character at the right of the pattern string. In addition, if the "bad character" is not included in the pattern string, the right-most occurrence position is-1.
    • Good suffix rule: when character mismatch, post shift = good suffix position in pattern string-a good suffix occurs once on the pattern string, and 1 if the good suffix does not reappear in the pattern string.

The following examples illustrate the BM algorithm. For example, given A string of text "Here's A simple EXAMPLE", and the pattern string "EXAMPLE", now to find out if the pattern string is in the text string, if present, returns the position of the pattern string in the text string.

1. First, the "text string" is aligned with the head of the pattern string and is compared from the tail. "S" and "E" do not match. At this point, "S" is called the "bad Character" (character), which is the mismatched character, which corresponds to the 6th bit of the pattern string. and "S" is not included in the pattern string "EXAMPLE" (equivalent to the right-most occurrence position is-1), which means that the pattern string can be moved back 6-(-1) = 7 bits, thus directly to the "S" of the next bit.


2. Still comparing from the tail, it is found that "P" and "E" do not match, so "P" is "bad character". However, "P" is included in the pattern string "EXAMPLE". Because the "bad character" of "P" corresponds to the 6th digit of the pattern string (numbering starting from 0), and the most right occurrence in the pattern string is 4, the pattern string is moved back 6-4 = 2 bits and two "P" aligned.


3. By comparison, you get a "mple" match, called a "good suffix" (good suffix), which is a string that matches all tails. Note that "Mple", "PLE", "LE", "E" are good suffixes.

4. Found "I" and "A" Do not match: "I" is a bad character. If it is based on a bad character rule, then the pattern string should be moved back 2-(-1) = 3 bits. The question is, is there a better way to move?

5. The better way to move is to use the good suffix rule: when the character is mismatch, the post shift number = The position of the good suffix in the pattern string-the position of the last occurrence of the good suffix in the pattern string, and 1 if the good suffix does not reappear in the pattern string.
Of all the "good suffixes" (Mple, PLE, LE, E), only "E" appears on the head of "EXAMPLE", so it moves back 6-0 = 6 bits.
As you can see, the "bad character rule" can only move 3 bits, and "good suffix rule" moves 6 bits. The larger value of each of these two rules is moved back. The number of moving bits of these two rules is only related to the pattern string, regardless of the original text string.

6. Continue from the tail start comparison, "P" and "E" do not match, so "P" is "bad character", according to "bad character rule", 6-4 = 2 bit. Because it is the last one to mismatch, has not yet obtained a good suffix.

From the above, the BM algorithm is not only high efficiency, but also ingenious, easy to understand.

The Classical BM algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.