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.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
Contents
  1. Lists in consecutive memory

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 jjth 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 nn 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 cc words. If the first node starts at L0+cL_0 + c, the jjth node starts at L0+cjL_0 + cj. A stack keeps one number TT, the number of nodes. An insertion adds 11 to TT and stores the item at X[T]X[T]. A deletion reads X[T]X[T] and subtracts 11. A queue keeps two numbers, FF and RR, 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 jj has a base BASE[j]\mathrm{BASE}[j] and a top TOP[j]\mathrm{TOP}[j], and its items are in the cells BASE[j]+1,…,TOP[j]\mathrm{BASE}[j] + 1, \ldots, \mathrm{TOP}[j]. The cells between TOP[j]\mathrm{TOP}[j] and BASE[j+1]\mathrm{BASE}[j+1] are the room of stack jj. When stack ii 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 X[1],X[2],…,X[n]X[1], X[2], \ldots, X[n] of cc words each. If L0L_0 is the base address, then LOC(X[j])=L0+cj\mathrm{LOC}(X[j]) = L_0 + cj, and so LOC(X[j+1])=LOC(X[j])+c\mathrm{LOC}(X[j+1]) = \mathrm{LOC}(X[j]) + c.

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

Explanation. The address is a linear function of jj, so no search is needed. A program that reads the jjth node loads jj in an index register and adds a constant. In MIX, with c=1c = 1: LD1 J then LDA L0,1.

Example. Let L0=1000L_0 = 1000 and c=3c = 3. Then X[1]X[1] is at 10031003, X[2]X[2] at 10061006 and X[5]X[5] at 10151015.

Definition C2.2.2.2.2 (a sequential stack, TAOCP eqs. 2.2.2(2) and (3)). A stack in the cells X[1],…,X[M]X[1], \ldots, X[M] has a number TT, the count of its items, with T=0T = 0 for the empty stack. The insertion of yy is: T←T+1T \leftarrow T + 1, X[T]←yX[T] \leftarrow y. The deletion into yy is: y←X[T]y \leftarrow X[T], T←T−1T \leftarrow T - 1.

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 aa, then bb. Then T=2T = 2 and X[1],X[2]=a,bX[1], X[2] = a, b. A deletion gives y=by = b and T=1T = 1. The cell X[2]X[2] still holds bb, 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 T←T+1T \leftarrow T + 1 the test "if T>MT > M then OVERFLOW" comes before the store. In the deletion, the test "if T=0T = 0 then UNDERFLOW" comes before the read.

Node: 2.2.2-(2a) · Lean: not yet stated

Explanation. When the test for overflow finds T>MT > M, the value of TT is already M+1M + 1. A program that goes on after the overflow must put TT back to MM, or must make room.

Example. A stack with M=4M = 4 cells gets the operations aa, bb, delete, cc, dd, ee, ff, delete, delete, delete, delete, delete. Each row is the state after the operation. A dash shows a deletion.

Operation Signal TT Cells X[1..4]X[1..4], items only
insert aa 1 aa
insert bb 2 a ba\ b
delete gives bb 1 aa
insert cc 2 a ca\ c
insert dd 3 a c da\ c\ d
insert ee 4 a c d ea\ c\ d\ e
insert ff OVERFLOW (TT was 55, set back to 44) 4 a c d ea\ c\ d\ e
delete gives ee 3 a c da\ c\ d
delete gives dd 2 a ca\ c
delete gives cc 1 aa
delete gives aa 0 empty
delete UNDERFLOW 0 empty

Program C2.2.2.2.4 (push and pop in MIX). Let rI1 hold TT, let SIZE hold MM, and let the cell X[j]X[j] 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 1+2+1+2=6u1 + 2 + 1 + 2 = 6u when there is no overflow, since INC1 and JG take 1u1u and CMP1 and STA take 2u2u. A pop takes 1+2+1=4u1 + 2 + 1 = 4u.

Definition C2.2.2.2.5 (a queue with moving pointers, TAOCP eqs. 2.2.2(4) and (5)). A queue keeps two numbers, FF and RR, with F=RF = R for the empty queue. The insertion is R←R+1R \leftarrow R + 1, X[R]←yX[R] \leftarrow y. The deletion is F←F+1F \leftarrow F + 1, y←X[F]y \leftarrow X[F].

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

Explanation. The items are in the cells X[F+1],…,X[R]X[F+1], \ldots, X[R]. Both numbers only grow. After MM 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 F=R=0F = R = 0.

Example. Start with F=R=0F = R = 0. Insert aa, bb, cc: R=3R = 3. Delete: F=1F = 1, y=ay = a. Delete: F=2F = 2, y=by = b. Insert dd: R=4R = 4. The queue holds c,dc, d in the cells X[3],X[4]X[3], X[4]. The cells X[1],X[2]X[1], X[2] 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 X[1],…,X[M]X[1], \ldots, X[M] as a circle, with X[1]X[1] after X[M]X[M]. The insertion is: if R=MR = M then R←1R \leftarrow 1, else R←R+1R \leftarrow R + 1; if R=FR = F then OVERFLOW; X[R]←yX[R] \leftarrow y. The deletion is: if F=RF = R then UNDERFLOW; if F=MF = M then F←1F \leftarrow 1, else F←F+1F \leftarrow F + 1; y←X[F]y \leftarrow X[F]. Both numbers start at 11.

Node: 2.2.2-(6a) · Lean: not yet stated

Explanation. The items are in the cells that follow FF up to RR, going round the circle. The state F=RF = R is the empty queue. So a full queue must not have F=RF = R, and the test for overflow refuses an insertion that would make them equal. For this reason the queue holds at most M−1M - 1 items (exercise 2.2.2-1). The cell at FF is always free, since it holds the last item that was deleted.

Example. Take M=5M = 5 and F=R=1F = R = 1.

Operation Signal FF RR Cells X[1..5]X[1..5]
insert AA 1 2 ⋅ A ⋅ ⋅ ⋅\cdot\ A\ \cdot\ \cdot\ \cdot
insert BB 1 3 ⋅ A B ⋅ ⋅\cdot\ A\ B\ \cdot\ \cdot
insert CC 1 4 ⋅ A B C ⋅\cdot\ A\ B\ C\ \cdot
insert DD 1 5 ⋅ A B C D\cdot\ A\ B\ C\ D
insert EE OVERFLOW (4 items) 1 5 ⋅ A B C D\cdot\ A\ B\ C\ D
delete gives AA 2 5 ⋅ ⋅ B C D\cdot\ \cdot\ B\ C\ D
delete gives BB 3 5 ⋅ ⋅ ⋅ C D\cdot\ \cdot\ \cdot\ C\ D
insert EE 3 1 E ⋅ ⋅ C DE\ \cdot\ \cdot\ C\ D
insert FF 3 2 E F ⋅ C DE\ F\ \cdot\ C\ D
insert GG OVERFLOW (4 items) 3 2 E F ⋅ C DE\ F\ \cdot\ C\ D
delete gives CC 4 2 E F ⋅ ⋅ DE\ F\ \cdot\ \cdot\ D
delete gives DD 5 2 E F ⋅ ⋅ ⋅E\ F\ \cdot\ \cdot\ \cdot
delete gives EE 1 2 ⋅ F ⋅ ⋅ ⋅\cdot\ F\ \cdot\ \cdot\ \cdot
delete gives FF 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 M=5M = 5 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 44 with 55 cells.

Example C2.2.2.2.7 (two stacks growing toward each other). An area of M=6M = 6 cells. Stack 1 has the top T1T_1 and uses the cells 1,2,…1, 2, \ldots. Stack 2 has the top T2T_2 and uses the cells 6,5,…6, 5, \ldots, with T2=7T_2 = 7 when it is empty. An insertion into stack 1 gives T1←T1+1T_1 \leftarrow T_1 + 1, and an insertion into stack 2 gives T2←T2−1T_2 \leftarrow T_2 - 1. Overflow means T1=T2−1T_1 = T_2 - 1 before the insertion.

Operation T1T_1 T2T_2 Cells 1 to 6
start 0 7 empty
insert pp in 1 1 7 p ⋅ ⋅ ⋅ ⋅ ⋅p\ \cdot\ \cdot\ \cdot\ \cdot\ \cdot
insert qq in 1 2 7 p q ⋅ ⋅ ⋅ ⋅p\ q\ \cdot\ \cdot\ \cdot\ \cdot
insert rr in 1 3 7 p q r ⋅ ⋅ ⋅p\ q\ r\ \cdot\ \cdot\ \cdot
insert uu in 2 3 6 p q r ⋅ ⋅ up\ q\ r\ \cdot\ \cdot\ u
insert vv in 2 3 5 p q r ⋅ v up\ q\ r\ \cdot\ v\ u
insert ww in 2 3 4 p q r w v up\ q\ r\ w\ v\ u
insert ss in 1 overflow no room: T1=T2−1T_1 = T_2 - 1
delete from 2 3 5 p q r ⋅ v up\ q\ r\ \cdot\ v\ u gives ww
insert ss in 1 4 5 p q r s v up\ q\ r\ s\ v\ u

Stack 1 holds 44 items and stack 2 holds 22 in the last row. A split of the area into two halves of 33 cells each would have overflowed at the item ss 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 L0L_0 in the address. In MIX the access to the IIth 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 2+2+2+2=8u2 + 2 + 2 + 2 = 8u against 4u4u 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 nn stacks in the cells L0+1,…,L∞L_0 + 1, \ldots, L_\infty. Stack ii has the items in the cells BASE[i]+1,…,TOP[i]\mathrm{BASE}[i] + 1, \ldots, \mathrm{TOP}[i], and BASE[n+1]=L∞\mathrm{BASE}[n + 1] = L_\infty. The insertion into stack ii is: TOP[i]←TOP[i]+1\mathrm{TOP}[i] \leftarrow \mathrm{TOP}[i] + 1; if TOP[i]>BASE[i+1]\mathrm{TOP}[i] > \mathrm{BASE}[i+1] then OVERFLOW, else store yy in the cell TOP[i]\mathrm{TOP}[i]. The deletion is: if TOP[i]=BASE[i]\mathrm{TOP}[i] = \mathrm{BASE}[i] then UNDERFLOW, else read the cell TOP[i]\mathrm{TOP}[i] and subtract 11 from TOP[i]\mathrm{TOP}[i]. At the start BASE[j]=TOP[j]=L0\mathrm{BASE}[j] = \mathrm{TOP}[j] = L_0 for 1≤j≤n1 \le j \le n.

Node: 2.2.2-(9) · Lean: not yet stated

Explanation. All the room of the area is at the right of stack nn at the start. A stack that is full is a stack with TOP[i]=BASE[i+1]\mathrm{TOP}[i] = \mathrm{BASE}[i+1]. The two statements for the insertion and the deletion are (9) and (10), and the start values are (11).

Example. With n=3n = 3, L0=0L_0 = 0 and L∞=6L_\infty = 6: BASE=(0,0,0)\mathrm{BASE} = (0, 0, 0), TOP=(0,0,0)\mathrm{TOP} = (0, 0, 0) and BASE[4]=6\mathrm{BASE}[4] = 6. Stack 3 can take 66 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 ii, TAOCP Section 2.2.2). Choose one of three cases. In case (a), find the smallest kk with i<k≤ni < k \le n and TOP[k]<BASE[k+1]\mathrm{TOP}[k] < \mathrm{BASE}[k+1]. If it exists, move the cells BASE[i+1]+1,…,TOP[k]\mathrm{BASE}[i+1] + 1, \ldots, \mathrm{TOP}[k] up by one cell, last cell first, and add 11 to BASE[j]\mathrm{BASE}[j] and TOP[j]\mathrm{TOP}[j] for i<j≤ki < j \le k. In case (b), when no such kk exists, find the largest kk with 1≤k<i1 \le k < i and TOP[k]<BASE[k+1]\mathrm{TOP}[k] < \mathrm{BASE}[k+1], move the cells BASE[k+1]+1,…,TOP[i]−1\mathrm{BASE}[k+1] + 1, \ldots, \mathrm{TOP}[i] - 1 down by one cell, first cell first, and subtract 11 from BASE[j]\mathrm{BASE}[j] and TOP[j]\mathrm{TOP}[j] for k<j≤ik < j \le i. 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 ii, and the stacks between ii and kk keep their order. In case (b) the insertion has already raised TOP[i]\mathrm{TOP}[i], so the cells to move end at TOP[i]−1\mathrm{TOP}[i] - 1, and the program then lowers TOP[i]\mathrm{TOP}[i] 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 aa and bb in stack 1, cc in 3, dd in 2, delete from 1, insert ee in 3, ff and gg in 1, hh 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 aa in 1 (a), k=3k = 3 0, 1, 1 1, 1, 1 a ⋅ ⋅ ⋅ ⋅ ⋅a\ \cdot\ \cdot\ \cdot\ \cdot\ \cdot 0
insert bb in 1 (a), k=3k = 3 0, 2, 2 2, 2, 2 a b ⋅ ⋅ ⋅ ⋅a\ b\ \cdot\ \cdot\ \cdot\ \cdot 0
insert cc in 3 none 0, 2, 2 2, 2, 3 a b c ⋅ ⋅ ⋅a\ b\ c\ \cdot\ \cdot\ \cdot 0
insert dd in 2 (a), k=3k = 3 0, 2, 3 2, 3, 4 a b d c ⋅ ⋅a\ b\ d\ c\ \cdot\ \cdot 1
delete from 1 none 0, 2, 3 1, 3, 4 a ⋅ d c ⋅ ⋅a\ \cdot\ d\ c\ \cdot\ \cdot 1
insert ee in 3 none 0, 2, 3 1, 3, 5 a ⋅ d c e ⋅a\ \cdot\ d\ c\ e\ \cdot 1
insert ff in 1 none 0, 2, 3 2, 3, 5 a f d c e ⋅a\ f\ d\ c\ e\ \cdot 1
insert gg in 1 (a), k=3k = 3 0, 3, 4 3, 4, 6 a f g d c ea\ f\ g\ d\ c\ e 4
insert hh in 2 (c) 0, 3, 4 3, 4, 6 unchanged 4
delete from 2 none 0, 3, 4 3, 3, 6 a f g ⋅ c ea\ f\ g\ \cdot\ c\ e 4

At the insertion of gg the three cells dd, cc, ee move up one cell each, and a fa\ f stay. Stack 2 holds one item and needs to move only when it is below the overflowing stack. The insertion of hh finds no free cell: stack 1 has 33 cells, stack 2 has 11 and stack 3 has 22, and 3+1+2=63 + 1 + 2 = 6 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 BASE[j]=TOP[j]=L0+⌊j−1n(L∞−L0)⌋\mathrm{BASE}[j] = \mathrm{TOP}[j] = L_0 + \lfloor \frac{j-1}{n}(L_\infty - L_0) \rfloor for 1≤j≤n1 \le j \le n.

Node: 2.2.2-(13) · Lean: not yet stated

Explanation. The area is cut in nn nearly equal parts. The start saves at most a fixed number of overflows, since the later overflows are due to unequal growth.

Example. With n=3n = 3, L0=0L_0 = 0 and L∞=12L_\infty = 12 the bases are ⌊0⌋,⌊4⌋,⌊8⌋=0,4,8\lfloor 0 \rfloor, \lfloor 4 \rfloor, \lfloor 8 \rfloor = 0, 4, 8. Stack 3 has the cells 99 to 1212.

Theorem C2.2.2.2.12 (the mean number of moves, TAOCP eq. 2.2.2(14)). Let mm insertions be made, each into a stack at∈{1,…,n}a_t \in \{1, \ldots, n\}, and let each of the nmn^m sequences a1,…,ama_1, \ldots, a_m 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 12(1−1n)(m2).\frac{1}{2}\left(1 - \frac{1}{n}\right)\binom{m}{2}.

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 n=3n = 3 and m=4m = 4 the value is 12⋅23⋅6=2\frac12 \cdot \frac23 \cdot 6 = 2. A check by python over all 34=813^4 = 81 sequences gives the total 162162 moves, the mean 22. For n=2n = 2 and m=3m = 3 the value is 12⋅12⋅3=34\frac12 \cdot \frac12 \cdot 3 = \frac34. For n=3n = 3 and m=5m = 5 it is 103\frac{10}{3}, and for n=4n = 4 and m=4m = 4 it is 94\frac94, and python gives the same values.

Proof. Claim: at each time the stacks 1,…,n−11, \ldots, n - 1 are tight, that is TOP[k]=BASE[k+1]\mathrm{TOP}[k] = \mathrm{BASE}[k+1] for k<nk < n, and stack nn has all the room. This is true at the start by (11). An insertion into stack nn needs no move. An insertion into stack i<ni < n overflows, since the stack is tight. In the case (a) the smallest k>ik > i with room is k=nk = n, because the stacks i+1,…,n−1i + 1, \ldots, n - 1 are tight. So the move shifts all items of the stacks i+1,…,ni + 1, \ldots, n up by one cell, and the number of moved words is the number of those items. After the move the stacks 1,…,n−11, \ldots, n - 1 are tight again. So the cost of an insertion into stack ii is the number of earlier insertions that went into the stacks with a number larger than ii. Take the ttth insertion. Each of the t−1t - 1 earlier insertions goes into a stack of the number larger than ata_t with probability (n−at)/n(n - a_t)/n, and ata_t is uniform, so the mean of this probability is 1n∑i=1nn−in=n−12n\frac1n \sum_{i=1}^{n} \frac{n-i}{n} = \frac{n-1}{2n}. The mean cost of the ttth insertion is (t−1)n−12n(t-1)\frac{n-1}{2n}. The sum over t=1,…,mt = 1, \ldots, m is n−12n(m2)\frac{n-1}{2n}\binom m2, which is the claim. □\square

Algorithms and programs

Algorithm 2.2.2G (reallocate the stacks after an overflow, TAOCP Algorithm 2.2.2G). Stack ii has overflowed in the insertion (9), so TOP[i]\mathrm{TOP}[i] is already larger by 11. The array OLDTOP[j]\mathrm{OLDTOP}[j] holds the value of TOP[j]\mathrm{TOP}[j] 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 nn 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, SUM\mathrm{SUM} is the number of free cells, counted as if the new item of stack ii were in its stack. In G5, τj=(j−1)α+β∑k<jD[k]\tau_j = (j-1)\alpha + \beta \sum_{k<j} D[k], so τj\tau_j grows with jj, and the room that stack j−1j - 1 gets is ⌊τj⌋−⌊τj−1⌋≥0\lfloor\tau_j\rfloor - \lfloor\tau_{j-1}\rfloor \ge 0. The sum of all the room is below SUM\mathrm{SUM}, because τn+1=nα+β INC=0.1 SUM+0.9 SUM=SUM\tau_{n+1} = n\alpha + \beta\,\mathrm{INC} = 0.1\,\mathrm{SUM} + 0.9\,\mathrm{SUM} = \mathrm{SUM}, and the stack nn keeps the rest. So the new bases are increasing, each stack has a place for its items, and the stack ii has a place for its new item.

A worked run, with moves up. Let n=3n = 3, L0=0L_0 = 0, L∞=20L_\infty = 20, BASE=(0,4,12)\mathrm{BASE} = (0, 4, 12), TOP=(3,13,15)\mathrm{TOP} = (3, 13, 15) and OLDTOP=(1,8,14)\mathrm{OLDTOP} = (1, 8, 14). Stack 2 overflowed: its cells are 5,…,125, \ldots, 12, and TOP[2]=13>BASE[3]=12\mathrm{TOP}[2] = 13 > \mathrm{BASE}[3] = 12. The steps give:

Step Values
G1, G2 SUM=20−3−9−3=5\mathrm{SUM} = 20 - 3 - 9 - 3 = 5; D=(2,5,1)D = (2, 5, 1); INC=8\mathrm{INC} = 8
G3 SUM≥0\mathrm{SUM} \ge 0, go on
G4 α=0.1⋅5/3=1/6\alpha = 0.1 \cdot 5 / 3 = 1/6; β=0.9⋅5/8=9/16\beta = 0.9 \cdot 5 / 8 = 9/16
G5, j=2j = 2 τ=0+1/6+2⋅9/16=31/24\tau = 0 + 1/6 + 2 \cdot 9/16 = 31/24; NEWBASE[2]=0+3+1−0=4\mathrm{NEWBASE}[2] = 0 + 3 + 1 - 0 = 4
G5, j=3j = 3 τ=31/24+1/6+5⋅9/16=205/48\tau = 31/24 + 1/6 + 5 \cdot 9/16 = 205/48; NEWBASE[3]=4+9+4−1=16\mathrm{NEWBASE}[3] = 4 + 9 + 4 - 1 = 16

Stack 2 has TOP[2]−BASE[2]=9\mathrm{TOP}[2] - \mathrm{BASE}[2] = 9 cells in G5, and the pending item is counted in this size. Stack 1 stays, stack 2 stays, and stack 3 moves up by 44. After the algorithm, the stack 2 has TOP[2]=13\mathrm{TOP}[2] = 13 and the free cells are 11 after stack 1, 33 after stack 2 and 11 after stack 3, in all 5=SUM5 = \mathrm{SUM}.

A worked run, with moves down. Let n=3n = 3, L∞=20L_\infty = 20, BASE=(0,6,10)\mathrm{BASE} = (0, 6, 10), TOP=(3,8,21)\mathrm{TOP} = (3, 8, 21) and OLDTOP=(3,8,14)\mathrm{OLDTOP} = (3, 8, 14). Stack 3 has overflowed, since TOP[3]=21>20\mathrm{TOP}[3] = 21 > 20.

Step Values
G1, G2 SUM=20−3−2−11=4\mathrm{SUM} = 20 - 3 - 2 - 11 = 4; D=(0,0,7)D = (0, 0, 7); INC=7\mathrm{INC} = 7
G4 α=0.4/3=2/15\alpha = 0.4/3 = 2/15; β=3.6/7=18/35\beta = 3.6/7 = 18/35
G5, j=2j = 2 τ=2/15\tau = 2/15; NEWBASE[2]=0+3+0−0=3\mathrm{NEWBASE}[2] = 0 + 3 + 0 - 0 = 3
G5, j=3j = 3 τ=2/15+2/15+0=4/15\tau = 2/15 + 2/15 + 0 = 4/15; NEWBASE[3]=3+2+0−0=5\mathrm{NEWBASE}[3] = 3 + 2 + 0 - 0 = 5

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 nn. The cost of G6 is the cost of Algorithm R, which moves each item at most once. So one repacking costs O(n)O(n) plus the number of items that change place, and at most O(n+O(n + the number of items)).

Algorithm 2.2.2R (relocate the stacks, TAOCP Algorithm 2.2.2R). The stacks are moved to the bases NEWBASE[j]\mathrm{NEWBASE}[j] and BASE\mathrm{BASE} and TOP\mathrm{TOP} are adjusted. The stacks that move down are moved first, in the order 1,2,…,n1, 2, \ldots, n. Then the stacks that move up are moved, in the order n,n−1,…,1n, n-1, \ldots, 1.

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 jj starts at n+1n + 1 after R2 has ended with j>nj > n.

Invariant. In R2 to R3, the stacks 1,…,j−11, \ldots, j-1 that had to move down are in their new place, and the others are as they were. A stack jj that moves down moves into the cells from NEWBASE[j]+1\mathrm{NEWBASE}[j] + 1 up. This region does not meet the stacks k<jk < j in their new places, since NEWBASE[j]≥NEWBASE[k]+\mathrm{NEWBASE}[j] \ge \mathrm{NEWBASE}[k] + the size of stack kk. It does not meet the old place of a stack k<jk < j that moves up either, since such a stack has NEWBASE[k]>BASE[k]\mathrm{NEWBASE}[k] > \mathrm{BASE}[k], so NEWBASE[j]≥NEWBASE[k]+size[k]>TOP[k]\mathrm{NEWBASE}[j] \ge \mathrm{NEWBASE}[k] + \text{size}[k] > \mathrm{TOP}[k]. The same argument, turned around, holds for the moves up.

A worked run. The second run of Algorithm G above gives NEWBASE=(0,3,5)\mathrm{NEWBASE} = (0, 3, 5) with BASE=(0,6,10)\mathrm{BASE} = (0, 6, 10) and the stack 3 shortened by 11 to TOP[3]=20\mathrm{TOP}[3] = 20. The cells 1 to 20 are shown by items only: stack 1 is x1x2x3\mathrm{x}_1\mathrm{x}_2\mathrm{x}_3, stack 2 is y1y2\mathrm{y}_1\mathrm{y}_2 and stack 3 is z1,…,z10\mathrm{z}_1, \ldots, \mathrm{z}_{10}.

Step jj dd BASE TOP Cells 1 to 20
start 0, 6, 10 3, 8, 20 x1x2x3 ⋅ ⋅ ⋅ y1y2 ⋅ ⋅ z1…z10\mathrm{x}_1\mathrm{x}_2\mathrm{x}_3\,\cdot\,\cdot\,\cdot\,\mathrm{y}_1\mathrm{y}_2\,\cdot\,\cdot\,\mathrm{z}_1 \ldots \mathrm{z}_{10}
R3 2 3 0, 3, 10 3, 5, 20 x1x2x3 y1y2 ⋅⋅⋅⋅⋅ z1…z10\mathrm{x}_1\mathrm{x}_2\mathrm{x}_3\,\mathrm{y}_1\mathrm{y}_2\,\cdot\cdot\cdot\cdot\cdot\,\mathrm{z}_1 \ldots \mathrm{z}_{10}
R3 3 5 0, 3, 5 3, 5, 15 x1x2x3 y1y2 z1…z10 ⋅ ⋅ ⋅ ⋅ ⋅\mathrm{x}_1\mathrm{x}_2\mathrm{x}_3\,\mathrm{y}_1\mathrm{y}_2\,\mathrm{z}_1 \ldots \mathrm{z}_{10}\,\cdot\,\cdot\,\cdot\,\cdot\,\cdot

Then R2 ends with j>nj > n, and R4 finds no stack that moves up, so the algorithm stops. The moves are 2+10=122 + 10 = 12 words. After the step G6 gives TOP[3]=16\mathrm{TOP}[3] = 16, and the new item z11\mathrm{z}_{11} goes into the cell 1616.

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 LL and in R5 decreasing LL, so that a block that moves by dd cells does not overwrite a cell that is not yet copied. The loop in R2 to R3 ends when j>nj > n, and the loop in R4 to R5 ends when j=1j = 1.

Cost. At most one move of each item, so at most the number of items, plus O(n)O(n) 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 M=5M = 5: 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 INC\mathrm{INC} can be zero in step G4: the stack that overflowed has grown by at least one cell since the last repacking, so INC≥1\mathrm{INC} \ge 1. 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 00.

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 SUM\mathrm{SUM} 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 12(1−1n)(m2)\tfrac12(1-\tfrac1n)\binom m2 in a few lines.

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