{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# final_sol - CS300 Algorithms 2008 Fall semester nal-term...

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

CS300 Algorithms 2008 Fall semester final-term 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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern