Proofs: Sweep status structure
These are the human proofs of Sweep status structure, at full length.
The sweep status keeps the active segments in their order along the sweep line full
This proof shows: correctness.
We use the pseudocode of the page. is a finite set of segments, none vertical, no two overlapping (two distinct segments share at most one point). A segment has endpoints and with , slope , and for as on the page. For a position , the active set is , and the key at is , compared lexicographically.
Lemma 1 (keys are distinct). If are both in , then .
Proof. If and , both segments lie on the same line through , and both contain the points of that line with abscissa in , an interval of positive length since and . So they share infinitely many points and overlap, a contradiction.
Lemma 2 (the key order is the order just to the right). Let with . There is an such that if and only if for each .
Proof. For in both domains, . If , then by continuity has the sign of on some interval , and that sign decides . If , then by Lemma 1, and has the sign of for each , which again decides .
Lemma 3 (no crossing, no change of order). Let be two segments that both span the closed interval with , and that have no common point with abscissa in the open interval . Then the sign of is the same nonzero value for each , and the same sign, or zero, at and at .
Proof. The function is affine on . It has no zero in , since a zero is a common point. An affine function with a sign change on an interval has a zero inside it (it is continuous), so has one sign on . By continuity, and have this sign or are .
The sweep. The event positions are : the abscissas of all endpoints and of all points that two segments of share. At each event position , the sweep does, in this order:
- SET-X;
- DELETE for each stored with ;
- for each point with abscissa through which two or more stored segments pass, reverse the contiguous block of these segments (SWAP when there are two);
- INSERT for each with .
Claim. After step 4 at the event position , the list holds exactly , sorted by in increasing order. Each ABOVE, BELOW and NEIGHBORS-OF-POINT made at this moment returns the neighbors in the order of just to the right of .
Proof. By induction on the events. Before the first event is empty, and the steps at are the step below with an empty list.
Step. Assume the claim after the event , and let . Between them no segment starts or ends and no two segments meet (each meeting point is an event), so each spans , by and the fact that is an event position, so .
SET-X. Let be two consecutive entries of , first. By the hypothesis , so by Lemma 2 at some points of . By Lemma 3, on all of and . So after line L1 the values do not decrease along (the y-order), and the entries that tie at are consecutive. Two entries that tie at share the point .
LOCATE under the y-order. Line L4 is a binary search for the first position whose value is at least ; it is correct because the values do not decrease. All entries with the value follow it contiguously, and is one of them, so the loop of lines L5 to L8 reaches and returns its position. It stops after at most iterations, where is the size of the block, because grows by 1 each time and the block ends.
Step 2. DELETE removes one entry; a sublist of a list with the y-order has the y-order. After step 2, holds the with .
Step 3. Let and the block of stored segments through , . Each of them has and , so is interior to each. By the SET-X paragraph, the block is in increasing order of on , so just to the left of . For close to , , so increasing order of just left of is decreasing order of slope. Reversing the block gives increasing order of slope, which is order inside the block, since all have the same . For two segments the reversal is line L14, and the precondition of SWAP holds: is directly above and they meet at . Reversal moves entries only inside a block of equal values, so the y-order holds. After step 3, the list is sorted by : entries with different values are in y-order, and entries with equal values form one block through one point, now in slope order (a block of size 1 is sorted).
Step 4. Before each insert the list is sorted by and its entries are in , so their keys are distinct by Lemma 1. Line L2 finds the first position with a larger key, so all entries before it have a key less than or equal to , and by Lemma 1 (applied to , which is in since ) strictly less. Line L3 inserts there, and the list stays sorted by .
After step 4 the list holds the with , which are the segments of with (each of them has because no segment starts in ), together with the with . That is , sorted by .
Queries. ABOVE and BELOW read the entries next to the position that LOCATE returns, so they return the neighbors in order, which by Lemma 2 is the order along the line just to the right of . For NEIGHBORS-OF-POINT, lines L15 and L16 are binary searches in the non-decreasing values: positions before have value , positions from on have value , and positions to have value . So is the highest stored segment strictly below and the lowest one strictly above, or NIL when there is none.
Between events. The same argument with Lemma 3 shows that at each strictly between two events, the list is strictly increasing in and holds , so SET-X to such a position followed by queries also gives the order along the line.