Proofs: External queue
These are the human proofs of External queue, at full length.
The external queue returns items in FIFO order and makes at most 2n/B block transfers full
This proof shows: correctness, complexity.
We use the pseudocode of the page with block size . The state is the head , the disk blocks and the tail . Let .
Invariant. At the start and after each operation: (I1) is the abstract queue, from the front to the back; (I2) each has exactly items; (I3) and .
Base. For the empty queue all three parts are empty, so the invariant holds.
Enqueue. Line L1 appends to and so to , which is now the old queue followed by . Now . If , lines L3 and L4 append as a block and empty . This does not change , the new block has items, and . Otherwise already. does not change.
Dequeue. If is not empty, only line L10 runs: it removes the first item of , which is the first item of . If is empty and , line L7 makes and removes from the disk part, so does not change and . If is empty and , then . If is empty the queue is empty, and line L8 reports the error. Otherwise line L9 moves into , which does not change , and gives and . In both cases is now not empty, and line L10 removes and returns the first item of , the front of the queue. (I2) holds because no block was written, and (I3) holds because has at most items and only shrinks in line L10.
By induction the invariant holds after every operation, and each dequeue returns the oldest item that is still in the queue.
Theorem (I/O bound). Starting from the empty queue, a sequence with enqueues and any number of dequeues, operations in all, makes at most writes and at most as many reads, so at most block transfers.
Proof. Writes. Follow one item . It enters in its enqueue (line L1). It leaves only through line L3, into a disk block, or through line L9, into . From a disk block it goes only to (line L7), and from only out of the queue (line L10). So is in at most one written block. Each write contains exactly items, all different, so the number of writes satisfies , and .
Reads. Each read in line L7 removes one block from the disk part, and blocks enter the disk part only by writes. So the number of reads is at most .
Hence the transfers number at most .