


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



If we have text string S and pattern string P , we want to determine whether or not P is found in S, i.e. P is a substring of S.
Strings are arrays of characters. Characters however can be interpreted as integers, with their exact values depending on what type of encoding is being used (e.g. ASCII, Unicode). This means we can treat strings as arrays of integers. Finding a way to convert an array of integers into a single integer allows us to hash strings with hash functions that expect numbers as input. Since strings are arrays and not single elements, comparing two strings for equality is not as straightforward as comparing two integers for equality. To check to see if string A and string B are equal, we would have to iterate through all of A’s elements and all of B’s elements, making sure that A[i] = B[i] for all i. This means that string comparison depends on the length of the strings. Comparing two n-length strings takes O(n) time. Also, since hashing a string usually involves iterating through the string’s elements, hashing a string of length n also takes O(n) time.
Say P has length L and S has length n. One way to search for P in S:
This method takes O(nL) time. We can improve on this runtime by using a rolling hash. In step 2. we looked at O(n) substrings independently and took O(L) to hash them all. These sub- strings however have a lot of overlap. For example, looking at length 5 substrings of “algorithms”, the first two substrings are “algor” and “lgori”. Wouldn’t it be nice if we could take advantage of the fact that the two substrings share “lgor”, which takes up most of each substring, to save some computation? It turns out we can with rolling hashes.
Let’s step back from strings for a second. Say we have P and S be two integer arrays:
The length 5 substrings of S will be denoted as such:
We want to see if P ever appears in S using the three steps in the method above. Our hash function will be:
h(k) = (k[0]10^4 + k[1]10^3 + k[2]10^2 + k[3]10^1 + k[4]10^0 ) mod m (7)
Or in other words, we will take the length 5 array of integers and concatenate the integers into a 5 digit number, then take the number mod m. h(P ) = 90210 mod m, h(S 0 ) = 48902 mod m, and h(S 1 ) = 89021 mod m. Note that with this hash function, we can use h(S 0 ) to help calculate h(S 1 ). We start with 48902, chop off the first digit to get 8902, multiply by 10 to get 89020, and then add the next digit to get 89021. More formally:
h(Si+1) = [(h(Si) − (10^5 ∗ first digit of Si)) ∗ 10 + next digit after Si] mod m (8)
We can imagine a window sliding over all the substrings in S. Calculating the hash value of the next substring only inspects two elements: the element leaving the window and the element entering the window. This is a dramatic difference from before, where we calculated each sub- string’s hash values independently and would have to look at L elements for each hash calculation. Finding the hash value of the next substring is now a O(1) operation. In this numerical example, we looked at single digit integers and set our base b = 10 so that we can interpret the arithmetic easier. To generalize for other base b and other substring length L, our hash function is
h(k) = (k[0]bL−^1 + k[1]bL−^2 + k[2]bL−^3 ...k[L − 1]b^0 ) mod m (9)
And calculating the next hash value is:
h(Si+1) = b(h(Si) − bL−^1 S[i]) + S[i + L] mod m (10)
which takes O(nL) time, making string comparison the performance bottleneck now. We can increase table size and modify our hash function so that the hash table has O(n^2 ) slots, leading to an expectation of O( (^) n^1 ) collisions for each substring of T. This solves our problem and returns the total runtime to O(n) but we may not necessarily have the resources to create a large table like that. Instead, we will take advantage of string signatures. In addition to inserting the actual sub- string into the hash table, we will also associate each substring with another hash value, hs(k). Note that this hash value is different from the one we used to insert the substring into the hash table. The hsk hash function actually maps strings to a range 0 to n^2 as opposed to 0 to n like h(k). Now, when we have collisions inside the hash table, before we actually do the expensive string comparison operation, we first compare the signatures of the two strings. If the signatures of the two strings do not match, then we can skip the string comparison. For two substrings k 1 and k 2 , only if h(k 1 ) = h(k 2 ) and hs(k 1 ) = hs(k 2 ) do we actually make the string comparison. For a well chosen hs(k) function, this will reduce the expected time spent doing string comparisons back to O(n), keeping the common substring problem’s runtime at O(n).