8-1 Probabilistic lower bounds on comparison sorting

In this problem, we prove a probabilistic Ω(nlgn)\Omega(n\lg n) lower bound on the running time of any deterministic or randomized comparison sort on nn distinct input elements. We begin by examining a deterministic comparison sort AA with decision tree TAT_A. We assume that every permutation of AA's inputs is equally likely.

a. Suppose that each leaf of TAT_A is labeled with the probability that it is reached given a random input. Prove that exactly n!n! leaves are labeled 1/n!1 / n! and that the rest are labeled 00.

b. Let D(T)D(T) denote the external path length of a decision tree TT; that is, D(T)D(T) is the sum of the depths of all the leaves of TT. Let TT be a decision tree with k>1k > 1 leaves, and let LTLT and RTRT be the left and right subtrees of TT. Show that D(T)=D(LT)+D(RT)+kD(T) = D(LT) + D(RT)+k.

c. Let d(k)d(k) be the minimum value of D(T)D(T) over all decision trees TT with k>1k > 1 leaves. Show that d(k)=min1ik1{d(i)+d(ki)+k}d(k) = \min _{1 \le i \le k - 1} \{ d(i) + d(k - i) + k \}. (Hint:\textit{Hint:} Consider a decision tree TT with kk leaves that achieves the minimum. Let i0i_0 be the number of leaves in LTLT and ki0k - i_0 the number of leaves in RTRT.)

d. Prove that for a given value of k>1k > 1 and ii in the range 1ik11 \le i \le k - 1, the function ilgi+(ki)lg(ki)i\lg i + (k - i) \lg(k - i) is minimized at i=k/2i = k / 2. Conclude that d(k)=Ω(klgk)d(k) = \Omega(k\lg k).

e. Prove that D(TA)=Ω(n!lg(n!))D(T_A) = \Omega(n!\lg(n!)), and conclude that the average-case time to sort nn elements is Ω(nlgn)\Omega(n\lg n).

Now, consider a randomized comparison sort BB. We can extend the decision-tree model to handle randomization by incorporating two kinds of nodes: ordinary comparison nodes and "randomization" nodes. A randomization node models a random choice of the form RANDOM(1,r)\text{RANDOM}(1, r) made by algorithm BB; the node has rr children, each of which is equally likely to be chosen during an execution of the algorithm.

f. Show that for any randomized comparison sort BB, there exists a deterministic comparison sort AA whose expected number of comparisons is no more than those made by BB.

a. There are n!n! possible permutations of the input array because the input elements are all distinct. Since each is equally likely, the distribution is uniformly supported on this set. So, each occurs with probability 1n!\frac{1}{n!} and corresponds to a different leaf because the program needs to be able to distinguish between them.

b. The depths of particular elements of LTLT or RTRT are all one less than their depths when considered elements of TT. In particular, this is true for the leaves of the two subtrees. Also, {LT,RT}\{LT, RT\} form a partition of all the leaves of TT. Therefore, if we let L(T)L(T) denote the leaves of TT,

D(T)=L(T)DT()=L(LT)DT()+L(RT)DT()=L(LT)(DLT()+1)+L(RT)(DRT()+1)=L(LT)DLT()+L(RT)DRT()+k=D(LT)+D(RT)+k. \begin{aligned} D(T) & = \sum_{\ell \in L(T)} D_T(\ell) \\ & = \sum_{\ell \in L(LT)} D_T(\ell) + \sum_{\ell \in L(RT)} D_T(\ell) \\ & = \sum_{\ell \in L(LT)} (D_{LT}(\ell) + 1) + \sum_{\ell \in L(RT)} (D_{RT}(\ell) + 1) \\ & = \sum_{\ell \in L(LT)} D_{LT}(\ell) + \sum_{\ell \in L(RT)} D_{RT}(\ell) + k \\ & = D(LT) + D(RT) + k. \end{aligned}

c. Suppose we have a TT with kk leaves so that D(T)=d(k)D(T) = d(k). Let i0i_0 be the number of leaves in LTLT. Then, d(k)=D(T)=D(LT)+D(RT)+kd(k) = D(T) = D(LT) + D(RT) + k. However, we can pick LTLT and RTRT to minimize the external path length.

d. We treat ii as a continuous variable, and take a derivative to find critical points. The given expression has the following as a derivative with respect to ii

1ln2+lgi+1ln2lg(ki)=2ln2+lg(iki),\frac{1}{\ln 2} + \lg i + \frac{1}{\ln 2} - \lg(k - i) = \frac{2}{\ln 2} + \lg\left(\frac{i}{k - i}\right),

which is 00 when we have iki=22ln2=2lge2=e2\frac{i}{k - i} = 2^{-\frac{2}{\ln 2}} = 2^{-\lg e^2} = e^{-2}. Therefore, (1+e2)i=k(1 + e^{-2})i = k, i=k1+e2i = \frac{k}{1 + e^{-2}}.

Since we are picking the two subtrees to be roughly equal size, the total depth will be order lgk\lg k, with each level contributing kk, so the total external path length is at least klgkk\lg k.

e. Since before we that a tree with kk leaves needs to have external length klgkk\lg k, and that a sorting tree needs at least n!n! trees, a sorting tree must have external tree length at least n!lg(n!)n!\lg (n!). Since the average case run time is the depth of a leaf weighted by the probability of that leaf being the one that occurs, we have that the run time is at least n!lg(n!)n!=lg(n!)Ω(nlgn)\frac{n!\lg (n!)}{n!} = \lg (n!) \in \Omega(n\lg n).

f. Since the expected runtime is the average over all possible results from the random bits, if every possible fixing of the randomness resulted in a higher runtime, the average would have to be higher as well.