Proofs: Skyline sweep
These are the human proofs of Skyline sweep, at full length.
The skyline sweep outputs the shortest list of key points of the skyline full
This proof shows: correctness, termination.
Setting. The input is buildings with and . Building is active at when . Let , and when no building is active. Let be the distinct values of all and , as built in line L1. We write .
Lemma 1 ( is a step function on the events). For , is constant on . Also for and for .
Proof. Take with . If building is active at but not at , then and , so , which contains no value of . If is active at but not at , then , the same contradiction. So the same buildings are active at and at , and . For , each , so no building is active. For , each , so no building is active.
Lemma 2 (heap invariant). Line L2 sorts by . For , after line L8 in the iteration for :
(a) The entries inserted so far are exactly the buildings with , and is their number. (b) Each building that is active at has its entry in . (c) If is not empty, its top entry belongs to a building that is active at .
Proof. Part (a), by induction on . Before the first iteration no entry was inserted and . Assume that before the iteration for the inserted entries are those with (none when ). Since is sorted by and each is a value of , the next buildings in have , and those with come first. The loop of line L5 inserts exactly them and stops at the first building with or at . No lies strictly between and , so the inserted entries are now those with .
Part (b). Let building be active at . By (a) its entry was inserted. An entry is removed only by line L8 in some iteration , and only when its . But , so the entry of was never removed.
Part (c). The loop of line L7 stops only when is empty or the top entry has . The top entry was inserted, so by (a) its . With its building is active at .
Lemma 3 (the height). After line L9 in the iteration for , .
Proof. If is empty, then by Lemma 2(b) no building is active at , so . Otherwise let be the top entry, so is the largest height in . By Lemma 2(c), is the height of an active building, so . By Lemma 2(b), each active building has its entry in , so each active height is at most , and . So .
Theorem. SKYLINE-SWEEP terminates and returns the list , where by convention. This list is the unique shortest list of key points: its values increase, consecutive heights differ, on for consecutive points , of , and before the first point and from the last point on.
Proof. Termination. The loop of line L4 runs times. Each run of line L6 increases by 1 and , so line L6 runs at most times in total. Each run of line L8 removes one of the at most inserted entries, so line L8 runs at most times in total. So the algorithm stops.
The output. We show by induction on that after the iteration for , and is the part of with . Before the first iteration and is empty. In the iteration for , Lemma 3 gives , and by the hypothesis . Lines L10 and L11 append exactly when , and then set . If , then already equals . At the end, .
describes . By Lemma 1, is constant on each , and between two consecutive points of the value does not change at the events in between, because those events are not in . So on . Before the first point of , is 0 on by Lemma 1 and does not change at the events before the first point. After the last point of , does not change, and by Lemma 1, so and from on. Consecutive heights in differ because each point of has a height different from the value of just before it, which is the height of the previous point.
is the shortest list. Let be any list of key points that describes in this way. At each with , changes value at : it is on and at . A list that describes by values on half-open pieces must start a new piece at each point where changes, so has a point at . So contains a point at each of , and . If also consecutive heights of differ, has no point where does not change, so .
Cost of the heap operations. The termination argument also gives the cost: at most INSERT and at most EXTRACT-MAX calls in total, each on a heap of at most entries, so time for the heap, plus for the sorts of lines L1 and L2.