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.1-7

Exercise 1.1-7

not stated yet in Lean solution written

RatingM21
MarkersM (mathematical), recommended
KindK7, analyze: find the mean, the variance or the distribution of a cost from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Fix mm and let nn range over all positive integers. Show that the average number UmU_m of runs of step E1 is well defined, and find how it is related to TmT_m.

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

The average over infinitely many nn needs a definition. Take the average over n=1,…,Nn = 1, \ldots, N and ask for its limit as N→∞N \to \infty.

Hint 2

For n>mn > m, the first pass of Algorithm E only swaps the two numbers.

Hint 3

After the swap, the pair (n,m)(n, m) is an input of the kind that gives TmT_m.

Solution

Show the solution

Let D(u,v)D(u, v) be the number of runs of step E1 on the input (u,v)(u, v). The average over n≤Nn \le N is Um(N)=1N∑n=1ND(m,n),U_m(N) = \frac{1}{N} \sum_{n=1}^{N} D(m, n), and UmU_m is the limit of Um(N)U_m(N) as N→∞N \to \infty.

Step 1: the count for large nn. For n>mn > m the first run of E1 gives r=mr = m, since m<nm < n. Step E3 then sets the pair to (n,m)(n, m). So D(m,n)=1+D(n,m)D(m, n) = 1 + D(n, m) for n>mn > m. Here D(n,m)D(n, m) is the count with second argument mm.

Step 2: D(n,m)D(n, m) depends on n mod mn \bmod m. If mm divides nn, then D(n,m)=1D(n, m) = 1. Otherwise the first division gives the remainder ρ=n mod m\rho = n \bmod m with 0<ρ<m0 < \rho < m, and the pair is (m,ρ)(m, \rho). So D(n,m)D(n, m) is a function d(ρ)d(\rho) of ρ=n mod m\rho = n \bmod m, where d(0)=1d(0) = 1 and d(ρ)=D(ρ,m)d(\rho) = D(\rho, m) for 0<ρ<m0 < \rho < m. (The latter is true since D(ρ,m)=1+D(m,ρ)D(\rho, m) = 1 + D(m, \rho) as in step 1.) Also d(0)=D(m,m)d(0) = D(m, m). So Tm=1m∑ρ=1mD(ρ,m)=1m∑ρ=0m−1d(ρ),T_m = \frac{1}{m} \sum_{\rho = 1}^{m} D(\rho, m) = \frac{1}{m} \sum_{\rho = 0}^{m-1} d(\rho), since the value ρ=m\rho = m in the first sum gives the same count as ρ=0\rho = 0.

Step 3: the limit. The number mm is fixed. For N>mN > m, N Um(N)=∑n=1mD(m,n)+∑n=m+1N(1+d(n mod m)).N\,U_m(N) = \sum_{n=1}^{m} D(m, n) + \sum_{n=m+1}^{N} \bigl(1 + d(n \bmod m)\bigr). The first sum has mm terms and is a constant CmC_m. In the second sum each block of mm consecutive values of nn gives each residue ρ\rho once, so a full block contributes m+∑ρd(ρ)=m(1+Tm)m + \sum_\rho d(\rho) = m(1 + T_m). Write N−m=qm+sN - m = qm + s with 0≤s<m0 \le s < m. The second sum is qm(1+Tm)qm(1 + T_m) plus a part of at most mm terms, and each term is at most 1+max⁡d1 + \max d. Divide by NN and let N→∞N \to \infty: the constant CmC_m and the partial block vanish in the limit, and qm/N→1qm / N \to 1. So Um=lim⁡N→∞Um(N)=1+Tm.U_m = \lim_{N \to \infty} U_m(N) = 1 + T_m. The limit exists, so UmU_m is well defined, and it is TmT_m plus one.

Check

For m=5m = 5, T5=13/5T_5 = 13/5, so U5=18/5=3.6U_5 = 18/5 = 3.6. The counts D(5,n)D(5, n) for n=1,…,15n = 1, \ldots, 15 are 1,2,3,2,1,3,4,5,4,2,3,4,5,4,21, 2, 3, 2, 1, 3, 4, 5, 4, 2, 3, 4, 5, 4, 2. From n=6n = 6 on they repeat with period five, with values 3,4,5,4,23, 4, 5, 4, 2, whose average is 18/518/5. Computed averages: U5(100)=3.51U_5(100) = 3.51, U5(1000)=3.591U_5(1000) = 3.591 and U5(105)=3.59991U_5(10^5) = 3.59991, which approach 3.63.6.

Remarks

The extra 11 is the wasted swap, which is the cost that the swap step E0 of the C1 chapter removes. The same argument works for any quantity that depends on nn only through n mod mn \bmod m and has a bounded change at small nn.

This solution is checked, 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.