Volume C2 · Information structures · TAOCP Section 2.2.2
Sequential allocation
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.2 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, Section 2.1. Introduction to information structures draft
- C1.3.1, in C1. Basic concepts not written yet
Contents
Lists in consecutive memory
Overview
A sequential list keeps its nodes in consecutive places of memory, one after the other. It is the oldest way to store a list, and it is fast: the machine reaches the th node with one addition. The price is the room. A list that can grow needs room after its last node, and when several lists grow and shrink in one area, there is no place for the room that is right for all of them. Section 2.2.2 of TAOCP studies this price. It first treats one list (a stack, a queue, a circular queue) with the tests for overflow and underflow. Then it treats two stacks that grow toward each other, and then stacks with moving bases. Here it gives a simple method (move the stacks above the full one) and the method of Garwick (repack all stacks at once, with room in proportion to recent growth). The last part is a count of the moves of the simple method. A reader needs the stack and the queue of Section 2.2.1 and the MIX of C1.
The ideas
One list is an array with a pointer. Let a list have nodes of words. If the first node starts at , the th node starts at . A stack keeps one number , the number of nodes. An insertion adds to and stores the item at . A deletion reads and subtracts . A queue keeps two numbers, and , and both only grow. This is fast, but the pointers run out of room sooner or later even if the queue is short. So the cells are set in a circle, and a pointer that passes the last cell goes back to the first.
Overflow and underflow are two tests. An insertion to a full area is an overflow. A deletion from an empty list is an underflow. An underflow is often not an error: a program deletes until the underflow to empty a stack. An overflow is an error unless the program can make room.
Two stacks share one area by growing toward each other. Stack 1 starts at the left end of the area and grows right. Stack 2 starts at the right end and grows left. They collide only when the total is full. This is the best that a fixed place for the bottoms can do: with three or more lists, there is no way to have both a fixed bottom for each list and an overflow only when the total is full.
Many stacks make the bottoms move. Give up the fixed bottoms. Stack has a base and a top , and its items are in the cells . The cells between and are the room of stack . When stack is full, the program finds room in a neighbour and moves blocks of items by one cell, or it moves all stacks to new bases in one pass.
Definitions, results and proofs
Definition C2.2.2.2.1 (the address of a node, TAOCP eq. 2.2.2(1)). A sequential list has nodes of words each. If is the base address, then , and so .
Node: 2.2.2-(1) · Lean: not yet stated
Explanation. The address is a linear function of , so no search is needed. A program that reads the th node loads in an index register and adds a constant. In MIX, with : LD1 J then LDA L0,1.
Example. Let and . Then is at , at and at .
Definition C2.2.2.2.2 (a sequential stack, TAOCP eqs. 2.2.2(2) and (3)). A stack in the cells has a number , the count of its items, with for the empty stack. The insertion of is: , . The deletion into is: , .
Node: 2.2.2-(2) · Lean: not yet stated
Explanation. Two assignments in each case. The deletion reads before it counts down, and the insertion counts up before it stores.
Example. Take the empty stack. Insert , then . Then and . A deletion gives and . The cell still holds , but it is not part of the stack.
Definition C2.2.2.2.3 (the stack with tests, TAOCP eqs. 2.2.2(2a) and (3a)). In the insertion, after the test "if then OVERFLOW" comes before the store. In the deletion, the test "if then UNDERFLOW" comes before the read.
Node: 2.2.2-(2a) · Lean: not yet stated
Explanation. When the test for overflow finds , the value of is already . A program that goes on after the overflow must put back to , or must make room.
Example. A stack with cells gets the operations , , delete, , , , , delete, delete, delete, delete, delete. Each row is the state after the operation. A dash shows a deletion.
| Operation | Signal | Cells , items only | |
|---|---|---|---|
| insert | 1 | ||
| insert | 2 | ||
| delete | gives | 1 | |
| insert | 2 | ||
| insert | 3 | ||
| insert | 4 | ||
| insert | OVERFLOW ( was , set back to ) | 4 | |
| delete | gives | 3 | |
| delete | gives | 2 | |
| delete | gives | 1 | |
| delete | gives | 0 | empty |
| delete | UNDERFLOW | 0 | empty |
Program C2.2.2.2.4 (push and pop in MIX). Let rI1 hold , let SIZE hold , and let the cell be at address X + j.
Node: 2.2.2-prog-pushpop · Lean: not yet stated
PUSH INC1 1 T <- T + 1
CMP1 SIZE compare T with M
JG OVERFLOW if T > M
STA X,1 X[T] <- rA
POP J1Z UNDERFLOW if T = 0
LDA X,1 rA <- X[T]
DEC1 1 T <- T - 1
Cost. A push takes when there is no overflow, since INC1 and JG take and CMP1 and STA take . A pop takes .
Definition C2.2.2.2.5 (a queue with moving pointers, TAOCP eqs. 2.2.2(4) and (5)). A queue keeps two numbers, and , with for the empty queue. The insertion is , . The deletion is , .
Node: 2.2.2-(4) · Lean: not yet stated
Explanation. The items are in the cells . Both numbers only grow. After insertions the queue has no more room, even if it is empty at that time. The exception is a queue that is often empty, and then the program can reset .
Example. Start with . Insert , , : . Delete: , . Delete: , . Insert : . The queue holds in the cells . The cells are free but the pointers are past them.
Definition C2.2.2.2.6 (a circular queue, TAOCP eqs. 2.2.2(6), (7), (6a), (7a)). A circular queue uses the cells as a circle, with after . The insertion is: if then , else ; if then OVERFLOW; . The deletion is: if then UNDERFLOW; if then , else ; . Both numbers start at .
Node: 2.2.2-(6a) · Lean: not yet stated
Explanation. The items are in the cells that follow up to , going round the circle. The state is the empty queue. So a full queue must not have , and the test for overflow refuses an insertion that would make them equal. For this reason the queue holds at most items (exercise 2.2.2-1). The cell at is always free, since it holds the last item that was deleted.
Example. Take and .
| Operation | Signal | Cells | ||
|---|---|---|---|---|
| insert | 1 | 2 | ||
| insert | 1 | 3 | ||
| insert | 1 | 4 | ||
| insert | 1 | 5 | ||
| insert | OVERFLOW (4 items) | 1 | 5 | |
| delete | gives | 2 | 5 | |
| delete | gives | 3 | 5 | |
| insert | 3 | 1 | ||
| insert | 3 | 2 | ||
| insert | OVERFLOW (4 items) | 3 | 2 | |
| delete | gives | 4 | 2 | |
| delete | gives | 5 | 2 | |
| delete | gives | 1 | 2 | |
| delete | gives | 2 | 2 | empty |
| delete | UNDERFLOW | 2 | 2 | empty |
The cells show the items only, and a dot is a cell that is not part of the queue. The maximum of four items with is reached twice. The two pointers pass the end of the area and come back, so the run uses a queue that is never longer than with cells.
Example C2.2.2.2.7 (two stacks growing toward each other). An area of cells. Stack 1 has the top and uses the cells . Stack 2 has the top and uses the cells , with when it is empty. An insertion into stack 1 gives , and an insertion into stack 2 gives . Overflow means before the insertion.
| Operation | Cells 1 to 6 | ||
|---|---|---|---|
| start | 0 | 7 | empty |
| insert in 1 | 1 | 7 | |
| insert in 1 | 2 | 7 | |
| insert in 1 | 3 | 7 | |
| insert in 2 | 3 | 6 | |
| insert in 2 | 3 | 5 | |
| insert in 2 | 3 | 4 | |
| insert in 1 | overflow | no room: | |
| delete from 2 | 3 | 5 | gives |
| insert in 1 | 4 | 5 |
Stack 1 holds items and stack 2 holds in the last row. A split of the area into two halves of cells each would have overflowed at the item of stack 1.
Example C2.2.2.2.8 (relative addressing, TAOCP eq. 2.2.2(8)). When the base of a list moves, the base is kept in a word BASE, and the program cannot use a constant in the address. In MIX the access to the th word of a list takes four instructions instead of two.
Node: 2.2.2-(8) · Lean: not yet stated
LD1 I rI1 <- I
LDA BASE(0:2) rA <- the base address, in the address bytes
STA *+1(0:2) store it into the address of the next instruction
LDA *,1 rA <- X[I]; the address is changed by the previous line
The time is against for the two instructions with a fixed base. The extra cost is paid at each access. So a fixed base is cheaper, and it is lost when the bases move.
Definition C2.2.2.2.9 (many stacks in one area, TAOCP eqs. 2.2.2(9), (10), (11)). There are stacks in the cells . Stack has the items in the cells , and . The insertion into stack is: ; if then OVERFLOW, else store in the cell . The deletion is: if then UNDERFLOW, else read the cell and subtract from . At the start for .
Node: 2.2.2-(9) · Lean: not yet stated
Explanation. All the room of the area is at the right of stack at the start. A stack that is full is a stack with . The two statements for the insertion and the deletion are (9) and (10), and the start values are (11).
Example. With , and : , and . Stack 3 can take items before an overflow, and stacks 1 and 2 overflow at once.
Definition C2.2.2.2.10 (the simple repacking after an overflow of stack , TAOCP Section 2.2.2). Choose one of three cases. In case (a), find the smallest with and . If it exists, move the cells up by one cell, last cell first, and add to and for . In case (b), when no such exists, find the largest with and , move the cells down by one cell, first cell first, and subtract from and for . In case (c), when no room exists in any stack, the area is full and the program stops with an overflow.
Node: 2.2.2-def-repack · Lean: not yet stated
Explanation. The order of the moves matters. In (a) the cells move up, so the last cell goes first, or it would overwrite a cell not yet moved. In (b) the cells move down, so the first cell goes first. The moves make one free cell at the top of stack , and the stacks between and keep their order. In case (b) the insertion has already raised , so the cells to move end at , and the program then lowers with the others to make the free cell for the new item.
Example. Three stacks in six cells, from the start of the previous example. The run is: insert and in stack 1, in 3, in 2, delete from 1, insert in 3, and in 1, in 2, delete from 2. Each row is the state after the operation. A dot is a free cell or a cell that is not in a stack.
| Operation | Case | BASE | TOP | Cells 1 to 6 | Words moved so far |
|---|---|---|---|---|---|
| insert in 1 | (a), | 0, 1, 1 | 1, 1, 1 | 0 | |
| insert in 1 | (a), | 0, 2, 2 | 2, 2, 2 | 0 | |
| insert in 3 | none | 0, 2, 2 | 2, 2, 3 | 0 | |
| insert in 2 | (a), | 0, 2, 3 | 2, 3, 4 | 1 | |
| delete from 1 | none | 0, 2, 3 | 1, 3, 4 | 1 | |
| insert in 3 | none | 0, 2, 3 | 1, 3, 5 | 1 | |
| insert in 1 | none | 0, 2, 3 | 2, 3, 5 | 1 | |
| insert in 1 | (a), | 0, 3, 4 | 3, 4, 6 | 4 | |
| insert in 2 | (c) | 0, 3, 4 | 3, 4, 6 | unchanged | 4 |
| delete from 2 | none | 0, 3, 4 | 3, 3, 6 | 4 |
At the insertion of the three cells , , move up one cell each, and stay. Stack 2 holds one item and needs to move only when it is below the overflowing stack. The insertion of finds no free cell: stack 1 has cells, stack 2 has and stack 3 has , and is the whole area.
Definition C2.2.2.2.11 (an equal start, TAOCP eq. 2.2.2(13)). To avoid early overflows when the stacks are expected to have about the same size, start with for .
Node: 2.2.2-(13) · Lean: not yet stated
Explanation. The area is cut in nearly equal parts. The start saves at most a fixed number of overflows, since the later overflows are due to unequal growth.
Example. With , and the bases are . Stack 3 has the cells to .
Theorem C2.2.2.2.12 (the mean number of moves, TAOCP eq. 2.2.2(14)). Let insertions be made, each into a stack , and let each of the sequences be equally likely. Start as in (11) with an area that is large enough for all items, and use the simple repacking of the case (a). The mean number of moved words is
Node: 2.2.2-(14) · Lean: not yet stated
Explanation. The number of moves grows as the square of the number of insertions. This is the price of packing many tables tight. The theorem is exercise 9 of the book. The model has no deletions.
Example. For and the value is . A check by python over all sequences gives the total moves, the mean . For and the value is . For and it is , and for and it is , and python gives the same values.
Proof. Claim: at each time the stacks are tight, that is for , and stack has all the room. This is true at the start by (11). An insertion into stack needs no move. An insertion into stack overflows, since the stack is tight. In the case (a) the smallest with room is , because the stacks are tight. So the move shifts all items of the stacks up by one cell, and the number of moved words is the number of those items. After the move the stacks are tight again. So the cost of an insertion into stack is the number of earlier insertions that went into the stacks with a number larger than . Take the th insertion. Each of the earlier insertions goes into a stack of the number larger than with probability , and is uniform, so the mean of this probability is . The mean cost of the th insertion is . The sum over is , which is the claim.
Algorithms and programs
Algorithm 2.2.2G (reallocate the stacks after an overflow, TAOCP Algorithm 2.2.2G). Stack has overflowed in the insertion (9), so is already larger by . The array holds the value of at the end of the last repacking. The algorithm finds new bases, calls Algorithm R to move the stacks, and then the insertion can go on. It may also find that the area is full.
Node: Algorithm 2.2.2G · Lean: not yet stated
The idea. The free room is shared in two parts. One tenth of it is cut in equal parts, and nine tenths are shared in proportion to the growth of each stack since the last repacking. A stack that grew fast is likely to grow again, so it gets more room.
The steps.
G1: SUM := Linf - L0; INC := 0
G2: for j := 1 to n:
SUM := SUM - (TOP[j] - BASE[j])
if TOP[j] > OLDTOP[j]: D[j] := TOP[j] - OLDTOP[j]; INC := INC + D[j]
else D[j] := 0
G3: if SUM < 0: stop, the area is full
G4: alpha := 0.1 * SUM / n; beta := 0.9 * SUM / INC
G5: NEWBASE[1] := BASE[1]; sigma := 0
for j := 2 to n:
tau := sigma + alpha + D[j-1] * beta
NEWBASE[j] := NEWBASE[j-1] + TOP[j-1] - BASE[j-1] + floor(tau) - floor(sigma)
sigma := tau
G6: TOP[i] := TOP[i] - 1; run Algorithm R; TOP[i] := TOP[i] + 1
for j := 1 to n: OLDTOP[j] := TOP[j]
Invariant. After G2, is the number of free cells, counted as if the new item of stack were in its stack. In G5, , so grows with , and the room that stack gets is . The sum of all the room is below , because , and the stack keeps the rest. So the new bases are increasing, each stack has a place for its items, and the stack has a place for its new item.
A worked run, with moves up. Let , , , , and . Stack 2 overflowed: its cells are , and . The steps give:
| Step | Values |
|---|---|
| G1, G2 | ; ; |
| G3 | , go on |
| G4 | ; |
| G5, | ; |
| G5, | ; |
Stack 2 has cells in G5, and the pending item is counted in this size. Stack 1 stays, stack 2 stays, and stack 3 moves up by . After the algorithm, the stack 2 has and the free cells are after stack 1, after stack 2 and after stack 3, in all .
A worked run, with moves down. Let , , , and . Stack 3 has overflowed, since .
| Step | Values |
|---|---|
| G1, G2 | ; ; |
| G4 | ; |
| G5, | ; |
| G5, | ; |
Stacks 2 and 3 both move down, and stack 3, which grew most, gets the whole free room.
Cost. Steps G1 to G5 take time proportional to . The cost of G6 is the cost of Algorithm R, which moves each item at most once. So one repacking costs plus the number of items that change place, and at most the number of items.
Algorithm 2.2.2R (relocate the stacks, TAOCP Algorithm 2.2.2R). The stacks are moved to the bases and and are adjusted. The stacks that move down are moved first, in the order . Then the stacks that move up are moved, in the order .
Node: Algorithm 2.2.2R · Lean: not yet stated
The idea. A stack that moves down can go to a place that is free after the stacks below it have moved down. A stack that moves up can go to a place that is free after the stacks above it have moved up. A stack that moves down and a stack that moves up never need the same cell.
The steps.
R1: j := 1
R2: j := j + 1 while j <= n and NEWBASE[j] >= BASE[j]; if j > n go to R4
R3: d := BASE[j] - NEWBASE[j]
for L := BASE[j]+1 to TOP[j]: CONTENTS(L - d) := CONTENTS(L)
BASE[j] := NEWBASE[j]; TOP[j] := TOP[j] - d; go to R2
R4: j := j - 1 while j > 1 and NEWBASE[j] <= BASE[j]; if j = 1 stop
R5: d := NEWBASE[j] - BASE[j]
for L := TOP[j] down to BASE[j]+1: CONTENTS(L + d) := CONTENTS(L)
BASE[j] := NEWBASE[j]; TOP[j] := TOP[j] + d; go to R4
In R4 the index starts at after R2 has ended with .
Invariant. In R2 to R3, the stacks that had to move down are in their new place, and the others are as they were. A stack that moves down moves into the cells from up. This region does not meet the stacks in their new places, since the size of stack . It does not meet the old place of a stack that moves up either, since such a stack has , so . The same argument, turned around, holds for the moves up.
A worked run. The second run of Algorithm G above gives with and the stack 3 shortened by to . The cells 1 to 20 are shown by items only: stack 1 is , stack 2 is and stack 3 is .
| Step | BASE | TOP | Cells 1 to 20 | ||
|---|---|---|---|---|---|
| start | 0, 6, 10 | 3, 8, 20 | |||
| R3 | 2 | 3 | 0, 3, 10 | 3, 5, 20 | |
| R3 | 3 | 5 | 0, 3, 5 | 3, 5, 15 |
Then R2 ends with , and R4 finds no stack that moves up, so the algorithm stops. The moves are words. After the step G6 gives , and the new item goes into the cell .
Correctness. Each word is moved once at most. It is moved to a cell that is free at that time, by the invariant. The order of copying in R3 is increasing and in R5 decreasing , so that a block that moves by cells does not overwrite a cell that is not yet copied. The loop in R2 to R3 ends when , and the loop in R4 to R5 ends when .
Cost. At most one move of each item, so at most the number of items, plus for the tests. Stack 1 never moves, so the largest stack should be first if the program knows which it is.
The exercises
The table has the 19 exercises of Section 2.2.2. The folder of X1 has no file for this section yet, and the Solution column says so.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 2.2.2-1 | 15 | not set | not yet stated | not in X1 yet | |
| 2.2.2-2 | 22 | not set | not yet stated | not in X1 yet | |
| 2.2.2-3 | 21 | not set | not yet stated | not in X1 yet | |
| 2.2.2-4 | 25 | not set | not yet stated | not in X1 yet | |
| 2.2.2-5 | 35 | not set | not yet stated | not in X1 yet | |
| 2.2.2-6 | 10 | not set | not yet stated | not in X1 yet | |
| 2.2.2-7 | 12 | not set | not yet stated | not in X1 yet | |
| 2.2.2-8 | 26 | not set | not yet stated | not in X1 yet | |
| 2.2.2-9 | 27 | M | not set | not yet stated | not in X1 yet |
| 2.2.2-10 | 28 | M | not set | not yet stated | not in X1 yet |
| 2.2.2-11 | 30 | M | not set | not yet stated | not in X1 yet |
| 2.2.2-12 | 28 | M | not set | not yet stated | not in X1 yet |
| 2.2.2-13 | 42 | HM | not set | not yet stated | not in X1 yet |
| 2.2.2-14 | 43 | HM | not set | not yet stated | not in X1 yet |
| 2.2.2-15 | 40 | not set | not yet stated | not in X1 yet | |
| 2.2.2-16 | 20 | not set | not yet stated | not in X1 yet | |
| 2.2.2-17 | 30 | not set | not yet stated | not in X1 yet | |
| 2.2.2-18 | 30 | M | not set | not yet stated | not in X1 yet |
| 2.2.2-19 | 16 | not set | not yet stated | not in X1 yet |
Exercise 1 asks for the capacity of the circular queue, and the run above shows the answer for : four items. Exercise 2 extends the circular queue to a deque. Exercises 3 to 5 extend MIX with indirect addressing and compare it with the code of eq. (8). Exercise 6 asks which operations in the figure of the book overflow or underflow, and the table of the three stacks above is a model for the work. Exercise 7 asks if can be zero in step G4: the stack that overflowed has grown by at least one cell since the last repacking, so . Exercise 8 adapts the repacking to queues. Exercises 9 to 11 are the model of the mean number of moves, and the theorem above is exercise 9. Exercises 12 to 14 are about two stacks that grow toward each other, and the asymptotic study of the maximum of their sizes. Exercise 15 is a simulation. Exercise 16 is about the two-stack layout. Exercise 17 shows that a good start saves a fixed number of overflows. Exercise 18 bounds the time of Algorithms G and R in the worst case. Exercise 19 is about indexing from .
Lean
The repository tamnd/taocp-lean has no declaration for Chapter 2, so nothing here is stated in Lean. A model of this section would need an area as a function from cells to words, a list of stacks as a pair of sequences BASE and TOP, and Algorithm R as a function on such states. The theorem to state is that Algorithm R keeps the contents of each stack and sets the bases to the new values. The count of moves in the theorem above would be a statement about the mean of a function over all sequences of stack numbers. These statements are not stated yet.
Notes and history
The method of Algorithms G and R is due to J. Garwick. D. S. Wise and D. C. Watson studied it further in 1976, and A. S. Fraenkel suggested in 1979 to use pairs of stacks that grow toward each other. The book notes that no theory describes the mean behaviour of Algorithm G, and that experience shows that it works well when the memory is half full. When the memory is nearly full, Algorithm R is slow and the overflows are frequent. So the book suggests a stop in step G3 if is less than a minimum that the programmer chooses. The circular queue is the same mechanism as the buffers of Section 1.4.4 in C1.
Section 2.2.2 of TAOCP shows how to keep a linear list in consecutive words of memory. One list is easy: the address of an item is a linear function of its place, a stack needs one pointer, a queue needs two, and a circle of cells lets a queue run for ever. Two stacks can share one area by growing toward each other. Many stacks in one area are hard: when one stack is full, the others must give room, and this means moving whole blocks of memory. The section gives a simple method and a method of J. Garwick, Algorithms G and R, and it ends with the count of moves of the simple method. This chapter of C2 runs each of these by hand on a small area, costs them, and proves the count in a few lines.
The source of this page. Something is wrong? Report it.