ps1sol - Introduction to Algorithms Massachusetts Institute...

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 10 6.046J/18.410J SMA5503 Handout 11 Problem Set 1 Solutions Exercise 1-1. Do Exercise 2.3-5 on page 37 in CLRS. Solution: Procedure B INARY-S EARCH takes a sorted array , a value , and a range low high of the array, in which the value should be searched for. The procedure compares to the midpoint of the range and decides to eliminate half the range from further consideration. Both iterative and recursive length . versions are given. These versions should be initially called with the range Both procedures terminate the search unsuccessfully when the range is empty (i.e., low high) and terminate successfully if the value has been found. Based on the comparison of to the middle element in the searched range, the search continues with the range halved. The recurrence , whose solution is . for these procedures is therefore 8 PI C % 8 BRQAHGBA7 ' C ! 8 7 % 8 4FEDB""BA)@97 mid high low mid 460 5 # ! 4"3 % & ' ' ( 1 2 3 4 5 6 7 8 low high R ECURSIVE -B INARY-S EARCH if low high then return NIL low high mid if mid then return mid mid if then return R ECURSIVE -B INARY-S EARCH else return R ECURSIVE -B INARY-S EARCH 2 # ! $" 10 ) ' ( % & 1 2 3 4 5 6 7 8 I TERATIVE -B INARY-S EARCH while low high low high do mid if mid then return mid mid if then low mid else high mid return NIL low high 2 Exercise 1-2. Do Exercise 2.3-7 on page 37 in CLRS. Solution: The following algorithm solves the problem: S Handout 11: Problem Set 1 Solutions 1.Sort the elements in 3.If S S using mergesort. be the value of the removed element. S S T S 2.Remove the last element from . Let T 0 V % W()2U d T U is nonempty, look for in using binary search. . U T % `Y)XV a $T S f d gXeT U T 4.If contains such an element , then STOP, since we have found and such that Otherwise, repeat Step 2. V S q S 8 5.If is empty, then no two elements in a bT sum to . c Notice that when we consider an element of during th iteration, we don't need to look at the elements that have already been considered in previous iterations. Suppose there exists , such that . If , i.e. if has been reached prior to , then we would have found when we were searching for during th iteration and the algorithm would have terminated then. Step 1 takes time. Step 2 takes time. Step 3 requires at most time. Steps 24 are repeated at most times. Thus, the total running time of this algorithm is . We can do a more precise analysis if we notice that Step 3 actually requires time at th iteration. However, if we evaluate , we get , which is . So the total running time is still . Exercise 1-3. Do Exercise 3.1-1 on page 50 in CLRS. Solution: By the definition of -notation (CLRS p. 42) we must show that there exist positive constants , and such that for , yxw8 % % uin ! v4u% 8 d s 8 g 8 BAm)iBAt@Afd @89md"!rs@9BgBAmd 8 g 8 8 d % 8 BAmqp @9Bg @9md 8 8 g 8 d 8 3BAh@BAmo4n5i @9Bg @9me4s5i3BAh@Afe 5 8 d l k j 8 g 8 d 8 ' 6`8 C c @8PQIwAHC 8 8 P QI 8 PI 8 BRQwAHC c 0 8 PI 3EAQAHC 0 8 P 4`AQI 4Fy c 0 8 P a uAxI b 8 8 PI 8 @xw9EC 8 PI 8 @xw9HC a $T dTv0uV c r tsq d T a T % pi$ghV , Without loss of generality, let max( , . Thus, selecting since definition. s 8 i@9g . Clearly, and and . Also, satisfies the Exercise 1-4. Do Exercise 4.1-6 on page 67 in CLRS. Solution: 8 "Qg}| P ~I % { ! % $gz8 Let or, equivalently, . The recurrence becomes 5@ m{ e$G { o&7 ! ! 7 ! % Handout 11: Problem Set 1 Solutions We will need one more substitution: Let . The recurrence then becomes: 3 Exercise 1-5. Rank the following functions by order of growth; that is, find an arrangement of the functions satisfying , , ..., . Partiand are in the same class if and only if tion your list into equivalsts a constant such that for all but finitely many1 values of . We should thus expect the definition of notation in multivariate functions to allow for only finitely many points (tuples) to be outside the stated range. @8 | g 9p 8 @8 | Amd y 8 g 8 @9BF@5x@Afd 3B8Ag9sG@9md y % 8 @8 9 | y Remarkably, famous computer scientists have used Definition 2 without being aware of its deficiencies. Nevertheless, their theorems and analyses carry over to Definition 1, because the functions they analyzed satisfy two key properties. The first property is "monotonicity": 1 or equivalently, "for infinitely many values of " sm (m sf (a) and (b) or (c) Unrestricted 6 Definition 3 A two-variable function if and for all nonnegative | | Amd Handout 11: Problem Set 1 Solutions is monotonically increasing and . 8 (b) Explain this definition in plain English. Solution: is monotonically increasing if whenever either (or both) of the A function function's arguments increase, the function's value either increases or remains constant, but never decreases. B8 | Afd The second property is more complicated: Definition 4 A two-variable function is multiplicatively separable if there exist a constant and two one-variable functions, and , such that whenever , , and , we have | X9w s 5i@8 @8 19emXA5i@8 0 | g | | 9Bg @8 | 9Bg s 8 s| s r | 9g 8 @A and Intuitively, increasing one argument of a multiplicatively separable function increases the value of by at most a multiplicative factor which can be bounded in terms of that argument itself, independent of the other argument. i. ii. iii. v. vi. g @8 | Ag 0 R46u8 | g 8 Am@A iB8 | Ag (c) For each of the following functions 8 5RB8 | % 8 | % h5RB8 | Ag , argue that is multiplicatively separable. Solution: 8 BA | X9 We need to show that there exist functions ! % 8 GR@A ! % | GRXA and with the desired properties. i. . . g 45E8 @8 )Am)xB8 | d | d Am)xB8 @8 | 9md | Amd D ! ! % gRB8 @ $gRB8 { ! % g ! % { gRB8 | Ag | Ag | Ag | Ag Handout 11: Problem Set 1 Solutions ii. . . However, this is true only if we assume that ! % 8 GR@A ! % | GRXA ! % 8 GR@A % | )RXA 7 iii. v. vi. . . . . . . (d) Prove that the following two functions are not multiplicatively separable: $gRB8 { ! % | Ag Solution: i. Proof by contradiction: Suppose the function was multiplicatively separable. Then we would have: ! s | GxX9w ! | o { $fXA5 { ! And so would not be bounded in terms of the argument itself, independent of the other argument Indeed, in this case, would not necessarily hold under the "or" interpretation if it holds under the "and" interpretation. For example, consider the function ! s )8 3B8 A9s@8 9md | g y % | u}| % ! % uz8 % R{ ! @8 R4! ! s )| { for for for Again would not be bounded in terms of the argument itself, independent of the other argument2 . Similarly, if is true under the "and" interpretation, it is not necessarily true under the "or" interpretation. For example, consider the function " ` 2 notice that grows without bounds as grows | us | . Then holds whenever but is not necessarily true under the "or" definition. ii. Similarly, 0 1| | s | xX9w 0 | | 19eXA) | 8 s w)8 ! % 8 % }ppu| Let 3B8 | g y % AuB8 3@8 | g y % A9sg@8 | Afd | 9md | 9md 5RB8 | % | XA | XA | Ag i. ii. s 8 and , s | ! % 8 GR@A vn ! % | GRXA bw ! % 8 GR@A ! % | GRXA and nB8 | g A l$k(j 1Ru9 $( uA 4sj 0 | l k j | l k | g | 0 | | Af45A 46RuA4fnuA B8 @8 ! 0 | g 0 | | vuAf46RuA4fnuA @8 6uAfnuA 0 | g | 8 s w18 | r u6r| @8 uAg | B8 | g A) 8 8 l k A4sj nu| s | l k j AB$(Ag n8 8 8 l k A$(j nu| | l k AB$(j Y5B8 g 8 l k A4sj nu| n8 8 l k A4sj nu| |Al4ksj gi5 n8 8A4sj nu| A4sj d l k | l k 3B8 A9sG@8 Amd | g y % | | l k AB$(j )i@8 Amd d | | l k A4sj ) 8 nu| d d 8 8 l k AB$(j u| | l k j g AB$(A1x@8 s p 8 s 18 | s u1r| g w8 u| B8 @8 8 Suppose that is monotonically increasing and is multiplicatively separable. Suppose further that , that is, there exist positive constants , , and such that for all and . |A5B8 Af) g | d | g y % 9B9G@8 9md | for for }| % ! s )| ! | % RB8 | Afd with . Then holds whenever but is not necessarily true under the "or" definition. | s uu| 3B8 | g y % A9s@8 | Amd (e) Prove that there exists a constant (f) Prove that there exists a constant | Afd | Amd 8 s w)8 ! % 8 % wu| for all nonnegative and . 8 | for all nonnegative d so Putting these two inequalities together, Because Suppose for example that and . (The other cases are similar.) By repeated application of multiplicative separability, By monotonicity of , Solution: suffices. and . holds under the `and' interpretation, such that such that 8 | Solution: % gw Handout 11: Problem Set 1 Solutions and Handout 11: Problem Set 1 Solutions (The Thus, 's are necessary to deal with the possibility that takes on values less than .) $( 46RuA $( u9w $s5 l k j 0 | l k j | l k j % 9 suffices in this case. 3@8 | g y % A9sg@8 | 9md (g) Conclude that Solution: Solution: Awaiting ideas from students... Problem 1-2. Tree Traversal The following pseudocode is a standard recursive tree-traversal algorithm for counting the number of nodes in a tree . The initial call is C OUNT-N ODES . C OUNT-N ODES 1 if NIL 2 then return 3 else return V w9 % uV bbA Solution: C V 4Ht@ w9 7 V h@3w left right Solution: V 3w y % V sgw97 (b) Use the substitution method to prove that size V w V w9 7 % V )w97 V w97 (a) Give a recurrence for in terms of left and right . . V w7 f `V Define size to be the number of nodes in the subtree rooted at node the worst-case running time of C OUNT-N ODES . V w V w9 V V C OUNT-N ODES left C OUNT-N ODES right (h) (Extra credit.) Give a proper generalization of tion. 3@8 Therefore, in particular the `or' definition. | g y % A9sG@8 | 9md holds under the unrestricted interpretation and thus to two variables. Justify your defini- nB8 | g % A5@8 8 | g Ae$1B8 | | Afd % $) Let . By parts (a) and (b), for all l k $sj . and , , and let denote 10 Handout 11: Problem Set 1 Solutions For convenience, let denote , that is, the size of the tree whose root is node . In order to prove that , we need to show that there exists a constant such that . term 3 . Assume that there exists some Proof. Let be an upper bound on the constant such that: A common compiler optimization of this code, called tail recursion, is to replace one of the recursive calls with a loop, resulting in the following pseudocode: V w notice that the 3 term does not depend on the size of the tree V right right a % wV right ' c % c if if V V a c V Let right denote the th right descendant of , that is, V V Y)Y C OUNT-N ODES -TAIL 1 2 while NIL 3 do 4 right 5 return hV C OUNT-N ODES -TAIL left V ǥ w w9 V C V 4FED@3w V Y 0 iA50 V Y ie60 V iY Y wV iY v ih @ww9V V 7 7 left left left right right right V w9 That is, we assume that the statement holds for all trees whose size is less than We want to prove that . left right . Since right From part (a), we have: are smaller than , we have left C V 4Hv w9 7 V &w3w9 V i5xwV&7 7 % V )w&7 % V w&7 V V w9 V % % % T for all trees with , . . and T i5iAT7 V r T 4HC T Uc 9" V s% y V w97 V i5xw9V7 T s % V a T Handout 11: Problem Set 1 Solutions Consider the loop invariant V 11 C OUNT-N ODES -TAIL left right where executed. s is the number of times the while loop (lines 24) in C OUNT-N ODES -TAIL has been Solution: Let be the new value of executions we have % after one more execution of the loop. Since after (d) Prove that C OUNT-N ODES -TAIL returns Solution: Since is the smallest positive integer for which right NIL , the algorithm will exit the loop after exactly iterations, and return the current value of . So if Equation (1) holds, then indeed C OUNT-N ODES -TAIL will return % V V R C OUNT-N ODES -TAIL left right a ! % BV V w9h a ! V w9h V wh V wh Let be the smallest positive integer for which right 5 So it holds for V C OUNT-N ODES -TAIL left right a NIL . V Yp V C OUNT-N ODES -TAIL left right C OUNT-N ODES -TAIL left right QV C OUNT-N ODES -TAIL left right a a ) at the end of the -st execution we will have V C OUNT-N ODES -TAIL left right a ) (c) Prove that if Equation (1) holds for , then it holds for . a a a (1) % a Y5i Y) % % % 12 Handout 11: Problem Set 1 Solutions However, we need to prove that Equation (1) holds. Proof. Base Case: Consider a tree of size 1. The loop will execute only once and at the end of the loop , as predicted by Equation(1). So the base case holds. Inductive step: The inductive step was taken care of in part (c). (e) Prove by induction that C OUNT-N ODES -TAIL Solution: Base case: tree of size . As shown above, the algorithm returns , which is the right answer. Inductive step: assume that the algorithm returns the right result for all trees up to . We want to prove that it will return the right result for any tree of as well. Consider a tree . As shown in part (d), the algorithm returns V R % y C OUNT-N ODES -TAIL left right 0 V Uc 6w" Since all left subtrees have size at most , we know that QV a C OUNT-N ODES -TAIL left right correctly counts the number of leaves of all the left subtrees. Since the tree consists of right-most nodes plus all their left subtrees, the algorithm returns the right result for trees of as well. Problem 1-3. Polynomial multiplication V Uc w9"e V wh to form the polynomial | hpnuvfuAg V | V | | 42 " $2 4 " $ " V # % %u| % | % u| | V " If we have two linear polynomials multiplications and , we can multiply them using the four coefficient V w9 correctly computes size V R C OUNT-N ODES -TAIL left right a a V w V a ! a ! V @wh V w9h V . V Uc w" 0 V Uc gw" Handout 11: Problem Set 1 Solutions (a) Give a divide-and-conquer algorithm for multiplying two polynomials of degree-bound based on this formula. Solution: 8 8 13 The four polynomial products puted recursively. , , , and (b) Give and solve a recurrence for the worst-case running time of your algorithm. Solution: 8 @9HC Since we can perform the dividing and combining of polynomials in time cursive polynomial multiplication gives us a running time of 9HC 8 8 C ! 8 7 BAHtBv"@9&b % 8 @A7 Solution: We can use the following 3 multiplications: so the polynomial product is V H"o | V | 0 | uhHFuWYuW0 " V 2z " % vGi " 3 V z" (c) Show how to multiply two linear polynomials coefficient multiplications. and V V Rw w9 wV91w " V 1" 0 ! 1"B8 w9b w V V V " V w1w9 w9b ww V V V V V V " V w90w9og Fw9b w V V V 3wvw9 " ) Vw9Vbo 3w9V FVw9VA V 3w9 | Ag " 'o V | % u) % V w9 A " V )Ho " " % %u| % u| V w9b where , , product is then V w9 , and are polynomials of degree . The polynomial are com- , re- using only three ! v@8 We can use this idea to recursively multiply polynomials of degree , where a power of 2, as follows: and be polynomials of degree , and divide each into the upper Let and lower terms: 0 5z8 V w 0 hp8 V V Fw % V w is V w9 " ' V V w9b % V w9 & % | % V V w w9 % V w & ! "@8 (& % V w V w9 % 14 Handout 11: Problem Set 1 Solutions Solution: The algorithm is the same as in part (a), except for the fact that we need only compute three products of polynomials of degree to get the polynomial product. (e) Give and solve a recurrence for the worst-case running time of your algorithm. Solution: Similar to part (b): ! "B8 Alternative solution Instead of breaking a polynomial into two smaller polynomials and such that , as we did above, we could do the following: Collect all the even powers of and substitute to create the polynomial . Then collect all the odd powers of , factor out and substitute to create the second polynomial . Then we can see that V % `T V w9 V Both and are polynomials of (roughly) half the original size and degree, and we can proceed with our multiplications in a way analogous to what was done above. Notice that, at each level , we need to compute (where ), which takes time per level and does not affect the asymptotic running time. V % pbT T % T T A 4HC T A 8 (d) Give a divide-and-conquer algorithm for multiplying two polynomials of degree-bound based on your formula from part (c). % V % rvT V w AHC 8 8 C ! 8 BAHt""@97 T A Dww9 V V % V V T % V WBAGw9 676 5 V w9 8 AHC % % 8 BA7 % 4 V w T A % % % V w9 V ww T A ...
View Full Document

This note was uploaded on 07/09/2009 for the course CSE 6.046J/18. taught by Professor Piotrindykandcharlese.leiserson during the Fall '04 term at MIT.

Ask a homework question - tutors are online