










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!











Perhaps the most important class of data structures maintain a set of items, indexed by keys.
You are given the task of reading in n numbers and then printing them out in sorted order. Suppose you have access to a balanced dictionary data structure, which supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in O(log n) time.
Collisions are the set of keys mapped to the same bucket. If the keys are uniformly distributed, then each bucket should contain very few keys! The resulting short lists are easily searched!
0 1 2 3 4 5 6 7 8 9 10 11
It is the job of the hash function to map keys to integers. A good hash function:
The first step is usually to map the key to a big integer, for example
h =
keylength∑ i=
128 i^ × char(key[i])
The first three digits of the Social Security Number
0 1 2 3 4 5 6 7 8 9
The last three digits of the Social Security Number
0 1 2 3 4 5 6 7 8 9
Input: A text string t and a pattern string p. Problem: Does t contain the pattern p as a substring, and if so where? E.g: Is Skiena in the Bible?
The simplest algorithm to search for the presence of pattern string p in text t overlays the pattern string at every position in the text, and checks whether every pattern character matches the corresponding text character. This runs in O(nm) time, where n = |t| and m = |p|.
This reduces string matching to n − m + 2 hash value computations (the n − m + 1 windows of t, plus one hash of p), plus what should be a very small number of O(m) time verification steps. The catch is that it takes O(m) time to compute a hash func- tion on an m-character string, and O(n) such computations seems to leave us with an O(mn) algorithm again.
Look closely at our string hash function, applied to the m characters starting from the jth position of string S:
H(S, j) =
m∑− 1 i=
αm−(i+1)^ × char(si+j )
A little algebra reveals that
H(S, j + 1) = (H(S, j) − αm−^1 char(sj))α + sj+m
Thus once we know the hash value from the j position, we can find the hash value from the (j + 1)st position for the cost of two multiplications, one addition, and one subtraction. This can be done in constant time.