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 · 1.1-1

Exercise 1.1-1

not stated yet in Lean solution written

Rating10
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Rotate the values of four variables a,b,c,da, b, c, d one place to the left, so that aa gets the old bb, bb gets the old cc, cc gets the old dd and dd gets the old aa, with as few replacements as 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

Look at the swap of two variables in Section 1.1. The extra variable keeps one value that the first replacement would destroy.

Hint 2

A rotation of four values follows a cycle. Save the value that is overwritten first, then let each variable take the value of its neighbour in turn.

Hint 3

A lower bound: look at the first replacement and ask where the old value of the variable that it overwrites can still be found.

Solution

Show the solution

Use one helper variable tt and five replacements: t←a,a←b,b←c,c←d,d←t.t \leftarrow a,\quad a \leftarrow b,\quad b \leftarrow c,\quad c \leftarrow d,\quad d \leftarrow t. After the first replacement tt holds the old aa. The next three replacements copy bb into aa, cc into bb and dd into cc. Each of them reads a variable that no replacement has changed yet, so each reads an old value. The last replacement puts the old aa, saved in tt, into dd. The result is (a,b,c,d)=(b0,c0,d0,a0)(a, b, c, d) = (b_0, c_0, d_0, a_0) where the index 00 means the original value.

Five replacements are also the least number. A replacement copies one value into one variable, because the values are arbitrary and no operation on them is known. Each of the four variables ends with a value different from its original value, so each needs at least one replacement, which gives at least 44. Suppose that exactly 44 replacements do the job. Then each of a,b,c,da, b, c, d is the target of exactly one replacement, and no helper variable exists. Let xx be the target of the first replacement. This replacement destroys the old value of xx, and no other variable holds a copy of it. But the old value of xx is needed in the final value of the variable before xx in the cycle, and that variable is not xx. It is the target of a later replacement, and at that time the old value of xx is gone. This is a contradiction. So at least 55 replacements are needed, and the sequence above is best.

Check

Start with (a,b,c,d)=(1,2,3,4)(a, b, c, d) = (1, 2, 3, 4). After t←at \leftarrow a the state is t=1t = 1. After a←ba \leftarrow b it is a=2a = 2, then b←cb \leftarrow c gives b=3b = 3, c←dc \leftarrow d gives c=4c = 4 and d←td \leftarrow t gives d=1d = 1. The final state is (2,3,4,1)(2, 3, 4, 1), which is the left rotation of (1,2,3,4)(1, 2, 3, 4).

Remarks

The same method rotates kk variables with k+1k + 1 replacements. Exercise 1.1-3 also removes useless replacements, but from an algorithm.

This solution is checked, 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.