12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

354 Chap. 9 Searching(c) How many students must be in the class for the probability <strong>to</strong> be at least50% that there are 2 who share a birthday in the same month?9.13 Assume that you are hashing key K <strong>to</strong> a hash table of n slots (indexed from0 <strong>to</strong> n − 1). For each of the following functions h(K), is the function acceptableas a hash function (i.e., would the hash program work correctly forboth insertions <strong>and</strong> searches), <strong>and</strong> if so, is it a good hash function? FunctionR<strong>and</strong>om(n) returns a r<strong>and</strong>om integer between 0 <strong>and</strong> n − 1, inclusive.(a) h(k) = k/n where k <strong>and</strong> n are integers.(b) h(k) = 1.(c) h(k) = (k + R<strong>and</strong>om(n)) mod n.(d) h(k) = k mod n where n is a prime number.9.14 Assume that you have a seven-slot closed hash table (the slots are numbered0 through 6). Show the final hash table that would result if you used thehash function h(k) = k mod 7 <strong>and</strong> linear probing on this list of numbers:3, 12, 9, 2. After inserting the record with key value 2, list for each emptyslot the probability that it will be the next one filled.9.15 Assume that you have a ten-slot closed hash table (the slots are numbered 0through 9). Show the final hash table that would result if you used the hashfunction h(k) = k mod 10 <strong>and</strong> quadratic probing on this list of numbers:3, 12, 9, 2, 79, 46. After inserting the record with key value 46, list for eachempty slot the probability that it will be the next one filled.9.16 Assume that you have a ten-slot closed hash table (the slots are numbered0 through 9). Show the final hash table that would result if you used thehash function h(k) = k mod 10 <strong>and</strong> pseudo-r<strong>and</strong>om probing on this list ofnumbers: 3, 12, 9, 2, 79, 44. The permutation of offsets <strong>to</strong> be used by thepseudo-r<strong>and</strong>om probing will be: 5, 9, 2, 1, 4, 8, 6, 3, 7. After inserting therecord with key value 44, list for each empty slot the probability that it willbe the next one filled.9.17 What is the result of running sfold from Section 9.4.1 on the followingstrings? Assume a hash table size of 101 slots.(a) HELLO WORLD(b) NOW HEAR THIS(c) HEAR THIS NOW9.18 Using closed hashing, with double hashing <strong>to</strong> resolve collisions, insert thefollowing keys in<strong>to</strong> a hash table of thirteen slots (the slots are numbered0 through 12). The hash functions <strong>to</strong> be used are H1 <strong>and</strong> H2, defined below.You should show the hash table after all eight keys have been inserted.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!