Maximal Shift
Sunday designed an algorithm where the pattern characters are scanned from the one which will lead to a larger shift to the one which will lead to a shorter shift. Doing so one may hope to maximize the lengths of the shifts.
The preprocessing phase of the Maximal Shift algorithm consists in sorting the pattern characters in decreasing order of their shift and then in building the Quick Search bad-character shift function (see chapter Quick Search algorithm) and a good-suffix shift function adapted to the scanning order of the pattern characters. It can be done in O(m2+
) time and O(m+
) space complexity.
The searching phase of the Maximal Shift algorithm has a quadratic worst case time complexity.
Main features:
- variant of the Quick Search algorithm;
- quadratic worst case time complexity;
- preprocessing phase in O(m2+
) time and O(m+
) space complexity. - searching phase in O(mn) time complexity.
Example:
Preprocessing phase

Tables used by Maximal Shift 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 |
| 1 | |||||||||||||||||||||||
| G | C | A | G | A | G | A | G |
Shift by: 1 (qsBc[G]=adaptedGs[0])
| 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 |
Shift by: 2 (qsBc[A])
| 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 |
Shift by: 2 (qsBc[A])
| G | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 8 | 7 | 2 | 1 | 6 | 5 | 4 | 3 | ||||||||||||||||
| G | C | A | G | A | G | A | G |
Shift by: 9 (qsBc[T])
| 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 |
Shift by: 7 (qsBc[C])
The Maximal Shift algorithm performs 12 character comparisons on the example.
void preQsBc(char *x, int m, int qsBc[]) {
int i;
for (i = 0; i < ASIZE; ++i)
qsBc[i] = m + 1;
for (i = 0; i < m; ++i)
qsBc[x[i]] = m - i;
}
/* Construct an ordered pattern from a string. */
void orderPattern(char *x, int m, int (*pcmp)(),
pattern *pat) {
int i;
for (i = 0; i <= m; ++i) {
pat[i].loc = i;
pat[i].c = x[i];
}
qsort(pat, m, sizeof(pattern), pcmp);
}
/* Find the next leftward matching shift for
the first ploc pattern elements after a
current shift or lshift. */
int matchShift(char *x, int m, int ploc,
int lshift, pattern *pat) {
int i, j;
for (; lshift < m; ++lshift) {
i = ploc;
while (--i >= 0) {
if ((j = (pat[i].loc - lshift)) < 0)
continue;
if (pat[i].c != x[j])
break;
}
if (i < 0)
break;
}
return(lshift);
}
/* Constructs the good-suffix shift table
from an ordered string. */
void preAdaptedGs(char *x, int m, int adaptedGs[],
pattern *pat) {
int lshift, i, ploc;
adaptedGs[0] = lshift = 1;
for (ploc = 1; ploc <= m; ++ploc) {
lshift = matchShift(x, m, ploc, lshift, pat);
adaptedGs[ploc] = lshift;
}
for (ploc = 0; ploc <= m; ++ploc) {
lshift = adaptedGs[ploc];
while (lshift < m) {
i = pat[ploc].loc - lshift;
if (i < 0 || pat[ploc].c != x[i])
break;
++lshift;
lshift = matchShift(x, m, ploc, lshift, pat);
}
adaptedGs[ploc] = lshift;
}
}
typedef struct patternScanOrder {
int loc;
char c;
} pattern;
int minShift[XSIZE];
/* Computation of the MinShift table values. */
void computeMinShift(char *x, int m) {
int i, j;
for (i = 0; i < m; ++i) {
for (j = i - 1; j >= 0; --j)
if (x[i] == x[j])
break;
minShift[i] = i - j;
}
}
/* Maximal Shift pattern comparison function. */
int maxShiftPcmp(pattern *pat1, pattern *pat2) {
int dsh;
dsh = minShift[pat2->loc] - minShift[pat1->loc];
return(dsh ? dsh : (pat2->loc - pat1->loc));
}
/* Maximal Shift string matching algorithm. */
void MS(char *x, int m, char *y, int n) {
int i, j, qsBc[ASIZE], adaptedGs[XSIZE];
pattern pat[XSIZE];
/* Preprocessing */
computeMinShift(x ,m);
orderPattern(x, m, maxShiftPcmp, pat);
preQsBc(x, m, qsBc);
preAdaptedGs(x, m, adaptedGs, pat);
/* Searching */
j = 0;
while (j <= n - m) {
i = 0;
while (i < m && pat[i].c == y[j + pat[i].loc])
++i;
if (i >= m)
OUTPUT(j);
j += MAX(adaptedGs[i], qsBc[y[j + m]]);
}
}