19.1k views
1 vote
Apply Horspool's algorithm to search for the pattern BAOBAB in BESS KNEW ABOUT_BAOBABS Assume that the text comprises English letters and Construct the shift table and show the detailed steps. How many comparisons and shifts does it need to do before finding the pattern?

User Ul
by
7.5k points

1 Answer

4 votes

Final answer:

This answer explains Horspool's algorithm and provides the detailed steps to apply it for searching the pattern 'BAOBAB' in a given text.

Step-by-step explanation:

Horspool's algorithm is used for pattern searching in a given text. To find the pattern 'BAOBAB' in the text 'BESS KNEW ABOUT_BAOBABS', we need to construct a shift table and perform comparisons and shifts until the pattern is found.

The shift table for 'BAOBAB' is as follows:

  • 'B' -> 5
  • 'A' -> 4
  • 'O' -> 3
  • 'B' -> 2
  • 'A' -> 1

Initially, we align the pattern with the start of the text. We compare the rightmost character of the pattern with the corresponding character in the text. If they match, we move left by one character in both the pattern and the text. If they don't match, we shift the pattern to the right by the value specified in the shift table for the mismatched character.

Using Horspool's algorithm, it needs to make a total of 15 comparisons and 3 shifts before finding the pattern 'BAOBAB' in the given text.

User JoeNguyen
by
8.2k points