Skip to content

Boyer-Moore-Horspool

words

Level 2 of 3

Also called Horspool's algorithm and Boyer-Moore-Horspool algorithm.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Horspool looks for coa in cocoa. How many comparisons does it make?

the answer, and why

the answer is A

The table: c jumps 2, o 1, any other letter 3. Place 1: the text's c is not the pattern's a, 1, and c jumps 2. Place 3: a, o and c match, 3. a stands only last in coa, so it is not in the table: it jumps 3, past the end. That is 4.

placeletters abovecomparisonsjump
1coc1c: 2
3coa3, a matcha: 3
coa tried at 2 places of cocoa. Place 2 is jumped over.

if you chose another

  • That is brute force: it tries all 3 places and compares from the front, 3 + 1 + 3.
  • That counts place 3's matches but not place 1's comparison.
  • That tries place 2 as well, which c's jump of 2 skips.

question 2

Each search looks for ana in a text of 9 letters. Which makes Horspool compare the most?

the answer, and why

the answer is D

The table: a jumps 2, n 1, any other letter 3. In anananana, ana matches at places 1, 3, 5 and 7, 3 comparisons each, and a jumps 2 each time: 12.

if you chose another

  • From the end back, a matches, then n differs from a: 2 a place. a jumps 2, so places 1, 3, 5 and 7 are tried: 8.
  • n differs from the pattern's last a at once: 1 a place. n jumps 1, so all 7 places are tried: 7.
  • b is not in the table: 1 comparison, then a jump of 3. Places 1, 4 and 7: 3.

question 3

A text has 600 letters and a pattern 300. At most how many comparisons can Horspool make, whatever the letters?

the answer, and why

the answer is B

No place compares more than the pattern's 300 letters, and every jump is at least 1. So at most 600 - 300 + 1, which is 301, places are tried: at most 300 times 301, which is 90,300. A text and a pattern of all a's make exactly that many.

this tests where the formula comes from, on the learn page

if you chose another

  • That is the most places, at one comparison each.
  • That compares 300 letters at each of 600 places, but the pattern fits at only 301.
  • That is the fewest, a pattern of b's in a text of a's.

question 4

Horspool looks for bbbbb in a text of a's. The text grows from 1,000 letters to 2,000. About how many times as many comparisons?

the answer, and why

the answer is A

A place is tried for each 5 letters, as question 3 of the learn page's section 4 finds, so 200 comparisons, then 400. Twice the letters, twice the comparisons.

this tests where the formula comes from, on the learn page

if you chose another

  • That is how the worst grows, as the text's length squared.
  • The pattern stays at 5 letters, but the text holds twice as many places.
  • That is the jump's length, not how the count grows.

pen and paper

question 5about 6 minutes

A text holds the letters cabbage. Search it for bag with Horspool, numbering places from 1.

  1. Write the table of jumps.
  2. Which places are tried, and how many comparisons at each?
  3. At which place is bag found, and how many comparisons in all?
a hint

Compare from the pattern's last letter back, then jump by the text's letter above that last letter.

a worked answer

The table: b jumps 2, a 1, any other letter 3. Place 1, cab: g is not b, 1, and b jumps 2. Place 3, bba: g is not a, 1, and a jumps 1. Place 4, bag: g, a and b match, 3, and g jumps 3, past the end. bag is found at place 4, and the comparisons are 1 + 1 + 3, which is 5.

placeletters abovecomparisonsjump
1cab1b: 2
3bba1a: 1
4bag3, a matchg: 3
bag tried at 3 places of cabbage. Places 2 and 5 are jumped over.

this tests how it works, on the learn page

what a full answer includes
  • b 2, a 1, any other letter 3.
  • Places 1, 3 and 4, with 1, 1 and 3.
  • Place 4; 5 comparisons.

question 6about 8 minutes

A text holds 801 letters, all a. The pattern has half as many, rounded up.

  1. How many letters has the pattern, and at how many places is it tried if it is all a?
  2. How many comparisons then?
  3. How many if it is all b?
a hint

The learn page's section 4 counts both: question 2 for all a, question 1 for all b.

a worked answer

Half of 801 rounded up is 401 letters. All a: a jumps 1, so it is tried at every place from 1 to 801 - 401 + 1, which is 401. Each place compares all 401 letters, so 401 times 401, which is 160,801. All b: 1 comparison at place 1; a jump of 401 lands on place 402, past the last place, 401. So 1 in all.

Question 2: how many comparisons, MM, on a text of nn a's and a pattern of mm a's, mm half of nn rounded up?
  1. the move: a place's most

    the line:
    a place comparesat most m letters\begin{gathered} \text{a place compares} \\ \text{at most } m \text{ letters} \end{gathered}

    why it is allowed: At a place the pattern's letters are compared from its last back, one each, until one differs. It has mm letters, so at most mm.

  2. the move: every letter matches

    the line:
    all a’s:m at every place\begin{gathered} \text{all a's:} \\ m \text{ at every place} \end{gathered}
    placetextpatterncomparisons
    1aaaaaa3, a match
    2aaaaaa3, a match
    3aaaaaa3, a match
    4aaaaaa3, a match
    all12
    A text of 6 a's and the pattern aaa. At each of the 4 places all 3 letters match. 4 times 3 is 12.

    why it is allowed: The text and the pattern are all a, so every letter matches from the end back: line 1's most, mm, at every place tried, each a match.

  3. the move: a jumps 1

    the line:
    a jumps 1:every place is tried\begin{gathered} \text{a jumps 1:} \\ \text{every place is tried} \end{gathered}
    letteraany other
    jump13
    The table for aaa. The nearest a before the last stands 1 from the end; any other letter jumps 3.

    why it is allowed: The text's letter above the pattern's last is always a. The nearest a before the last stands 1 from the end, so the letter a jumps 1. For nn of 1 or 2 the pattern is one a, with no letter before its last, so it jumps its whole length, 1 again. The pattern moves on one place at a time.

  4. the move: the places

    the line:
    places: n−m+1\text{places: } n - m + 1

    why it is allowed: By line 3, every place from 1 to the last, where the pattern's mm letters lie under letters n−m+1n - m + 1 to nn. For 6 letters and 3, places 1 to 4.

  5. the move: the pattern half the text

    the line:
    m is ⌈n2⌉,n−m+1 is ⌊n2⌋+1\begin{gathered} m \text{ is } \lceil \frac{n}{2} \rceil, \\ n - m + 1 \text{ is } \lfloor \frac{n}{2} \rfloor + 1 \end{gathered}

    why it is allowed: The case takes mm as half of nn rounded up. Half of nn rounded up and half rounded down add up to nn, so n−mn - m is half rounded down, and n−m+1n - m + 1 is 1 more. For 6, 3 and 4.

  6. the move: multiply by the places

    the line:
    M=⌈n2⌉(⌊n2⌋+1)M = \lceil \frac{n}{2} \rceil (\lfloor \frac{n}{2} \rfloor + 1)

    why it is allowed: Line 2's mm at each place, times line 4's places, both written in nn by line 5. For 6, 3 times 4, which is 12; for 7, 4 times 4, which is 16.

  7. the move: no search costs more

    the line:
    no text and pattern ofthese lengths cost more\begin{gathered} \text{no text and pattern of} \\ \text{these lengths cost more} \end{gathered}

    why it is allowed: By line 1 no place costs more than mm, and every jump is at least 1. So no search tries more than line 4's places: at most line 6's MM. For 6, 12.

  8. the move: a ceiling

    the line:
    M≤n2M \le n^2

    why it is allowed: For nn of 1 or more: n2\frac{n}{2} is at most nn, and nn is whole, so ⌈n2⌉\lceil \frac{n}{2} \rceil is at most nn. ⌊n2⌋\lfloor \frac{n}{2} \rfloor is a whole number below nn, so ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 is at most nn. Multiply the two. For 6, 12 is at most 36.

  9. the move: read the bound

    the line:
    M∈O(n2)M \in O(n^2)

    why it is allowed: By line 8, with the fixed multiple 1.

  10. the move: a floor

    the line:
    M≥n24M \ge \frac{n^2}{4}

    why it is allowed: Rounding up never makes a number smaller, so ⌈n2⌉\lceil \frac{n}{2} \rceil is at least n2\frac{n}{2}. Rounding down takes off less than 1, so ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 is more than n2\frac{n}{2}. Multiply the two, as n2\frac{n}{2} times n2\frac{n}{2} is n24\frac{n^2}{4}. For 6, 12 against 9.

  11. the move: read the bound

    the line:
    M∈Ω(n2)M \in \Omega(n^2)

    why it is allowed: By line 10, with the fixed multiple a quarter.

  12. the move: both together

    the line:
    M∈Θ(n2)M \in \Theta(n^2)

    why it is allowed: Lines 9 and 11 bound MM by the same growth.

try it with a small nn: at n=6n = 6, line 6 gives M=12M = 12, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 401 letters, 401 places.
  • 160,801.
  • 1.