Exercise · Volume 1 · 2.2.1-10
Exercise 2.2.1-10
not stated yet in Lean solution written
| Rating | M25 |
|---|---|
| Markers | M (mathematical) |
| Kind | K1, prove: show that a stated fact is true from the written solution |
| Section | Section 2.2.1 of Volume 1 |
The task
An output-restricted deque has three operations: S inserts the next car at the left end, Q inserts it at the right end, and X removes the car at the left end. Define admissible sequences of these letters so that every permutation that the deque can give comes from exactly one admissible sequence.
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
Two different sequences can give one permutation. Find the two reasons for this. One happens when the deque is empty. The other happens at the pair XQ.
Hint 2
Fix the choice in both cases, and prove that what is left is unique.
Solution
Show the solution
Plan. State four rules. Show that every run can be rewritten into an admissible one with the same output. Then show that two different admissible sequences give different outputs.
The rules. A sequence is admissible if it satisfies these four conditions.
- It has letters
Xand letters fromS,Q. - In every prefix, the number of
Xis at most the number ofSandQtogether. - If a prefix has as many
XasSandQtogether, and a letter follows it, the letter isQ. - The pair
XQdoes not occur.
Rule 2 says that the deque is not empty when an X comes. Rule 3 says that an insert into the empty deque is written Q, and not S. Rule 4 says that QX is written in place of XQ.
Every obtainable permutation has an admissible sequence. Take any run that gives the permutation. Apply two changes as long as one of them applies.
(a) If an S inserts into the empty deque, replace it by Q. The result is the same, because the left end and the right end of an empty deque are the same place.
(b) If XQ occurs, replace it by QX. The deque is not empty before the X. If it holds a single car , then XQ removes and puts the new car in the empty deque, and QX puts at the right of and removes . The result is the same. If the deque has more cars, the two changes act on different ends, so they do not disturb each other.
Each change (a) lowers the number of S. Each change (b) keeps this number and moves one X to the right. So the changes stop. The output does not change. The final sequence satisfies rules 3 and 4. Rule 1 and rule 2 hold for every run.
Different admissible sequences give different permutations. Take two different admissible sequences and . Cut off the longest common prefix. Both machines then hold the same cars, in the same order, and the next input car is the same car . Inside a deque, two cars leave in their left to right order, because removals take the left end and inserts do not change the order of cars already present. The two sequences have different next letters. Let be the deque after the common prefix.
If is empty, rule 3 gives Q as the next letter in both sequences, since both have letters X and so both continue. So is not empty. Let be its leftmost car.
One is S and the other is Q. In the first run, is left of , and in the second run is right of . So leaves before in the first, and after in the second.
One is S and the other is X. In the run with S, the car is left of , so leaves before . In the run with X, the car leaves now, before has entered. The two outputs order and differently.
One is Q and the other is X. Say has Q and has X. Let continue with a run of X, as long as possible, and let be what remains of after this run. If is empty and goes on, rule 3 asks for Q after an X, and rule 4 forbids that. If ends there, the car never leaves. So is not empty, and the letter after the run of X exists, and it is not Q by rule 4, so it is S. With this S, the car goes to the left of every car of , so leaves before a car . In , the car is right of all cars of , so leaves before . The outputs differ.
In every case the two outputs differ.
Check
For I listed all words over S, Q, X that satisfy the four rules. There are of them. Their outputs are all different, and they form exactly the set of permutations that an output-restricted deque gives. The two sequences in the text, QQXSXSXX and SXQSXSXX, both give . Only the first is admissible.
Remarks
Rules 3 and 4 together say that an X that empties the deque is the last letter of the sequence. This fact makes the count of Exercise 2.2.1-11 simple. The counts agree with the of Exercise 2.2.1-9.
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.