ps1 - Introduction to Algorithms Massachusetts Institute of...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT 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 three-hole punched paper. You will often be called upon to "give an algorithm" to solve a certain problem. Your write-up 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 1-1. Do Exercise 2.3-5 on page 37 in CLRS. Exercise 1-2. Do Exercise 2.3-7 on page 37 in CLRS. 2 Exercise 1-3. Do Exercise 3.1-1 on page 50 in CLRS. Exercise 1-4. Do Exercise 4.1-6 on page 67 in CLRS. Handout 7: Problem Set 1 Exercise 1-5. 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 1-1. Asymptotic notation for multivariate functions The generalization of asymptotic notation from one variable to multiple variables is surprisingly tricky. One proper generalization of O-notation 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 two-variable 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 two-variable function g(m, n) is multiplicatively separable if there exist a constant L 0 and two one-variable 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 1-2. Tree Traversal The following pseudocode is a standard recursive tree-traversal algorithm for counting the number of nodes in a tree R. The initial call is C OUNT-N ODES(root[R]). C OUNT-N ODES(x) 1 if x = NIL 2 then return 0 3 else return 1 + C OUNT-N ODES(left[x]) + C OUNT-N 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 worst-case running time of C OUNT-N 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 OUNT-N ODES -TAIL(x) 1 s0 2 while x = NIL 3 do s s + 1 + C OUNT-N 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+ k-1 i=0 i x if i = 0 , right[right i-1 [x]] if i > 0 . C OUNT-N 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 OUNT-N 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 OUNT-N ODES -TAIL returns K(x)-1 NIL. K(x) + i=0 C OUNT-N ODES -TAIL(left[right i [x]]) . (e) Prove by induction that C OUNT-N ODES -TAIL(x) correctly computes size(x). Problem 1-3. 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 divide-and-conquer algorithm for multiplying two polynomials of degree-bound n based on this formula. (b) Give and solve a recurrence for the worst-case 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 divide-and-conquer algorithm for multiplying two polynomials of degree-bound n based on your formula from part (c). (e) Give and solve a recurrence for the worst-case 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.

Ask a homework question - tutors are online