Proofs: Rectangle intersection sweep
These are the human proofs of Rectangle intersection sweep, at full length.
The rectangle intersection sweep returns a pair if and only if two rectangles intersect full
This proof shows: correctness, termination.
We use the pseudocode of the page. The rectangles are with and , for . We write for the -interval of . Two closed intervals and meet if and only if and , so two rectangles intersect if and only if their -intervals meet and their -intervals meet.
After line L2, is the sorted list of the events. Event is the insert of , and event is the delete of . Since and , the insert of comes before the delete of in . A rectangle is active at an event if its insert comes before in and its delete does not. By lines L6 and L10, just before the loop processes , the set holds exactly the rectangles that are active at .
Termination. The loop of line L4 runs at most once for each of the events, and each iteration does a constant number of operations on . So the algorithm stops.
Lemma 1 (activity). Let . Then is active at the insert of if and only if the insert of comes before the insert of and . In particular, if is in when is inserted, then the -intervals and meet.
Proof. The delete of is and the insert of is . In lexicographic order, the delete of comes after the insert of if and only if , or (then decides). So it comes after if and only if . This proves the equivalence. If is active at the insert of , then (the insert of comes first) and . So and , and the -intervals meet.
Lemma 2 (soundness). If the algorithm returns a pair, the two rectangles intersect.
Proof. A pair is returned only in line L8 or line L9, during the insert of some . The other member, or , is in and is not , so by Lemma 1 its -interval meets that of . In line L8, precedes in the key order, so , and the test gives . Together with , this gives and , so and meet. In line L9, follows , so , and the test gives ; with this gives , so and meet. In both cases both intervals meet, and the rectangles intersect.
Lemma 3 (neighbors). Let be a finite set of closed intervals that are pairwise disjoint, ordered by the key . Let be a new interval with a key not in , and let and be the predecessor and the successor of in . If meets some interval of , then meets or meets .
Proof. Since the intervals of are pairwise disjoint and closed, for two of them before in key order we have , and they do not meet, so . Let meet .
Case 1: precedes . Then exists. If , we are done. Otherwise precedes , and precedes . By the remark above, . Since meets , . So . But precedes , so . This is a contradiction, so .
Case 2: follows . Then exists, and or precedes . In both cases . Since meets , . So . Since follows , . So and meet.
Lemma 4 (completeness). If two rectangles intersect, the algorithm returns a pair.
Proof. For an intersecting pair , call the one whose insert comes later in its late member, and call the insert of the late member the time of the pair. Assume that intersecting pairs exist, and let , the insert of , be the earliest time over all intersecting pairs. If the algorithm returns at an event before , it returns a pair and we are done. So assume that it reaches .
The active intervals are disjoint at . Let and be two rectangles in just before , with the insert of after the insert of . Both are active at , so is active at the insert of (its delete comes after , which is after the insert of ). By Lemma 1 their -intervals meet. If and met, would be an intersecting pair of time before , against the choice of . So the intervals of the active rectangles are pairwise disjoint.
Some active interval meets . Let be an intersecting pair with time , so the insert of comes before . The -intervals meet, so . By Lemma 1, is active at , so . And meets .
Now line L6 inserts with key , which differs from each key in by its index. By Lemma 3, meets or . If it meets , then and , so the test of line L8 is true. If it does not meet but meets , then line L8 is false or does not exist, and , so the test of line L9 is true. In both cases the algorithm returns a pair at .
Theorem. RECT-INTERSECT stops. It returns a pair with and if such a pair exists, and it returns NONE otherwise.
Proof. Termination was shown above. If an intersecting pair exists, Lemma 4 shows that the algorithm returns a pair, and Lemma 2 shows that the pair intersects. If no intersecting pair exists, Lemma 2 shows that no pair can be returned, so the loop ends and line L11 returns NONE.