CMPUT 204 - Problem Set #4
CMPUT 204 - Problem Set #4
CMPUT 204 - Problem Set #4
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>CMPUT</strong> <strong>204</strong> - <strong>Problem</strong> <strong>Set</strong> <strong>#4</strong>(Preparation for Quizzes on Wednesday (11/15), Monday (11/20), and Tuesday (11/21))Scope. Permutation generation algorithms. Divide and conquer algorithms with applicationsto problems in searching, sorting, computational geometry, graphs, and combinatorialoptimization. Heaps and Heapsort.1. Denote by P[1,n] the set of all n! permutations of the integers 1 to n. The dictionaryordering of P[1,n] is simply obtained by treating each integer as a letter of thealphabet. Thus, the dictionary ordering of the six permutations in the set P[1,3] isgiven by:(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3, 1, 2), (3,2, 1).True or false: In a dictionary ordering of P[1,n] the successor of a permutation(a 1 ,a 2 , · · · ,a n ) is obtained by finding the rightmost adjacent elements a i , and a i+1such that a i < a i+1 , and exchanging their values. That is the successor of the permutation(3, 2,5,4,1) is the permutation (3, 5,2,4,1). If false, explain how to fix.}{{} }{{}2. Consider the following version of Quicksort that uses a stack in the following way:• initially, the borders (the pair of indices 1, and n) of the input array are savedon the stack, and• subsequently, after a call to Partition, the function stacks the borders of thesublist L[SplitPoint+1,last], and immediately works on the other sublistL[first,SplitPoint-1].function QuicksortWithStack(L[1..n]) {Push([1,n])while (stack is not empty) {Pop ([first, last])while (first < last) {SplitPoint= Partition(L[first .. last)Push ([SplitPoint+1,last])last= SplitPoint -1}}}Now, assume that the above function is modified so that after each call to Partitionthe borders of the larger sublist are stacked, and the other sublist is sorted first.The goal of this question is to examine possible space-time tradeoffs for the modifiedalgorithm.(a) Given the input list (1, 6, 23, 15, 9, 25, 30, 2, 41), show the contents of the stackafter each stack operation done by the algorithm.1
(b) Give a big-O expression for the maximum stack size in the best case (among allinputs of n keys). Describe an initial arrangement of the n input keys that givesrise to a best case.(c) Repeat (b) for the worst case.(d) Give big-O expressions for the number of comparisons required to sort the arrangementsdescribed in (b) and (c).(e) In light of the above, does the ranking of inputs according to space usage agreewith the ranking according to the execution time?3. For each of the following problems, devise a divide-and-conquer algorithm, write arecurrence equation for the worst case running time T(n) , and find a Θ expression ofT(n).(a) (Textbook problem 1, page 154) For the one-dimensional version of the closestpairproblem, i.e., for the problem of finding two closest numbers among a givenset of n real numbers, design an algorithm that is directly based on the D&Ctechnique, and determine its efficiency class.(b) Given a set S of n points in the (x,y)-plane, compute the area of the convex-hullof S.4. Suppose a character array to be sorted (into alphabetical order) by Heapsort initiallycontains the following sequence of letters: C O M P L E X I T Y(a) show how they would be arranged in the array after the heap construction phase.Draw a tree representation of the data after each data swap.(b) How many key comparisons are done to construct the heap with these keys?5. A list of distinct keys in decreasing order is to be sorted (into increasing order) byHeapsort.(a) How many comparisons of keys are done in the heap construction phase if thereare 10 keys?(b) How many are done if there are n keys? Show how you derive your answer.(c) Is a list in decreasing order a best case, worst case, or intermediate case for theheap construction? Justify your answer.2