Xpbctbxabpqxctbpg Abxab: The Boyer-Moore Algorithm Right-To-Left Scan
Xpbctbxabpqxctbpg Abxab: The Boyer-Moore Algorithm Right-To-Left Scan
Right-to-left scan
12345678901234567
xpbctbxabpqxctbpg
tpabxab
Boyer-Moore
Page 1
P = abadab
Boyer-Moore
Page 2
x a b c d
R(x) 5 6 0 4
bacbcdabaaba
abadab
abadab
abadab
abadab
Boyer-Moore
Page 3
Boyer-Moore
Shift by 2
Shift by 3
Shift by 1
Page 4
T
P
x
P
x
x
x
y
T
x
t
P
Boyer-Moore
Page 5
Boyer-Moore
t
P
t
y
t
Page 6
P
s
Boyer-Moore
s
y
Page 7
Boyer-Moore
Page 8
123456789012345678
prstabstubabvqxrst
*
qcabdabdab
1234567890
123456789012345678
prstabstubabvqxrst
qcabdabdab
1234567890
Boyer-Moore
Page 9
L(i)
Boyer-Moore
n
P
Boyer-Moore
Page 10
Page 11
Boyer-Moore
z
1
y
L(i)
y
i
n
Page 12
Example
Nj(P)
P
Example
j
P[j]
Nj(P)
1 2 3 4 5 6 7 8 9
c a b d a b d a b
0 0 2 0 0 5 0 0 *
Boyer-Moore
j
P[j]
Nj(P)
1 2 3 4 5 6 7 8 9
c a b d a b d a b
0 0 2 0 0 5 0 0 *
Pr[j]
Zj(Pr)
b a d b a d b a c
* 0 0 5 0 0 2 0 0
Theorem.
L(i) is the largest j such that Nj(P) |P[i..n]|.
L(i) is the largest j such that Nj(P)= |P[i..n]|.
Boyer-Moore
Page 14
Page 15
Boyer-Moore
Page 16
Boyer-Moore
Page 17
Page 19
Boyer-Moore(P,T)
compute L(i) and L(i) for each position i of P
compute R(x) for each x in the alphabet
kn
while k m do
i n; h k
while i > 0 and P[i] = T[h] do
i--; h-if i = 0 then
report occurrence of P in T ending at T[k]
k k + n l(2)
else
shift P (increase k) by the maximum
amount determined by the bad character
rule and the good suffix rule
Boyer-Moore
Page 18