taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

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
Contents
  1. Circular lists and polynomial arithmetic

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 Λ\Lambda, 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 Λ\Lambda. 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 LINK(PTR)\mathrm{LINK}(\mathrm{PTR}) 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 Λ\Lambda 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 n≥1n \ge 1 items has nodes N1,…,NnN_1, \ldots, N_n with LINK(Ni)=LOC(Ni+1)\mathrm{LINK}(N_i) = \mathrm{LOC}(N_{i+1}) for i<ni < n and LINK(Nn)=LOC(N1)\mathrm{LINK}(N_n) = \mathrm{LOC}(N_1). The variable PTR holds LOC(Nn)\mathrm{LOC}(N_n). The empty list has PTR=Λ\mathrm{PTR} = \Lambda.

Node: 2.2.4-(1) · Lean: not yet stated

Explanation. The list is read from the left end N1=LINK(PTR)N_1 = \mathrm{LINK}(\mathrm{PTR}) to the right end Nn=PTRN_n = \mathrm{PTR}. For n=1n = 1 the one node links to itself. A loop over the whole list starts at LINK(PTR)\mathrm{LINK}(\mathrm{PTR}) and stops after it has used the node PTR.

Example. Let PTR =130= 130 and let the links be 130→120→100→110→130130 \to 120 \to 100 \to 110 \to 130, with the items ww at 120120, xx at 100100, yy at 110110 and zz at 130130. The list from the left is w,x,y,zw, x, y, z. The left end is LINK(130)=120\mathrm{LINK}(130) = 120.

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 yy at left: take a node PP, INFO(P)←y\mathrm{INFO}(P) \leftarrow y, LINK(P)←LINK(PTR)\mathrm{LINK}(P) \leftarrow \mathrm{LINK}(\mathrm{PTR}), LINK(PTR)←P\mathrm{LINK}(\mathrm{PTR}) \leftarrow P. Insert yy at right: insert at left, then PTR←P\mathrm{PTR} \leftarrow P. Delete at left into yy: P←LINK(PTR)P \leftarrow \mathrm{LINK}(\mathrm{PTR}), y←INFO(P)y \leftarrow \mathrm{INFO}(P), LINK(PTR)←LINK(P)\mathrm{LINK}(\mathrm{PTR}) \leftarrow \mathrm{LINK}(P), return PP. For PTR=Λ\mathrm{PTR} = \Lambda the insertion sets PTR←LINK(P)←P\mathrm{PTR} \leftarrow \mathrm{LINK}(P) \leftarrow P and the deletion signals UNDERFLOW. After a deletion, if P=PTRP = \mathrm{PTR}, the list was a single node, and the program sets PTR←Λ\mathrm{PTR} \leftarrow \Lambda.

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 PP, 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 100,110,120,130100, 110, 120, 130 in this order. Freed nodes are not used again in this run.

Operation PTR List from the left Links
start Λ\Lambda empty none
insert xx at right 100 xx 100→100100 \to 100
insert yy at right 110 x,yx, y 100→110→100100 \to 110 \to 100
insert ww at left 110 w,x,yw, x, y 110→120→100→110110 \to 120 \to 100 \to 110
insert zz at right 130 w,x,y,zw, x, y, z 130→120→100→110→130130 \to 120 \to 100 \to 110 \to 130
delete at left, y=wy = w 130 x,y,zx, y, z 130→100→110→130130 \to 100 \to 110 \to 130
delete at left, y=xy = x 130 y,zy, z 130→110→130130 \to 110 \to 130
delete at left, y=yy = y 130 zz 130→130130 \to 130
delete at left, y=zy = z Λ\Lambda empty none
delete at left Λ\Lambda empty UNDERFLOW

In the fourth row, the node ww is put between PTR =110= 110 and the old left end 100100, and PTR does not change. In the last deletion with data, P=130=PTRP = 130 = \mathrm{PTR}, so the list had one node and PTR becomes Λ\Lambda. 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 PTR≠Λ\mathrm{PTR} \ne \Lambda, interchange the values of AVAIL and LINK(PTR)\mathrm{LINK}(\mathrm{PTR}). Then set PTR←Λ\mathrm{PTR} \leftarrow \Lambda. The interchange is P←AVAILP \leftarrow \mathrm{AVAIL}, AVAIL←LINK(PTR)\mathrm{AVAIL} \leftarrow \mathrm{LINK}(\mathrm{PTR}), LINK(PTR)←P\mathrm{LINK}(\mathrm{PTR}) \leftarrow P.

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 a,b,ca, b, c with PTR at the node of cc, and let the free chain be f→g→Λf \to g \to \Lambda with AVAIL at ff. After the interchange, AVAIL=a\mathrm{AVAIL} = a, LINK(c)=f\mathrm{LINK}(c) = f, and the free chain from AVAIL is a→b→c→f→ga \to b \to c \to f \to g. The check by a program gave this chain.

Cost. In MIX, with LINK=(0:2)\mathrm{LINK} = (0{:}2) and the links of AVAIL and PTR in their (4:5)(4{:}5) fields, the code LD1 PTR(4:5), J1Z DONE, LDA AVAIL, LDX 0,1(LINK), STA 0,1(LINK), STX AVAIL, STZ PTR takes 2+1+2+2+2+2+2=13u2 + 1 + 2 + 2 + 2 + 2 + 2 = 13u for a nonempty list, however long.

Definition C2.2.2.4.4 (join two circular lists, TAOCP eq. 2.2.4(3)). Let PTR1\mathrm{PTR}_1 and PTR2\mathrm{PTR}_2 point to two disjoint circular lists L1L_1 and L2L_2. If PTR2≠Λ\mathrm{PTR}_2 \ne \Lambda: if PTR1≠Λ\mathrm{PTR}_1 \ne \Lambda, interchange LINK(PTR1)\mathrm{LINK}(\mathrm{PTR}_1) and LINK(PTR2)\mathrm{LINK}(\mathrm{PTR}_2); then PTR1←PTR2\mathrm{PTR}_1 \leftarrow \mathrm{PTR}_2 and PTR2←Λ\mathrm{PTR}_2 \leftarrow \Lambda. The list L2L_2 is then at the right of L1L_1.

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 L1L_1 to the left end of L2L_2, and the right end of L2L_2 to the left end of L1L_1. 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 L1=a,b,cL_1 = a, b, c with PTR1\mathrm{PTR}_1 at cc and L2=d,eL_2 = d, e with PTR2\mathrm{PTR}_2 at ee. Before: LINK(c)=a\mathrm{LINK}(c) = a and LINK(e)=d\mathrm{LINK}(e) = d. After the swap: LINK(c)=d\mathrm{LINK}(c) = d and LINK(e)=a\mathrm{LINK}(e) = a. Walking from aa gives a,b,c,d,ea, b, c, d, e and then aa again, and PTR1\mathrm{PTR}_1 is at ee. For the split, start from the circle a,b,c,d,ea, b, c, d, e with PTR1\mathrm{PTR}_1 at cc and PTR2\mathrm{PTR}_2 at ee. The same swap gives the circles a,b,ca, b, c and d,ed, e. In MIX the join of two nonempty lists takes 2+1+2+1+2+2+2+2+2+2=18u2 + 1 + 2 + 1 + 2 + 2 + 2 + 2 + 2 + 2 = 18u 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 nn items has n+1n + 1 nodes: the head HH and the items N1,…,NnN_1, \ldots, N_n, with LINK(H)=LOC(N1)\mathrm{LINK}(H) = \mathrm{LOC}(N_1) and LINK(Nn)=LOC(H)\mathrm{LINK}(N_n) = \mathrm{LOC}(H). The list is empty when LINK(H)=LOC(H)\mathrm{LINK}(H) = \mathrm{LOC}(H).

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 HH. 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 H=40H = 40 and the items p,qp, q be at 6060 and 8080. The links are 40→60→80→4040 \to 60 \to 80 \to 40. Insert rr at the left: take a node at 9090, LINK(90)←LINK(40)=60\mathrm{LINK}(90) \leftarrow \mathrm{LINK}(40) = 60, LINK(40)←90\mathrm{LINK}(40) \leftarrow 90. The links are 40→90→60→80→4040 \to 90 \to 60 \to 80 \to 40, and the list is r,p,qr, p, q. For the empty list LINK(40)=40\mathrm{LINK}(40) = 40, and the same two assignments give 40→90→4040 \to 90 \to 40, 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 xx, yy, zz with integer coefficients is a circular list of nodes. A node has a coefficient COEF, a key ABC, and a link LINK. A term c xAyBzCc\,x^A y^B z^C has COEF =c≠0= c \ne 0 and ABC=Ab2+Bb+C\mathrm{ABC} = A b^2 + B b + C, with 0≤A,B,C<b0 \le A, B, C < b. Along the links the keys of the terms decrease. The last node is the sentinel with ABC=−1\mathrm{ABC} = -1 and COEF=0\mathrm{COEF} = 0, 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 (A,B,C)(A, B, C) read as words, since each of A,B,CA, B, C is less than bb. 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 b=100b = 100. The polynomial x3−3x2y+4y2+5x^3 - 3x^2y + 4y^2 + 5 has the terms with the keys 300300, 210210, 2020, and 00 and the coefficients 11, −3-3, 44, 55. The list is: sentinel →(300,1)→(210,−3)→(20,4)→(0,5)→\to (300, 1) \to (210, -3) \to (20, 4) \to (0, 5) \to sentinel. The zero polynomial is the sentinel with a link to itself.

Algorithms and programs

Algorithm 2.2.4A (add polynomial PP to polynomial QQ, TAOCP Algorithm 2.2.4A). The input is two polynomials in the form above, named by pointers PP and QQ to their sentinels. The polynomial PP does not change. The polynomial QQ is replaced by the sum Q+PQ + P. Pointers Q1Q_1 and Q2Q_2 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 PP with the key of the current term of QQ. A larger QQ term is passed. An equal key means that the coefficients are added, and a zero sum removes the node. A larger PP term is a new term, and a node for it is put in QQ. The pointer Q1Q_1 stays one node behind QQ, 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 PdoneP_{\text{done}} be the terms of PP that the algorithm has passed, let QorigQ_{\text{orig}} be the original QQ, and let S=Pdone+QorigS = P_{\text{done}} + Q_{\text{orig}}. At each entry to A2: (i) Q=LINK(Q1)Q = \mathrm{LINK}(Q_1); (ii) the nodes of QQ from the first term to Q1Q_1 are the nonzero terms of SS whose key is larger than ABC(Q)\mathrm{ABC}(Q), in decreasing order; (iii) the nodes from QQ to the sentinel are nodes of QorigQ_{\text{orig}} that the algorithm did not change.

Why it holds. At the start PdoneP_{\text{done}} is empty, the first part of (ii) is empty, and QQ is the first term. An advance in A2 moves the node QQ to the first part, and the terms of SS above the new QQ are those above the old QQ and the old QQ 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 SS is zero. A5 puts the term of PP, which is above ABC(Q)\mathrm{ABC}(Q), at the end of the first part. In every case the passed PP term has a key at least as large as ABC(Q)\mathrm{ABC}(Q) at the time, and QQ only moves toward smaller keys, so the passed terms of SS with keys above the new QQ are all in the first part. The loop stops when the keys of both sentinels meet, both −1-1. Then Pdone=PP_{\text{done}} = P, and the first part holds all terms of S=P+QorigS = P + Q_{\text{orig}}.

Run. Let b=100b = 100. Let P=3x2y+2xz−3P = 3x^2y + 2xz - 3 with the keys 210,101,0210, 101, 0 and let Q=x3−3x2y+4y2+5Q = x^3 - 3x^2y + 4y^2 + 5 with the keys 300,210,20,0300, 210, 20, 0. The table shows each pass of A2, the terms compared, and the list QQ after the step that follows. A term is written as (key, coefficient).

A2 pass ABC(P)\mathrm{ABC}(P) ABC(Q)\mathrm{ABC}(Q) Action List QQ after the action
1 210 300 P<QP < Q, advance QQ (300,1),(210,−3),(20,4),(0,5)(300,1), (210,-3), (20,4), (0,5)
2 210 210 equal, 3+(−3)=03 + (-3) = 0, A4 deletes (300,1),(20,4),(0,5)(300,1), (20,4), (0,5)
3 101 20 P>QP > Q, A5 inserts (300,1),(101,2),(20,4),(0,5)(300,1), (101,2), (20,4), (0,5)
4 0 20 P<QP < Q, advance QQ (300,1),(101,2),(20,4),(0,5)(300,1), (101,2), (20,4), (0,5)
5 0 0 equal, −3+5=2-3 + 5 = 2, A3 keeps (300,1),(101,2),(20,4),(0,2)(300,1), (101,2), (20,4), (0,2)
6 −1-1 −1-1 equal, sentinels, stop (300,1),(101,2),(20,4),(0,2)(300,1), (101,2), (20,4), (0,2)

The sum is x3+2xz+4y2+2x^3 + 2xz + 4y^2 + 2. In pass 2 the deleted node returns to AVAIL and Q1Q_1 stays at the node 300300. In pass 3, Q1Q_1 is the node 300300, and the new node goes between it and the node 2020. The run was checked with a program that follows the links.

Cost. Let m′m' be the number of keys that match and whose sum is zero, m′′m'' the number that match and whose sum is not zero, p′p' the number of terms of PP whose key is not in QQ, and q′q' the number of terms of QQ whose key is not in PP. Then PP has p=m′+m′′+p′p = m' + m'' + p' terms and QQ has q=m′+m′′+q′q = m' + m'' + q' terms. Each pass of A2 either advances QQ or consumes a term of PP, and the sentinel pass consumes the last one. The passes of A2 number p+q′+1p + q' + 1. A3 runs m′+m′′+1m' + m'' + 1 times, A4 runs m′m' times and A5 runs p′p' times. The result has q+p′−m′q + p' - m' terms. In the run above, p=3p = 3, q=4q = 4, m′=1m' = 1, m′′=1m'' = 1, p′=1p' = 1, q′=2q' = 2, so A2 runs 3+2+1=63 + 2 + 1 = 6 times, A3 runs 33 times, A4 once, A5 once, and the result has 4+1−1=44 + 1 - 1 = 4 terms. All these counts agree with the table.

Algorithm 2.2.4M (multiply, TAOCP Algorithm 2.2.4M). The input is three polynomials MM, PP, QQ. The algorithm replaces QQ by Q+M×PQ + M \times P.

Node: Algorithm 2.2.4M · Lean: not yet stated

The idea. The product M×PM \times P is the sum, over the terms c xaybzcc\,x^a y^b z^c of MM, of PP with each key raised by the key of the term and each coefficient multiplied by cc. Each such partial product is added to QQ by Algorithm A, with two changes: the key of a term of PP is ABC(P)+ABC(M)\mathrm{ABC}(P) + \mathrm{ABC}(M) and the coefficient is COEF(P)⋅COEF(M)\mathrm{COEF}(P) \cdot \mathrm{COEF}(M), except that the sentinel of PP keeps the key −1-1.

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 tt terms of MM, QQ is QorigQ_{\text{orig}} plus the product of PP with the sum of these tt terms. The exponent sums must stay below bb, 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 b=100b = 100. Let M=x+yM = x + y with the keys 100,10100, 10, let P=x2−xy+y2P = x^2 - xy + y^2 with the keys 200,110,20200, 110, 20, and let Q=0Q = 0. The table shows QQ after each cycle.

Cycle Term of MM Terms added QQ after the cycle Cancelled
1 xx (key 100) x3,−x2y,xy2x^3, -x^2y, xy^2 x3−x2y+xy2x^3 - x^2y + xy^2 0
2 yy (key 10) x2y,−xy2,y3x^2y, -xy^2, y^3 x3+y3x^3 + y^3 2

In cycle 2 the pass of Algorithm A meets the keys 210210 and 120120 with the sums −1+1=0-1 + 1 = 0 and 1−1=01 - 1 = 0, so two nodes return to AVAIL, and the new term y3y^3 is inserted. The final QQ is x3+y3x^3 + y^3, which is the product (x+y)(x2−xy+y2)(x + y)(x^2 - xy + y^2). 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 PP and the current QQ. If MM has μ\mu terms, the time is at most μ\mu runs of A, each with at most p+qmax⁡+1p + q_{\max} + 1 passes of A2, where qmax⁡q_{\max} is the largest size of QQ. The algorithm never copies PP. The values ABC(M)+ABC(P)ABC(M) + ABC(P) 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 p+q′+1p + q' + 1, m′+m′′+1m' + m'' + 1, m′m' and p′p'. So the time is linear in p+qp + q. The storage in use at the end is q+p′−m′+1q + p' - m' + 1 nodes of QQ, counting the sentinel.

Node: 2.2.4-anal-A · Lean: not yet stated

Explanation. Algorithm A visits each node of PP once and each node of QQ 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 p=3p = 3 and q=4q = 4 with m′=1m' = 1, m′′=1m'' = 1, the total is 3+2+1=63 + 2 + 1 = 6 passes of A2, as in the run. If PP and QQ are two separate copies of one polynomial, then each key matches and no sum is zero, so m′′=p=qm'' = p = q, p′=q′=0p' = q' = 0, and A2 runs p+1p + 1 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 −1-1 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 Λ\Lambda. 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.