Proofs: Two-pointer interval list intersection
These are the human proofs of Two-pointer interval list intersection, at full length.
The two-pointer scan returns every nonempty intersection in order full
This proof shows: correctness, termination.
We use the pseudocode of the page. The lists are and . Write and . For closed intervals the lists are sorted and disjoint when and for all and . For half-open intervals the condition is and . Line L5 tests for closed intervals and for half-open ones.
Claim. INTERSECT-LISTS stops, and it returns the list of all nonempty sets , each once, sorted from left to right.
Lemma 1 (line L5). In the step for the pair , line L5 appends a piece if and only if , and the piece it appends is .
Proof. For closed intervals, if and only if , that is . So the intersection is , and it is nonempty if and only if . For half-open intervals the same argument gives , which is nonempty if and only if .
Lemma 2 (the discard). If , then for every . If , then for every .
Proof. Take . Since is sorted and disjoint, . For closed intervals , so every point of is greater than , and every point of is at most . For half-open intervals , so every point of is at least , and every point of is less than . In both cases the two sets are disjoint. The second statement is the same with the roles of and exchanged.
Lemma 3 (order). If , then every point of is less than every point of , and the same holds for .
Proof. By the sorted and disjoint condition, is at most , and for closed intervals . A point of is at most (less than for half-open intervals), and a point of is at least . In each case the first is less than the second.
Invariant. Let be the set of pairs with , , and or . Before each test of line L2:
(I) is the list of the nonempty sets with , each once, sorted from left to right.
Initialization. Line L1 sets , so is empty, and is empty.
Maintenance. Assume (I), with and , so the loop body runs. By Lemma 1, line L5 appends if it is nonempty, and nothing else. The pair is not in , so is not already in .
First, comes after every piece already in . Take a piece in . If , the piece lies in and lies in , so every point of the piece is less than every point of by Lemma 3. If , the same holds with . So stays sorted.
Next, consider line L6. If , it sets . Then . The pair is handled by line L5, and for the set is empty by Lemma 2, since . So is the list for , and (I) holds.
Otherwise , and line L7 sets . Then . The pair is handled by line L5, and for the set is empty by the second part of Lemma 2, since . This case includes the tie . So (I) holds.
Termination. Each step increases by exactly 1, and the loop runs only while . So it stops after at most steps when , and at once when or .
Conclusion. When the loop stops, or . In the first case every satisfies ; in the second every satisfies . Either way contains all pairs, and by (I) line L8 returns all nonempty sets , each once, sorted from left to right. These pieces are pairwise disjoint, because two different pairs differ in or in , and the intervals of each list are disjoint.