1. Gyanastra
  2. Algorithms
  3. String Matching with a Finite Automaton and Boyer-Moore
  4. Boyer-Moore: the bad-character rule
Free lesson, no account needed 4 min read

Algorithms · String Matching with a Finite Automaton and Boyer-Moore

Boyer-Moore: the bad-character rule

Boyer-Moore compares the pattern against the text right to left within each alignment, and on a mismatch shifts the pattern by more than one using two heuristics: the bad-character rule and the good-suffix rule. GATE concentrates on the bad-character rule.

3.1 The rule in one line

If, at alignment ss, the mismatch occurs at pattern position jj (1-based from the left) against text character c=T[s+j]c = T[s+j], then:

  • If cc occurs in PP to the left of position jj, let kk be the rightmost such occurrence; shift the pattern so that P[k]P[k] aligns with T[s+j]T[s+j]. Shift amount =j−k= j - k.
  • If cc does not occur in P[1..j−1]P[1..j-1], shift the pattern past the mismatching character: shift =j= j.
  • If cc occurs only to the right of jj, the bad-character rule gives a non-positive shift; use a shift of 11 (in the full algorithm, take the max with the good-suffix shift, which is always ≥1\ge 1).

The preprocessing is a table last[c]\text{last}[c] = index of the last (rightmost) occurrence of cc in PP, or 00 if absent. Then the shift is max⁡(1, j−last[c])\max(1,\, j - \text{last}[c]). This simple textbook form can shift less than the "rightmost occurrence left of jj" rule above when cc also occurs to the right of jj in PP (then j−last[c]≤0j-\text{last}[c]\le 0 and the shift is 11).

3.2 Worked example

P=P = ababaca (m=7m=7), T=T = abababacaba (n=11n=11). Last-occurrence table: a→7, b→4, c→6; all others 0.

Indices: P=a,b,a,b,a,c,aP = a,b,a,b,a,c,a and T=a,b,a,b,a,b,a,c,a,b,aT = a,b,a,b,a,b,a,c,a,b,a. Align PP at shift s=0s=0 and compare from the right.

T: a b a b a b a c a b a
P: a b a b a c a          (s = 0)
           ^ j = 6: T[6]=b, P[6]=c  (P[7]=a=T[7] matched first)

P[7]=a=T[7]P[7]=a=T[7] matches, then P[6]=c≠T[6]=bP[6]=c\ne T[6]=b: mismatch at j=6j=6 with c=bc=b, last[b]=4\text{last}[b]=4. Shift =6−4=2=6-4=2 (2 comparisons so far).

Align at s=2s=2:

T: a b a b a b a c a b a
P:     a b a b a c a      (s = 2)

Right to left: P[7]=a=T[9]P[7]=a=T[9] ✓, P[6]=c=T[8]P[6]=c=T[8] ✓, P[5]=a=T[7]P[5]=a=T[7] ✓, P[4]=b=T[6]P[4]=b=T[6] ✓, P[3]=a=T[5]P[3]=a=T[5] ✓, P[2]=b=T[4]P[2]=b=T[4] ✓, P[1]=a=T[3]P[1]=a=T[3] ✓. Match at shift 2 (T[3..9]=T[3..9]= ababaca).

Boyer-Moore used 2 alignments (2 + 7 = 9 character comparisons); the naive algorithm uses 3 alignments (shifts 0, 1, 2) and 6+1+7=146+1+7=14 comparisons. The first alignment shifted by 2 after only two comparisons.

3.3 Complexity

  • Preprocessing (bad-character table): O(m+σ)O(m + \sigma).
  • Matching with the bad-character rule alone: worst case O(nm)O(nm) (for example T=anT=a^n, P=bam−1P=ba^{m-1}: every alignment costs mm comparisons and shifts by only 11).
  • With both heuristics (original Boyer-Moore): O(n+m)O(n+m) when the pattern does not occur in the text, but still O(nm)O(nm) worst case when the pattern occurs many times (e.g. T=anT=a^n, P=amP=a^m). Adding Galil's rule makes the worst case O(n+m)O(n+m) always. In practice it is sublinear, with best case Ω(n/m)\Omega(n/m) comparisons.

Comparison of the five algorithms

AlgorithmPreprocessingMatchingWorst case totalExtra spaceDirection
Naive00O((n−m+1)m)O((n-m+1)m)O(nm)O(nm)O(1)O(1)left→right
Rabin–KarpΘ(m)\Theta(m)O(n−m+1)O(n-m+1) expected, O((n−m+1)m)O((n-m+1)m) worstO(nm)O(nm)O(1)O(1)left→right
Finite automatonO(mσ)O(m\sigma)Θ(n)\Theta(n)O(mσ+n)O(m\sigma + n)Θ(mσ)\Theta(m\sigma)left→right
KMPΘ(m)\Theta(m)Θ(n)\Theta(n)Θ(n+m)\Theta(n+m)Θ(m)\Theta(m)left→right
Boyer–MooreO(m+σ)O(m+\sigma)Ω(n/m)\Omega(n/m) best caseO(nm)O(nm) if matches are frequent; O(n+m)O(n+m) if pattern absent or with Galil's ruleO(m+σ)O(m+\sigma)right→left

Key contrasts to memorise: the automaton is the only one whose matching phase is Θ(n)\Theta(n) with no dependence on mm at all, but it pays Θ(mσ)\Theta(m\sigma) space. KMP achieves the same total time with only Θ(m)\Theta(m) space by computing fallbacks on the fly. Boyer–Moore is the fastest in practice because it skips text.


Pages 1–2 of 2