9. Medians and Order Statistics
Exercises
9.1-1
Available in the latest revision of the IM.
9.1-2
Available in the latest revision of the IM.
9.1-3
The minimum number of races it takes to determine the fastest three horses out of 25 is 7. Here is the procedure.
Determine the other two horses
Using the result of the previous race we can select candidates for the final race. The forth and fifth placed horse, together with their groups, can be immediately eliminated. They cannot be candidates for the second and third place. Furthermore, the rest of the horses from a group of the third placed horse can be excluded, too. We need to take 2 horses from the winner's group and 1 horse from the group of the second placed horse. Now, we have 5 horses for the 7th race. The first and second horse from this race are the additional two we are looking for.
★ 9.1-4
Let represent the set of numbers which are potentially the minimum and let denote the set of numbers which are potentially the maximum. Initially, all numbers are potentially either the minimum or maximum, so . There are two types of comparisons:
Between two numbers that are members of both sets.
Between two numbers from the same set.
The first type of comparison reduces the size of both and by one. The reason is that a smaller of the two numbers cannot anymore be a member of and the larger of the two numbers cannot anymore remain in . The second type of comparison only reduces the matching set by one without having any impact on the other. So, to attain the lower bound we must leverage the first type of comparison as much as possible.
As with proving the upper bound, we process elements in pairs. We can form pairs, so after the first stage . Observe that these sets can have at most one element in common, which happens when is odd. At any rate, the minimum must be searched among members of and the maximum among those from . We already know the lower bound on the amount of comparisons needed, which is for a set of elements. This is where we can only use the second type of comparison (see above).
The total number of comparisons required is
9.2-1
Available in the latest revision of the IM.
9.2-2
Available in the latest revision of the IM.
9.2-3
In each partitioning the maximum of the current subarray should be selected as a pivot. For example, the first iteration should choose 9, the second 8, and so on. The sequence stops when the last subarray becomes [0].
9.2-4
Available in the latest revision of the IM.
9.3-1
Available in the latest revision of the IM.
9.3-2
Let , thus . The subarray may contain at most 4 elements, that should be added to both sides of a partition. The recurrence becomes
The driving function satisfies the polynomial-growth condition, so according to the Akra-Bazzi theorem, replacing with , where for some constant and sufficiently large , leaves the asymptotic solution unaffected. Therefore, we still have running time.
9.3-3
Available in the latest revision of the IM.
★ 9.3-4
Assume that all elements are distinct, otherwise, augment them with positional information to make them distinct. We apply the decision tree model to prove the statement from the book:
Each internal node represents a comparison between two elements.
Each leaf corresponds to a conclusion reached by the algorithm after a sequence of comparisons.
For the algorithm to correctly identify the th smallest element , every input that leads to a leaf where is output must have as the th smallest element.
Assume the algorithm reaches a leaf where it outputs as the th smallest element. Suppose, for the sake of contradiction, that there exists an element such that the relation between and is not determined, either directly or via transitive inference, by the comparisons made along the path to that leaf. Then, there exist two inputs sharing the same path toward :
In one
In the other .
But this is a contradiction. If , then the th smallest element cannot be given that is also the th smallest element when . Hence, the relation between and every other element must be determined by the comparisons already made. No need for performing any additional comparisons.
9.3-5
You can find the solution here.
9.3-6
Available in the latest revision of the IM.
9.3-7
Here is a simple geometric proof that the median of the -coordinates minimizes the total spur length. Observe that the -coordinates have no significance on the result. Form pairs of wells based on their coordinates in the following manner:
Pair the absolute highest well with the absolute lowest well. To minimize their spur lengths, the pipeline must be between them.
Pair the second highest with the second lowest. The pipeline must be between them, too.
Continue this inward. By doing this, you create a set of nested intervals. To minimize the total distance, the pipeline must be placed in the intersection of all these intervals. The intersection of all these nested pairs naturally collapses precisely onto the median.
Use the linear time algorithm from the book to find the median of coordinates.
9.3-8
Available in the latest revision of the IM.
9.3-9
Available in the latest revision of the IM.
9.3-10
Available in the latest revision of the IM.
More Generic LeetCode Example
Below is the solution in C++ for the problem associated with this exercise.
Problems
9-1 Largest numbers in sorted order
Available in the latest revision of the IM.
9-2 Variant of randomized selection
Available in the latest revision of the IM.
9-3 Weighted median
Available in the latest revision of the IM.
9-4 Small order statistics
Available in the latest revision of the IM.
9-5 Alternative analysis of randomized selection
Available in the latest revision of the IM.
9-6 Select with groups of 3
a.
We follow the template from the book for any odd constant . The number of groups is at most . There are at least elements greater than or equal to the pivot, and at least elements less than or equal to the pivot. This leaves at most
elements in the recursive call. The recurrence becomes
Since is odd, . We have
We prove that using the substitution method for some constant and .
We should choose to dominate the constant hidden in the . Therefore, .
b.
When the recurrence becomes , which evidently resolves to using the Akra-Bazzi method with and a driving function .
c.
Read about the repeated step algorithm here (see Section 3 in the paper). It contains a visual representation of what is going on, explanation how it differs from grouping by 9, and contains an analysis of the running time.
d.
See part (c).
Last updated