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
Unformatted text preview: CS300 Algorithms 2008 Fall semester finalterm solution 1 1.1 The number of leaf nodes is d n/ 2 e . Thus, the number of internal nodes is n d n/ 2 e = b n/ 2 c . FixHeap(Root) constructs a heap assuming that both the left and right trees of Root are already heaps. Since leaf nodes are already heaps by themselves, you do not need to worry about them. Therefore, you need to start to fix the heap from the first internal node from the last index, which is b n/ 2 c th node. 1.2 Every internal node performs FixHeap operation to construct a heap. Because it’s already sorted in the decreasing order, there is no key movement. Thus, you only need to compare with you direct children. When a node has two children, you need two comparisons, and similarly when a node has one child, you need just one comparison. If n is even, the number of internal nodes is n/ 2 and the last internal node has one child node, so the number of comparison is 2 × ( n/ 2 1) + 1 = n 1. If n is odd, the number of internal nodes is ( n 1) / 2 and all internal nodes have two children nodes, so the number of comparisons is 2 × ( n 1) / 2 = n 1. 1.3 In the case of decreasing order, you do not need move a key during FixHeap operations, which minimizes the number of comparisons as n 1. 1 2 2.1 To find the largest key, you need to perform n 1 comparisons. And to find the second largest key, note that any key that loses to a key other than the largest key cannot be the second largest. Thus, you keep track of all the keys that lose directly to the largest key and then find the largest of those. To do that, we pair the keys and compare the pairs in rounds until only one round remains. Since the root of this tournament tree is the largest key and the height is at most d log n e (+1, depending on the definition of height), the largest key has been involved in d log n e matches. This means that to find the second largest key, we need to find the largest key among d log n e keys which takes d log n e 1. In total, ( n 1) + ( d log n e  1) = n + d log n e  2 comparisons are needed. 2.2 Our adversary introduces the weight w ( x i ) for each key x i . All w ( x i ) are initialized to 1. Our adversary uses w ( x i ) and w ( x j ) to give the result of the comparison of a pair ( x i , x j ). • If w ( x i ) > w ( x j ), then the adversary changes the values such that x i > x j , w ( x i ) := w ( x i ) + w ( x j ) and w ( x j ) := 0 • If w ( x i ) = w ( x j ) > 0, the adversary changes the values such that x i > x j , w ( x i ) := w ( x i ) + w ( x j ) and w ( x j ) := 0 • If w ( x i ) < w ( x j ), then the adversary changes the values such that x i < x j , w ( x j ) := w ( x j ) + w ( x i ) and w ( x i ) := 0 • If w ( x i ) = w ( x j ) = 0, then the answer is consistent with previously assigned values.) = 0, then the answer is consistent with previously assigned values....
View
Full
Document
This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.
 Spring '08
 Unkown
 Algorithms

Click to edit the document details