I failed the whole evening to calculate a simple shift table for the search term "anabanana" for use in the Boyer and Moore pattern matching algorithm.
I found the following example without any explanations:
Can anybody help me understand and explain the image's method for finding the shift table?
I think I understand what is done here, so i'll try and explain.
The line "Wort" is the pattern you are analysing, there is (in my opinion) no need to consider the row "Text" above. Instead assume an additional row containing the zero-based position of the char within your pattern from left to right.
The length m
of the pattern p[]
depicted is 9.
Each row below I name p_i[]
where i is the index on the right
Further explanation is based on 2:
In the lower rows beneath the pattern mark all characters matching the character in the pattern above. (Done here by crossing out)
for i=1 to m do
search in the rows below for a subpattern where p[m-i]<>p_j[m-1] (*)
and p[m-i+1, ..., m-1]=p_j[m-i+1, ..., m-1]
index j is your shift value for shift[i]
od
(*) Note: When the shifted pattern p_j
got shifted too far right, there will be empty chars to compare. In this case, you can always assume ==
or <>
as needed. Always use the minimum of all possible j
.
I hope this helps, albeit a bit late.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With