CSC 345 Homework 2, version 1 Fall 2014
Transcription
CSC 345 Homework 2, version 1 Fall 2014
CSC 345 Homework 2, version 1 Fall 2014 This homework is due Thursday, September 25, in class. Please place it in the folder for homework at the start of class. The questions are drawn from material in Chapters 4 and 6 of the text, and from the material given in class, on divide-and-conquer recurrences and heap sort. The homework is worth a total of 100 points. Remember to write your name at the top of each page, and start each problem on a new page. Please have your answers to problems in the correct order, use only one side of a page, do not use scrap paper, and staple your pages together. If you can’t solve a problem, state this, and write only what you know to be correct. Neatness and conciseness count. (1) (Solving recurrences) (15 points) Derive a Θ-bound on the solution to each of the following recurrences. Do not worry about taking floors or ceilings of arguments. (Hint: Use the theorem presented in class that gives the order of growth of the solution to divide-and-conquer recurrences.) (a) T (n) = 3 T (n/2) + n (b) T (n) = 2 T (2n/3) + n2 √ (c) T (n) = 8 T (n/4) + n n (2) (Analyzing recursion) (10 points) Derive a Θ-bound on the running time of the following recursive procedure F(p, q) as a function of n = q − p + 1. function F(p, q) begin n := q − p + 1 if n ≤ 3 then return n else begin c := 0 √ for i := 1 to b nc do c := c + 1 c := c + F(p+1, p + bn/4c) c := c + F(q − dn/4e, q−1) return c end end (Hint: Derive a recurrence equation for the running time T (n), and solve it.) (3) (Height of heaps) (20 points) (a) (10 points) For a heap of height h, derive expressions for its minimum and maximum number of nodes. P (Hint: The formula for a geometric series, 0≤i≤k ai = (ak+1 − 1)/(a − 1), may be helpful.) (b) (10 points) Prove that a heap of n nodes has height exactly blg nc. (Hint: Use your result from Part (a).) (4) (bonus) (Navigating a heap) (10 points) Prove that functions LeftChild, RightChild, and Parent for heap nodes are correct: namely, that given the index of a heap node, they compute the correct indices for the children and parent of the node. (Hint: First prove that function LeftChild is correct. The correctness of RightChild and Parent will follow from this.) (5) (Iterative Heapify) (10 points) Give transformed pseudocode for the procedure Heapify presented in class that performs the same computation but does not use recursion. (Hint: Use the transformation of tail-recursive procedures to equivalent iterative procedures.) (6) (Merging sorted lists) (30 points) Suppose you have k lists of numbers. Each list by itself is already sorted, and the total length of all the lists is n. Design an algorithm that merges the k sorted lists into one sorted list that contains all the numbers, and runs in O(n log k) total time. (Note: First present the idea behind your algorithm using prose and pictures, then give very high-level pseudocode for your algorithm, and finally perform an analysis of its running time.) (Hint: Use a heap.) (7) (Sorting almost-sorted lists) (15 points) Suppose you want to sort an array A of n numbers that is almost-sorted in the following sense: array A can be obtained from its sorted output by (1) choosing k pairs of locations, where 0 ≤ k ≤ bn/2c, and (2) swapping the numbers at each of these pairs of locations. Derive a tight O-bound on how much time Insertion Sort takes on an almost-sorted input array. This running time will be a function of n and k. When k = Θ(1), how does this time compare to running Merge Sort on the almost-sorted input? Note that Problem (4) is a bonus question, and is not required. (Bonus points are tallied separately, and are considered at the end of the semester for students who fall below the cutoff for a letter grade.)