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.
place
letters above
comparisons
1
aaa
3
2
aab
2, found
table
3
all
8
aab tried at 2 places of aaab, after its table.
if you chose another
AThat counts the search but leaves out the table's 3.
BThat is brute force, 3 at each place: no table, but it compares the text's second a again.
DThat is the table alone.
2question 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
Aabab 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.
Cb is not the pattern's first letter, a: 1 at each of 5 places, and 8 with the table.
Daba 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.
3question 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.
AThat is 2n, the search's ceiling in the learn page's section 6, with no table.
BThat is 2n + m: the table is at most 2m - 3, not 2m.
DThat is 3n - 3, a pattern as long as the text; this one is 500.
4question 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.
BThat is how a count would grow if it went as the text's length squared.
CThe pattern grows too, and every piece of the count grows with the text.
D3 is about the comparisons per letter, 2.5 rounded up, not how many times the count grows.
pen and paper
5question 5about 6 minutes
A text holds the letters cacacab. Search it for caca with Knuth-Morris-Pratt, numbering places from 1.
Write the table of borders, and count its comparisons.
At which places is caca found, and how many comparisons does the search make?
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.
place
letters above
compared
comparisons
1
caca
c, a, c, a
4, found
3
caca
ca lined up; c, a
2, found
table
3
all
9
caca found at places 1 and 3 of cacacab. At place 3 its first two letters are not compared again.
A text holds 401 letters, all a. The pattern has half as many letters, rounded up, all a but the last, which is b.
How many letters has the pattern, and how many comparisons does its table cost?
How many comparisons in the search?
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, , on a text of a's and a pattern of letters, a's ending in b, half of rounded up?15 of 15 shown
1the move: the table's a's
the line:
why it is allowed: Each of the starts from 2 letters to 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.
2the move: the table's b
the line:
start
a
aa
aab
border
0
1
0
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 . It falls back 1 letter at a time, by line 1, to a border of nothing, and differs at each: borders down to 0, comparisons. For aab, 2.
guess it, then
3the move: the table
the line:
why it is allowed: Lines 1 and 2 added. For aab, 3.
guess it, then
4the move: the first place
the line:
why it is allowed: The pattern's a's match the text's, then its b differs from an a.
guess it, then
5the move: each place after
the line:
place
letters above
comparisons
1
aaa
3
2
aaa
2
3
aaa
2
4
aaa
2
search
9
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 a's matched end with a border of 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.
guess it, then
6the move: the places
the line:
why it is allowed: One place at a time, by line 5. At the last place the pattern's letters end under letter , so the last place is . For 6 letters and 3, places 1 to 4.
guess it, then
7the move: the search
the line:
why it is allowed: Line 4's , then 2 at each place after place 1, by line 5. Line 6's places less place 1 are . For 6, 3 + 2 times 3.
guess it, then
8the move: multiply out
the line:
why it is allowed: Twice all of is twice less twice . Adding line 7's gives one back: . For 6, 12 - 3, which is 9.
guess it, then
9the move: add the two
the line:
why it is allowed: Lines 3 and 8 added: and . For 6, 3 + 9, which is 12.
guess it, then
10the move: the pattern half the text
the line:
why it is allowed: Line 9 with as half of rounded up. For 6, 12 + 3 - 3, which is 12; for 7, 14 + 4 - 3, which is 15.
guess it, then
11the move: a ceiling
the line:
why it is allowed: Half of rounded up is at most , so line 10 is at most , less than . For 6, 12 against 18.
guess it, then
12the move: read the bound
the line:
why it is allowed: By line 11, with the fixed multiple 3.
guess it, then
13the move: a floor
the line:
why it is allowed: From 3 up, half of rounded up is at least 2. So line 10 is at least 1 less than twice , and that is at least . For 6, 12 against 6.
guess it, then
14the move: read the bound
the line:
why it is allowed: By line 13, with the fixed multiple 1.
guess it, then
15the move: both together
the line:
why it is allowed: Lines 12 and 14 bound by the same growth, the text's.
guess it, then
try it with a small : at , line 10 gives , which you can work out by hand