Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology SingaporeMIT 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 videoconferencing session on Wednesday, September 26. Reading: Chapters 6, 7, 5.15.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 threehole punched paper. SMA students: Each problem should be done on a separate sheet (or sheets) of twohole 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 convoluted and obtuse descriptions. Exercise 21. Do Exercise 5.31 on page 104 of CLRS. Exercise 22. Do Exercise 6.12 on page 129 of CLRS. 2 Exercise 23. Do Exercise 6.43 on page 136 of CLRS. Exercise 24. Do Exercise 7.22 on page 153 of CLRS. Exercise 25. Do Problem 73 on page 161 of CLRS. Problem 21. Averagecase 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 averagecase 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 brieﬂy 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$%dQ#P6¥5t ¢t Y ¢ #! $"' X $) #! i' 4A 1 C Y¢ &D y XQ5t 1Y &1 i' 'b
C qC A& y&C b w Wu 045TEh xvBt Deﬁne 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 Deﬁne a sequence We say that a permutation for all . (c) How many Let be an input pattern, and let output pattern. Deﬁne to be the set of permutations of that satisfy to be the set of permutations of that satisfy . Problem 22. 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 efﬁcient implementation of E XTRACTM AX in a ary maxheap. Analyze its running time in terms of and . (d) Give an efﬁcient implementation of I NSERT in a ary maxheap. Analyze its running time in terms of and . # Y U 1 q 1h b prW #! C A $) f`c 4 n¤ n¤ w U
W
wW §g W w U
W g W
W w l k & k Y o 5&n¥¤¢ m & Ik& Ii §§g o& Un¥¤¢ mlk& &k& 0$ p$E(ki Y g & Uon¥¤¢ W o U&n%¤¢ W o U&7%¤¢ o& Un¥¤¢ ¤& & u&C Y ) vTy76 q Yq pk¥Q¢5t mT$& pXEYhg l k & k& ki W 0 & {Qi 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 & XE(kjhg
to be an output pattern if if if if of satisﬁes 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 ﬁxed 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 averagecase 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 04Ttsnpk fpTk W # Y 5 4r1 !& & 0)('%¢ ¤ ¦ ¢ ©¨§¥¤£¡ o& Un¥¤¢ Deﬁne a sequence , and to be an . input pattern if , for be an , and likewise deﬁne ¤ ¤ ¤ #! $" 4 Handout 8: Problem Set 2
(e) Give an efﬁcient implementation of I NCREASE K EY , which ﬁrst sets and then updates the ary maxheap 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.
 Spring '01
 CharlesE.Leiserson
 Algorithms

Click to edit the document details