Proofs: Interval containment by sorting
These are the human proofs of Interval containment by sorting, at full length.
Interval containment by sorting keeps one copy of each maximal interval full
This proof shows: correctness, termination.
We use the pseudocode of the page. The intervals are nonempty and of one type, so holds if and only if and . Write when comes before in . By line L1, if and only if , or the left ends are equal and , or both end points are equal and .
Claim. CONTAINMENT-SORT stops after steps. For each , if nil, then and . And $parent[k] = $ nil if and only if is maximal (no interval of the family properly contains it) and no index has .
Lemma 1. If and , then , unless and .
Proof. We have . If , then . Otherwise the left ends are equal and . If , then . Otherwise the intervals are equal, and exactly when .
Lemma 2. If , then . So if also , then .
Proof. The sort is by left end first.
Invariant. Let be the set of indices processed so far. Before each step of the loop of line L3:
(I) If is empty, $best = $ nil. Otherwise , , and $parent[best] = $ nil.
Initialization. Line L2 sets $best = $ nil and is empty.
Maintenance. Let be the next index. If $best = $ nil, then is empty, line L6 sets , and stays nil, so (I) holds for . Otherwise, if , line L5 sets , and keeps its value, which is still the maximum of the right ends over ; is unchanged, because . If , then is the new maximum, line L6 sets , and is nil. In both cases (I) holds.
Each step decides its interval. Consider the step for , with the set of indices before in .
If line L5 runs, then , so and , and . By Lemma 2, .
If line L6 runs, then either is empty or by (I). So no has , and no contains . Suppose properly contains . Then , so by Lemma 1, a contradiction. Suppose with . Then by the tie rule, and contains , a contradiction. So is maximal and has no equal copy with a smaller index.
Conversely. If is properly contained in some , then by Lemma 1, so is not empty and by (I); line L5 runs. If for some , then , and again ; line L5 runs. So in both cases nil.
Termination. Line L1 is a sort of keys, and the loop of line L3 runs once for each of the indices. Each is written at most once, in the step for , so later steps do not change it.
Conclusion. The two parts above give the claim: every non-nil is a container of with a different index, and is nil exactly for the first copy, by index, of each maximal interval.