Solu2

Topics: Recurrence relation, Sorting algorithm, Divide and conquer algorithm Pages: 51 (11506 words) Published: December 3, 2014
This ﬁle contains the exercises, hints, and solutions for Chapter 2 of the book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by A. Levitin. The problems that might be challenging for at least some students are marked by ; those that might be diﬃcult for a majority of students are marked by .

Exercises 2.1
1. For each of the following algorithms, indicate (i) a natural size metric for its inputs; (ii) its basic operation; (iii) whether the basic operation count can be diﬀerent for inputs of the same size:

a. computing the sum of n numbers
b. computing n!
c. ﬁnding the largest element in a list of n numbers
d. Euclid’s algorithm
e. sieve of Eratosthenes
f. pen-and-pencil algorithm for multiplying two n-digit decimal integers 2. a. Consider the deﬁnition-based algorithm for adding two n-by-n matrices. What is its basic operation? How many times is it performed as a function of the matrix order n? As a function of the total number of elements in the input matrices?

b. Answer the same questions for the deﬁnition-based algorithm for matrix multiplication.
3. Consider a variation of sequential search that scans a list to return the number of occurrences of a given search key in the list. Will its eﬃciency diﬀer from the eﬃciency of classic sequential search?

4. a. Glove selection There are 22 gloves in a drawer: 5 pairs of red gloves, 4 pairs of yellow, and 2 pairs of green. You select the gloves in the dark and can check them only after a selection has been made. What is the smallest number of gloves you need to select to have at least one matching pair in the best case? in the worst case? (after [Mos01], #18) b. Missing socks Imagine that after washing 5 distinct pairs of socks, you discover that two socks are missing. Of course, you would like to have the largest number of complete pairs remaining. Thus, you are left with 4 complete pairs in the best-case scenario and with 3 complete pairs in the worst case. Assuming that the probability of disappearance for each 1

of the 10 socks is the same, ﬁnd the probability of the best-case scenario; the probability of the worst-case scenario; the number of pairs you should expect in the average case. (after [Mos01], #48)

5. a. Prove formula (2.1) for the number of bits in the binary representation of a positive integer.
b. What would be the analogous formula for the number of decimal digits? c. Explain why, within the accepted analysis framework, it does not matter whether we use binary or decimal digits in measuring n’s size. 6. Suggest how any sorting algorithm can be augmented in a way to make the best-case count of its key comparisons equal to just n − 1 (n is a list’s size, of course). Do you think it would be a worthwhile addition to any sorting algorithm?

7. Gaussian elimination, the classic algorithm for solving systems of n linear equations in n unknowns, requires about 13 n3 multiplications, which is the algorithm’s basic operation.
a. How much longer should you expect Gaussian elimination to work on a system of 1000 equations versus a system of 500 equations? b. You are considering buying a computer that is 1000 times faster than the one you currently have. By what factor will the faster computer increase the sizes of systems solvable in the same amount of time as on the old computer?

8. For each of the following functions, indicate how much the function’s value will change if its argument is increased fourfold.

a. log2 n
b. n
c. n
d. n2
e. n3
f. 2n
9. Indicate whether the ﬁrst function of each of the following pairs has a smaller, same, or larger order of growth (to within a constant multiple) than the second function.
a. n(n + 1) and 2000n2

b. 100n2 and 0.01n3

c. log2 n and ln n

d. log22 n and log2 n2

e. 2n−1 and 2n

f. (n − 1)! and n!

10. Invention of chess According to a well-known legend, the game of chess was invented many centuries ago in northwestern India by a sage named Shashi. When he took...