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.8-40

Exercise 1.2.8-40

not stated yet in Lean solution written

RatingM25
MarkersM (mathematical)
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 1.2.8 of Volume 1. Companion chapter: Fibonacci numbers

The task

Solve f(1)=0f(1) = 0 and f(n)=min⁡0<k<nmax⁡(1+f(k),2+f(n−k))f(n) = \min_{0<k<n}\max(1 + f(k), 2 + f(n-k)) for n>1n > 1.

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

Compute f(n)f(n) for n≤14n \le 14 and look at the numbers.

Hint 2

Let SmS_m be the set of nn with f(n)≤mf(n) \le m. Show that SmS_m is an initial segment of the positive integers, and find its length with a recurrence in mm.

Solution

Show the solution

Let Sm={n≥1:f(n)≤m}S_m = \{n \ge 1 : f(n) \le m\}. Claim: Sm={1,…,Fm+1}S_m = \{1, \ldots, F_{m+1}\} for all m≥0m \ge 0.

Base cases. f(1)=0f(1) = 0, and f(n)≥2f(n) \ge 2 for n>1n > 1, because the maximum is at least 2+f(n−k)≥22 + f(n - k) \ge 2. So S0=S1={1}S_0 = S_1 = \{1\}, and F1=F2=1F_1 = F_2 = 1.

Step. Let m≥2m \ge 2 and assume the claim for m−1m - 1 and m−2m - 2. For n>1n > 1, f(n)≤mf(n) \le m if and only if there is a kk with 0<k<n0 < k < n such that 1+f(k)≤m1 + f(k) \le m and 2+f(n−k)≤m2 + f(n-k) \le m, that is, k∈Sm−1={1,…,Fm}k \in S_{m-1} = \{1, \ldots, F_m\} and n−k∈Sm−2={1,…,Fm−1}n - k \in S_{m-2} = \{1, \ldots, F_{m-1}\}. Such a kk exists if and only if 2≤n≤Fm+Fm−1=Fm+12 \le n \le F_m + F_{m-1} = F_{m+1}. (For 2≤n≤Fm+12 \le n \le F_{m+1} take k=min⁡(Fm,n−1)k = \min(F_m, n - 1). Then n−k≥1n - k \ge 1 and n−k≤Fm−1n - k \le F_{m-1}.) Also 1∈Sm1 \in S_m. So Sm={1,…,Fm+1}S_m = \{1, \ldots, F_{m+1}\}.

Hence f(n)f(n) is the least mm with n≤Fm+1n \le F_{m+1}. For n≥2n \ge 2 this is the mm with

Fm<n≤Fm+1,F_m < n \le F_{m+1},

and f(1)=0f(1) = 0.

Check

A direct python computation of the recurrence gives f(1),…,f(14)=0,2,3,4,4,5,5,5,6,6,6,6,6,7f(1), \ldots, f(14) = 0, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7. The formula gives f(2)=2f(2) = 2 (F2=1<2≤F3=2F_2 = 1 < 2 \le F_3 = 2), f(3)=3f(3) = 3 (2<3≤32 < 3 \le 3), f(4)=f(5)=4f(4) = f(5) = 4 (3<n≤53 < n \le 5), f(6)=⋯=f(8)=5f(6) = \cdots = f(8) = 5, f(9)=⋯=f(13)=6f(9) = \cdots = f(13) = 6 and f(14)=7f(14) = 7. The two lists agree for all n<60n < 60.

Remarks

The function is the minimum height of a tree in which a left branch costs 11 and a right branch costs 22, with nn leaves. The optimal trees are the Fibonacci trees. The method shows that a recurrence of min-max type can be solved by finding the sets {n:f(n)≤m}\{n : f(n) \le m\}.

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.