Algorithm Search⌘K

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 Ri=[ai,bi]×[ci,di]R_i = [a_i, b_i] \times [c_i, d_i] with ai≤bia_i \le b_i and ci≤dic_i \le d_i, for i=1,…,ni = 1, \ldots, n. We write Ii=[ci,di]I_i = [c_i, d_i] for the yy-interval of RiR_i. Two closed intervals [u,v][u, v] and [u′,v′][u', v'] meet if and only if u≤v′u \le v' and u′≤vu' \le v, so two rectangles intersect if and only if their xx-intervals meet and their yy-intervals meet.

After line L2, EE is the sorted list of the 2n2n events. Event (ai,0,i)(a_i, 0, i) is the insert of ii, and event (bi,1,i)(b_i, 1, i) is the delete of ii. Since ai≤bia_i \le b_i and 0<10 < 1, the insert of ii comes before the delete of ii in EE. A rectangle is active at an event ee if its insert comes before ee in EE and its delete does not. By lines L6 and L10, just before the loop processes ee, the set SS holds exactly the rectangles that are active at ee.

Termination. The loop of line L4 runs at most once for each of the 2n2n events, and each iteration does a constant number of operations on SS. So the algorithm stops.

Lemma 1 (activity). Let j≠ij \ne i. Then jj is active at the insert of ii if and only if the insert of jj comes before the insert of ii and bj≥aib_j \ge a_i. In particular, if jj is in SS when ii is inserted, then the xx-intervals [aj,bj][a_j, b_j] and [ai,bi][a_i, b_i] meet.

Proof. The delete of jj is (bj,1,j)(b_j, 1, j) and the insert of ii is (ai,0,i)(a_i, 0, i). In lexicographic order, the delete of jj comes after the insert of ii if and only if bj>aib_j > a_i, or bj=aib_j = a_i (then 1>01 > 0 decides). So it comes after if and only if bj≥aib_j \ge a_i. This proves the equivalence. If jj is active at the insert of ii, then aj≤aia_j \le a_i (the insert of jj comes first) and bj≥aib_j \ge a_i. So aj≤ai≤bia_j \le a_i \le b_i and ai≤bja_i \le b_j, and the xx-intervals meet. □\square

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 ii. The other member, pp or ss, is in SS and is not ii, so by Lemma 1 its xx-interval meets that of RiR_i. In line L8, pp precedes ii in the key order, so cp≤cic_p \le c_i, and the test gives dp≥cid_p \ge c_i. Together with ci≤dic_i \le d_i, this gives cp≤dic_p \le d_i and ci≤dpc_i \le d_p, so IpI_p and IiI_i meet. In line L9, ss follows ii, so ci≤csc_i \le c_s, and the test gives cs≤dic_s \le d_i; with cs≤dsc_s \le d_s this gives ci≤dsc_i \le d_s, so IiI_i and IsI_s meet. In both cases both intervals meet, and the rectangles intersect. □\square

Lemma 3 (neighbors). Let TT be a finite set of closed intervals that are pairwise disjoint, ordered by the key (c,index)(c, \text{index}). Let J=[c,d]J = [c, d] be a new interval with a key not in TT, and let pp and ss be the predecessor and the successor of JJ in T∪{J}T \cup \{J\}. If JJ meets some interval of TT, then JJ meets pp or JJ meets ss.

Proof. Since the intervals of TT are pairwise disjoint and closed, for two of them [c′,d′][c', d'] before [c′′,d′′][c'', d''] in key order we have c′≤c′′c' \le c'', and they do not meet, so d′<c′′d' < c''. Let A=[cA,dA]∈TA = [c_A, d_A] \in T meet JJ.

Case 1: AA precedes JJ. Then pp exists. If p=Ap = A, we are done. Otherwise AA precedes pp, and pp precedes JJ. By the remark above, cp>dAc_p > d_A. Since AA meets JJ, dA≥cd_A \ge c. So cp>cc_p > c. But pp precedes JJ, so cp≤cc_p \le c. This is a contradiction, so p=Ap = A.

Case 2: AA follows JJ. Then ss exists, and s=As = A or ss precedes AA. In both cases cs≤cAc_s \le c_A. Since AA meets JJ, cA≤dc_A \le d. So cs≤dc_s \le d. Since ss follows JJ, c≤cs≤dsc \le c_s \le d_s. So JJ and ss meet. □\square

Lemma 4 (completeness). If two rectangles intersect, the algorithm returns a pair.

Proof. For an intersecting pair {j,i}\{j, i\}, call the one whose insert comes later in EE its late member, and call the insert of the late member the time of the pair. Assume that intersecting pairs exist, and let e∗e^\ast, the insert of i∗i^\ast, be the earliest time over all intersecting pairs. If the algorithm returns at an event before e∗e^\ast, it returns a pair and we are done. So assume that it reaches e∗e^\ast.

The active intervals are disjoint at e∗e^\ast. Let jj and kk be two rectangles in SS just before e∗e^\ast, with the insert of kk after the insert of jj. Both are active at e∗e^\ast, so jj is active at the insert of kk (its delete comes after e∗e^\ast, which is after the insert of kk). By Lemma 1 their xx-intervals meet. If IjI_j and IkI_k met, {j,k}\{j, k\} would be an intersecting pair of time before e∗e^\ast, against the choice of e∗e^\ast. So the intervals of the active rectangles are pairwise disjoint.

Some active interval meets Ii∗I_{i^\ast}. Let {j,i∗}\{j, i^\ast\} be an intersecting pair with time e∗e^\ast, so the insert of jj comes before e∗e^\ast. The xx-intervals meet, so bj≥ai∗b_j \ge a_{i^\ast}. By Lemma 1, jj is active at e∗e^\ast, so j∈Sj \in S. And IjI_j meets Ii∗I_{i^\ast}.

Now line L6 inserts i∗i^\ast with key (ci∗,i∗)(c_{i^\ast}, i^\ast), which differs from each key in SS by its index. By Lemma 3, Ii∗I_{i^\ast} meets IpI_p or IsI_s. If it meets IpI_p, then cp≤ci∗c_p \le c_{i^\ast} and ci∗≤dpc_{i^\ast} \le d_p, so the test of line L8 is true. If it does not meet IpI_p but meets IsI_s, then line L8 is false or pp does not exist, and cs≤di∗c_s \le d_{i^\ast}, so the test of line L9 is true. In both cases the algorithm returns a pair at e∗e^\ast. □\square

Theorem. RECT-INTERSECT stops. It returns a pair (i,j)(i, j) with i≠ji \ne j and Ri∩Rj≠∅R_i \cap R_j \ne \emptyset 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. ■\blacksquare

Back to Rectangle intersection sweep