Midtermsol - FUNDAMENTAL ALGORITHMS MIDTERM SOLUTIONS You must omit at least 10 points Mark on your booklet which part or parts of problems you are

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

View Full Document Right Arrow Icon
FUNDAMENTAL ALGORITHMS MIDTERM SOLUTIONS You must omit at least 10 points. Mark on your booklet which part or parts of problems you are omitting. Maximum score: 140. What is slight can still be great, if it is written in a natural, Fowing and easy style - and at the same time bears the mark of sound composition. – letter to Wolfgang Mozart from his father, 1778 1. (35) These questions concern a max-heap A with auxilliary structures length[A] , Left[i] , Right[i] and Parent[i] . (a) (5) Give the max-heap property. (That is, what is the relationship between the values A [ i ] that one needs for a heap.) Solution: A[Parent[i]] A[i] (b) (5) Suppose length[A] =100 and the values A [ i ] are distinct. What are the possible i for which A [ i ] is the second largest value? (You must give all possible i .) Solution: The biggest is the root and the second biggest is one of its children, so i = 2 , 3 are the possibilities. (c) (5) Suppose length[A] =100 and the values A [ i ] are distinct. What are the possible i for which A [ i ] is the smallest value? (You must give all possible i .) Solution: It must be a leaf so 51 i 100. (d) (10) Describe the algorithm MAX-HEAP-INSERT(A,key) which adds to heap A a new entry with value key . (Either a description in words or a pseudocode program would be ±ne.) Solution: ²irst increment heapsize and place key in the last po- sition. Set x = heapsize. Now while x n = 1 and A [ x ] is less than A [ parent [ x ]], interchange those two and reset x parent [ x ]. (e) (10) How long (in worst case) does the above algorithm take as a function of n = length[A] ? Give a reason for your answer. Solution: You climb up the tree to the top so it takes Θ(lg n ) steps. 2. (35) These questions concern QUICKSORT . We will take PARTITION(A,p,r) as given . (This procedure rearranges A [ p ··· r ], placing all entries less
Background image of page 1

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

View Full DocumentRight Arrow Icon
than A [ r ] to the left and all entries greater than A [ r ] to the right. You
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Page1 / 5

Midtermsol - FUNDAMENTAL ALGORITHMS MIDTERM SOLUTIONS You must omit at least 10 points Mark on your booklet which part or parts of problems you are

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