11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Sec. 9.6 Exercises 3379.9 Write an algorithm to implement the move-to-front self-organizing list heuristic,assuming th<strong>at</strong> the list is implemented using an array. In particular, writea function MoveToFront th<strong>at</strong> takes as input a value to be searched for <strong>and</strong>which adjusts the list appropri<strong>at</strong>ely. If the value is not already in the list, addit to the beginning of the list.9.10 Write an algorithm to implement the transpose self-organizing list heuristic,assuming th<strong>at</strong> the list is implemented using an array. In particular, writea function Transpose th<strong>at</strong> takes as input a value to be searched for <strong>and</strong>which adjusts the list appropri<strong>at</strong>ely. If the value is not already in the list, addit to the end of the list.9.11 Write functions for computing union, intersection, <strong>and</strong> set difference on arbitrarilylong bit vectors used to represent set membership as described inSection 9.3. Assume th<strong>at</strong> for each oper<strong>at</strong>ion both vectors are of equal length.9.12 Compute the probabilities for the following situ<strong>at</strong>ions. These probabilitiescan be computed analytically, or you may write a computer program to gener<strong>at</strong>ethe probabilities by simul<strong>at</strong>ion.(a) Out of a group of 23 students, wh<strong>at</strong> is the probability th<strong>at</strong> 2 studentsshare the same birthday?(b) Out of a group of 100 students, wh<strong>at</strong> is the probability th<strong>at</strong> 3 studentsshare the same birthday?(c) How many students must be in the class for the probability to be <strong>at</strong> least50% th<strong>at</strong> there are 2 who share a birthday in the same month?9.13 Assume th<strong>at</strong> you are hashing key K to a hash table of n slots (indexed from0 to 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 th<strong>at</strong> you have a seven-slot closed hash table (the slots are numbered0 through 6). Show the final hash table th<strong>at</strong> 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 th<strong>at</strong> it will be the next one filled.9.15 Assume th<strong>at</strong> you have a ten-slot closed hash table (the slots are numbered 0through 9). Show the final hash table th<strong>at</strong> would result if you used the hashfunction h(k) = k mod 10 <strong>and</strong> quadr<strong>at</strong>ic 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 th<strong>at</strong> it will be the next one filled.

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

Saved successfully!

Ooh no, something went wrong!