100% found this document useful (2 votes)
1K views3 pages

Hash Solution

The document discusses several hash table exercises involving inserting values into hash tables with different collision strategies, constructing a hash function to insert unique 3-character IDs into a hash table of size 24, choosing an appropriate new table size when an existing table becomes over 3/4 full, and using a hash table with linear chaining to efficiently find customers that spent within a given range each month to send targeted promotions.

Uploaded by

lekhaperumal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views3 pages

Hash Solution

The document discusses several hash table exercises involving inserting values into hash tables with different collision strategies, constructing a hash function to insert unique 3-character IDs into a hash table of size 24, choosing an appropriate new table size when an existing table becomes over 3/4 full, and using a hash table with linear chaining to efficiently find customers that spent within a given range each month to send targeted promotions.

Uploaded by

lekhaperumal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Hash Table Exercises - Solutions problem 1: Given the values {2341, 4234, 2839, 430, 22, 397, 3920},

a hash table of size 7, and hash function h(x) = x mod 7, show the resulting tables after inserting the values in the given order with each of these collision strategies. {2341, 4234, 2839, 430, 22, 397, 3920} h(x) = x mod 7 2341 % 7 = 3 4234 % 7 = 6 2839 % 7 = 4 430 % 7 = 3 22 % 7 = 1 397 % 7 = 5 3920 % 7 = 0 1. separate chaining 0 [3920] 1 [22] 2 [ ] 3 [2341, 430] 4 [2839] 5 [397] 6 [4234] 2. linear probing 0 [397] 1 [22] 2 [3920] 3 [2341] 4 [2839] 5 [430] 6 [4234] 3. quadratic probing 0 [430] 1 [22] 2 [3920] 3 [2341] 4 [2839] 5 [397] 6 [4234] 430 collides at 3: next - +1 = 4 next - +4 = 7 % 7 = 0 3920 collides at 0: next - +1 = 1 next - +4 = 4 next - +9 = 9 % 7 = 2 4. double hashing with second hash function h'(x) = (2x - 1) mod 7 0 [3920] 1 [430] 2 [22] 3 [2341] 4 [2839] 5 [397] 6 [4234] (2*430-1) % 7 = 5 430 = 3 = 3+1*5 = 8 % 7 = 1 (2*22-1) % 7 = 1 22 = 1 = 1 + 1*1 = 2

problem 2: Suppose you need to insert unique 3-character IDs into a hash table, where each ID is made up of some combination of two of the capital letters A-D, followed by one of the lower case letters x-z, such as: ABx, DCy, BBz, etc. Repeat letters are allowed in an ID. 1. There are 48 possible codes: 4 * 4 * 3 2. The table only needs to be size 24 in order to ensure that no bucket exceeds two codes at a given time. There are many possible ways to write the hashCode function for these IDs. A correct hash function will have each ID colliding with at most one other ID. Constructing a correct hash function (Interpret str[0] and str[1] as the result of a mapping from A->0, B->1, C->2, D->3 ; str->[2] is interpreted as x->0, y->1, z->2.) row: str[0] column: str[1] using integer arithmetic This table: str[0] + (str[1] / 2)*2 A B C D A 0 2 4 6 B 0 2 4 6 C 1 3 5 7 D 1 3 5 7 This table produces exactly one collision for each two-letter pair. Now we need a unique mapping combining the above function from 0-7 and the resulting mapping of str[2]: row: str[0] + (str[1] / 2)*2 col: str[2] This table: str[0] + (str[1] / 2) * 2 + str[2] * 8 0 1 2 3 4 5 6 0 0 1 2 3 4 5 6 1 8 9 10 11 12 13 14 2 16 17 18 19 20 21 22

7 7 15 23

We now have a unique mapping for 48 IDs into 24 slots.The function to produce this works as follows: public int hashCode(String ID) { int hashcode = 0; Integer A = new Integer('A'); Integer x = new Integer('x'); hashcode += ID.charAt(0) - A.intValue(); hashcode += 2 * ((ID.charAt(1) - A.intValue) / 2); hashcode += 8 * (ID.charAt(2) - x.intValue()); return hashcode; } Any function which produces at most one collision for any given ID is also acceptable.

problem 3: Suppose a hash table with capacity M=31 gets to be over 3/4ths full. We decide to rehash. What is a good size choice for the new table to reduce the load factor below .5 and also avoid collisions? 3/4ths full: more than ceil(3/4 * 31) = 24 elements Must be a prime to avoid collisions Two possible answers to reduce the load factor below .5: 1) M = next prime higher than 31*2 = 62 M = 67 2) M = next prime higher than 24*2 = 48 M = 53 problem 4: Suppose you are running a food service business which has special promotions for frequent customers. Each month you send out discount offers to different groups based on their spending in your establishment. For example, one month those who spent $25-50 might get a free drink coupon, or another month those who spent $50-100 might get a 2-for-1 lunch offer. The spending ranges will change each month, and you want to be able to find the right group of people each month in O(K + log N) time, where K is the size of the group and N is the total number of customers. What data structure(s) would you choose and how would you implement a solution to this problem? (sketch briefly) Assumptions: The algorithm runtime does not include construction of the customer database; the customer database is created *before* knowing the interval of spending ranges. Create a hash table using linear chaining to resolve collisions. The key for the hash table will be the amount that the customer spent last month at the food service business. The value will be some representation for the customer. The hash code function will have to preserve the order of the keys. The O(K+logN) algorithm to find the correct group of customers for an interval $X-$Y: Do a binary search over the keys in the hash table (this is why order must be preserved in the keys of the hash table). Continue until the smallest key >= $X is found. Start iterating linearly through the hash table in order by key. Examine each key and add the corresponding customers (or list of customers, in the case where two customers spend the same amount.) When a key larger than $Y is found, stop iterating through and return the list of customers encountered. Other solutions which also produce the result in O(K+logN) are acceptable.

problem 5 Many answers are possible. A good justification will include an explanation of why the data structure is particularly efficient to add items each semester, and find out information about groups of courses according to the department that they're in.

You might also like