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.1

Introduction to information structures

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.1 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
Contents
  1. Nodes, fields and links

Overview

Section 2.1 of TAOCP says what Chapter 2 is about: the tables of information that a program keeps, and the relations between the items of a table. A table can be a plain list, a grid, a tree or a net of many links. The chapter studies how to store such a table in the memory of a machine, and how to build, change and read it with few operations. The section gives the vocabulary (node, field, link, null link, link variable) and the notation for fields. It uses MIX, the machine of Section 1.3.1, and its assembly language of Section 1.3.2, both explained in C1. A reader who skips the MIX programs of the chapter should still study how each structure sits in memory, since that is the content.

The ideas

A table is a set of nodes. A node is a group of consecutive words of memory. It is cut into named parts, the fields. In the simplest case a node is one word with one field. In the example of the section, a node stands for one playing card and takes two words. The first word holds the fields NEXT, TAG, SUIT and RANK. The second word holds a five-character name, TITLE. This chapter uses a layout of its own for the first word, so that the reader does not mix it with another one. The field NEXT takes the sign and the first two bytes, the field TAG takes byte 3, the field SUIT takes byte 4 and the field RANK takes byte 5.

Word Sign and byte 1, 2 Byte 3 Byte 4 Byte 5
first word NEXT, field (0:2) TAG, field (3:3) SUIT, field (4:4) RANK, field (5:5)
second word TITLE, field (1:5)

The address of a node is the address of its first word. The address is also called a link, a pointer or a reference. A link can be stored in a field of another node. This is the main idea of the chapter: a node does not need to lie next to the node that follows it, since it can hold the address of that node. The special value Λ\Lambda, the null link, means "no node". In MIX it is the number 00, because no node is ever placed at address 00.

A pile of cards shows the idea. Let a pile of three cards be stored at addresses 1204, 905 and 2100. The card at 1204 is the top of the pile. Each card holds the address of the card below it. Here TAG =1=1 means face down and TAG =0=0 means face up. SUIT is 1, 2, 3 or 4 for clubs, diamonds, hearts or spades. RANK is 1 to 13 for ace to king.

Address NEXT TAG SUIT RANK TITLE The card
1204 905 1 3 7 7 H seven of hearts, face down, the top
905 2100 0 4 12 12 S queen of spades, face up
2100 0 0 1 2 2 C two of clubs, face up, the bottom

The variable TOP holds 12041204. The number 00 in the last row is Λ\Lambda. The same pile as a picture of arrows, where the memory addresses disappear because the algorithms do not need them:

TOP --> [ 7 H | face down ] --> [ 12 S | face up ] --> [ 2 C | face up ] --> Λ
          at 1204                at 905                 at 2100

A field is read through a link. The notation F(L)\mathrm{F}(L) means the field F of the node at address LL. With the pile above, RANK(905)=12\mathrm{RANK}(905) = 12, SUIT(TOP)=3\mathrm{SUIT}(\mathrm{TOP}) = 3, NEXT(TOP)=905\mathrm{NEXT}(\mathrm{TOP}) = 905 and RANK(NEXT(TOP))=12\mathrm{RANK}(\mathrm{NEXT}(\mathrm{TOP})) = 12. The expression F(L)\mathrm{F}(L) is a variable only when L≠ΛL \ne \Lambda. The name F alone has no value.

Two meanings of one name. In an algorithm the name TOP means the value of the variable, the address of the top card. In MIX assembly language the name TOP means the address of the word that holds that value. So the instruction LDA TOP brings the address of the top card into register A. The instruction LDA TOP(NEXT) brings a field of the word named TOP, which is not a node at all. To read NEXT(TOP)\mathrm{NEXT}(\mathrm{TOP}) the program first loads TOP into an index register and then uses the register as the address: LD1 TOP and LDA 0,1(NEXT). This is the most common error of a beginner with linked memory.

Definitions, results and proofs

Definition C2.2.1.1 (node, field, address, link; TAOCP eq. 2.1(1)). A node is a block of c≥1c \ge 1 consecutive words of memory. It is cut into named fields. A field is a part of a word, given by a pair of bytes (l ⁣: ⁣r)(l\!:\!r) of MIX, or a whole word. The address of the node is the address of its first word. A value that is an address of a node is a link. The link Λ\Lambda is a value that is not the address of any node.

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

Explanation. The definition says what a program may assume about a node. A field is an exact part of a word, so a program can load, store and compare it with one instruction of MIX. A link is a number, so it is stored in a field like any other number. The only property that the null link needs is that it differs from every address of a node. A program keeps this true by not putting a node at address 00.

Example. The card at 905 has NEXT =2100= 2100. In base 64, 2100=32⋅64+522100 = 32 \cdot 64 + 52, so the bytes 1 and 2 of its first word are 32 and 52. With TAG =0= 0, SUIT =4= 4 and RANK =12= 12, the first word is +  32  52  0  4  12+\;32\;52\;0\;4\;12. The second word holds the five characters of TITLE, padded with blanks. The card is the queen of spades.

Definition C2.2.1.2 (a field through a link, TAOCP eq. 2.1(4)). Let F be a field name and L≠ΛL \ne \Lambda a link. Then F(L)\mathrm{F}(L) is the field F of the node at address LL, and it is a variable: it can be read and it can be set. When L=ΛL = \Lambda, F(L)\mathrm{F}(L) is not defined. A link variable is a variable whose value is a link. All references to nodes go through link variables, or through link fields of nodes.

Node: 2.1-(4) · Lean: not yet stated

Explanation. The notation hides the address arithmetic. Inside the machine, F(L)\mathrm{F}(L) is the part F of the words at L,L+1,…L, L+1, \ldots. The notation also fixes what the algorithms may do: they can set NEXT(P)←Q\mathrm{NEXT}(P) \leftarrow Q, and this changes the pile. They cannot talk about NEXT alone.

Example. Take the pile of the table above. SUIT(NEXT(TOP))=SUIT(905)=4\mathrm{SUIT}(\mathrm{NEXT}(\mathrm{TOP})) = \mathrm{SUIT}(905) = 4, spades. NEXT(NEXT(NEXT(TOP)))=NEXT(NEXT(905))=NEXT(2100)=Λ\mathrm{NEXT}(\mathrm{NEXT}(\mathrm{NEXT}(\mathrm{TOP}))) = \mathrm{NEXT}(\mathrm{NEXT}(905)) = \mathrm{NEXT}(2100) = \Lambda. The expression NEXT(NEXT(NEXT(NEXT(TOP))))\mathrm{NEXT}(\mathrm{NEXT}(\mathrm{NEXT}(\mathrm{NEXT}(\mathrm{TOP})))) is not defined: it asks for a field of Λ\Lambda.

Definition C2.2.1.3 (a variable that holds a whole node, TAOCP eq. 2.1(6)). Let NODE be a field specification that covers a whole node. The assignment CARD←NODE(L)\mathrm{CARD} \leftarrow \mathrm{NODE}(L) sets the variable CARD to the whole contents of the node at LL. Then SUIT(CARD)\mathrm{SUIT}(\mathrm{CARD}) is the suit field of the copy, and CARD is not a link.

Node: 2.1-(6) · Lean: not yet stated

Explanation. A variable that holds a whole node is a convenience for algorithms. Its fields are read with the same notation as the fields of a node in memory, but its own address is irrelevant. It is a copy, so a later change to the card in the pile does not change CARD.

Example. After CARD←NODE(905)\mathrm{CARD} \leftarrow \mathrm{NODE}(905) with the pile above, RANK(CARD)=12\mathrm{RANK}(\mathrm{CARD}) = 12 and NEXT(CARD)=2100\mathrm{NEXT}(\mathrm{CARD}) = 2100. Now set RANK(905)←11\mathrm{RANK}(905) \leftarrow 11. Then RANK(905)=11\mathrm{RANK}(905) = 11 and RANK(CARD)=12\mathrm{RANK}(\mathrm{CARD}) = 12 still.

Definition C2.2.1.4 (the whole-node copy as word copies, TAOCP eq. 2.1(7)). If a node has cc words, the assignment CARD←NODE(L)\mathrm{CARD} \leftarrow \mathrm{NODE}(L) means the cc assignments CONTENTS(LOC(CARD)+j)←CONTENTS(L+j)\mathrm{CONTENTS}(\mathrm{LOC}(\mathrm{CARD}) + j) \leftarrow \mathrm{CONTENTS}(L + j) for 0≤j<c0 \le j < c. Here CONTENTS(A)\mathrm{CONTENTS}(A) is the full word at address AA, and LOC(V)\mathrm{LOC}(V) is the address of the cell that holds the variable VV.

Node: 2.1-(7) · Lean: not yet stated

Explanation. This ties the notation to the machine. For a variable VV in one full word, CONTENTS(LOC(V))=V\mathrm{CONTENTS}(\mathrm{LOC}(V)) = V. The reverse equation LOC(CONTENTS(V))=V\mathrm{LOC}(\mathrm{CONTENTS}(V)) = V does not hold in general. Exercise 2.1-2 asks when it holds.

Example. A node of c=2c = 2 words is at address 905905. The copy sets two words, CONTENTS(LOC(CARD))\mathrm{CONTENTS}(\mathrm{LOC}(\mathrm{CARD})) from CONTENTS(905)\mathrm{CONTENTS}(905), and CONTENTS(LOC(CARD)+1)\mathrm{CONTENTS}(\mathrm{LOC}(\mathrm{CARD}) + 1) from CONTENTS(906)\mathrm{CONTENTS}(906). If CARD is stored at addresses 35003500 and 35013501, the two MIX word moves are from 905905 to 35003500 and from 906906 to 35013501.

Algorithms and programs

Algorithm C2.2.1.5 (put a card face up on the pile, TAOCP Algorithm 2.1A). The input is a pile with the link variable TOP and a new card node whose address is NEWCARD. The output is the pile with the new card on top and face up. The pile may be empty, with TOP=Λ\mathrm{TOP} = \Lambda.

Node: Algorithm 2.1A · Lean: not yet stated

The idea. The new card must point to the old top, and TOP must point to the new card. The order matters. If TOP is changed first, the address of the old top is lost.

The steps.

PutOnTop(NEWCARD):
  NEXT(NEWCARD) := TOP        -- A1: the new card points to the old top
  TOP := NEWCARD              -- A2: the pile starts at the new card
  TAG(TOP) := 0               -- A3: face up

A worked run. The pile is the three cards of the table. The new card is the ace of diamonds, stored at address 3000 with the old garbage NEXT(3000)=777\mathrm{NEXT}(3000) = 777 and TAG(3000)=1\mathrm{TAG}(3000) = 1. Each row shows the state after the step.

Step TOP NEXT(3000) TAG(3000) The pile from TOP
start 1204 777 1 1204, 905, 2100
A1 1204 1204 1 1204, 905, 2100
A2 3000 1204 1 3000, 1204, 905, 2100
A3 3000 1204 0 3000, 1204, 905, 2100

Between A1 and A2 the pile is still the old one, and the new card is outside it. After A2 the new card is the top. The garbage value 777777 is gone after A1.

Correctness. The invariant after step A1 is: the chain from NEWCARD by NEXT is NEWCARD followed by the chain from TOP. After A2 the chain from TOP is NEWCARD followed by the old pile, which is the claim of the algorithm. Step A3 changes only the field TAG of the new top. If TOP is Λ\Lambda at the start, A1 sets NEXT(NEWCARD)=Λ\mathrm{NEXT}(\mathrm{NEWCARD}) = \Lambda and the pile has one card, so the empty pile needs no special case. The algorithm has three steps and no loop, so it stops.

Cost. The algorithm does three assignments, whatever the size of the pile.

Program C2.2.1.6 (put a card face up on the pile, TAOCP Program 2.1A). The program uses the fields as defined above. It has no test and its time does not depend on the pile.

Node: Program 2.1A · Lean: not yet stated

NEXT  EQU  0:2
TAG   EQU  3:3
      LD1  NEWCARD      A1: rI1 <- NEWCARD
      LDA  TOP          rA  <- TOP
      STA  0,1(NEXT)    NEXT(rI1) <- rA
      ST1  TOP          A2: TOP <- rI1
      STZ  0,1(TAG)     A3: TAG(rI1) <- 0

The register I1 holds the link variable. Each of the five instructions loads or stores one word, and each of these takes 2 units of time in MIX. The total time is 5⋅2=10u5 \cdot 2 = 10u. The instruction STA 0,1(NEXT) stores the sign and the two last bytes of rA into the field (0 ⁣: ⁣2)(0\!:\!2) of the word at address rI1+0\mathrm{rI1} + 0, and STZ 0,1(TAG) stores zero into byte 3 of the same word. The word TOP holds an address in its bytes 4 and 5 with the sign ++, so the stored value is the correct link.

A run of the program. Take the same pile and the new card at 3000. The columns show the registers and the words after each instruction.

After rI1 rA NEXT(3000) TOP TAG(3000)
start ? ? 777 1204 1
LD1 NEWCARD 3000 ? 777 1204 1
LDA TOP 3000 1204 777 1204 1
STA 0,1(NEXT) 3000 1204 1204 1204 1
ST1 TOP 3000 1204 1204 3000 1
STZ 0,1(TAG) 3000 1204 1204 3000 0

Algorithm C2.2.1.7 (count the cards of the pile, TAOCP Algorithm 2.1B). The input is the link variable TOP. The output is the integer NN, the number of cards of the pile.

Node: Algorithm 2.1B · Lean: not yet stated

The idea. Walk down the pile by NEXT, and add one to a counter for each card, until the link is Λ\Lambda.

The steps.

CountCards():
  N := 0; X := TOP            -- B1
  while X != Λ:               -- B2: stop when the link is null
    N := N + 1; X := NEXT(X)  -- B3: count a card, go down one card
  return N

A worked run. Use the pile of three cards, before the new card is put on it. Each row is the state after the step.

Step N X Next step
B1 0 1204 B2
B2 (X is not Λ\Lambda) 0 1204 B3
B3 1 905 B2
B2 1 905 B3
B3 2 2100 B2
B2 2 2100 B3
B3 3 Λ\Lambda B2
B2 (X=ΛX = \Lambda) 3 Λ\Lambda stop

Correctness. The invariant at step B2 is: NN cards have been passed, and XX is the link to the card after them, so the pile has NN plus the length of the chain from XX cards. At B1 it holds with N=0N = 0 and X=TOPX = \mathrm{TOP}. Step B3 adds one to NN and moves XX by one card, so the invariant holds again. When X=ΛX = \Lambda the chain from XX is empty, so NN is the number of cards. The algorithm stops if the pile is finite and has no loop, since XX moves down one card in each pass.

Cost. For a pile of kk cards, B1 runs once, B2 runs k+1k+1 times and B3 runs kk times. The run above has k=3k = 3: one B1, four B2 and three B3.

A program in MIX. The counter NN is in rI3 and the link XX is in rI1. The names TOP and NEXT are as in Program 2.1A.

      ENT3 0            B1: N <- 0
      LD1  TOP              X <- TOP
      J1Z  DONE         B2: if X = Λ, stop
LOOP  INC3 1            B3: N <- N + 1
      LD1  0,1(NEXT)        X <- NEXT(X)
      J1NZ LOOP         B2: if X != Λ, repeat
DONE  ...               rI3 = number of cards

The times are 1, 2 and 1 units for the first three instructions, which run once. The loop has 1+2+1=41 + 2 + 1 = 4 units. For kk cards the total is 4+4k4 + 4k units. For the pile of three cards the time is 16u16u, and for the pile of four cards (after the new card is put on top) it is 20u20u. The same formula gives 4u4u for an empty pile, since J1Z DONE jumps at once.

The exercises

The table has the nine exercises of Section 2.1. The kind is the exkind of the solution file in X1.

Exercise Rating Markers Kind Lean Solution
2.1-1 4 K3 not yet stated X1, Exercise 2.1-1
2.1-2 10 K3 not yet stated X1, Exercise 2.1-2
2.1-3 11 K5 not yet stated X1, Exercise 2.1-3
2.1-4 18 K5 not yet stated X1, Exercise 2.1-4
2.1-5 21 K5 not yet stated X1, Exercise 2.1-5
2.1-6 6 K2 not yet stated X1, Exercise 2.1-6
2.1-7 4 K2 not yet stated X1, Exercise 2.1-7
2.1-8 18 K6 not yet stated X1, Exercise 2.1-8
2.1-9 23 K6 not yet stated X1, Exercise 2.1-9

Exercise 1 reads two values from the picture of a pile. Exercise 2 asks when LOC(CONTENTS(V))=V\mathrm{LOC}(\mathrm{CONTENTS}(V)) = V. Exercise 6 asks which notation gives the suit after the whole-node copy. Exercise 7 is the point of the section about the two meanings of a name: it compares LDA TOP(NEXT) with LD1 TOP followed by LDA 0,1(NEXT). Exercise 3 removes the top card, the undo of Algorithm 2.1A. Exercise 4 puts a card face down at the bottom of a pile that may be empty. Exercise 5 removes the bottom card, and it needs a walk down the pile with a trailing link, as in Algorithm 2.2.4A. Exercise 8 asks for a MIX program for Algorithm 2.1B, and the program of this chapter is a model for it. Exercise 9 asks for a program that prints the card names, with parentheses around face down cards.

Lean

The repository tamnd/taocp-lean has a model of MIX in its folder TAOCP/MIX, and no declaration for Chapter 2 yet. The items of this section are definitions of notation and two three-step algorithms, so no theorem of the section needs a Lean statement. A formal model of linked memory would be a finite map from addresses to nodes, with 00 not in its domain. The statements "the chain from TOP after Algorithm A is NEWCARD followed by the old chain" and "Algorithm B returns the length of the chain" would then be statements about that map. The statements are not stated yet, and this chapter claims no Lean result.

Notes and history

The idea of the link goes back to the work of A. Newell, J. C. Shaw and H. A. Simon on information processing languages in the middle of the 1950s, where it was used for the lists of the logic theory program. The programming language LISP of J. McCarthy made the idea common. The section of the book says that a programmer does not need a system of Lists to use linked memory, and that the techniques of this chapter apply directly in assembly language or in any language that has addresses or references. The null link is written Λ\Lambda in the book. In a language with references it is null, nil or a zero pointer. In MIX it is the number 00.

One remark on the layout of the cards in this chapter. The book puts the link NEXT in bytes 4 and 5 and the tag, suit and rank in bytes 1 to 3. This chapter puts NEXT in the sign and bytes 1 and 2 instead, so that a reader who compares the two layouts sees that only the access pattern matters: a field is a pair (l ⁣: ⁣r)(l\!:\!r), and its name in a program is a matter of an EQU.

Section 2.1 of TAOCP opens Chapter 2. It has no theorem. It fixes the words and the notation that all later sections use: a node, a field, an address, a link, the null link Λ\Lambda, a link variable, and the notation F(L)\mathrm{F}(L) for the field F of the node at address LL. It shows the idea of linked memory on one small example, a pile of playing cards, and it gives two tiny algorithms on that pile: one puts a card on the pile, the other counts the cards. This chapter of C2 draws the pile three ways (as words of memory, as arrows and as a table of fields), runs both algorithms by hand on a pile of three cards, writes the programs in MIX with their costs, and shows the one point where beginners make mistakes: in assembly language a name stands for an address, and in an algorithm the same name stands for the value stored at that address.

The source of this page. Something is wrong? Report it.