ps3sol - CS161: Design and Analysis of Algorithms Summer...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS161: Design and Analysis of Algorithms Summer 2004 Problem Set #3 Solutions General Notes Regrade Policy: If you believe an error has been made in the grading of your problem set, you may resubmit it for a regrade. If the error consists of more than an error in addition of points, please include with your problem set a detailed explanation of which problems you think you deserve more points on and why. We reserve the right to regrade your entire problem set, so your final grade may either increase or decrease. If we ask for an answer in terms of n and d , please give the answer in terms of n and d and do not assume that d is a constant. Many people did this on the first problem as well as the skip list problem from the last problem set. In part (b) of problem 3, many people only showed that the probability that any slot had k elements was nQ k . You had to show the maximum number of elements was k . 1. [14 points] Analysis of d-ary Heaps Throughout this problem, when asked to give an implementation of a certain operation, please provide pseudocode or a verbal description with the same level of clarity and detail. When asked to analyze running times, your analysis does not need to be formal, but should be justified. Note that we are asking you to solve 6-2(e) before 6-2(d). Keep in mind that you may always (not only in this problem) use the results of any previous parts in answering a later part of a multi-part problem. (a) [1 point] Do problem 6-2(a) on page 143 of CLRS. Answer: A d-ary heap can be represented in a 1-dimensional array by keeping the root of the heap in A [1], its d children in order in A [2] through A [ d + 1], their children in order in A [ d +2] through A [ d 2 + d +1], and so on. The two procedures that map a node with index i to its parent and to its j th child (for 1 j d ) are D-PARENT( i ) 1 return ceilingleft ( i- 1) /d ceilingright D-CHILD( i,j ) 2 return d ( i- 1) + j + 1 (b) [1 point] Do problem 6-2(b) on page 143 of CLRS. Answer: Since each node has d children, the total number of nodes in a tree of height h is bounded above by 1 + d + ... + d h inclusively and below by 1+ d + ... + d h- 1 exclusively. This yields h = ceilingleft log d (1+ n ( d- 1)) ceilingright- 1 = (log d n ). Problem Set #3 Solutions 2 (c) [2 points] Give an efficient implementation of HEAPIFY in a d-ary max-heap. Analyze its running time in terms of d and n . Answer: First, we will need to find if the root element given to D-HEAPIFY is larger than all its children. If not, we swap it with its largest child and recursively call D-HEAPIFY on that child. D-HEAPIFY( A,i,d ) 1 largest i 2 for j 1 to d 3 j D-CHILD( i,j ) 4 if j heapsize [ A ] and A [ j ] > A [ largest ] 5 then largest j 6 if largest negationslash = i 7 then exchange A [ i ] A [ largest ] 8 D-HEAPIFY( A,largest,d ) This algorithm does ( d ) comparisons in each call to D-HEAPIFY as well as O (log d n ) calls to HEAPIFY, one for each level of the tree. Therefore, the total) calls to HEAPIFY, one for each level of the tree....
View Full Document

This document was uploaded on 05/25/2011.

Page1 / 8

ps3sol - CS161: Design and Analysis of Algorithms Summer...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online