ps2 - 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 September 19, 2001 6.046J/18.410J SMA5503 Handout 8 Problem Set 2 MIT students: This problem set is due in lecture on Monday, September 24. SMA students: This problem set is due after the video-conferencing session on Wednesday, September 26. Reading: Chapters 6, 7, 5.1-5.3. 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 instructor and time, 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. SMA students: Each problem should be done on a separate sheet (or sheets) of two-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 convoluted and obtuse descriptions. Exercise 2-1. Do Exercise 5.3-1 on page 104 of CLRS. Exercise 2-2. Do Exercise 6.1-2 on page 129 of CLRS. 2 Exercise 2-3. Do Exercise 6.4-3 on page 136 of CLRS. Exercise 2-4. Do Exercise 7.2-2 on page 153 of CLRS. Exercise 2-5. Do Problem 7-3 on page 161 of CLRS. Problem 2-1. Average-case performance of quicksort Handout 8: Problem Set 2 , but we have not We have shown that the expected time of randomized quicksort is yet analyzed the average-case performance of ordinary quicksort. We shall prove that, under the assumption that all input permutations are equally likely, not only is the running time of ordinary , but it performs essentially the same comparisons and exchanges between input quicksort elements as randomized quicksort. Let be a set of distinct elements which are provided in random order (all orders equally likely) as the input array to PARTITION , where is the size of the array. Let denote the initial value of . (a) Argue that is a random permutation of tions of the input subarray are equally likely. , that is, that all permuta- (b) Consider two input arrays and consisting of the elements of such that for all . Suppose that we run PARTITION on and and trace the two executions to record the branches taken, indices calculated, and exchanges performed — but not the actual array values manipulated. Argue briefly that the two execution traces are identical. Argue further that PARTITION performs the same permutation on both inputs. W #!  “ Xs  X˜ $"  • #!  ‘ !& &C A &  Y "•  •Tfe(ad6 —Q#P6$˜%d™˜—Q#P6–¥5t “ ¢t Y ‘ ¢ #! “ $"•' X” $)  ’ #!  ‘ ˆi'‚ 4A 1 C Y¢ &ˆD‰‚ y „† XQ‚5t 1Y &1‡ ˆi'‚ 'b ƒ„ … C qC A& y&C b w Wu 045€TEh xvBt Define as follows: if if if 1 #C X!s frAB  qphibgW 1 #$" fH'  !C A  # U  C eAd4bc!aY`¤ #!   X"  4 W 6 %#V64 SU  R# # T9  RS#QP6 CDAI6 76 2 1HFGE9  # ! DB@9 CA 2 2 876 # 2 5 4 31 !& & 0)('%¢ PARTITION 1 2 3 for to 4 do if 5 then 6 exchange 7 exchange 8 return #!   $"   Consider the implementation of PARTITION given in lecture on a subarray ¤ ¦ ¢ ©¨§¥¤£¡ ¤ ¦ ¢ ©¨§¥¤£¡ : Handout 8: Problem Set 2 3 Define a sequence We say that a permutation for all . (c) How many Let be an input pattern, and let output pattern. Define to be the set of permutations of that satisfy to be the set of permutations of that satisfy . Problem 2-2. Analysis of -ary heaps A -ary heap is like a binary heap, but (with one possible exception) nonleaf nodes have children instead of 2 children. (a) How would you represent a -ary heap in an array? (c) Give an efficient implementation of E XTRACT-M AX in a -ary max-heap. Analyze its running time in terms of and . (d) Give an efficient implementation of I NSERT in a -ary max-heap. Analyze its running time in terms of and . # Y U  ˆ1 q 1h b p™rW #! C A $) f`c 4 ‡ n¤ ‡ n¤ †w U… ‰W … w”W ƒ§g W †w U… ”W g W … ‰W w l k & ƒ “ ƒ‘ k Y o 5&n¥¤¢ m ƒ„• • & Ik€& Ii §ƒ§g  o& Un¥¤¢ mlk& &“k&‘ 0$–   p$E(ki Y g & Uon¥¤¢ W o U€&n%¤¢ W o U&7%¤¢  o& Un¥¤¢ ¤& & u&C Y )’   vTy76 q Yq pk‚¥•Q‚¢5t mT$€&– • pX€‘E€Yhg l k  & “ k& ki W 0•–   & “ ’{Q‚i ml‚& ‚&‘ o Hf6 C4A Yq &a76 y „† ~$k oY &‡ oHf6 b ƒ„ … C o 5&n¤¥¢ mlk& “k&‘ i Y Ep••  & $’EQk™}g C b o YwqC b Y qku 6 |f{z0'yptf$xhw ¤& & v& u Y )–   5)df76 mlk&  “k&‘ i Y Ep• • & X€’E(kjhg to be an output pattern if if if if of satisfies a pattern if input patterns are there? How many output patterns are there? (d) How many permutations of satisfy a particular input pattern? How many permutations of satisfy a particular output pattern? (e) Argue that PARTITION implements a bijection from to . (Hint: Use the fact from group theory that composing a fixed permutation with each of the possible permutations yields the set of all permutations.) (f) Suppose that before the call to PARTITION , the input subarray is a random permutation of , where . Argue that after PARTITION , the two resulting subarrays are random permutations of their respective elements. (g) Use induction to show that, under the assumption that all input permutations are equally likely, at each recursive call of Q UICKSORT , every element of belonging to is equally likely to be the pivot . (h) Use the analysis of R ANDOMIZED -Q UICKSORT to conclude that the average-case running time of Q UICKSORT on elements is . ‰ ¤ ‰ ‰ ¤ (b) What is the height of a -ary heap of elements in terms of and ? qC A&C bh r q y Y ‘ 04Ttsnpk fpTk ‰ W # Y 5 4rˆ1 !& & 0)('%¢ ¤ ¦ ¢ ©¨§¥¤£¡  o& Un¥¤¢ Define a sequence , and to be an . input pattern if , for be an , and likewise define ¤ ¤ ‰ ‰ ‰ ¤ ‰ #!   $"   ‰ ‰ 4 Handout 8: Problem Set 2 (e) Give an efficient implementation of I NCREASE -K EY , which first sets and then updates the -ary max-heap structure appropriately. Analyze its running time in terms of and . (f) When might it be better to use a -ary heap instead of a binary heap? 2 Q#P6  o&6& 5")'%¢ ‰ ¤ ‰ ‰ o  Œ‹ 5(&%#V64%¢ XŠ ...
View Full Document

This note was uploaded on 01/01/2011 for the course CS 5503 taught by Professor Charlese.leiserson during the Spring '01 term at MIT.

Ask a homework question - tutors are online