Proofs: Bentley's rectangle union sweep
These are the human proofs of Bentley's rectangle union sweep, at full length.
Bentley's sweep returns the area of the union of the rectangles full
This proof shows: correctness, termination.
We use the pseudocode of the page. The rectangles are with and , for . We write for the length (one-dimensional Lebesgue measure) of a set and for the area. For a node with index range , let be its -range. The leaves are the elementary intervals, and the two children of a node split its index range in two non-empty parts. If , the loop does not run and line L10 returns 0, which is correct. Assume .
Termination. The loop runs times. Each UPDATE call either returns at line L11 or L12, or calls UPDATE on the two children of a node that is not a leaf. (A leaf has , so it is either disjoint from or inside it, and it never reaches line L13.) The depth grows by one at each call, so the recursion stops at the leaves.
Canonical nodes. For , let be the set of nodes with whose parent range is not inside (or is the root). The ranges of the nodes of are pairwise disjoint, and their union is : each index is in the leaf , which is inside , and the highest ancestor of that leaf whose range is inside is the unique node of that contains . So when .
Lemma 1. UPDATE changes by for each and for no other node. It changes only at the nodes on the paths from the root to the nodes of .
Proof. By induction on the depth, a call UPDATE is made exactly for the nodes whose parent range meets but is not inside it (and for the root). For such a , line L11 returns if its range is disjoint from ; line L12 applies if its range is inside , and then by definition; otherwise line L13 calls both children, which again have a parent that meets but is not inside . Each node of has such a parent (or is the root), so it is reached, and line L12 runs there. Lines L14 to L16 run only at nodes that are called and do not return at line L11. A called node that returns at line L11 changes nothing. A called node that does not return meets , so it is on a path from the root to a node of .
Lemma 2 (counts). Let be the moment just before the first event with coordinate is processed. Call rectangle active at if . Then at , for each node , is the number of rectangles active at with and .
Proof. By Lemma 1 and induction over the events, is the sum, over the events processed so far, of if is in the set of the event's rectangle, and 0 otherwise. If , the set is empty and rectangle adds nothing. At , the events processed so far are exactly those with coordinate less than , since line L3 sorts by . So rectangle with adds if and if . Since , it adds 1 if and 0 otherwise.
Between these moments, inside a group of events with the same , a count can be off (for example when a rectangle with has its event sorted first). The proof uses the counts only at the moments of Lemma 2.
Lemma 3 (node invariant). After each event, for each node ,
Proof. Call the right side . At the start all counts and lengths are 0, so both sides are 0. Assume the claim before an event. By Lemma 1 the event changes counts only at nodes of , and lines L14 to L16 run at each node on the paths from the root to those nodes, after the calls on its children. The value can change only if a count in the subtree of changes, and then is on one of those paths. So it is enough to show that lines L14 to L16 compute when the values of the children are already right. We check the three cases.
- If : is in the union, and each for below is inside . So , as line L14 sets.
- If and is a leaf: the union is empty, so , as line L15 sets.
- If and has children : the union is the union of the two unions for and . These lie in and , which share at most one point. So , as line L16 sets.
Nodes not on the paths keep both and .
Lemma 4 (covered length). At a moment of Lemma 2, .
Proof. By Lemma 3, is the length of the union of over the nodes with . By Lemma 2, these are the nodes of for the active with . For such , the union of these is . An active with adds a single point, of length 0. So both sides are the length of the same set, up to a finite set of points.
Theorem. UNION-AREA returns .
Proof. Let be the distinct values among all and .
Step 1: the area is a sum over strips. Let . Every point of has . The vertical lines have area 0. So , with the open strip . For , rectangle meets the vertical line at if and only if , which holds if and only if and , because no or lies strictly between and . Call this set of rectangles ; it does not depend on . The section of at is , of length that does not depend on . By Fubini's theorem, .
Step 2: the loop adds these terms. Consider the events in sorted order. Within a group of events with the same , line L7 adds for each event after the first, because already. At the first event of the group with , (the of the previous group), and line L7 adds , with read before this group changes anything. This is the moment of Lemma 2 for . The rectangles active at have , that is and . This is . By Lemma 4, . At the first event, line L5 set to its , so line L7 adds 0.
So at the end, by Step 1.