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

Exercise 2.2.4-3

not stated yet in Lean solution written

Rating20
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 2.2.4 of Volume 1

The task

Find what operation (3) does when PTR1\mathrm{PTR}_1 and PTR2\mathrm{PTR}_2 point to nodes of the same circular list.

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

Number the nodes of the list in the order of the links, and let the two pointers be at positions ii and jj.

Hint 2

Treat the case i=ji = j separately.

Solution

Show the solution

Number the nodes x1,…,xnx_1, \dots, x_n around the circle, with LINK(xt)=xt+1\mathrm{LINK}(x_t) = x_{t+1} and LINK(xn)=x1\mathrm{LINK}(x_n) = x_1. Let PTR1=xi\mathrm{PTR}_1 = x_i and PTR2=xj\mathrm{PTR}_2 = x_j.

Operation (3) swaps the two links, then sets PTR1←PTR2\mathrm{PTR}_1 \leftarrow \mathrm{PTR}_2 and PTR2←Λ\mathrm{PTR}_2 \leftarrow \Lambda.

If i=ji = j the swap exchanges a link with itself, so nothing changes in the list, and PTR1←PTR2\mathrm{PTR}_1 \leftarrow \mathrm{PTR}_2 changes nothing. The only effect is PTR2←Λ\mathrm{PTR}_2 \leftarrow \Lambda.

If i≠ji \ne j, the swap sends xix_i to xj+1x_{j+1} and xjx_j to xi+1x_{i+1} (indices modulo nn). Following the new links from xi+1x_{i+1} we get xi+1,xi+2,…,xjx_{i+1}, x_{i+2}, \dots, x_j and then back to xi+1x_{i+1}. This is a circle of the nodes met on the way from PTR1\mathrm{PTR}_1 to PTR2\mathrm{PTR}_2, not counting the first and counting the last. Following the links from xj+1x_{j+1} we get xj+1,…,xix_{j+1}, \dots, x_i and then xj+1x_{j+1} again. It is a second circle with the remaining nodes. So the circle is cut into two circles by cutting it at two places and rejoining the ends crosswise.

The last two assignments make PTR1=xj\mathrm{PTR}_1 = x_j, which is on the first of these circles. So PTR1\mathrm{PTR}_1 now points at a circular list of the nodes that one meets when one follows the links from PTR1\mathrm{PTR}_1 to PTR2\mathrm{PTR}_2. The second circle, xj+1,…,xix_{j+1}, \dots, x_i, can no longer be reached from the pointers, and unless some other variable points into it, its nodes are lost.

Check

With n=6n = 6, i=2i = 2, j=5j = 5 the new circles are x3x4x5x_3 x_4 x_5, which holds PTR1=x5\mathrm{PTR}_1 = x_5, and x6x1x2x_6 x_1 x_2, with 3+3=63 + 3 = 6 nodes. A python run over all n≤8n \le 8 and all i≠ji \ne j confirmed that the circle of PTR1\mathrm{PTR}_1 has (j−i) mod n(j - i) \bmod n nodes and the other one has the rest.

Remarks

So operation (3) is a join for two different lists and a split for one list. For the split the two nodes at the cuts decide the sizes of the parts. Before using it, one has to know that the lists are disjoint.

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.