taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.10-18

Exercise 1.2.10-18

not stated yet in Lean solution written

RatingM28
MarkersM (mathematical)
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.10 of Volume 1. Companion chapter: Analysis of an algorithm

The task

Find the generating function of AA when the keys contain krk_r copies of the value rr for r=1,…,nr=1,\ldots,n (with ∑kr=n\sum k_r=n) in random order.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

A+1A+1 is the number of right-to-left maxima of the sequence.

Hint 2

Build the sequence by placing the copies of nn first, then those of n−1n-1 among them, and so on.

Hint 3

Find when a new value adds a right-to-left maximum.

Solution

Show the solution

Call a position kk a right-to-left maximum when X[k]>X[i]X[k]>X[i] for all i>ki>k. Position nn is always one. Step M4 runs once for each right-to-left maximum other than position nn, so A=R−1A=R-1, where RR is the number of right-to-left maxima.

All distinct arrangements of the multiset are equally likely. Build an arrangement in stages r=n,n−1,…,1r=n,n-1,\ldots,1: at stage rr the copies of rr are put among the entries with values greater than rr, which are already in place. Look at the sequence of the entries with value ≥r\ge r only. It is a uniformly random arrangement of its multiset, and the positions of the krk_r copies of rr in it are a uniformly random choice of krk_r places out of sr=kr+kr+1+⋯+kns_r=k_r+k_{r+1}+\cdots+k_n places.

Smaller values do not change which larger entries are right-to-left maxima, because an entry is one only if all entries to its right are smaller. A copy of rr is a right-to-left maximum among the entries ≥r\ge r if and only if it is the last entry, since a copy of rr with any entry to its right (of value rr or more) is not strictly larger than all of them. So passing from the entries >r>r to the entries ≥r\ge r adds one right-to-left maximum exactly when the last place is occupied by a copy of rr. That has probability kr/srk_r/s_r. The events for different rr are independent, because the relative placement of the rr's among the larger entries does not depend on the arrangement of the larger entries. So RR is a sum of independent indicator variables, and the generating function of RR is

∏r=1nkrz+kr+1+⋯+knkr+kr+1+⋯+kn,\prod_{r=1}^n\frac{k_rz+k_{r+1}+\cdots+k_n}{k_r+k_{r+1}+\cdots+k_n},

with 0/0=10/0=1 for rr larger than all present values. The factor for the largest value present is zz (probability 11). Since A=R−1A=R-1, we divide by zz:

GA(z)=1z∏r=1nkrz+kr+1+⋯+knkr+kr+1+⋯+kn.G_A(z)=\frac1z\prod_{r=1}^n\frac{k_rz+k_{r+1}+\cdots+k_n}{k_r+k_{r+1}+\cdots+k_n}.

If kn>0k_n>0 this is the product over r=1,…,n−1r=1,\ldots,n-1, with the factor r=nr=n equal to 11 instead of zz.

Check

For all kr=1k_r=1 the factor for rr is z+n−rn−r+1\frac{z+n-r}{n-r+1}. The product over r≥1r\ge1 is z(z+1)⋯(z+n−1)/n!z(z+1)\cdots(z+n-1)/n!, and dividing by zz gives (8). For (k1,…,k5)=(2,0,1,2,0)(k_1,\ldots,k_5)=(2,0,1,2,0) the formula gives GA=215z2+715z+25G_A=\frac2{15}z^2+\frac7{15}z+\frac25. A count of all 3030 arrangements of the multiset {1,1,3,4,4}\{1,1,3,4,4\} in Python gives 12,14,412,14,4 of them with A=0,1,2A=0,1,2, the same values.

Remarks

Here GA(1)=1G_A(1)=1 and the mean is ∑rkr/sr−1\sum_rk_r/s_r-1. The case of all distinct keys is the main text, and the case of mm distinct values with random multiplicities is Exercise 1.2.10-7.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.