Q1 a)Suppose that a 500*500 matrix that has 2000 nonzero terms is to be represented. How much space is needed when a 500*500 two-dimensional array of type int is used? How much space is needed when sparseMatrix is used? • Two-dimensional array – How many bytes are needed for each int? – How many elements? – So the solution is? 4 500*500 500*500*4 = 1,000,000 • Sparse matrix – How many bytes are needed for each element in sparse matrix? 12 (x, y, value) – How many elements? 2000 – So the solution is? 2000*12 = 60,000 b) How many nonzero elements must an m*n matrix have before the space required by sparseMatrix exceeds that required by an m*n two-dimensional array? You may assume that T is int. • Two-dimensional array – How many bytes are needed for each int? 4 – How many elements? m*n – How many in total? 4*m*n • Sparse matrix – How many bytes are needed for each element in sparse matrix? 12 (x, y, value) – How many nonzero elements? – So the solution is? 12p> 4mn p p > mn/3 Q2 • A three-dimensional m*n*p array has mnp elements. – a)Determine the amount of memory used when these mnp elements are stored using a three-dimensional C++ array and when they are stored in a one-dimensional array using row-major mapping. Assume that the elements are of type int. First do this exercise for the case m = 10, n=4, and p=2 and then for general m, n, p. C++ uses the array of arrays representation. When m = 10, n = 4, and p = 2, we have 1 one-dimensional array of size 10. Each element of this array references a two-dimensional 4 x 2 array. From Exercise 9, we know that each 4 x 2 array takes 48 bytes. So, the total space is 10 x 4 + 10 * 48 = 520 bytes. When a single one-dimensional array is used (i.e., when the elements are mapped using (say) row-major order), the space required is 10 x 4 x 2 x 4 = 320 bytes. The ratio is 520 / 320 = 1.63. • Row-major order |1 2 3| is 1 2 3 4 5 6 |4 5 6 | • For general m, n, and p, the array of arrays representation requires m x 4 + m (space for an n x p array) = 4m + m(4np + 4n) = 4mnp + 4mn + 4m bytes. The one-dimensional mapping takes m x n x p x 4 = 4mnp bytes b)How large can the ratio of the two memory requirements get? • (4mnp + 4mn + 4m) / (4mnp) = 1 + 1/p + 1/(np). This is maximum when p and n are least. For a three-dimensional array, p >= 2. and n >= 2 (note that when any of the dimensions is 1, the array is really of smaller dimension than 3). So, the maximum value of the ratio is 1 + 1/2 + 1/4 = 1.75. c)When is one scheme expected to provide faster element access than the other? The advantage of three-dimensional array is when the size of mnp is large, it may not easy to allocate a contiguous memory, but it needs more memory accesses to get an element. One dimensional array is faster for element access. But sometimes it may not easy to find the memory. Q3 Q14, page 399, chapter 10 (solution attached below) You have a hash table with b buckets, the hash function is f(k) = k %b (D=b). Which of the following b values satisfies the recommendation made for the hash function divisor is Example 10.10? That is, which of the following values gives a good hash function When using the hash function like above, some choices of D result in a good hash function and other choices result in a bad hash function. The ideal choice for D is a prime number, when you cannot find a prime number close to the table length you have in mind, you should choose D so that it is not divisible by any number between 2 and 19. (a) b = 93; The divisors (other than 1) of 93 are 3 and 31. Since 93 has a divisor between 2 and 19, it does not satisfy the recommendation of Example 10.10. (b) b=37 37 is a prime number. So, it satisfies the recommendation of Example 10.10. (c) b = 1024 1024 is divisible by 2. So, it does not satisfy the recommendation of Example 10.10. (d) b = 529 23 * 23 = 529. Since 529 has no divisor between 2 and 19, it satisfies the recommendation of Example 10.10. Q4 f(k) = k %13 Key table [] 0 1 2 3 4 5 6 7 8 9 10 23 10 11 12 Linear Probing: The easiest way to find a place to put an 23 is to search the table for the next available bucket and put it in. This method of handling overflows is called linear probing. Q17, page 399, chapter 10(solution on book's website) Use linear probing, a hash table with b = 13 buckets, and the hash function f(k) = k%b. Start with an empty hash table and insert pairs whose keys are 7, 42, 25, 70, 14, 38, 8, 21, 34, 11 (a) Draw the hash table following each insert. (b) What is the loading factor of your table after last insert. The loading factor is 10/13 = 0.77. (c) What’s the maximum and average number of buckets examined in a unsuccessful search of your table. The number of buckets examined during an unsuccessful search that starts at home bucket i is [3, 2, 1, 2, 1, 2, 1, 9, 8, 7, 6, 5, 4]. The average is 51/13 = 3.9. (d) What’s a maximum and average number of buckets examined in a successful search. When searching for each of [7, 42, 25, 70, 14, 38, 8, 21, 34, 11] the number of buckets examined is [1, 1, 1, 1, 1, 2, 1, 2, 3, 1]. The average is 14/10 = 1.4. (e)Compute U_n and S_n using your loading factor and the formulas for linear probing. How do these numbers compare with the numbers you computed in part (c) (d), explain any discrepancy. When the loading density is 0.77, the formulas yield 9.95 and 2.67 as the expected value for the average number of buckets examined in an unsuccessful and a successful search, respectively. These numbers are higher than in our example. The discrepancy is because the formulas do not tell you what will happen in an individual case but what happens on average as the number of elements in the table becomes very large. Q5 Determine a suitable value for the hash function divisor D when linear probing is used. Do this for each of the following situation. For this exercise we need to use the linear open addressing performance equations: Sn = 1/2[1 + 1/(1 - alpha)] Un = 1/2[1 + 1/(1 - alpha)2] where alpha is the loading density n/b. When the hash funstion is division, the divisor D equals the number of buckets b. a) (a) n = 50, S_n <= 3, U_n <= 20 Since Sn = 1/2[1 + 1/(1 - alpha)] <= 3, 1 - alpha >= 0.2. Also, since Un = 1/2[1 + 1/(1 - alpha)2] <= 20, 1 - alpha >= sqrt(1/39). So 1 - alpha >= max{0.2, sqrt(1/39)} = 0.2. Hence n/b = alpha <= 0.8 and so D = b >= n/0.8 = 50/0.8 = 62.5. Since D should be a prime or should have no prime divisors less than 20, we choose D = 67, which is a prime number. b) (b) n=500, S_n <=5 U_n <= 60 Since Sn = 1/2[1 + 1/(1 - alpha)] <= 5, 1 - alpha >= 1/9. Also, since Un = 1/2[1 + 1/(1 - alpha)2] <= 60, 1 - alpha >= sqrt(1/119). So 1 - alpha >= max{1/9, sqrt(1/119)} = 1/9. Hence n/b = alpha <= 8/9 and so D = b >= 9n/8 = 9*500/8 = 562.5. Since D should be a prime or should have no prime divisors less than 20, we choose D = 563, which is a prime number. c) (c) n= 10, S_n <=2, U_n <=10 Since Sn = 1/2[1 + 1/(1 - alpha)] <= 2, 1 - alpha >= 1/3. Also, since Un = 1/2[1 + 1/(1 - alpha)2] <= 10, 1 - alpha >= sqrt(1/19). So 1 - alpha >= max{1/3, sqrt(1/19)} = 1/3. Hence n/b = alpha <= 2/3 and so D = b >= 3n/2 = 3*10/2 = 15. Since D should be a prime or should have no prime divisors less than 20, we choose D = 17, which is a prime number. Q6. Delete for Linear Probing f(k) = k%13 Key table [] 25 26 15 0 1 2 28 4 3 4 36 5 19 6 7 8 9 23 24 12 10 11 12 What the table will look like after deleting element 24? After the pair with key 24 is removed: Key table [] 25 26 15 0 1 2 28 4 3 4 19 5 6 7 8 9 23 36 12 10 11 12 The keys that are checked for moving are 12, 25, 26, 15, 28, 4, 36, 19 i.e., in a circular way from right to left starting from the key next to the removed key and continue till we encounter an empty bucket. Now only 36 is moved to the location of the 24 and not others because if they were moved then they would come before the position of their home bucket in which case the search for them would fail.