Proofs: Sweep for maximum overlap
These are the human proofs of Sweep for maximum overlap, at full length.
The sweep for maximum overlap returns the depth full
This proof shows: correctness, termination.
We use the pseudocode of the page. The input is intervals with end points , all closed with , or all half-open with , or all open with . Let and , with for .
Claim. SWEEP-MAX-OVERLAP returns . For closed and half-open intervals, ; for open intervals, for all in an open gap with . For it returns .
Proof. If the loop does not run, and the result is . Let , and let be the events in the order of line L3. Call an interval active after step if its start event is among and its end event is not. Each interval has its start event before its end event in the order of line L3: if this follows from the coordinates, and if (closed only) the start comes first by the tie rule.
Step 1 (the counter). By induction on , after line L6 of step the variable equals the number of intervals active after step . Before the loop, . A event makes one more interval active, and a event makes one active interval inactive (its start came earlier). So follows .
Step 2 (no overshoot). Fix a step with event coordinate . Every active interval has its start among , so , and its end not among them, so .
- Closed: the active intervals all contain , so .
- Half-open or open, a start event: all ends at come before all starts at , so each end at is already processed, and an active interval has . An active interval with started at this coordinate. Each active interval then contains every point of with over the active ones, because . So .
- Half-open or open, an end event: no start at is processed yet, so each active interval has , and all contain the points of for . So .
Hence at all times, since is a maximum of values or 0.
Step 3 (the depth is reached). Let be a point with , and let be the set of intervals that contain ; . Let , so , and let be the last start event with coordinate .
- Closed: each has . Its start has coordinate at most , and it is processed by step , since the starts at are processed by the last of them. Its end has coordinate ; if it comes later, and if it comes after all starts at by the tie rule. So all of is active after step , and .
- Half-open or open: each has and (for half-open, ; for open, too). Its start is processed by step , and its end has a coordinate greater than , so it is not processed. So .
With Step 2, . Line L7 then sets at step or has done so earlier, and it never sets above .
Step 4 (the witness). Let be the step at which line L7 last changes , to the value . A change needs to grow, so is a start event; let be its coordinate. By Step 2 for a start event, the intervals active after step all contain in the closed case, and all contain in the half-open and open cases. For half-open intervals, each active one has , so it contains too. So for closed and half-open intervals, and on for open intervals.
Termination. The loop processes each of the events once.
Cost. Line L3 sorts events; the loop does steps of work.