Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology SingaporeMIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Day 2 6.046J/18.410J SMA5503 Handout 7 Problem Set 1
MIT students: This problem set is due in lecture on Day 6. Reading: Chapters 14, excluding 4.4; 28.2; 30.1. Both exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise solutions, you are responsible for material covered by the exercises. Mark the top of each sheet with your name, the course number, the problem number, your recitation section, the date, and the names of any students with whom you collaborated. MIT students: Each problem should be done on a separate sheet (or sheets) of threehole punched paper. You will often be called upon to "give an algorithm" to solve a certain problem. Your writeup should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convo luted and obtuse descriptions. Exercise 11. Do Exercise 2.35 on page 37 in CLRS. Exercise 12. Do Exercise 2.37 on page 37 in CLRS. 2 Exercise 13. Do Exercise 3.11 on page 50 in CLRS. Exercise 14. Do Exercise 4.16 on page 67 in CLRS. Handout 7: Problem Set 1 Exercise 15. Rank the following functions by order of growth; that is, find an arrangement g1 , g2 , . . . , g30 of the functions satisfying g1 = (g2 ), g2 = (g3 ), . . . , g29 = (g30 ). Parti tion your list into equivalence classes such that f (n) and g(n) are in the same class if and only if f (n) = (g(n)). lg(lg n) ( 2)lg n 3n 1 3lg n n3 (lg n)lg n n2 lg2 n 2n n! en ln n
k=1 n 1 lg (nn ) n1/ lg n ln ln n
k=2 n lg(n!) n2+sin n lg (lg n) n 2n nlg lg n n lg n k 1  1 k Problem 11. Asymptotic notation for multivariate functions The generalization of asymptotic notation from one variable to multiple variables is surprisingly tricky. One proper generalization of Onotation for two variables is the following: Definition 1 O(g(m, n)) = {f (m, n) : there exist positive constants m0 , n0 , and c such that 0 f (m, n) cg(m, n) for all m m0 or n n0 } . Consider the following alternative definition: Definition 2 O (g(m, n)) = {f (m, n) : there exist positive constants m0 , n0 , and c such that 0 f (m, n) cg(m, n) for all m m0 and n n0 } . (a) Explain why Definition 2 is a "bogus" definition. That is, what anomalies does the definition of O permit that are counterintuitive? You may find it helpful to illustrate your answer with a diagram of relevant regions of the m n plane. Remarkably, famous computer scientists have used Definition 2 without being aware of its deficien cies. Nevertheless, their theorems and analyses carry over to Definition 1, because the functions they analyzed satisfy two key properties. The first property is "monotonicity": Handout 7: Problem Set 1 Definition 3 A twovariable function f (m, n) is monotonically increasing if f (m, n) f (m + 1, n) and f (m, n) f (m, n + 1) for all nonnegative m and n. (b) Explain this definition in plain English. The second property is more complicated: Definition 4 A twovariable function g(m, n) is multiplicatively separable if there exist a constant L 0 and two onevariable functions, a(m) and b(n), such that whenever m 0, n 0, and g(m, n) L, we have g(m, n) a(m) g(m  1, n) and g(m, n) b(n) g(m, n  1) . 3 Intuitively, increasing one argument of a multiplicatively separable function g increases the value of g by at most a multiplicative factor which can be bounded in terms of that argument itself, independent of the other argument. (c) For each of the following functions g(m, n), argue that g is multiplicatively separable. i. ii. iii. v. vi. g(m, n) = m + n g(m, n) = m2 n g(m, n) = 2m + 2n g(m, n) = 2m+n m n g(m, n) = 22 +2 (d) Prove that the following two functions are not multiplicatively separable: i. g(m, n) = 2m n ii. g(m, n) = mn Suppose that f is monotonically increasing and g is multiplicatively separable. Suppose further that f (m, n) = O (g(m, n)), that is, there exist positive constants m0 , n0 , and c such that 0 f (m, n) cg(m, n) for all m m0 and n n0 . (e) Prove that there exists a constant r 0 such that f (m, n) r g (max(m, m0 ), max(n, n0 )) for all nonnegative m and n. 4 (f) Prove that there exists a constant s 0 such that g (max(m, m0 ), max(n, n0 )) s g(m, n) for all nonnegative m and n. (g) Conclude that f (m, n) = O(g(m, n)). Handout 7: Problem Set 1 (h) (Extra credit.) Give a proper generalization of to two variables. Justify your defini tion. Problem 12. Tree Traversal The following pseudocode is a standard recursive treetraversal algorithm for counting the number of nodes in a tree R. The initial call is C OUNTN ODES(root[R]). C OUNTN ODES(x) 1 if x = NIL 2 then return 0 3 else return 1 + C OUNTN ODES(left[x]) + C OUNTN ODES(right[x]) Define size(x) to be the number of nodes in the subtree rooted at node x R, and let T (x) denote the worstcase running time of C OUNTN ODES(x). (a) Give a recurrence for T (x) in terms of left(x) and right(x). (b) Use the substitution method to prove that T (x) = O(size(x)). A common compiler optimization of this code, called tail recursion, is to replace one of the recur sive calls with a loop, resulting in the following pseudocode: C OUNTN ODES TAIL(x) 1 s0 2 while x = NIL 3 do s s + 1 + C OUNTN ODES TAIL(left[x]) 4 x right[x] 5 return s Let right i [x] denote the ith right descendant of x, that is, right [x] = Consider the loop invariant s=k+
k1 i=0 i x if i = 0 , right[right i1 [x]] if i > 0 . C OUNTN ODES TAIL(left[right i [x]]), (1) Handout 7: Problem Set 1 5 where k 0 is the number of times the while loop (lines 24) in C OUNTN ODES TAIL has been executed. (c) Prove that if Equation (1) holds for k, then it holds for k + 1. Let K(x) be the smallest positive integer for which right K(x) [x] = (d) Prove that C OUNTN ODES TAIL returns
K(x)1 NIL. K(x) + i=0 C OUNTN ODES TAIL(left[right i [x]]) . (e) Prove by induction that C OUNTN ODES TAIL(x) correctly computes size(x). Problem 13. Polynomial multiplication If we have two linear polynomials ax+b and cx+d, we can multiply them using the four coefficient multiplications m1 m2 m3 m4 to form the polynomial m1 x2 + (m2 + m3 )x + m4 . (a) Give a divideandconquer algorithm for multiplying two polynomials of degreebound n based on this formula. (b) Give and solve a recurrence for the worstcase running time of your algorithm. (c) Show how to multiply two linear polynomials ax + b and cx + d using only three coefficient multiplications. (d) Give a divideandconquer algorithm for multiplying two polynomials of degreebound n based on your formula from part (c). (e) Give and solve a recurrence for the worstcase running time of your algorithm. = = = = ac, ad, bc, bd ...
View
Full
Document
This note was uploaded on 07/08/2009 for the course CS 5503 taught by Professor Charlese.leiserson during the Fall '01 term at MIT.
 Fall '01
 CharlesE.Leiserson
 Algorithms

Click to edit the document details