Volume C2 · Information structures · TAOCP Section 2.2.4
Circular lists
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 2.2.4 of the book. Study the section: what to read first and the exercises, easiest first. The items of the section and their status.
Read first
- C2.2.3, in C2. Information structures not written yet
- C1.3.1, in C1. Basic concepts not written yet
Circular lists and polynomial arithmetic
Overview
A circular list is a linked list in which the last node points to the first node. No node has the link , so a program has no natural place to stop, and it must stop when it comes back to its starting node. In exchange, the list has no first or last node in the structure, and a pointer to any node reaches all the others. Section 2.2.4 of TAOCP uses this for two things. A circular list with a pointer to its last node is a queue with one pointer, and it can be erased or joined to another circular list in constant time. A circular list with a sentinel node is the right form for a polynomial: the sentinel ends every pass and removes the case of the empty polynomial.
The ideas
A circle has no end. In a circular list the link of the last node is the address of the first node. A loop that walks along the links never meets . The loop must test for the node where it started, or for a special node.
A pointer to the last node gives both ends. Let PTR be the address of the last node. Then is the first node. Insertion at the right end is insertion at the left end followed by a move of PTR by one node. So one pointer does the work of the two pointers F and R of a linear queue.
The empty list needs its own value. If PTR is for the empty list, each of the three operations needs one added test. Without the test, the deletion of the last node leaves PTR on a free node.
Erase and join are constant time. The swap of two link values merges two circles into one, or cuts one circle into two. An erase puts a whole circle on the free-node stack with one swap, and a join of two lists is one swap. Neither depends on the length.
A list head replaces the test for the empty list. A special node, the list head, is in each list. The list is empty when the head links to itself. The price is that the right end is no longer one step away.
A sentinel ends each pass over a polynomial. A polynomial is a circular list of terms in decreasing order, with a sentinel node whose key is smaller than any term. Two polynomials end at the same moment: when the two sentinels meet. A pass of Algorithm A over the two lists needs no test for the end of a list.
Definitions, results and proofs
Definition C2.2.2.4.1 (a circular list with a pointer to the last node, TAOCP eq. 2.2.4(1)). A circular list of items has nodes with for and . The variable PTR holds . The empty list has .
Node: 2.2.4-(1) · Lean: not yet stated
Explanation. The list is read from the left end to the right end . For the one node links to itself. A loop over the whole list starts at and stops after it has used the node PTR.
Example. Let PTR and let the links be , with the items at , at , at and at . The list from the left is . The left end is .
Definition C2.2.2.4.2 (insert at left, insert at right, delete at left, TAOCP Section 2.2.4, operations a, b, c). Let the list be nonempty. Insert at left: take a node , , , . Insert at right: insert at left, then . Delete at left into : , , , return . For the insertion sets and the deletion signals UNDERFLOW. After a deletion, if , the list was a single node, and the program sets .
Node: 2.2.4-ops-abc · Lean: not yet stated
Explanation. The new node is put between PTR and the old left end. If the program then moves PTR to , the node becomes the right end. This is the whole difference between the left insertion and the right insertion. The three operations together give the output-restricted deque of Section 2.2.1: left insertion and left deletion give a stack, and right insertion and left deletion give a queue.
Example. Start with the empty list. The table shows the state after each operation. The nodes are taken from the addresses in this order. Freed nodes are not used again in this run.
| Operation | PTR | List from the left | Links |
|---|---|---|---|
| start | empty | none | |
| insert at right | 100 | ||
| insert at right | 110 | ||
| insert at left | 110 | ||
| insert at right | 130 | ||
| delete at left, | 130 | ||
| delete at left, | 130 | ||
| delete at left, | 130 | ||
| delete at left, | empty | none | |
| delete at left | empty | UNDERFLOW |
In the fourth row, the node is put between PTR and the old left end , and PTR does not change. In the last deletion with data, , so the list had one node and PTR becomes . A program without this step would leave PTR on a node that is on the free list.
Definition C2.2.2.4.3 (erase a circular list, TAOCP eq. 2.2.4(2)). If , interchange the values of AVAIL and . Then set . The interchange is , , .
Node: 2.2.4-(2) · Lean: not yet stated
Explanation. After the interchange, AVAIL names the old left end. The chain from AVAIL goes round the circle to PTR, and the link of PTR then leads to the old free chain. So all nodes of the circle are on the free chain. The interchange is valid for PTR at any node of the circle, since the circle is closed.
Example. Let the list be with PTR at the node of , and let the free chain be with AVAIL at . After the interchange, , , and the free chain from AVAIL is . The check by a program gave this chain.
Cost. In MIX, with and the links of AVAIL and PTR in their fields, the code LD1 PTR(4:5), J1Z DONE, LDA AVAIL, LDX 0,1(LINK), STA 0,1(LINK), STX AVAIL, STZ PTR takes for a nonempty list, however long.
Definition C2.2.2.4.4 (join two circular lists, TAOCP eq. 2.2.4(3)). Let and point to two disjoint circular lists and . If : if , interchange and ; then and . The list is then at the right of .
Node: 2.2.4-(3) · Lean: not yet stated
Explanation. Each circle has one link that comes back to its left end. The swap sends the right end of to the left end of , and the right end of to the left end of . The two circles become one circle. The same swap on two nodes of one circle cuts it into two circles. So the swap is also the split of a circular list.
Example. Let with at and with at . Before: and . After the swap: and . Walking from gives and then again, and is at . For the split, start from the circle with at and at . The same swap gives the circles and . In MIX the join of two nonempty lists takes with the code LD2 PTR2(4:5), J2Z DONE, LD1 PTR1(4:5), J1Z MOVE, LDA 0,1(LINK), LDX 0,2(LINK), STX 0,1(LINK), STA 0,2(LINK), and MOVE ST2 PTR1(4:5), STZ PTR2.
Definition C2.2.2.4.5 (a circular list with a list head, TAOCP eq. 2.2.4(4)). A list head is a node that belongs to the list but holds no item. The list with items has nodes: the head and the items , with and . The list is empty when .
Node: 2.2.4-(4) · Lean: not yet stated
Explanation. The head is at a fixed address, so the program needs no variable to find the list. The loop over the list stops when the link leads back to . The list is never empty as a structure, so the operations need no case for the empty list. The right end is not known without a walk, so insertion at the right is no longer a constant-time operation. Exercise 4 of the section, in X1 as Exercise 2.2.4-4, asks for the stack operations in this form.
Example. Let and the items be at and . The links are . Insert at the left: take a node at , , . The links are , and the list is . For the empty list , and the same two assignments give , with no special case.
Definition C2.2.2.4.6 (the node of a polynomial term, TAOCP eq. 2.2.4(5)). A polynomial in , , with integer coefficients is a circular list of nodes. A node has a coefficient COEF, a key ABC, and a link LINK. A term has COEF and , with . Along the links the keys of the terms decrease. The last node is the sentinel with and , and its link goes to the first term. The polynomial is named by a pointer to the sentinel.
Node: 2.2.4-(5) · Lean: not yet stated
Explanation. The numeric order of ABC is the order of the triples read as words, since each of is less than . The sentinel has a key lower than every term. The zero polynomial is the sentinel alone, linked to itself. The sentinel also serves as the head of the circular list, so no polynomial is empty as a structure.
Example. Let . The polynomial has the terms with the keys , , , and and the coefficients , , , . The list is: sentinel sentinel. The zero polynomial is the sentinel with a link to itself.
Algorithms and programs
Algorithm 2.2.4A (add polynomial to polynomial , TAOCP Algorithm 2.2.4A). The input is two polynomials in the form above, named by pointers and to their sentinels. The polynomial does not change. The polynomial is replaced by the sum . Pointers and are used inside the algorithm.
Node: Algorithm 2.2.4A · Lean: not yet stated
The idea. Both lists are in decreasing order of key. The algorithm walks down both lists at once, as in the merge of two sorted lists. It compares the key of the current term of with the key of the current term of . A larger term is passed. An equal key means that the coefficients are added, and a zero sum removes the node. A larger term is a new term, and a node for it is put in . The pointer stays one node behind , because a singly linked list can change a link only when the node before is known.
The steps.
A1: P := LINK(P); Q1 := Q; Q := LINK(Q)
A2: if ABC(P) < ABC(Q): Q1 := Q; Q := LINK(Q); repeat A2
if ABC(P) = ABC(Q): go to A3
if ABC(P) > ABC(Q): go to A5
A3: if ABC(P) < 0: stop (the pointers P and Q go back to their sentinels)
COEF(Q) := COEF(Q) + COEF(P)
if COEF(Q) = 0: go to A4
P := LINK(P); Q1 := Q; Q := LINK(Q); go to A2
A4: Q2 := Q; Q := LINK(Q); LINK(Q1) := Q; return Q2 to AVAIL
P := LINK(P); go to A2
A5: take a node Q2 from AVAIL; COEF(Q2) := COEF(P); ABC(Q2) := ABC(P)
LINK(Q2) := Q; LINK(Q1) := Q2; Q1 := Q2
P := LINK(P); go to A2
Invariant. Let be the terms of that the algorithm has passed, let be the original , and let . At each entry to A2: (i) ; (ii) the nodes of from the first term to are the nonzero terms of whose key is larger than , in decreasing order; (iii) the nodes from to the sentinel are nodes of that the algorithm did not change.
Why it holds. At the start is empty, the first part of (ii) is empty, and is the first term. An advance in A2 moves the node to the first part, and the terms of above the new are those above the old and the old itself. A match in A3 adds two coefficients. If the sum is not zero, the node joins the first part. If it is zero, A4 drops the node, which is right since the term of is zero. A5 puts the term of , which is above , at the end of the first part. In every case the passed term has a key at least as large as at the time, and only moves toward smaller keys, so the passed terms of with keys above the new are all in the first part. The loop stops when the keys of both sentinels meet, both . Then , and the first part holds all terms of .
Run. Let . Let with the keys and let with the keys . The table shows each pass of A2, the terms compared, and the list after the step that follows. A term is written as (key, coefficient).
| A2 pass | Action | List after the action | ||
|---|---|---|---|---|
| 1 | 210 | 300 | , advance | |
| 2 | 210 | 210 | equal, , A4 deletes | |
| 3 | 101 | 20 | , A5 inserts | |
| 4 | 0 | 20 | , advance | |
| 5 | 0 | 0 | equal, , A3 keeps | |
| 6 | equal, sentinels, stop |
The sum is . In pass 2 the deleted node returns to AVAIL and stays at the node . In pass 3, is the node , and the new node goes between it and the node . The run was checked with a program that follows the links.
Cost. Let be the number of keys that match and whose sum is zero, the number that match and whose sum is not zero, the number of terms of whose key is not in , and the number of terms of whose key is not in . Then has terms and has terms. Each pass of A2 either advances or consumes a term of , and the sentinel pass consumes the last one. The passes of A2 number . A3 runs times, A4 runs times and A5 runs times. The result has terms. In the run above, , , , , , , so A2 runs times, A3 runs times, A4 once, A5 once, and the result has terms. All these counts agree with the table.
Algorithm 2.2.4M (multiply, TAOCP Algorithm 2.2.4M). The input is three polynomials , , . The algorithm replaces by .
Node: Algorithm 2.2.4M · Lean: not yet stated
The idea. The product is the sum, over the terms of , of with each key raised by the key of the term and each coefficient multiplied by . Each such partial product is added to by Algorithm A, with two changes: the key of a term of is and the coefficient is , except that the sentinel of keeps the key .
The steps.
M1: M := LINK(M); if ABC(M) < 0 stop
M2: run Algorithm A on Q and P, with
ABC(P) read as: -1 if ABC(P) < 0, else ABC(P) + ABC(M)
COEF(P) read as: COEF(P) * COEF(M)
go to M1
Invariant. After the cycles for the first terms of , is plus the product of with the sum of these terms. The exponent sums must stay below , so that the key of a product term is the sum of two keys without a carry from one field to the next.
Run. Let . Let with the keys , let with the keys , and let . The table shows after each cycle.
| Cycle | Term of | Terms added | after the cycle | Cancelled |
|---|---|---|---|---|
| 1 | (key 100) | 0 | ||
| 2 | (key 10) | 2 |
In cycle 2 the pass of Algorithm A meets the keys and with the sums and , so two nodes return to AVAIL, and the new term is inserted. The final is , which is the product . The cycle counts of A2, A3, A4, A5 and the final lists were checked with a program that runs the algorithm on linked nodes.
Cost. A cycle of M is one run of Algorithm A with the polynomial and the current . If has terms, the time is at most runs of A, each with at most passes of A2, where is the largest size of . The algorithm never copies . The values and the product of coefficients are computed in the passes.
Analysis C2.2.2.4.7 (the cost of Algorithm A, TAOCP Section 2.2.4). The time of Algorithm A is a sum of fixed amounts for the passes of A2, the runs of A3, A4 and A5, and a fixed amount for the start. The numbers of passes are , , and . So the time is linear in . The storage in use at the end is nodes of , counting the sentinel.
Node: 2.2.4-anal-A · Lean: not yet stated
Explanation. Algorithm A visits each node of once and each node of at most once. It does not loop round either circle twice. The count of passes of A2 is exact, and the time of a MIX program is the sum of the passes times the cost of each instruction group. The sentinel is needed so that no pass tests for the end of a list.
Example. For and with , , the total is passes of A2, as in the run. If and are two separate copies of one polynomial, then each key matches and no sum is zero, so , , and A2 runs times.
The exercises
The table has the 18 exercises of Section 2.2.4. The Kind column is the kind that X1 gives to its solution.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 2.2.4-1 | 21 | K2 | not yet stated | X1, Exercise 2.2.4-1 | |
| 2.2.4-2 | 20 | K8 | not yet stated | X1, Exercise 2.2.4-2 | |
| 2.2.4-3 | 20 | K3 | not yet stated | X1, Exercise 2.2.4-3 | |
| 2.2.4-4 | 20 | K5 | not yet stated | X1, Exercise 2.2.4-4 | |
| 2.2.4-5 | 21 | K5 | not yet stated | X1, Exercise 2.2.4-5 | |
| 2.2.4-6 | 18 | K8 | not yet stated | X1, Exercise 2.2.4-6 | |
| 2.2.4-7 | 10 | informal | not yet stated | X1, Exercise 2.2.4-7 | |
| 2.2.4-8 | 10 | informal | not yet stated | X1, Exercise 2.2.4-8 | |
| 2.2.4-9 | 23 | K2 | not yet stated | X1, Exercise 2.2.4-9 | |
| 2.2.4-10 | 20 | K3 | not yet stated | X1, Exercise 2.2.4-10 | |
| 2.2.4-11 | 24 | K6 | not yet stated | X1, Exercise 2.2.4-11 | |
| 2.2.4-12 | 21 | K7 | not yet stated | X1, Exercise 2.2.4-12 | |
| 2.2.4-13 | 20 | K6 | not yet stated | X1, Exercise 2.2.4-13 | |
| 2.2.4-14 | 22 | K6 | not yet stated | X1, Exercise 2.2.4-14 | |
| 2.2.4-15 | 24 | K6 | not yet stated | X1, Exercise 2.2.4-15 | |
| 2.2.4-16 | 28 | M | K4 | not yet stated | X1, Exercise 2.2.4-16 |
| 2.2.4-17 | 22 | informal | not yet stated | X1, Exercise 2.2.4-17 | |
| 2.2.4-18 | 25 | K5 | not yet stated | X1, Exercise 2.2.4-18 |
Exercises 1 to 5 and 18 are on the circular list: the empty list, the join of two lists (the split of one circle in the join example is the case of exercise 3), the stack with a list head, reversal, and a list that can be walked both ways with one link. Exercises 6 to 10 and 17 ask why the representation of polynomials has its sentinel and its order, and the invariant above gives the reason in exercises 7 and 8. Exercises 11 to 16 build a small package of MIX subroutines for polynomials, with the time of the product in exercise 16. Do 11, 13 and 14 first, then 15 and 16.
Lean
The repository tamnd/taocp-lean has no declaration for Chapter 2, so nothing here is stated in Lean. A model of a circular list needs a finite set of nodes and a link function that is a cyclic permutation of the nodes of the list. The join and the split are then the product of that permutation with a transposition of two nodes. No Mathlib name is claimed here.
Notes and history
The book treats the polynomial package as an example of linked allocation with many lists in memory at the same time, and it gives the polynomial algorithms as a base for the exercises. The book also gives a MIX program for Algorithm A with its running time in terms of four counts. This chapter gives only the exact counts of the passes, which are enough to compute that time for any coding. The sentinel with key is a list head that also serves as the stop signal of every pass.
Section 2.2.4 of TAOCP changes one thing in the linked list of Section 2.2.3: the last node links back to the first node, not to . A program can then start at any node and reach all nodes. A pointer to the last node gives access to both ends of the list. Whole lists can be erased or joined with a few assignments, whatever their length.
The chapter has one file. It shows the three basic operations on a circular list with a pointer to the last node, including the empty list, and the erase and the join in constant time. It shows the list head as the other way to avoid the empty list. It then uses circular lists with a sentinel for polynomials in three variables: Algorithm A adds two polynomials in one pass, and Algorithm M multiplies by repeated use of Algorithm A. Each algorithm has an invariant, a hand run on a small input, and its cost.
Read Section 2.2.3 first. The exercises of this section have files in X1, and the exercise table links to them.
The source of this page. Something is wrong? Report it.