This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology SingaporeMIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Day 10 6.046J/18.410J SMA5503 Handout 12 Problem Set 2 Solutions
MIT students: This problem set is due in lecture on Day 8. 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. 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. 2 Exercise 21. Do Exercise 5.31 on page 104 of CLRS. Solution: R ANDOMIZE I N P LACE Handout 12: Problem Set 2 Solutions Exchange for to do Exchange For our base case, we have initialized to 2. Therefore we must show that for each possible 1permutation, the subarray A[1] contains this 1permutation with probability . Clearly this is the case, as each element has a chance of of being in the first position. Exercise 22. Do Exercise 6.12 on page 129 of CLRS. Solution: Now we use induction, and assume that all trees with nodes or fewer has a height of . Next we consider a tree with nodes. Looking at the node, we know its height is one greater than its parent (and since we're not in the base case, all nodes have a parent). The parent of the th node in the tree is also the th node in the tree. Therefore its height is . Then the th node in the tree has a height of . Therefore by induction we have shown that the height of an node tree is . Exercise 23. Do Exercise 6.43 on page 136 of CLRS. Solution: The running time of H EAPSORT on an array of length that is already sorted in increasing order is because even though it is already sorted, it will be transformed back into a heap and sorted. The running time of H EAPSORT on an array of length that is sorted in decreasing order will be . This occurs because even though the heap will be built in linear time, every time the element is removed and the H EAPIFY is called it will cover the full height of the tree. Exercise 24. Do Exercise 7.22 on page 153 of CLRS. Solution: q xihgf q pivgf S5@P f f xixiT F pihgf S5@P f xiT ! f 0Q@C f pigyT ! b q q F X g hgf b qqFX g b qqFX f qqFX 5@P qFX 5wC f q pivgf ! R ut e By definition, a case is correct. element heap has a height of . Therefore where X! bV VB T R 5Qdca`YXWU! QE S6
is e sr ! b q pihgf X `U! %A9 E 74Q1PI0H'G % E G 3 ) $ F E DCA@87520(' &%#"! B 9 !6 4 3 1 ) $ E !
and our base ) @t4 B C pig 6 B P pig 6 Handout 12: Problem Set 2 Solutions 3 The running time will be because every time partition is called, all of the elements will be put into the subarray of elements smaller than the partition. The recurrence will be which is clearly Exercise 25. Do Problem 73 on page 161 of CLRS. Solution: (a) This sort is intuitively correct because the largest rd of the elements will eventually be sorted amung their peers. If they are in the first third of the array to begin with, they will be sorted into the middle third. If they are in the middle or last third, then they will obviously be sorted into their proper position. Similarly any element which belongs in the each of the thirds will be sorted into position by the three subsorts. Which solves to (c) S TOOGE S ORT is slower than all of the other algorithms we have studied. I NSERTION , M ERGE S ORT , H EAPSORT , and Q UICKSORT . Therefore all other sorts are faster and these professors do not deserve tenure for this work! Problem 21. Averagecase performance of quicksort , 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 quicksort , but it performs essentially the same comparisons and exchanges between input elements as randomized quicksort. Consider the implementation of PARTITION given in lecture on a subarray PARTITION 1 2 3 for to 4 do if 5 then 6 exchange 7 exchange 8 return : 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 . B 6 b pnn Qiom B P xig v6 l 2kb B! BX b B Q86 T I`C`F6 Cv6 (b) b B C6 ! T G{z m Rp b X wU! B C pig 6 b xm pnn 5iom G y E E G %xm $ t $% E T ! E E t yvw p ! T u t m m s E cm G Bp9 9 wACrmq6 B 6 B P xig v6 b j i2gWUd hWD6 h f Be B W2v6 B C xig v6 l B B! R C6 T Qu6 4 Handout 12: Problem Set 2 Solutions
(a) Argue that is a random permutation of tions of the input subarray are equally likely. Solution: Given that is random (all orders are equally likely), there are possible permutations of the elements. Each element has a probability of being chosen as the pivot, therefore the number of permutations of the remaining elements is . Consequently the permutations are equally likely. Define as follows: if if if (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. Solution: PARTITION takes different branches based only on the comparisons made in the function. This is clear by observing line 4 of the function, as it is the only place where the sequence of instructions may differ. As the arrays have identical function values, they must take the same branches, calculate the same indicies and perform the same exchanges. Consequently PARTITION will perform the same partition on both arrays, which follows directly as the exchanges performed are identical. Define a sequence , and Define a sequence to be an . input pattern if to be an output pattern if if if if We say that a permutation for all . of satisfies a pattern b UgvB#6 @Q#9 Ib 9nnn 9 ~ 9! R} b w! T @oaU e @ , if y V a B6 X `U! VB! R WQ6 ~ } CUR y p9nnn9 i##@! T pnn Qi#qm nq ! T 9S e b 9 q qR ! 9nnn9 F9! AW@db E y w#9 W` 9nnn 9 n y E ! T b 9 zb E e 2Q 9 y E oR ! B 9 cav6 9nnn 9 IU##9 WIsb !uR b wqdPU E } ~! R b 9nnn9 9 F AcAb E 9nnn 9 IU##9 WIrb B 9 xav6 5n! T m p n pnn Qi! T m 56 bB VB! R 6 b X!V Qv75Q ! T m R p b pnn Qiom , that is, that all permuta pnn 5om Qiom #o pnn rm9Pmzb E B E 6 B E ov6 b pnn QAom Wo ~ 9! R y w! T 9 e @oI} for Handout 12: Problem Set 2 Solutions
(c) How many Solution: input patterns are there? How many 5 input patterns because we can choose positions out of There are possible positions to have . There is one ( ) output pattern because the pattern must be exactly negative ones followed by a 0, followed by ones. input pattern? How many (d) How many permutations of satisfy a particular permutations of satisfy a particular output pattern? Solution: permutations are possible of to satisfy a particular input pattern. This is the total number of ways to rearrange the elements which have a value of amongst themselves, and rearrange those with a value of 1 amongst themselves. There are also permutations possible to satisfy a particular output pattern for the same reason. 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 . be an , and likewise define (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.) Solution: All members of satisfy and so they all have the same result when the function is applied to its elements. Therefore by part (b) when all these inputs are given to PARTITION they are subject to the same permutation. Using the hint, we then know that after all of the distinct inputs are run through PARTITION that they will produce distinct outputs. From part we know that and are the all same size, and also we have proven that PARTITION is onto, and therefore PARTITION must be a bijection! (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. Solution: Using our solution from part (e), we know that after PARTITION is run on , we get all values in the set . Therefore we get all permutations of the ones and all permutations of the negative ones. Furthermore, we get each subarray permutations an equal number of times and so the subarrays are also random permutations. B 9 cav6 ! oR R y y ##9 9 b 0 9nnn x y y t R V a pnn QA! T m G ! R y x Sy 9 B 9 x6 B 9 xa6 y B 16 y 0 xm b B 9 xav6 ! R ! R qb V a y y B 9 xav6 VB! R VB R WQzrx6Wxy6 ! xR y ~ } CUsR y B 9 x6 ` y y Sy 9nnn 9 wQ9 Ir b VB! R VB R WQo6xv6 VB! R VB R WQg6xv6 B 9 xav6 output patterns are there? ! R x y 6 Handout 12: Problem Set 2 Solutions
(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 . Solution: The base case for the initial array; we know that it is randomly permuted, and so by part and each of its subarrays will also be randomly permuted after PARTITION . Therefore we can inductively apply at each partition to prove that every subarray will also be randomly permuted. (h) Use the analysis of R ANDOMIZED Q UICKSORT to conclude that the averagecase running time of Q UICKSORT on elements is . Solution: By part (g) we know that under the assumption that the input pattern is random every element is equally likely to be chosen as the pivot at each recursive call which then produces the same random distribution of quicksort traces as R ANDOMIZED Q UICKSORT . Therefore as their distribution is the same, the expectedcase analysis for R ANDOMIZED Q UICKSORT will apply to the average case of Q UICKSORT . Therefore the average case of Q UICKSORT time. also takes 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? Solution: The ary heap would be similar to a binary heap with the parent and child indexes calculated as follows: thChild[ ] where The root of the tree would be at index . Alternate Solution: A ary heap can be represented in a dimensional array as follows. The root is kept in , its children are kept in order in through , their children are kept in order in through , and so on. The two procedures that map a node with index to its parent and to its th child (for ), respectively, are; 1 y t #! T 1 T I(1 5HF ! b cm G Bp9 9 wACrmq6 B C xig v6 l E QF T (1 ! kb E ! R 1nn uoi#n e b t 1 B Pr6 #"! 1 1 t7E qzb E T 1 t q1 I`X hEf b% E wI) p 1 1 B i C @pg 6 l pnn Qiom B )6 1v t o! v #! T H1G B P6 1 Handout 12: Problem Set 2 Solutions
D  ARYPARENT 7 return D  ARYC HILD return To convince yourself that these procedures really work, verify that
D  ARYPARENT D  ARYC HILD for any . Notice that the binary heap procedures are a special case of the above procedures when . (b) What is the height of a ary heap of Solution: elements in terms of and ? which solves to . (c) Give an efficient implementation of E XTRACTM AX in a ary maxheap. Analyze its running time in terms of and . Solution: H EAPIFY 1 2 for 3 4 5 if 6 7 8 to if and then then Exchange H EAPIFY The running time of H EAPIFY is because at each depth we are doing loops, and we recurse to the depth of the tree. In H EAPIFY we compare the the th node and each of its children to find the maximum value for all of the nodes. Then if 1 E ! T Q! v B I1 nn I1 T n T 1 DQyaQ! B! R B T Q!uo1{6 @@pg B R 6 i v ! T U!uRq1{ B 6 ! uRq1 !R W 1 v 1 T 1 T ! v B i C @@xg 6 i BC 5@xg 16 l BAaA9 t q6 19 9 t G 2 E G $ E t b T%E 01zb t T t Gw %E 0H1G uv %E 1 T !uo1 e Gt R E B 19 9 AaA9 E v6 1 T Quq1v6 @xg 6 B! R 6 i b 1 ! R uo1 ! yR 1 T T T T nn I1 n 1 1 ! CORRECTION A ary heap would have a height of . We know that 1 9 E iB t 9 E 6 bB 6 F b G1 ! TSt7T QyR E %1 B! 6 B t9E6 X ! @1wBQuR E 6 BE6 1 1 v t ! v 1 8 Handout 12: Problem Set 2 Solutions
the maximum child is greater than the th node, we switch the two nodes and recurse on the child. E XTRACTM AX (A, n) 1 2 3 4 H EAPIFY (A, 1, n, d) 5 return The running time of this algorithm, is clearly constant work plus the time of H EAPIFY . E XTRACTM AX works by storing the value which as shown above is of the maximum element, moving the minimum element into the root of the heap, and then calling heapify to restore the heap property. (d) Give an efficient implementation of I NSERT in a ary maxheap. Analyze its running time in terms of and . Solution: See next problem part for I NCREASE K EY definition. I NSERT 1 2 3 I NCREASE K EY From the following problem part, we know I NCREASE K EY runs in time, therefore since I NSERT only adds constant time operations it is also . It is rather trivially correct as the algorithm has not changed because all calculations involving the number of children are performed by I NCREASE K EY . , which first sets (e) Give an efficient implementation of I NCREASE K EY and then updates the ary maxheap structure appropriately. Analyze its running time in terms of and . Solution: P E B C @i pg@6i xg l l B P 6 B 9 c9 E q6 1 E B P Uv3G16 l 1 1 B 9 9 PA%9 E v6 R b %% ! T &ġ B 19 9 9 aAv6 1 ) @t4 ! R b ytġ (G %#"! ) #!G It4 B 6 cr9 E GP5 Handout 12: Problem Set 2 Solutions
I NCREASE K EY 1 2 if 3 while and 4 do 5 Exchange 6 9 Our implementation loops proportionally to at most the depth of the tree, therefore it runs in time. I NCREASE K EY loops, at each step comparing the increased node to its parent and exchanging them if the heap property is violated. Therefore, once the algorithm terminates we know that the node once again satisfies the heap property and has the correct value. (f) When might it be better to use a ary heap instead of a binary heap? Solution: It would be better to use a ary heap when it is predicted that the heap will do many more I NSERT s and I NCREASE K EY s than E XTRACTM AX s because I NSERT and I NCREASE K EY are faster algorithms as increases while E XTRACTM AX gets slower. 1 E $ 8G 2 E G ! E E Gyw U D g E Gz b B 6 x9 E vP5&% E B 9 x9 E v6 1 1 B C Qv36 l ...
View Full
Document
 Fall '04
 PiotrIndykandCharlesE.Leiserson
 Algorithms

Click to edit the document details