Skip to content

Knuth-Morris-Pratt

words

Level 3 of 3

Also called KMP algorithm and Knuth-Morris-Pratt 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

Knuth-Morris-Pratt looks for aab in aaab. How many comparisons does it make, its table included?

the answer, and why

the answer is C

The table for aab: a matches a, 1; b differs from a, falls back to nothing and differs from a again, 2. The search: a and a match, b is not a, 3. The 2 a's matched end with a border a, so the pattern moves on 1 place: its second a matches the same text a, and b matches b, 2. 3 + 3 + 2 is 8.

placeletters abovecomparisons
1aaa3
2aab2, found
table3
all8
aab tried at 2 places of aaab, after its table.

if you chose another

  • That counts the search but leaves out the table's 3.
  • That is brute force, 3 at each place: no table, but it compares the text's second a again.
  • That is the table alone.

question 2

Each search looks for abab in a text of 8 letters. Which makes Knuth-Morris-Pratt compare the most, its table included?

the answer, and why

the answer is B

The table for abab costs 3. In aaaaaaaa each a matches the pattern's a, then the next a is not b. Only a had matched, and one letter has no border, so that same text a is compared with the pattern's a. That is 2 at each of 5 places, 10, and 13 with the table.

if you chose another

  • abab is found at places 1, 3 and 5. Its border ab lines up each time, so only 2 new letters: 4 + 2 + 2, and 11 with the table.
  • b is not the pattern's first letter, a: 1 at each of 5 places, and 8 with the table.
  • aba matches, 3; the next a is compared 3 times as the pattern falls back twice, 3. Then b, a and b match, 3 more, and abab is found at place 4: 9, and 12 with the table.

question 3

Knuth-Morris-Pratt searches a text of 1,000 letters for a pattern of 500. At most how many comparisons can it make, its table included, whatever the letters?

the answer, and why

the answer is C

Question 3 of the learn page's section 4. The table: at most 2 times 500 - 3, which is 997. The search: at most 2 times 1,000 - 500, which is 1,500. 997 + 1,500 is 2,497. A text of a's and a pattern of a's ending in b make exactly that many.

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

if you chose another

  • That is 2n, the search's ceiling in the learn page's section 6, with no table.
  • That is 2n + m: the table is at most 2m - 3, not 2m.
  • That is 3n - 3, a pattern as long as the text; this one is 500.

question 4

Knuth-Morris-Pratt searches a text of a's for a pattern of a's ending in b, half as long as the text. 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

Question 2 of the learn page's section 4: 2n + half of n rounded up - 3, so 2,497, then 4,997. Twice the letters, about twice the comparisons.

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

if you chose another

  • That is how a count would grow if it went as the text's length squared.
  • The pattern grows too, and every piece of the count grows with the text.
  • 3 is about the comparisons per letter, 2.5 rounded up, not how many times the count grows.

pen and paper

question 5about 6 minutes

A text holds the letters cacacab. Search it for caca with Knuth-Morris-Pratt, numbering places from 1.

  1. Write the table of borders, and count its comparisons.
  2. At which places is caca found, and how many comparisons does the search make?
  3. How many comparisons in all?
a hint

A start's border is the longest piece that both begins and ends it, short of the whole start. After the pattern is found, move it on so the border at its front stands where the border at its back stood. abab found at place 1 moves on to place 3.

a worked answer

The starts c, ca, cac and caca have borders 0, 0, 1 and 2. Writing them: a is not c, 1; c matches c, 1; a matches a, 1: 3 comparisons. The search: c, a, c and a match at place 1, 4. Its border ca lines up at place 3 under the text's ca. The search carries on at the text's 5th letter: c and a match, 2, and caca is found at place 3 too. The pattern then moves on to place 5, where caca no longer fits. 4 + 2 is 6, and 3 + 6 is 9 in all.

placeletters abovecomparedcomparisons
1cacac, a, c, a4, found
3cacaca lined up; c, a2, found
table3
all9
caca found at places 1 and 3 of cacacab. At place 3 its first two letters are not compared again.

this tests how it works, on the learn page

what a full answer includes
  • 0, 0, 1, 2; 3 comparisons.
  • Places 1 and 3; 6 comparisons.
  • 9.

question 6about 8 minutes

A text holds 401 letters, all a. The pattern has half as many letters, rounded up, all a but the last, which is b.

  1. How many letters has the pattern, and how many comparisons does its table cost?
  2. How many comparisons in the search?
  3. How many in all?
a hint

Question 2 of the learn page's section 4 counts the table and the search apart.

a worked answer

Half of 401 rounded up is 201 letters. The table: 199 for the a's and 200 for the b, which is 2 times 201 - 3, 399. The search: 201 at place 1, then 2 at each of the 200 places after, 601. In all 399 + 601, which is 1,000, as 2 times 401 + 201 - 3.

Question 2: how many comparisons, MM, on a text of nn a's and a pattern of mm letters, a's ending in b, mm half of nn rounded up?
  1. the move: the table's a's

    the line:
    table, the a’s:m−2\begin{gathered} \text{table, the a's:} \\ m - 2 \end{gathered}

    why it is allowed: Each of the m−2m - 2 starts from 2 letters to m−1m - 1 ends in a, which matches the a after the border so far at once. That is 1 comparison each, and the border grows by 1. So a run of a's has a border 1 shorter than itself. For aab, 1.

  2. the move: the table's b

    the line:
    table, the b:m−1\begin{gathered} \text{table, the b:} \\ m - 1 \end{gathered}
    startaaaaab
    border010
    The table for aab. aa costs 1; aab's b differs from a after borders 1 and 0, 2 more.

    why it is allowed: The last start's b differs from the a after a border of m−2m - 2. It falls back 1 letter at a time, by line 1, to a border of nothing, and differs at each: borders m−2m - 2 down to 0, m−1m - 1 comparisons. For aab, 2.

  3. the move: the table

    the line:
    table: 2m−3\text{table: } 2m - 3

    why it is allowed: Lines 1 and 2 added. For aab, 3.

  4. the move: the first place

    the line:
    place 1:m comparisons\begin{gathered} \text{place 1:} \\ m \text{ comparisons} \end{gathered}

    why it is allowed: The pattern's m−1m - 1 a's match the text's, then its b differs from an a.

  5. the move: each place after

    the line:
    each place after:2 comparisons\begin{gathered} \text{each place after:} \\ 2 \text{ comparisons} \end{gathered}
    placeletters abovecomparisons
    1aaa3
    2aaa2
    3aaa2
    4aaa2
    search9
    A text of 6 a's and aab. 3 at place 1, then 2 at each place after. With the table's 3, 12.

    why it is allowed: The m−1m - 1 a's matched end with a border of m−2m - 2 a's, by line 1. So the pattern moves on 1 place and carries on at the same text letter, where its last a matches; then its b differs from the next a.

  6. the move: the places

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

    why it is allowed: One place at a time, by line 5. At the last place the pattern's mm letters end under letter nn, so the last place is n−m+1n - m + 1. For 6 letters and 3, places 1 to 4.

  7. the move: the search

    the line:
    search: m+2(n−m)\text{search: } m + 2(n - m)

    why it is allowed: Line 4's mm, then 2 at each place after place 1, by line 5. Line 6's places less place 1 are n−mn - m. For 6, 3 + 2 times 3.

  8. the move: multiply out

    the line:
    search: 2n−m\text{search: } 2n - m

    why it is allowed: Twice all of n−mn - m is twice nn less twice mm. Adding line 7's mm gives one mm back: 2n−m2n - m. For 6, 12 - 3, which is 9.

  9. the move: add the two

    the line:
    in all: 2n+m−3\text{in all: } 2n + m - 3

    why it is allowed: Lines 3 and 8 added: 2m−32m - 3 and 2n−m2n - m. For 6, 3 + 9, which is 12.

  10. the move: the pattern half the text

    the line:
    M=2n+⌈n2⌉−3M = 2n + \lceil \frac{n}{2} \rceil - 3

    why it is allowed: Line 9 with mm as half of nn rounded up. For 6, 12 + 3 - 3, which is 12; for 7, 14 + 4 - 3, which is 15.

  11. the move: a ceiling

    the line:
    M≤3nM \le 3n

    why it is allowed: Half of nn rounded up is at most nn, so line 10 is at most 3n−33n - 3, less than 3n3n. For 6, 12 against 18.

  12. the move: read the bound

    the line:
    M∈O(n)M \in O(n)

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

  13. the move: a floor

    the line:
    M≥nM \ge n

    why it is allowed: From 3 up, half of nn rounded up is at least 2. So line 10 is at least 1 less than twice nn, and that is at least nn. For 6, 12 against 6.

  14. the move: read the bound

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

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

  15. the move: both together

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

    why it is allowed: Lines 12 and 14 bound MM by the same growth, the text's.

try it with a small nn: at n=6n = 6, line 10 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
  • 201 letters; 399.
  • 601.
  • 1,000.