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

Exercise 2.2.1-7

not stated yet in Lean solution written

Rating25
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

Use a deque in place of the stack. Find a permutation of 12341234 that (a) an input-restricted deque gives and an output-restricted deque does not, (b) an output-restricted deque gives and an input-restricted deque does not, (c) neither of the two gives.

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

Look at permutations that start with 44. The car 44 is the last car to enter, so all cars have entered before it leaves.

Hint 2

For an input-restricted deque, insert all four cars at the left end. For an output-restricted deque, the four cars sit in the deque in the output order.

Solution

Show the solution

Plan. Study the permutations that start with 44. They are the same for all three parts.

Let the output start with 44. Then all of the cars 1,2,3,41, 2, 3, 4 entered before the first removal.

Input-restricted deque. Cars enter at the left only, so the deque reads 4 3 2 14\,3\,2\,1 from left to right. Each further removal takes the largest or the smallest of the cars that remain. So the rest of the output must take, at each step, the maximum or the minimum of what is left.

Output-restricted deque. Cars leave at the left only, so the output is the left to right order of the deque. The cars 1,2,3,41, 2, 3, 4 entered in this order, each at an end. So 11 is somewhere, and each larger car is at the left or right end of the cars before it. The arrangement falls to its minimum 11 and then rises again.

Now test the three permutations.

(a) 41324132. For the input-restricted deque: after 44 comes 11 (the minimum of {1,2,3}\{1,2,3\}), then 33 (the maximum of {2,3}\{2,3\}), then 22. It works. For the output-restricted deque the arrangement 4 1 3 24\,1\,3\,2 is not a fall and a rise, because it rises from 11 to 33 and then falls to 22. So it fails.

(b) 42134213. For the output-restricted deque: insert 11, then 22 at the left, then 33 at the right, then 44 at the left. The deque is 4 2 1 34\,2\,1\,3, and removing at the left gives 42134213. For the input-restricted deque: after 44 comes 22, but 22 is neither the maximum nor the minimum of {1,2,3}\{1,2,3\}. So it fails.

(c) 42314231. For the input-restricted deque: after 44 comes 22, which is neither the maximum nor the minimum of {1,2,3}\{1,2,3\}. For the output-restricted deque: the arrangement 4 2 3 14\,2\,3\,1 falls, rises, and falls again. Both fail.

So the answers are (a) 41324132, (b) 42134213, (c) 42314231. The search below shows that each of them is the only answer for its part.

Check

I generated all permutations of 12341234 for both machines by search. The input-restricted set has 2222 permutations, and so has the output-restricted set. The first minus the second is {4132}\{4132\}. The second minus the first is {4213}\{4213\}. The only permutation that is in neither is 42314231.

Remarks

Both restricted deques give 2222 permutations of 12341234, so the count does not separate them. Exercise 2.2.1-9 explains why the counts are equal for every nn.

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.