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
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 , the null link, means "no node". In MIX it is the number , because no node is ever placed at address .
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 means face down and TAG 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 . The number in the last row is . 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 means the field F of the node at address . With the pile above, , , and . The expression is a variable only when . 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 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 consecutive words of memory. It is cut into named fields. A field is a part of a word, given by a pair of bytes 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 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 .
Example. The card at 905 has NEXT . In base 64, , so the bytes 1 and 2 of its first word are 32 and 52. With TAG , SUIT and RANK , the first word is . 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 a link. Then is the field F of the node at address , and it is a variable: it can be read and it can be set. When , 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, is the part F of the words at . The notation also fixes what the algorithms may do: they can set , and this changes the pile. They cannot talk about NEXT alone.
Example. Take the pile of the table above. , spades. . The expression is not defined: it asks for a field of .
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 sets the variable CARD to the whole contents of the node at . Then 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 with the pile above, and . Now set . Then and still.
Definition C2.2.1.4 (the whole-node copy as word copies, TAOCP eq. 2.1(7)). If a node has words, the assignment means the assignments for . Here is the full word at address , and is the address of the cell that holds the variable .
Node: 2.1-(7) · Lean: not yet stated
Explanation. This ties the notation to the machine. For a variable in one full word, . The reverse equation does not hold in general. Exercise 2.1-2 asks when it holds.
Example. A node of words is at address . The copy sets two words, from , and from . If CARD is stored at addresses and , the two MIX word moves are from to and from to .
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 .
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 and . 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 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 at the start, A1 sets 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 . The instruction STA 0,1(NEXT) stores the sign and the two last bytes of rA into the field of the word at address , 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 , 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 .
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 ) | 0 | 1204 | B3 |
| B3 | 1 | 905 | B2 |
| B2 | 1 | 905 | B3 |
| B3 | 2 | 2100 | B2 |
| B2 | 2 | 2100 | B3 |
| B3 | 3 | B2 | |
| B2 () | 3 | stop |
Correctness. The invariant at step B2 is: cards have been passed, and is the link to the card after them, so the pile has plus the length of the chain from cards. At B1 it holds with and . Step B3 adds one to and moves by one card, so the invariant holds again. When the chain from is empty, so is the number of cards. The algorithm stops if the pile is finite and has no loop, since moves down one card in each pass.
Cost. For a pile of cards, B1 runs once, B2 runs times and B3 runs times. The run above has : one B1, four B2 and three B3.
A program in MIX. The counter is in rI3 and the link 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 units. For cards the total is units. For the pile of three cards the time is , and for the pile of four cards (after the new card is put on top) it is . The same formula gives 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 . 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 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 in the book. In a language with references it is null, nil or a zero pointer. In MIX it is the number .
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 , 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 , a link variable, and the notation for the field F of the node at address . 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.