Proofs: Sweep for union length
These are the human proofs of Sweep for union length, at full length.
The sweep for union length returns the measure of the covered set full
This proof shows: correctness, termination.
We use the pseudocode of the page, with the test of line L6 written as for a fixed integer ; the page uses . The intervals have end points , , and each end point may be open or closed. Let be the coverage and .
Claim. SWEEP-UNION-LENGTH returns , the Lebesgue measure of , for any order of the events with equal coordinates.
Proof. Let be the events in the order of line L3, with coordinates . If the loop does not run and the result is . Assume .
Step 1: the coverage on a gap. Fix with and . Since the events are sorted, the events are exactly the events with coordinate at most . So after line L7 has processed ,
Each with also has , so the difference is . Let . No end point lies in this open gap. An interval contains if and only if (the end points are not in the gap, so their type does not matter), if and only if and , if and only if . Hence for every in the gap.
Step 2: the invariant. For let . We show:
(I) Just before line L6 processes , for , and also after the loop for : and .
Initialization. Before , , so line L6 adds nothing, and line L8 sets . Every point is in no interval, and the point has measure 0, so . So (I) holds for .
Maintenance. Assume and before line L6 handles .
- If , line L6 adds or nothing, so , since .
- If , then is the last event at , and by Step 1 the current equals on the whole gap . Line L6 adds if , that is, if the gap lies in ; otherwise the gap is disjoint from and nothing is added. The point has measure 0. So .
Lines L7 and L8 then set .
Termination. The loop of line L5 processes each of the events once, so it stops after steps. Then . Every point is to the right of all end points and lies in no interval, so and . Line L9 returns it.
Tie order. The proof used the order of events with equal coordinates only through Step 1, which looks at the counter after the last event of a coordinate. That value does not depend on the order within the coordinate. So every tie order gives the same result.
Cost. Line L3 sorts events; the loop does steps of work each.