{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ps3sol

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

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

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

View Full Document
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 running time for this algorithm is O ( d log d n ).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online