find a word in a text
Find where a short word, the pattern, stands in a longer text. Race four methods on the same text and pattern.
A step is one letter compared with another. For Rabin-Karp, working out a fingerprint, one number for a stretch of letters, is a step too.
In the race below each method has a lane. At each tick of the clock, every lane still running makes one more step. Fewest steps wins.
All four race on kookaburra, looking for burra. Burra's first letter can sit under any of the text's first 6 letters: at place 6 its last letter sits under the text's 10th. These are its 6 places, and it matches at place 6. A search finds every place the pattern matches, so it carries on after a match. Below, each method's steps on kookaburra.
Choose "a comparison" under "one tick is", and Rabin-Karp's fingerprints are not counted: on kookaburra it then wins with 5 comparisons, against Horspool's 7.
Brute force tries every place from the left. At each, it compares letters from the left until two differ or the whole pattern matches.
Boyer-Moore-Horspool, Horspool for short, compares from the pattern's right end. Then it reads the text letter over that end, and jumps the pattern on until a copy of that letter, before the pattern's end, sits under it. With no such copy, it jumps right past. Its jumps come from a list made from the pattern first, which compares no letters, so costs no steps.
Rabin-Karp works out a fingerprint for the pattern, and one for the stretch of text at each place. Each letter gets a number, a 1 up to z 26, multiplied by 26 once for each letter after it. The numbers are added, and the fingerprint is the remainder after dividing by 11. It compares letters only where the two fingerprints agree.
| place | fingerprint | letters compared | steps |
|---|---|---|---|
| burra itself | 6 | 0 | 1 |
| 1 | 2 | 0 | 1 |
| 2 | 10 | 0 | 1 |
| 3 | 2 | 0 | 1 |
| 4 | 0 | 0 | 1 |
| 5 | 7 | 0 | 1 |
| 6 | 6 | 5 | 6 |
| all | 5 | 12 |
Knuth-Morris-Pratt first compares the pattern with itself, to build a table. For each letter, the table holds how many of the pattern's first letters still line up after the letter just past it differs: for abab, 0, 0, 1, 2. So it slides the pattern on and never moves back in the text.
| stage | letters compared | steps |
|---|---|---|
| its table | u, r, r and a against b, each different | 4 |
| places 1 to 5 | k, o, o, k and a against b | 5 |
| place 6 | b, u, r, r, a: all the same | 5 |
| all | 14 |
or make one
race these, up to four
Choose two to four racers.
one tick is
Without scripts the race cannot run, so each lane shows where its racer ends.
Tick 14: no lane has made more than 14 steps. Brute-force search, boyer-moore-horspool, rabin-karp and knuth-morris-pratt have finished.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
brute-force search
done in 10 stepsboyer-moore-horspool
done in 7 stepsrabin-karp
done in 12 stepsknuth-morris-pratt
done in 14 stepswhat the marks mean
- letters compared and the same, or letters being read
- letters compared and different
- a letter of a match found
- the letter a jump is worked out from
- a letter no match can start at
- any other letter
- how much of its race a lane has run
which suits this text and pattern
On this text of 10 letters, boyer-moore-horspool made the fewest steps: 7, against 10 for brute-force search, 12 for rabin-karp and 14 for knuth-morris-pratt.
each racer's own page
- brute-force search
- boyer-moore-horspool
- rabin-karp
- knuth-morris-pratt
the starting racers, then on a longer text
Horspool wins on kookaburra, and Knuth-Morris-Pratt comes last. A text of a's and a pattern of a's turn that over: Knuth-Morris-Pratt wins, as the shapes below show, and a longer text widens the gap. Take 1,000 a's and a pattern of 500 a's. The pattern can stand at 501 places, 1,000 less 500, plus 1, and matches at every one. Brute force compares all 500 letters at each: 500 at each of 501 places, 250,500 steps. So does Horspool: the pattern has an a just before its end, so it jumps only 1 place each time. Rabin-Karp compares the same, plus 502 fingerprints, one at each place and the pattern's own: 251,002. Knuth-Morris-Pratt builds its table in 499 steps, one for each a after the first, then compares each text letter once: 499 + 1,000 = 1,499.
First, their steps on the starting text. Then on 1,000 a's and a pattern of 500 a's. The fewest in each row is in bold.
| brute-force search | boyer-moore-horspool | rabin-karp | knuth-morris-pratt | |
|---|---|---|---|---|
| the starting textkookaburra, burra | 10 | 7 | 12 | 14 |
| a text of 1,000 letters | 250,500 | 250,500 | 251,002 | 1,499 |
the starting racers on a text of 10 letters in each shape
Each shape below is a kind of text and pattern the race can make. Horspool makes the fewest steps on three of the four. On aaaaaaaaaa and aaaaa every place matches: Horspool makes 30 steps, as the picture below shows, and Knuth-Morris-Pratt 14. On aaaaaaaaaa and bbbbb, every stretch of 5 letters in the text is aaaaa. The stretch aaaaa is 475,255, which is 11 times 43,205, and bbbbb is twice that, 950,510, so both leave 0 after dividing by 11. So every fingerprint agrees by chance, and Rabin-Karp compares a letter at all 6 places, beside its 7 fingerprints: 13 steps. Use Horspool on everyday text, where it jumps far. Avoid it where the pattern ends in a run of one letter that the text repeats: it then compares that run again at every place and jumps 1. Against ten a's, aaaaa and baaaa each cost it 30 steps. On aaaaa, which matches at every place, Knuth-Morris-Pratt makes 14, the fewest. On baaaa brute force makes 6, as its first letter, b, differs at once, and Knuth-Morris-Pratt 10: 4 for its table, then its b against the text's first 6 a's.
| method | letters compared | also | steps |
|---|---|---|---|
| brute force | 5 at each of 6 places | 30 | |
| Horspool | 5 at each of 6 places | 30 | |
| Rabin-Karp | 5 at each of 6 places | 7 fingerprints | 37 |
| Knuth-Morris-Pratt | each a once: 10 | its table: 4 | 14 |
Their steps on the text and pattern the race makes in each shape.
| brute-force search | boyer-moore-horspool | rabin-karp | knuth-morris-pratt | |
|---|---|---|---|---|
| a's, and a's ending in baaaaaaaaaa, aaaab | 30 | 6 | 7 | 22 |
| a's, and a'saaaaaaaaaa, aaaaa | 30 | 30 | 37 | 14 |
| a's, and b'saaaaaaaaaa, bbbbb | 6 | 2 | 13 | 10 |
| random letters, the pattern in thembabccabcbc, bccab | 11 | 6 | 12 | 12 |