9-2 Weighted median
For distinct elements with positive weights such that , the weighted (lower) median is the element satisfying
and
For example, if the elements are and each element equals its weight (that is, for ), then the median is , but the weighted median is .
a. Argue that the median of is the weighted median of the with weights for .
b. Show how to compute the weighted median of elements in worstcase time using sorting.
c. Show how to compute the weighted median in worst-case time using a linear-time median algorithm such as from Section 9.3.
The post-office location problem is defined as follows. We are given points with associated weights . We wish to find a point (not necessarily one of the input points) that minimizes the sum , where is the distance between points and .
d. Argue that the weighted median is a best solution for the -dimensional postoffice location problem, in which points are simply real numbers and the distance between points and is .
e. Find the best solution for the -dimensional post-office location problem, in which the points are coordinate pairs and the distance between points and is the Manhattan distance given by .
a. Let be the number of smaller than . When weights of are assigned to each , we have and . The only value of which makes these sums and respectively is when , and this value must be the median since it has equal numbers of which are larger and smaller than it.
b. First use mergesort to sort the 's by value in time. Let be the sum of the weights of the first elements of this sorted array and note that it is to update . Compute until you reach such that and . The weighted median is .
c. We modify to do this in linear time. Let be the median of medians. Compute and and check if either of these is larger than . If not, stop. If so, recurse on the collection of smaller or larger elements known to contain the weighted median. This doesn't change the runtime, so it is .
d. Let be the minimizer, and suppose that is not the weighted median. Let be small enough such that , where we don't include if . If is the weighted median and , choose . Otherwise choose . Thus, we have
the difference in sums will take the opposite sign of epsilon.
e. Observe that
It will suffice to minimize each sum separately, which we can do since we choose and individually. By part (e), we simply take to be such that is the weighted median of the -coordinates of the 's and py is the weighted medain of the -coordiantes of the 's.