Skip to content

Rabin-Karp

words

Level 2 of 3

Also called Karp-Rabin algorithm and Rolling hash search.

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

Rabin-Karp looks for ate in abdicate. The fingerprints agree only at places 2 and 6. How many letter comparisons does it make?

the answer, and why

the answer is B

Place 2, bdi: b is not a, 1. Place 6, ate: a, t and e match, 3. No other place compares. That is 4.

if you chose another

  • That compares all 3 letters at both places, but place 2 stops at its first letter.
  • That counts only the match, not the chance agreement at place 2.
  • That compares all 3 letters at all 6 places, the most any search of 8 letters and 3 could make.

question 2

Each text has 10 letters and each pattern 5. Which makes Rabin-Karp compare the fewest letters?

the answer, and why

the answer is B

aabaa is aaaaa plus 676: its middle letter, worth 26 times 26, is b (2), not a (1). 676 leaves 5, so aabaa leaves 5, never aaaaa's 0. No place agrees, and no letter is compared.

if you chose another

  • aaaaa and bbbbb both leave 0, so every place agrees by chance and compares 1 letter, 6 in all.
  • babab matches at places 2, 4 and 6, 5 letters each, and no other place agrees, so 15.
  • ccccc's number is 3 times aaaaa's, so it leaves 0 too: every place agrees by chance and compares 1 letter, 6 in all.

question 3

A text has 1,200 letters, all a. Rabin-Karp searches it for 600 a's. How many steps, fingerprints plus comparisons?

the answer, and why

the answer is B

1,200 - 600 + 1 is 601 places, so 602 fingerprints with the pattern's. Every place matches, 600 comparisons each, 360,600. Together 361,202.

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

if you chose another

  • That is the comparisons alone.
  • That is the fingerprints alone, the count if no place agreed.
  • That is 600 comparisons at 1,200 places, but there are only 601 places.

question 4

A text of a's doubles from 1,000 letters to 2,000. The pattern, a's ending in b, is always half the text's length. About how many times as many steps?

the answer, and why

the answer is C

No place agrees, so the steps are the fingerprints, about half the text. 502 becomes 1,002.

this tests how it scales, on the learn page

if you chose another

  • That is the worst, which grows as nn times nn.
  • 1 means no growth, but twice the text gives about twice the places, each one a fingerprint.
  • That is how a count growing as nn times nn times nn grows.

pen and paper

question 5about 6 minutes

Rabin-Karp searches addenda for den. den's fingerprint is 10. The places' fingerprints, in order, are 3, 8, 10, 8 and 10.

  1. At which places are letters compared, and how many at each?
  2. Where does den stand?
  3. How many steps, fingerprints plus comparisons, in all?
a hint

Compare letters only where a place's fingerprint is 10, from the left, until one differs or all 3 match.

a worked answer

Places 3 and 5 have 10. Place 3, den: d, e and n match, 3. Place 5, nda: n is not d, 1, an agreement by chance. So den stands at place 3. Fingerprints: den's and the 5 places', 6. Comparisons: 3 + 1, which is 4. Steps: 6 + 4, which is 10.

placeletters abovefingerprintcomparisons
1add30
2dde80
3den103, a match
4end80
5nda101, by chance
den under each place of addenda. Only places 3 and 5 share den's 10.

this tests how it works, on the learn page

what a full answer includes
  • Places 3 and 5: 3 and 1.
  • Place 3.
  • 10.

question 6about 8 minutes

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

  1. How many letters has the pattern, and how many fingerprints are worked out?
  2. How many steps if it is a's ending in b?
  3. How many if it is all a?
a hint

Questions 1 and 2 of the learn page's section 4 count each.

a worked answer

Half of 1,001 rounded up is 501 letters, so 1,001 - 501 + 1, which is 501 places, and 502 fingerprints with the pattern's. a's ending in b: no place agrees, 502 steps. All a: 501 comparisons at each of 501 places, 251,001, plus the 502 fingerprints, 251,503.

Question 2: how many steps, MM, on a text of nn a's and a pattern of a's half as long, 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: Letters are compared only where the fingerprints agree, from the left, until one differs. The pattern has mm letters, so at most mm.

  2. the move: every place agrees and matches

    the line:
    a’s against a’s:m at every place\begin{gathered} \text{a's against a's:} \\ m \text{ at every place} \end{gathered}
    placeabovefingerprintcompared
    1aaa103
    2aaa103
    3aaa103
    4aaa103
    all5: 4 places and aaa's12
    A text of 6 a's and the pattern aaa. Every fingerprint is 10, and every place is a match. 5 fingerprints and 12 comparisons.

    why it is allowed: Every place has mm a's above it, the pattern's own letters, so its fingerprint too. Every place agrees, and all mm letters match.

  3. the move: multiply by the places

    the line:
    comparisons:⌈n2⌉(⌊n2⌋+1)\begin{gathered} \text{comparisons:} \\ \lceil \frac{n}{2} \rceil (\lfloor \frac{n}{2} \rfloor + 1) \end{gathered}

    why it is allowed: Section 4's question 1, lines 1, 3 and 6, give ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 places. This question's line 2 gives ⌈n2⌉\lceil \frac{n}{2} \rceil comparisons at each. For 6, 4 times 3, which is 12.

  4. the move: add the fingerprints

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

    why it is allowed: A step is a fingerprint or a comparison. Section 4's question 1, lines 2, 3 and 6, give ⌊n2⌋+2\lfloor \frac{n}{2} \rfloor + 2 fingerprints, whatever the letters. For 6, 5 + 12, which is 17; for 7, 5 + 16, which is 21.

  5. the move: gather the places

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

    why it is allowed: Line 4's ⌊n2⌋+2\lfloor \frac{n}{2} \rfloor + 2 is the places, ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1, plus 1. The places taken ⌈n2⌉\lceil \frac{n}{2} \rceil times and once more make ⌈n2⌉+1\lceil \frac{n}{2} \rceil + 1 times, and the 1 stays. For 7, 5 + 16 is 4 + 1 + 4 times 4, which is 4 times 5, plus 1, 21.

  6. the move: a ceiling

    the line:
    M≤3n2M \le 3n^2

    why it is allowed: From 1 up, half of nn rounded up is at most nn, so plus 1 at most 2n2n. Half rounded down is a whole number below nn, so plus 1 at most nn. Their product is at most twice n2n^2, and the last 1 at most n2n^2. For 6, 17 against 108.

  7. the move: read the bound

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

    why it is allowed: By line 6, with the fixed multiple 3.

  8. 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 line 5's ⌈n2⌉+1\lceil \frac{n}{2} \rceil + 1 is more than 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} too. Multiply, as n2\frac{n}{2} times n2\frac{n}{2} is n24\frac{n^2}{4}; the last 1 only adds. For 6, 17 against 9.

  9. the move: read the bound

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

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

  10. the move: both together

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

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

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

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

what a full answer includes
  • 501 letters, 502 fingerprints.
  • 502.
  • 251,503.