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.4-18

Exercise 2.2.4-18

not stated yet in Lean solution written

Rating25
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 2.2.4 of Volume 1

The task

Design a way of representing a circular list that can be walked in both directions, with only one link field in each node. The hint says that two pointers to successive nodes xi−1x_{i-1} and xix_i should be enough to find xi+1x_{i+1} and xi−2x_{i-2}.

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

Put into the link of a node a value from which, given one of its two neighbours, one gets the other.

Hint 2

The exclusive or of two bit strings is its own inverse.

Solution

Show the solution

Let LOC(x)\mathrm{LOC}(x) denote the address of a node, and let ⊕\oplus be the exclusive or of the bits of two addresses. In the node xix_i store

LINK(xi)=LOC(xi+1)⊕LOC(xi−1).\mathrm{LINK}(x_i) = \mathrm{LOC}(x_{i+1}) \oplus \mathrm{LOC}(x_{i-1}).

Suppose we know LOC(xi−1)\mathrm{LOC}(x_{i-1}) and LOC(xi)\mathrm{LOC}(x_i). Then

LOC(xi+1)=LINK(xi)⊕LOC(xi−1),\mathrm{LOC}(x_{i+1}) = \mathrm{LINK}(x_i) \oplus \mathrm{LOC}(x_{i-1}),

because a⊕b⊕b=aa \oplus b \oplus b = a. This gives the forward walk: from the pair (xi−1,xi)(x_{i-1}, x_i) we get the pair (xi,xi+1)(x_i, x_{i+1}). The backward walk is symmetric. From the same pair,

LOC(xi−2)=LINK(xi−1)⊕LOC(xi),\mathrm{LOC}(x_{i-2}) = \mathrm{LINK}(x_{i-1}) \oplus \mathrm{LOC}(x_i),

and the pair (xi,xi−1)(x_i, x_{i-1}) gives (xi−1,xi−2)(x_{i-1}, x_{i-2}). So the direction is decided only by the order in which we keep the two pointers.

An operation needs a start, that is, two adjacent nodes. It is convenient to keep two adjacent nodes in the circle as list heads, which are never deleted. Their addresses are known, so one can always start from them, and a list with no items is a circle of those two nodes.

To insert a new node cc between adjacent nodes aa and bb, set LINK(c)←LOC(a)⊕LOC(b)\mathrm{LINK}(c) \leftarrow \mathrm{LOC}(a) \oplus \mathrm{LOC}(b), LINK(a)←LINK(a)⊕LOC(b)⊕LOC(c)\mathrm{LINK}(a) \leftarrow \mathrm{LINK}(a) \oplus \mathrm{LOC}(b) \oplus \mathrm{LOC}(c) and LINK(b)←LINK(b)⊕LOC(a)⊕LOC(c)\mathrm{LINK}(b) \leftarrow \mathrm{LINK}(b) \oplus \mathrm{LOC}(a) \oplus \mathrm{LOC}(c). The deletion of cc between aa and bb is the same two updates of LINK(a)\mathrm{LINK}(a) and LINK(b)\mathrm{LINK}(b), because ⊕\oplus undoes itself, and then cc is freed.

Any operation that can be undone can replace ⊕\oplus. Addition modulo the size of the address space works too: store LOC(xi+1)+LOC(xi−1)\mathrm{LOC}(x_{i+1}) + \mathrm{LOC}(x_{i-1}), and recover LOC(xi+1)\mathrm{LOC}(x_{i+1}) by subtracting LOC(xi−1)\mathrm{LOC}(x_{i-1}).

Check

With 8 random addresses in a circle, the forward walk from two adjacent nodes returned the original order, the backward walk returned the reversed order, and a node inserted between two adjacent nodes appeared in the walk, and disappeared again after the deletion.

Remarks

The cost is that a walk always needs two pointers, and one cannot jump to a node by its address and then move, since the neighbour is unknown. The storage is the same as for a singly linked list.

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.