For each character of the alphabet, a bucket collects all the positions of that character in x. When a character occurs k times in the pattern, there are k corresponding positions in the bucket of the character. When the word is much shorter than the alphabet, many buckets are empty.
The preprocessing phase of the Skip Search algorithm consists in computing the buckets for all the characters of the alphabet: for c in z[c] = {i : 0
i
m-1 and x[i] = c} The space and time complexity of this preprocessing phase is O(m+
).
The main loop of the search phase consists in examining every m-th text character, y[j] (so there will be n / m main iterations). For y[j], it uses each position in the bucket z[y[j]] to obtain a possible starting position p of x in y. It performs a comparison of x with y beginning at position p, character by character, until there is a mismatch, or until all match.
The Skip Search algorithm has a quadratic worst case time complexity but the expected number of text character inspections is O(n).
Main features:
Example:
Preprocessing phase
Z table used by Skip Search algorithm.
Searching phase
G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
2 | 1 | ||||||||||||||||||||||
G | C | A | G | A | G | A | G | ||||||||||||||||
G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
1 | - | ||||||||||||||||||||||
G | C | A | G | A | G | A | G | ||||||||||||||||
G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ||||||||||||||||
G | C | A | G | A | G | A | G |
Shift by: 8
G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
1 |
Shift by: 8
G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
2 | 1 | ||||||||||||||||||||||
G | C | A | G | A | G | A | G |
The Skip Search algorithm performs 14 character comparisons on the example.