0% found this document useful (0 votes)
32 views3 pages

Boyer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Boyer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Boyer-Moore Algorithm

The Boyer-Moore algorithm is an efficient string-matching algorithm that finds the occurrences of a
pattern P of length mm in a text T of length n. It is based on heuristics that allow it to skip sections of the
text, making it much faster in practice compared to naive string-matching algorithms.

Key Features

1. Preprocessing Phase:

• Constructs two tables (Bad Character Rule and Good Suffix Rule) to optimize text-
skipping.

2. Matching Phase:

• Compares the pattern P with the text T starting from the rightmost character of the
pattern.

• Uses the two heuristics to skip unnecessary comparisons.

3. Efficiency:

• Best case: O(n/m) (skips large portions of text).

• Worst case: O(n+m).

Key Heuristics

1. Bad Character Rule

• Definition:

• When a mismatch occurs at position i, shift the pattern so that the mismatched
character in the text aligns with its last occurrence in the pattern.

• If the character is not in the pattern, skip the entire pattern past the mismatched
character.

• Key Insight:

• A mismatch near the right of the pattern allows a big jump.

• Example:

• Text: ABCDABCD

• Pattern: ABCD

• Mismatch: Text's character C doesn’t match the pattern's D.

• Shift: Align C in the text with its last occurrence in the pattern.

2. Good Suffix Rule

• Definition:

• After a mismatch, shift the pattern so that the matched suffix aligns with another
occurrence of the same suffix in the pattern, or with a prefix of the pattern.
• Key Insight:

• Utilize the already matched portion of the pattern to skip unnecessary comparisons.

• Example:

• Text: ABCDABCD

• Pattern: BCDAB

• Matched suffix: AB

• Shift: Align the suffix AB with its next occurrence in the pattern.

Algorithm Steps

1. Preprocessing Phase:

• Create the Bad Character Table:

• Stores the last occurrence of every character in the pattern.

• For characters not in the pattern, store −1−1.

• Create the Good Suffix Table:

• Determines shifts based on suffix matches.

2. Matching Phase:

• Start with the pattern aligned at the beginning of the text.

• Compare characters from right to left in the pattern.

• On a mismatch:

• Compute the shifts using the Bad Character Rule and Good Suffix Rule.

• Shift the pattern by the larger of the two computed values.

• On a complete match:

• Record the position.

• Shift the pattern using the Good Suffix Rule.

3. Repeat until the pattern moves past the text.

Advantages

1. Efficient for large patterns in long texts.

2. Works well with dense alphabets.

3. Skips sections of the text intelligently, reducing comparisons.

Disadvantages

1. Preprocessing overhead.

2. Performance degrades when the pattern contains many repetitions.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy