Design & Analysis of algorithm- 6
Design & Analysis of algorithm- 6
○ The prefix for the above string can be: a, ab, or aba. The prefix excludes the last string
character.
○ The suffix for the above string can be: b, ba, or bab. It excludes the first character of the
string.
○ String = ababcb
○ Index value: 1 2 3 4 5
○ a b a b d
○ 0 0 1 2 0
○ We also know (from the above definition) that lps[j-1] is the count of characters of pat[0…j-1]
that are both proper prefix and suffix.
Let us execute the KMP Algorithm to find whether 'P' occurs in 'T.'
For 'p' the prefix function, ? was computed previously and is as follows:
3. n be the length of the pattern and m be the length of the text. Here, m = 10 and n = 3.
Let d be the number of characters in the input set. Here, we have taken input set {A, B, C, ..., J}. So, d =
10. You can assume any suitable value for d.
4. Let us calculate the hash value of the pattern.
In the calculation above, choose a prime number (here, 13) in such a way that we can perform all the
calculations with single-precision arithmetic.
5. Calculate the hash value for the text-window of size m.
For the first window ABC,
hash value for text(t) = Σ(v * dn-1) mod 13
= ((1 * 102) + (2 * 101) + (3 * 100)) mod 13
= 123 mod 13
=6
= 233 mod 13
= 12
In order to optimize this process, we make use of the previous hash value in the following way.
t = ((d * (t - v[character to be removed] * h) + v[character to be added] ) mod 13
= ((10 * (6 - 1 * 9) + 3 )mod 13
= 12
Where, h = dm-1 = 103-1 = 100.
The worst-case complexity occurs when spurious hits occur a number for all the windows.