{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# midtermsol - FUNDAMENTAL ALGORITHMS MIDTERM SOLUTIONS You...

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

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, flowing 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 fine.) Solution: First increment heapsize and place key in the last po- sition. Set x = heapsize. Now while x negationslash = 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

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

View Full Document
than A [ r ] to the left and all entries greater than A [ r ] to the right. You are not being asked to write it!!)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}