Proofs: Interval partitioning greedy
These are the human proofs of Interval partitioning greedy, at full length.
The partitioning greedy uses exactly as many resources as the depth full
This proof shows: correctness, termination.
We use the pseudocode of the page. The intervals are nonempty and of one type, with . Say that an interval is over by when for half-open and open intervals and for closed intervals. Line L4 tests whether the interval at the heap minimum is over by . Let be the depth, the largest number of intervals that contain one point.
Lemma 1 (over by means disjoint). Let and with . Then if and only if is over by .
Proof. For closed intervals, if and only if , since and contains . For half-open intervals, contains and the points just right of it, and contains every point of , so they are disjoint if and only if . For open intervals, contains the points just right of , and contains every point of ; if , the points just right of and left of are in both, since , and if every point of is less than .
Lemma 2 (two intervals through a point). Under the same assumption , if is not over by , then and both contain the point . Here for closed and half-open intervals. For open intervals, is a fixed point with and no end point of any input interval in ; it exists because there are finitely many end points.
Proof. For closed intervals, and , so . For half-open intervals, and , so . For open intervals, and . The end points and are greater than , so , and ; so .
Invariant. Let be the set of indices processed so far. Before each step of the loop of line L3:
(I) The heap holds exactly one pair for each , where is the right end of the interval , the interval of with label that was processed last. Two intervals of with the same label are disjoint.
Initialization. is empty, and the heap is empty.
Maintenance. Let be the next index, . Every interval of starts at or before , by the order of line L1.
Line L5 runs. The minimum pair is , and is over by . The intervals with label , in processing order, are , and took label when was over by , so . So every has and is over by whenever is: if and (or ), the same holds for . By Lemma 1, each is disjoint from . Line L7 gives label and puts back, so (I) holds.
Line L6 runs. Then gets the new label , which no other interval has, and line L7 adds its pair. (I) holds.
Optimality. Consider the step in which line L6 opens resource , at . If the heap is not empty, its minimum is not over by , so no pair is: each for has right end at least the minimum, so it is not over by either. Each starts at or before . By Lemma 2, applied to and , each and contain the point , which depends only on . So intervals contain one point, and . On the other side, the intervals through one point pairwise meet, so a valid labeling gives them different labels, and every valid labeling has at least labels. The final is the largest value opened, so , and by (I) at the end the labeling is valid.
Termination. Line L1 sorts keys. The loop runs times, each with at most one EXTRACT-MIN and one INSERT on a heap of at most pairs.