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

Exercise 2.2.1-2

not stated yet in Lean solution written

Rating15
KindK2, decide: say whether a claim holds from the written solution
SectionSection 2.2.1 of Volume 1

The task

Decide whether six cars in the order 123456123456 can leave a stack in the order 325641325641, and in the order 154623154623. Show a way to do it when it is possible.

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

Write each move as S (input to stack) or X (stack to output). The cars enter in the order 1,2,…,61, 2, \ldots, 6.

Hint 2

For the second order, look at the moment when car 55 leaves. Which of the cars 22 and 33 are then in the stack, and in which order?

Solution

Show the solution

First order, 325641325641. The sequence of moves SSSXXSSXSXXX works. The table shows the stack from bottom to top after each move.

move stack output
S 1
S 1 2
S 1 2 3
X 1 2 3
X 1 3 2
S 1 4 3 2
S 1 4 5 3 2
X 1 4 3 2 5
S 1 4 6 3 2 5
X 1 4 3 2 5 6
X 1 3 2 5 6 4
X 3 2 5 6 4 1

Second order, 154623154623. It is impossible. Car 11 leaves first, and then car 55. The cars enter in increasing order, so cars 22 and 33 enter the stack before car 55 does. They leave after car 55, because the output has 55 before both. So at the time when car 55 leaves, the stack holds 22 and 33, with 33 above 22 because 33 entered later. A stack gives up 33 before 22. The wanted output has 22 before 33. This is a contradiction.

Check

I generated all permutations of 123456123456 that a stack gives, by a search over all move sequences. There are 132132 of them. The order 325641325641 is among them, and the order 154623154623 is not.

Remarks

The argument for the second order is a special case of the rule of Exercise 2.2.1-5. The three cars 5,2,35, 2, 3 are in the forbidden relative order.

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.