For searches that won’t yield a match, givng up faster saves us time, the same principle applies to life too.
Developed by Robert S. Boyer and J Strother Moore in 1977, the Boyer-Moore matching algorithm is widely used for pattern matching. It’s a common benchmark for exact matching.
In Naive Exact Matching, the pattern P slides/shifts from left to right, and at each alignment, we check for matching from left to right for each substring of length P.
In Boyer-Moore matching, P also slides from left to right, but at each alignment, the matching is checked from right to left.
More importantly, when a mismatch is found, it uses two tricks to skip forward many alignments that do not need to checked.
This means instead of shifting P one character to the right and check every possible alignments like in naive exact matching, P is often shifted many steps, skipping many alignments that we know for sure will not be a match.
The two tricks are the bad character rule and the good suffix rule.
1.The bad character rule
To get a sense of this rule, imagine the following case in naive exact matching.
P: word T: Hello world word! ---------word------- ---0123456----------
As P slides to offset 6, the navie exact matching checks if P “word” matches with the substring “worl” at the current alignment, and found a mismatch of “l” in T and “d” in P.
Since the character “l” does not exist in P at all, instead of sliding P one offset to the right and check the next substring of T, we can slide the entirely of P pass this character - since any cases where “l” overlapping with P will NOT be a match.
P: word T: Hello world word! ---------word------- -------------word--- ---01234567890------
Instead of sliding the pattern “word” from offset 6 to offset 7, we now slide it directly to offset 10, skipping 3 steps/alignments (7, 8, 9) in between.
The bad character rule states that upon finding a mismatch, skip alignments (sliding P forward, to the right) until (a) this mismatch becomes a match, or the entirely of P slides past the mismatched character.
It makes sense, since for a match to happen, either this particular character needs to be matched (when P is overlapping with the character), or P moves pass it entirely (no longer overlapping with the character).
Note that Boyer-Moore checks matching from right to left, so in the previous example, it would first check “l” in T and “d” in P, which is a mismatch, and the bad character rule would slide P entirely pass this “bad character”.
2.The good suffix rule
The good suffix rule makes more sense with a case where P is longer.
T: CGTGCCTACTAACTTACTTACTTACGCGAA P: CTTACTTAC
In the case above, we are checking P for a match at offset 0 from right to left, and found a mismatch of “C” in the text and “T” in the pattern. Because Boyer Moore checks from right to left, when found a mismatched character, the substring after that character will be matched - the “good suffix”, in our case it’s “TAC”. As we slide P forward to find an alignment that would result in a match, this “good suffix” will need to stay matched somewhere in P, for our case, P will be skipped forward until another “TAC” matches this good suffix.
T: CGTGCCTACTAACTTACTTACTTACGCGAA P: CTTACTTAC P: CTTACTTAC
Here, instead of shifting P one offset to the right for the next alignment, we shift 4 offsets to make the good suffix “TAC” stay matched, skipping 3 alignments in the process.
The good suffix rule is that upon finding a mismatched character, the “good suffix” (the substring from right after the character to the end of P) will be used to skip alignments - skip until (a) another part of P matches the good suffix, or (b) P move past the good suffix.
Look up table and time complexity
The number of skips we can perform for the bad character and good suffix rules is entirely depended on the pattern P.
In practice, P is preprocessed to build a look up table for both rules, so upon a mismatch, we will know now many steps to skip in constant time by checking the table.
If the length of pattern P is m, and length of text T is n, Naive exact match has a time complexity of O(mn).
For Boyer-Moore, the worst-case time complexity is also O(mn), when there are many mismatches. However, in practice, it’ll often skip many alignments and be faster than the naive exact match, the best-case time complexity for Boyer-Moore is O(m/n).
Implementing Boyer-Moore matching in Python
An example of implementing Boyer-Moore matching algorithm in Python, and use it to match short DNA reads back to a genome can be found in this Google Colab file.
Very briefly, we have a class “BoyerMoore” that preprocesses a pattern P, building look up tables for the bad character rule and the good suffix rule.
Then similar to exact match, we slide/align P at different offsets and compare P to substrings of T from right to left, upon finding a mismatch, we check the skips we can perform according to the bad character rule and the good suffix rule, and uses the bigger skip of the two.
The original paper:
Boyer, RS and Moore, JS. “A fast string searching algorithm.” Communications of the ACM 20.10 (1977): 762-772
Coding problem: implementing Boyer-Moore
Play around with the codes in this Google Colab file.
We’ll implement the Boyer-Moore algorithm and use it to match short DNA reads to a genome.
We will compare Boyer-Moore and naive exact match by checking
(a) the number of character comparisons performed and
(b) the number of alignments tried.
These measurements indicate the amount of calcualtion/time needed for the algorithms.
Coding project
Download the genome file (part of human chromosome 1) and address the questions below.
Q1. How many alignments does Boyer-Moore try when matching the string GGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGG (derived from human Alu sequences) to the excerpt of human chromosome 1?
Q2. How many alignments does the naive exact matching algorithm try when matching the string GGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGG (derived from human Alu sequences) to the excerpt of human chromosome 1?
Q3. How many character comparisons does the naive exact matching algorithm try when matching the string GGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGG (derived from human Alu sequences) to the excerpt of human chromosome 1?
Files needed:
The Python module for Boyer-Moore preprocessing:
http://d28rh4a8wq0iu5.cloudfront.net/ads1/code/bm_preproc.py
This module provides the BoyerMoore class, which encapsulates the preprocessing info used by the boyer_moore function above. Second, download the provided excerpt of human chromosome 1:
The reference genome:
http://d28rh4a8wq0iu5.cloudfront.net/ads1/data/chr1.GRCh38.excerpt.fasta
Q1 Number of alignments for Boyer-Moore
Q2,3 Nuber of alignments & character comparison for naive exact matching
Appendix: Boymer_Moore Class Implementation
Showing the content of the bm_preproc.py for reference
Creating a p_bm object that preprocesses pattern p, implementing the look up tables for the bad character rule and the good suffix rule.