Solution to Exercise 9.1-1
The smallest ofnnumbers can be found withn−1 comparisons by conducting a tournament as follows: Compare all the numbers in pairs. Only the smaller of each pair could possibly be the smallest of alln, so the problem has been reduced to that ofÞnding the smallest ofn/2numbers. Compare those numbers in pairs, and so on, until there’s just one number left, which is the answer.
To see that this algorithm does exactlyn−1 comparisons, notice that each number except the smallest loses exactly once. To show this more formally, draw a binary tree of the comparisons the algorithm does. Thennumbers are the leaves, and each number that came out smaller in a comparison is the parent of the two numbers that were compared. Each non-leaf node of the tree represents a comparison, and there are n −1 internal nodes in an n-leaf full binary tree (see Exercise (B.5-3)), so exactlyn−1 comparisons are made.
In the search for the smallest number, the second smallest number must have come out smallest in every comparison made with it until it was eventually compared with the smallest. So the second smallest is among the elements that were com- pared with the smallest during the tournament. ToÞnd it, conduct another tourna- ment (as above) toÞnd the smallest of these numbers. At mostlgn(the height of the tree of comparisons) elements were compared with the smallest, soÞnding the smallest of these takeslgn −1 comparisons in the worst case.
The total number of comparisons made in the two tournaments was
n−1+ lgn −1=n+ lgn −2 in the worst case.
Solution to Exercise 9.3-1
For groups of 7, the algorithm still works in linear time. The number of elements greater thanx (and similarly, the number less thanx) is at least
4 1 2 n 7 −2 ≥ 2n 7 −8,
9-10 Solutions for Chapter 9: Medians and Order Statistics
and the recurrence becomes
T(n)≤T(n/7)+T(5n/7+8)+O(n) ,
which can be shown to be O(n)by substitution, as for the groups of 5 case in the text.
For groups of 3, however, the algorithm no longer works in linear time. The number of elements greater thanx, and the number of elements less thanx, is at least 2 1 2 n 3 −2 ≥ n 3 −4, and the recurrence becomes
T(n)≤T(n/3)+T(2n/3+4)+O(n) ,
which does not have a linear solution.
We can prove that the worst-case time for groups of 3 is(nlgn). We do so by deriving a recurrence for a particular case that takes(nlgn)time.
In counting up the number of elements greater than x (and similarly, the num- ber less than x), consider the particular case in which there are exactly12n3 groups with medians ≥ x and in which the “leftover” group does contribute 2 elements greater than x. Then the number of elements greater than x is exactly 212n3−1+1 (the −1 discounts x’s group, as usual, and the +1 is con- tributed byx’s group)=2n/6 −1, and the recursive step for elements≤ x has
n−(2n/6 −1) ≥ n−(2(n/6+1)−1) = 2n/3−1 elements. Observe also that theO(n)term in the recurrence is really(n), since the partitioning in step 4 takes(n)(not justO(n)) time. Thus, we get the recurrence
T(n)≥T(n/3)+T(2n/3−1)+(n)≥T(n/3)+T(2n/3−1)+(n) ,
from which you can show thatT(n) ≥ cnlgn by substitution. You can also see thatT(n)is nonlinear by noticing that each level of the recursion tree sums ton. [In fact, any odd group size≥5works in linear time.]
Solution to Exercise 9.3-3
A modiÞcation to quicksort that allows it to run inO(nlgn)time in the worst case uses the deterministic PARTITION algorithm that was modiÞed to take an element to partition around as an input parameter.
SELECTtakes an array A, the bounds pandr of the subarray inA, and the ranki
of an order statistic, and in time linear in the size of the subarrayA[p. .r] it returns theith smallest element in A[p. .r].
BEST-CASE-QUICKSORT(A,p,r) if p<r theni ← (r −p+1)/2 x ←SELECT(A,p,r,i) q ←PARTITION(x) BEST-CASE-QUICKSORT(A,p,q −1) BEST-CASE-QUICKSORT(A,q+1,r)
Solutions for Chapter 9: Medians and Order Statistics 9-11
For ann-element array, the largest subarray that BEST-CASE-QUICKSORTrecurses on hasn/2 elements. This situation occurs whenn =r − p+1 is even; then the subarray A[q+1. .r] hasn/2 elements, and the subarrayA[p. .q−1] hasn/2−1 elements.
Because BEST-CASE-QUICKSORT always recurses on subarrays that are at most half the size of the original array, the recurrence for the worst-case running time is
T(n)≤2T(n/2)+(n)=O(nlgn).
Solution to Exercise 9.3-5
We assume that are given a procedure MEDIAN that takes as parameters an ar- rayAand subarray indices pandr, and returns the value of the median element of
A[p. .r] in O(n)time in the worst case.
Given MEDIAN, here is a linear-time algorithm SELECTforÞnding theith small- est element in A[p. .r]. This algorithm uses the deterministic PARTITION algo- rithm that was modiÞed to take an element to partition around as an input parame- ter. SELECT(A,p,r,i) if p=r then return A[p] x ←MEDIAN(A,p,r) q ←PARTITION(x) k ←q− p+1 ifi =k then return A[q] elseifi<k
then returnSELECT(A,p,q−1,i)
else returnSELECT(A,q +1,r,i−k)
Because x is the median of A[p. .r], each of the subarrays A[p. .q −1] and
A[q+1. .r] has at most half the number of elements of A[p. .r]. The recurrence for the worst-case running time of SELECTisT(n)≤ T(n/2)+O(n)=O(n).
Solution to Exercise 9.3-8
Let’s start out by supposing that the median (the lower median, since we know we have an even number of elements) is in X. Let’s call the median valuem, and let’s suppose that it’s in X[k]. Then k elements of X are less than or equal to m and
n−kelements of Xare greater than or equal tom. We know that in the two arrays combined, there must benelements less than or equal tomandnelements greater than or equal tom, and so there must ben−k elements ofY that are less than or equal tomandn−(n−k)=kelements ofY that are greater than or equal tom.
9-12 Solutions for Chapter 9: Medians and Order Statistics
Thus, we can check thatX[k] is the lower median by checking whetherY[n−k]≤
X[k] ≤ Y[n−k+1]. A boundary case occurs for k =n. Thenn−k =0, and there is no array entryY[0]; we only need to check that X[n]≤Y[1].
Now, if the median is in X but is not in X[k], then the above condition will not hold. If the median is in X[k], wherek <k, then X[k] is above the median, and
Y[n−k+1]< X[k]. Conversely, if the median is inX[k], wherek >k, then
X[k] is below the median, andX[k]<Y[n−k].
Thus, we can use a binary search to determine whether there is an X[k] such that eitherk <nandY[n−k]≤ X[k]≤Y[n−k+1] ork =nandX[k]≤Y[n−k+1]; if weÞnd such anX[k], then it is the median. Otherwise, we know that the median is in Y, and we use a binary search to Þnd a Y[k] such that either k < n and
X[n −k] ≤ Y[k] ≤ X[n −k +1] or k = n andY[k] ≤ X[n −k +1]; such a
Y[k] is the median. Since each binary search takes O(lgn)time, we spend a total ofO(lgn)time.
Here’s how we write the algorithm in pseudocode: TWO-ARRAY-MEDIAN(X,Y)
n ←length[X] n also equalslength[Y]
median←FIND-MEDIAN(X,Y,n,1,n)
ifmedian=NOT-FOUND
thenmedian←FIND-MEDIAN(Y,X,n,1,n)
returnmedian
FIND-MEDIAN(A,B,n,low,high)
iflow>high
then returnNOT-FOUND
else k ← (low+high)/2
ifk =nandA[n]≤ B[1]
then return A[n]
elseifk <nandB[n−k]≤ A[k]≤ B[n−k+1]
then return A[k]
elseif A[k] >B[n−k+1]
then returnFIND-MEDIAN(A,B,n,low,k−1)
else returnFIND-MEDIAN(A,B,n,k+1,high)
Solution to Exercise 9.3-9
In order toÞnd the optimal placement for Professor Olay’s pipeline, we need only
Þnd the median(s) of the y-coordinates of his oil wells, as the following proof explains.
Claim
The optimaly-coordinate for Professor Olay’s east-west oil pipeline is as follows:
• Ifn is even, then on either the oil well whosey-coordinate is the lower median
or the one whosey-coordinate is the upper median, or anywhere between them.
Solutions for Chapter 9: Medians and Order Statistics 9-13
Proof We examine various cases. In each case, we will start out with the pipeline at a particular y-coordinate and see what happens when we move it. We’ll denote by s the sum of the north-south spurs with the pipeline at the starting location, andswill denote the sum after moving the pipeline.
We start with the case in whichnis even. Let us start with the pipeline somewhere on or between the two oil wells whose y-coordinates are the lower and upper me- dians. If we move the pipeline by a vertical distanced without crossing either of the median wells, thenn/2 of the wells become d farther from the pipeline and
n/2 becomed closer, and sos =s+dn/2−dn/2=s; thus, all locations on or between the two medians are equally good.
Now suppose that the pipeline goes through the oil well whosey-coordinate is the upper median. What happens when we increase the y-coordinate of the pipeline byd >0 units, so that it moves above the oil well that achieves the upper median? All oil wells whosey-coordinates are at or below the upper median becomedunits farther from the pipeline, and there are at leastn/2+1 such oil wells (the upper median, and every well at or below the lower median). There are at mostn/2−1 oil wells whose y-coordinates are above the upper median, and each of these oil wells becomes at mostd units closer to the pipeline when it moves up. Thus, we have a lower bound ons ofs ≥ s+d(n/2+1)−d(n/2−1) = s+2d > s. We conclude that moving the pipeline up from the oil well at the upper median increases the total spur length. A symmetric argument shows that if we start with the pipeline going through the oil well whosey-coordinate is the lower median and move it down, then the total spur length increases.
We see, therefore, that when n is even, an optimal placement of the pipeline is anywhere on or between the two medians.
Now we consider the case whennis odd. We start with the pipeline going through the oil well whosey-coordinate is the median, and we consider what happens when we move it up byd >0 units. All oil wells at or below the median becomed units farther from the pipeline, and there are at least(n+1)/2 such wells (the one at the median and the(n−1)/2 at or below the median. There are at most(n−1)/2 oil wells above the median, and each of these becomes at most d units closer to the pipeline. We get a lower bound ons of s ≥ s +d(n+1)/2−d(n−1)/2 =
s +d > s, and we conclude that moving the pipeline up from the oil well at the median increases the total spur length. A symmetric argument shows that moving the pipeline down from the median also increases the total spur length, and so the optimal placement of the pipeline is on the median. (claim) Since we know we are looking for the median, we can use the linear-time median-
Þnding algorithm.
Solution to Problem 9-1
We assume that the numbers start out in an array.
a. Sort the numbers using merge sort or heapsort, which take(nlgn)worst-case time. (Don’t use quicksort or insertion sort, which can take(n2)time.) Put
9-14 Solutions for Chapter 9: Medians and Order Statistics
the i largest elements (directly accessible in the sorted array) into the output array, taking(i)time.
Total worst-case running time: (nlgn+i)=(nlgn)(becausei≤n).
b. Implement the priority queue as a heap. Build the heap using BUILD-HEAP, which takes (n) time, then call HEAP-EXTRACT-MAX i times to get the i
largest elements, in(ilgn)worst-case time, and store them in reverse order of extraction in the output array. The worst-case extraction time is (ilgn)
because
• i extractions from a heap with O(n)elements takesi ·O(lgn)= O(ilgn)
time, and
• half of thei extractions are from a heap with≥ n/2 elements, so thosei/2
extractions take(i/2)(lg(n/2))=(ilgn)time in the worst case. Total worst-case running time: (n+ilgn).
c. Use the SELECTalgorithm of Section 9.3 toÞnd theith largest number in(n)
time. Partition around that number in(n)time. Sort thei largest numbers in (ilgi)worst-case time (with merge sort or heapsort).
Total worst-case running time: (n+ilgi).
Note that method (c) is always asymptotically at least as good as the other two methods, and that method (b) is asymptotically at least as good as (a). (Com- paring (c) to (b) is easy, but it is less obvious how to compare (c) and (b) to (a). (c) and (b) are asymptotically at least as good as (a) becausen,ilgi, andilgnare all O(nlgn). The sum of two things that areO(nlgn)is also O(nlgn).)
Solution to Problem 9-2
a. The median x of the elements x1,x2, . . . ,xn, is an elementx = xk satisfying
|{xi : 1≤i≤nandxi <x}| ≤ n/2 and|{xi : 1≤i ≤n andxi >x}| ≤ n/2. If each elementxi is assigned a weightwi =1/n, then we get
xi<x wi = xi<x 1 n = 1 n · xi<x 1 = 1 n · |{xi : 1≤i ≤nandxi <x}| ≤ 1 n · n 2 = 1 2 , and xi>x wi = xi>x 1 n
Solutions for Chapter 9: Medians and Order Statistics 9-15 = 1 n · xi>x 1 = 1 n · |{xi : 1≤i ≤nandxi >x}| ≤ 1 n · n 2 = 1 2 ,
which proves thatxis also the weighted median ofx1,x2, . . . ,xnwith weights wi =1/n, fori =1,2, . . . ,n.
b. We Þrst sort the nelements into increasing order byxi values. Then we scan the array of sorted xi’s, starting with the smallest element and accumulating weights as we scan, until the total exceeds 1/2. The last element, sayxk, whose weight caused the total to exceed 1/2, is the weighted median. Notice that the total weight of all elements smaller than xk is less than 1/2, because xk was theÞrst element that caused the total weight to exceed 1/2. Similarly, the total weight of all elements larger than xk is also less than 1/2, because the total weight of all the other elements exceeds 1/2.
The sorting phase can be done in O(nlgn)worst-case time (using merge sort or heapsort), and the scanning phase takes O(n)time. The total running time in the worst case, therefore, is O(nlgn).
c. We Þnd the weighted median in (n)worst-case time using the (n)worst- case median algorithm in Section 9.3. (Although the Þrst paragraph of the section only claims anO(n)upper bound, it is easy to see that the more precise running time of(n)applies as well, since steps 1, 2, and 4 of SELECTactually take(n)time.)
The weighted-median algorithm works as follows. If n ≤ 2, we just return the brute-force solution. Otherwise, we proceed as follows. WeÞnd the actual medianxk of thenelements and then partition around it. We then compute the total weights of the two halves. If the weights of the two halves are each strictly less than 1/2, then the weighted median isxk. Otherwise, the weighted median should be in the half with total weight exceeding 1/2. The total weight of the “light” half is lumped into the weight ofxk, and the search continues within the half that weighs more than 1/2. Here’s pseudocode, which takes as input a set
9-16 Solutions for Chapter 9: Medians and Order Statistics WEIGHTED-MEDIAN(X) ifn=1 then returnx1 elseifn =2 then ifw1≥w2 then returnx1 else returnx2 else Þnd the medianxkofX = {x1,x2, . . . ,xn} partition the set Xaround xk
computeWL = xi<xkwi andWG = xi>xkwi ifWL <1/2 andWG <1/2 then returnxk elseifWL >1/2 thenwk ←wk+WG X← {xi ∈ X :xi ≤xk} returnWEIGHTED-MEDIAN(X) elsewk ←wk+WL X← {xi ∈ X :xi ≥xk} returnWEIGHTED-MEDIAN(X)
The recurrence for the worst-case running time of WEIGHTED-MEDIAN is
T(n)=T(n/2+1)+(n), since there is at most one recursive call on half the number of elements, plus the median elementxk, and all the work preceding the recursive call takes(n)time. The solution of the recurrence isT(n)=(n).
d. Let the n points be denoted by their coordinates x1,x2, . . . ,xn, let the corre-
sponding weights bew1, w2, . . . , wn, and letx =xk be the weighted median. For any point p, let f(p) =ni=1wi|p−xi|; we want toÞnd a point p such that f(p)is minimum. Letybe any point (real number) other thanx. We show the optimality of the weighted medianx by showing that f(y)− f(x)≥0. We examine separately the cases in which y >x andx > y. For anyx andy, we have f(y)− f(x) = n i=1 wi|y−xi| − n i=1 wi|x−xi| = n i=1 wi(|y−xi| − |x−xi|) .
When y >x, we bound the quantity|y−xi| − |x −xi|from below by exam- ining three cases:
1. x < y≤ xi: Here,|x −y| + |y−xi| = |x−xi|and|x −y| = y−x, which imply that|y−xi| − |x −xi| = − |x−y| =x−y.
2. x < xi ≤ y: Here,|y−xi| ≥ 0 and|xi −x| ≤ y −x, which imply that
|y−xi| − |x −xi| ≥ −(y−x)=x−y.
3. xi ≤x <y: Here,|x −xi| + |y−x| = |y−xi|and|y−x| = y−x, which imply that|y−xi| − |x −xi| = |y−x| = y−x.
Solutions for Chapter 9: Medians and Order Statistics 9-17
Separating out theÞrst two cases, in whichx <xi, from the third case, in which
x ≥xi, we get f(y)− f(x) = n i=1 wi(|y−xi| − |x−xi|) ≥ x<xi wi(x−y)+ x≥xi wi(y−x) = (y−x) x≥xi wi − x<xi wi . The property that xi<xwi < 1/2 implies that
x≥xiwi ≥ 1/2. This fact,
combined with y−x >0 andx<xiwi ≤1/2, yields that f(y)− f(x)≥0.