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

Exercise 2.2.1-3

not stated yet in Lean solution written

Rating25
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 2.2.1 of Volume 1

The task

Give a simple test that tells admissible sequences of S and X from inadmissible ones. Show that two different admissible sequences never give the same output permutation.

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

Count the cars in the stack after each move.

Hint 2

For the second part, look at the first move where two sequences differ, and ask which car is in play.

Solution

Show the solution

Plan. Track the size of the stack. Then compare two sequences at the first move where they differ.

The test. A sequence with nn letters S and nn letters X is admissible if and only if, in every prefix, the number of X is at most the number of S.

The stack size after a prefix is the number of S minus the number of X in it. An X is possible only when the stack is not empty, that is, when the size before it is at least 11. So the size must stay at 00 or more after every move, and this is the same as the stated rule. The count of S is nn, so no S is ever blocked by a lack of input cars. The count of X is also nn, so the stack is empty at the end and all cars have left.

Different sequences give different permutations. Let uu and vv be two different admissible sequences. Cut off the longest common prefix. After it, both stacks hold the same cars, and the same cars have entered. At the next move, one sequence has S and the other has X. Say uu has S and vv has X.

Let tt be the top car of the stack at this point. It exists, because vv is admissible and takes X. Let cc be the next car of the input. It has not entered yet in either sequence.

In uu, the car cc enters on top of tt, so tt leaves after cc. In vv, the car tt leaves now, and cc has not yet entered, so tt leaves before cc. The two outputs order the cars tt and cc in opposite ways. So they differ.

Check

For n=1,…,7n = 1, \ldots, 7 I listed all sequences with nn letters S and nn letters X. The prefix rule picked out the same sequences as a direct run of the stack. The outputs of these runs were all different, and they were exactly the permutations that a stack can give.

Remarks

The test is the same as the rule for a correct bracket sequence, with S as the left bracket and X as the right bracket. The one-to-one link between sequences and permutations is the base of the count in Exercise 2.2.1-4.

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.