# hw2 - (b Can you do better Explain your solution 4 It is...

This preview shows page 1. Sign up to view the full content.

Homework 2 Due: Tuesday, Feb. 23 1. Prove that the sum of heights of all nodes in a binary heap with n nodes is at most n - 1. Show a binary heap whose sum of heights is exactly n - 1. 2. Design an algorithm to compute the union of two input sets given as arrays, both of size O ( n ). The output should be an array of distinct elements that form the union of the sets. No ele- ment should appear more than once. The worst-case running time of your algorithm should be O ( n log n ). 3. Suppose we are given an n by n matrix M of integers, where each row is sorted in increasing order from left to right and each column is sorted in increasing order from top to bottom, and given an integer x . We want to determine if x is present in M . (a) It is straightforward to do this in O ( n log n ) time. Describe such an algorithm.
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (b) Can you do better? Explain your solution. 4. It is straightforward to use 2 n-3 comparisons to ±nd both the minimal and the maximal elements in a given set of n elements (how? you should think about this, but do not need to hand in the answer). Now design a divide-and-conquer (recursive) algorithm to accomplish the same. Your algorithm should use at most 3 n/ 2 comparisons. (Suppose n is a power of 2 .) 5. Given a “black-box” worst-case linear-time algorithm for ±nding the median, design an algorithm solving the selection problem for an arbitrary order statistic. (In other words, explain how to use the “black-box” to ±nd the k-th smallest element.) State and prove the time-complexity of your algorithm. 1...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online