Proofs: External stack
These are the human proofs of External stack, at full length.
The external stack returns the right items and makes at most n/B block transfers full
This proof shows: correctness, complexity.
We use the pseudocode of the page with block size . The state is the list of disk blocks and the buffer . Let be their concatenation.
Invariant. At the start and after each operation: (I1) is the abstract stack, from the bottom to the top; (I2) each has exactly items; (I3) .
Base. The empty stack has and empty, so is empty and (I2) and (I3) hold.
Push. If , lines L2 and L3 append the first items of to the disk part as a new block and remove them from . The concatenation does not change, the new block has items, and . Line L4 appends to , so becomes the old stack followed by , which is the new stack. Before line L4, : either it was below , or it is now . So after it.
Pop. If the abstract stack is empty, is empty, so is empty and , and line L6 reports the error. Otherwise, if is empty, then , and line L7 moves the last block into , which does not change and gives . Line L8 removes the last item of , which is the last item of , the top of the stack. (I1) holds, and (I2) and (I3) hold because no block was written and only became smaller or equal to .
So by induction the invariant holds after every operation, and each pop returns the top of the stack.
Theorem (I/O bound). Starting from the empty stack, any sequence of pushes and pops makes at most block transfers.
Proof. Each operation makes at most one transfer: a push writes only if , and a pop reads only if is empty, and these are not both true in one operation. Let the transfers happen in the operations of the sequence, numbered from 1.
The first transfer is a write, since a read needs , and grows only by writes. A write needs , and each operation before it is a push or a pop without transfer, which changes by one, starting from 0. So at least operations come before it, and .
After the transfer in , and before the change of line L4 or L8, . So at the end of , is after a write or after a read. The transfer in needs or at the start of . The operations make no transfer, so each changes by exactly one. From or to or 0 needs at least such steps, so .
Hence , and since , when . So .