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 · 2.2.1-4

Exercise 2.2.1-4

not stated yet in Lean solution written

RatingM34
MarkersM (mathematical)
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 2.2.1 of Volume 1

The task

Find a simple formula for ana_n, the number of permutations of nn elements that a stack can give.

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

Use Exercise 2.2.1-3. The number ana_n is the number of admissible sequences of nn letters S and nn letters X.

Hint 2

Split an admissible sequence at the first time when the stack becomes empty.

Solution

Show the solution

Plan. Count admissible sequences by their first return to the empty stack. This gives a recurrence and a generating function. Then read off the coefficients.

By Exercise 2.2.1-3, ana_n is the number of admissible sequences with nn letters of each kind. Put a0=1a_0 = 1 for the empty sequence.

A non-empty admissible sequence starts with S. Let the stack become empty for the first time after the matching X. Then the sequence has the form S α\alpha X β\beta, where α\alpha and β\beta are admissible. If α\alpha has kk letters of each kind, then β\beta has n−1−kn - 1 - k. This split is unique, so an=∑k=0n−1ak an−1−k(n≥1).a_n = \sum_{k=0}^{n-1} a_k\, a_{n-1-k}\qquad (n \ge 1).

Let A(z)=∑n≥0anznA(z) = \sum_{n \ge 0} a_n z^n. The recurrence says A=1+zA2A = 1 + zA^2. The root with A(0)=1A(0) = 1 is A(z)=1−1−4z2z.A(z) = \frac{1 - \sqrt{1 - 4z}}{2z}.

By the binomial series, 1−4z=1−∑k≥12k(2k−2k−1)zk.\sqrt{1-4z} = 1 - \sum_{k \ge 1} \frac{2}{k}\binom{2k-2}{k-1} z^k. The reason is that (1/2k)(−4)k=−2k(2k−2k−1)\binom{1/2}{k}(-4)^k = -\frac{2}{k}\binom{2k-2}{k-1} for k≥1k \ge 1. Hence A(z)=∑k≥11k(2k−2k−1)zk−1,A(z) = \sum_{k \ge 1} \frac{1}{k}\binom{2k-2}{k-1} z^{k-1}, and the coefficient of znz^n is an=1n+1(2nn)=(2nn)−(2nn−1).a_n = \frac{1}{n+1}\binom{2n}{n} = \binom{2n}{n} - \binom{2n}{n-1}.

Check

A search over all stack runs gave an=1,2,5,14,42,132,429,1430a_n = 1, 2, 5, 14, 42, 132, 429, 1430 for n=1,…,8n = 1, \ldots, 8. The formula gives the same values. The two forms of the formula agree for these nn.

Remarks

The numbers ana_n are the Catalan numbers. The book's answer takes another route, through a reflection of the bad sequences.

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.