Algorithm Search⌘K

Proofs: Calendar queue

These are the human proofs of Calendar queue, at full length.

The calendar queue returns an event of smallest time full

This proof shows: correctness, termination.

Let vb(t)=⌊t/w⌋\mathrm{vb}(t) = \lfloor t/w \rfloor for the current width ww. Since t↦t/wt \mapsto t/w is non-decreasing for w>0w > 0, and so is the floor, vb\mathrm{vb} is non-decreasing: t≤t′t \le t' implies vb(t)≤vb(t′)\mathrm{vb}(t) \le \mathrm{vb}(t'), and so vb(t)<vb(t′)\mathrm{vb}(t) < \mathrm{vb}(t') implies t<t′t < t'. Events are compared by the pair (time, arrival number), and vb\mathrm{vb} depends on the time only.

Invariants. Between operations, with nn events in the queue:

(B) each event with time tt is in the bucket B[vb(t) mod N]B[\mathrm{vb}(t) \bmod N], exactly once, and each bucket is sorted by (time, arrival);

(V) if n≥1n \ge 1, then vb(t)≥V\mathrm{vb}(t) \ge V for each event in the queue.

Both hold for the empty queue.

Lemma 1. Let (B) and (V) hold with n≥1n \ge 1. If B[V mod N]B[V \bmod N] is not empty and its first event xx has vb(x)=V\mathrm{vb}(x) = V, then xx is a smallest event of the queue. Else no event has the virtual bucket VV, and (V) holds with V+1V + 1.

Proof. Let yy be any event. If vb(y)>V=vb(x)\mathrm{vb}(y) > V = \mathrm{vb}(x), then yy has a larger time than xx. Else vb(y)=V\mathrm{vb}(y) = V by (V), so yy is in the same bucket B[V mod N]B[V \bmod N] by (B), and xx comes first in that sorted bucket, so x≤yx \le y. For the second part: if some event yy had vb(y)=V\mathrm{vb}(y) = V, the bucket would not be empty, and its first event xx would satisfy V≤vb(x)≤vb(y)=VV \le \mathrm{vb}(x) \le \mathrm{vb}(y) = V, since x≤yx \le y in the bucket and vb\mathrm{vb} is non-decreasing. So all events have vb≥V+1\mathrm{vb} \ge V + 1. □\square

INSERT. Line L2 puts the event into the bucket of (B) at its sorted place, so (B) holds. If the queue was empty, line L3 sets V=vb(t)V = \mathrm{vb}(t) and (V) holds for the one event. Else (V) held for the old events, and line L3 lowers VV if needed, so it holds for the new one too, and lowering VV keeps it for the old ones.

EXTRACT-MIN. Let n≥1n \ge 1. By Lemma 1, each iteration of the loop of lines L6 to L10 either finds a smallest event, removes it, and returns it, or moves VV up by one and keeps (V). The loop runs at most NN times. If it finds no event, line L11 compares the first events of all buckets; by (B), the first event of each bucket is its smallest, so the result is a smallest event of the queue. Line L12 sets V=vb(t)V = \mathrm{vb}(t) for this smallest tt; every event has a time at least tt, so vb≥V\mathrm{vb} \ge V, and (V) holds. Removing the first event of a bucket keeps (B), and removing any event keeps (V).

RESIZE. It takes all events out, picks a new width w′>0w' > 0 and a new N′N', puts each event into B[vb′(t) mod N′]B[\mathrm{vb}'(t) \bmod N'] and sorts each bucket, so (B) holds for the new vb′\mathrm{vb}'. It sets VV to the least vb′(t)\mathrm{vb}'(t), so (V) holds. Lemma 1 holds for any width, so the next operations are correct.

Theorem. Each operation stops, after at most NN iterations of the scan plus one direct search and one resize. Each EXTRACT-MIN on a non-empty queue returns a smallest event, and events with equal times leave in the order of arrival.

Proof. Termination is the bound on the loop. The answer is a smallest pair (time, arrival) by Lemma 1 and the analysis of the direct search, which gives the second claim. ■\blacksquare

Back to Calendar queue