Horspool Algorithm
Horspool Algorithm
• The algorithm is used for pattern matching. That is to find a substring in a given string
• Let us compare the traditional brute force pattern matching with the Horspool Pattern
Matching Algorithm.
Brute force pattern
matching
• Given string is
• Pattern string is
HORSPOOL ALGORITHM:
• Given string is
• Pattern string is
• In horspool algorithm, a shift table has to be constructed which will contain the number shifts to
the right
THE SHIFT TABLE IS CONSTRUCTED AS FOLLOWS:
• P array stores the pattern. The length of the pattern is m . The pattern is stored in p from 0 to m-1.
ST[0 to size-1] is the shift table where assize is the alphabet size.
Asize=256 for all the ASCII characters, asize=26 for A to Z ,asize=62 for A to Z , a to z,0 to 9
Algorithm Shift_table(P,ST)
ST[i]=m; //This loop will initialize m[length of the pattern] for all the characters in the shift table
ST[index]=m-1-j; //This loop will find the shift value for all the m-1 characters in the pattern(we know
} the last character in the pattern will not have a shift value)
}
CONSIDER THE FOLLOWING STRING:
Let us see how to find the substring RAJKUMAR in the given string using Horspool Algorithm.
The basic idea of horspool is to shift the pattern depending upon whether the last character in the pattern matches particular character in the
given string.
ALGORITHM FOR HORSPOOL
• Algorithm Horspool(S,P)
m=length(P); //m will contain length of the pattern for the given string
i=m-1; //because we will starting the comparison end in the reverse direction
while(i<=n-1)
k=0; //k will be pointing to the first character to the pattern string
while(k<=m-1 and P[m-1-k]=S[i-k]) //if the characters match and still there are characters left to match here pattern is matches then it will be increment the value of k
k=k+1;