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-5

Exercise 2.2.1-5

not stated yet in Lean solution written

RatingM28
MarkersM (mathematical)
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 2.2.1 of Volume 1

The task

Show that p1p2…pnp_1 p_2 \ldots p_n can be obtained from 12…n12\ldots n with a stack if and only if there are no indices i<j<ki < j < k with pj<pk<pip_j < p_k < p_i.

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

For the "only if" part, ask when each of the cars pjp_j and pkp_k must enter the stack.

Hint 2

For the "if" part, run the following rule. To output pjp_j, first push input cars until pjp_j is in the stack. Then pop it. Show that the rule never pops a wrong car.

Solution

Show the solution

Plan. Show that a bad triple forces a contradiction. Then show that, without bad triples, the greedy rule works.

Only if. Suppose i<j<ki < j < k and pj<pk<pip_j < p_k < p_i. The cars enter in increasing order, so pjp_j enters first, then pkp_k, then pip_i. The output has pip_i before pjp_j and pkp_k. So when pip_i leaves, the cars pjp_j and pkp_k have entered and have not left. They are in the stack, and pkp_k lies above pjp_j. A stack gives up pkp_k first. But the output has pjp_j before pkp_k. This is a contradiction.

If. Let pp have no bad triple. Use this rule for j=1,…,nj = 1, \ldots, n. If pjp_j is not yet in the stack, push the next input cars until pjp_j is pushed. Then pop pjp_j. The cars enter in increasing order, so the stack always has increasing values from the bottom to the top.

Suppose the rule fails at step jj. Then pjp_j is in the stack, and some car yy lies above it. We have pj<yp_j < y, because the stack increases upward. The car yy has not left, so y=pky = p_k for some k>jk > j. The car yy was pushed during some earlier step i<ji < j, while the rule sought pip_i. That step pushes the input up to pip_i, and y≠piy \ne p_i, so y<piy < p_i. Then pj<pk<pip_j < p_k < p_i with i<j<ki < j < k. This is a bad triple, and it does not exist. So the rule never fails, and it ends with the output p1…pnp_1 \ldots p_n.

Check

For n=1,…,8n = 1, \ldots, 8 I compared two sets. The first is the set of all permutations that a stack gives, found by search. The second is the set of permutations with no triple of the type above. The two sets were equal each time, with 1,2,5,14,42,132,429,14301, 2, 5, 14, 42, 132, 429, 1430 elements.

Remarks

In the language of patterns, the stack gives exactly the permutations that avoid the pattern 312312. The result also proves again that ana_n is the Catalan number, as Exercise 2.2.1-4 finds.

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.