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

Exercise 2.2.4-13

not stated yet in Lean solution written

Rating20
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 2.2.4 of Volume 1

The task

Write a MIX subroutine ERASE, called by JMP ERASE with rI1 =P= P, that returns all nodes of polynomial(P)\mathrm{polynomial}(P) to the AVAIL 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

The polynomial is a circular list. Operation (2) of the text gives the way to free a whole circular list in constant time.

Solution

Show the solution

The nodes form a circle P,a1,…,akP, a_1, \dots, a_k with PP the sentinel. Let the AVAIL list be v1,v2,…v_1, v_2, \dots. We set AVAIL←a1\mathrm{AVAIL} \leftarrow a_1, which is LINK(P)\mathrm{LINK}(P), and LINK(P)←v1\mathrm{LINK}(P) \leftarrow v_1. Now the old circle has been opened at the sentinel, and AVAIL\mathrm{AVAIL} runs through a1,…,ak,P,v1,…a_1, \dots, a_k, P, v_1, \dots.

ERASE STJ  EEXIT
      LDA  AVAIL        rA <- old top of AVAIL.
      LDX  1,1(LINK)    rX <- first node of the polynomial.
      STX  AVAIL
      STA  1,1(LINK)    The sentinel now points to the old top.
EEXIT JMP  *

The time is 1212 units, with the call, whatever the number of terms.

Check

I applied the subroutine in the simulator to random polynomials. After the call the AVAIL list had grown by p+1p + 1 nodes, and the first p+1p + 1 nodes were those of the polynomial. The time was 1212 units each time. For a polynomial that is 00, the list is the sentinel alone, and the code works as well, since then LINK(P)=P\mathrm{LINK}(P) = P.

Remarks

The sequence of the exercise statement, LD1 Q, JMP ERASE, LD1 P, JMP COPY, ST2 Q, therefore sets polynomial(Q)←polynomial(P)\mathrm{polynomial}(Q) \leftarrow \mathrm{polynomial}(P) in 22p+4622p + 46 units, where the sum counts the three loads and stores of 22 units each, the call of ERASE with 1212, and the call of COPY with 22p+2822p + 28.

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.