Algorithm Search⌘K

Proofs: Sweep for union length

These are the human proofs of Sweep for union length, at full length.

The sweep for union length returns the measure of the covered set full

This proof shows: correctness, termination.

We use the pseudocode of the page, with the test of line L6 written as count≥kcount \ge k for a fixed integer k≥1k \ge 1; the page uses k=1k = 1. The intervals have end points lj≤rjl_j \le r_j, 1≤j≤n1 \le j \le n, and each end point may be open or closed. Let c(x)=∣{j:x∈Ij}∣c(x) = |\{j : x \in I_j\}| be the coverage and Sk={x:c(x)≥k}S_k = \{x : c(x) \ge k\}.

Claim. SWEEP-UNION-LENGTH returns λ(Sk)\lambda(S_k), the Lebesgue measure of SkS_k, for any order of the events with equal coordinates.

Proof. Let e1,…,e2ne_1, \ldots, e_{2n} be the events in the order of line L3, with coordinates x1≤x2≤⋯≤x2nx_1 \le x_2 \le \cdots \le x_{2n}. If n=0n = 0 the loop does not run and the result is 0=λ(∅)0 = \lambda(\emptyset). Assume n≥1n \ge 1.

Step 1: the coverage on a gap. Fix ii with 1≤i<2n1 \le i < 2n and xi<xi+1x_i < x_{i+1}. Since the events are sorted, the events e1,…,eie_1, \ldots, e_i are exactly the events with coordinate at most xix_i. So after line L7 has processed eie_i,

count=∣{j:lj≤xi}∣−∣{j:rj≤xi}∣.count = |\{j : l_j \le x_i\}| - |\{j : r_j \le x_i\}|.

Each jj with rj≤xir_j \le x_i also has lj≤rj≤xil_j \le r_j \le x_i, so the difference is ∣{j:lj≤xi<rj}∣|\{j : l_j \le x_i < r_j\}|. Let x∈(xi,xi+1)x \in (x_i, x_{i+1}). No end point lies in this open gap. An interval IjI_j contains xx if and only if lj<x<rjl_j < x < r_j (the end points are not in the gap, so their type does not matter), if and only if lj≤xil_j \le x_i and rj≥xi+1r_j \ge x_{i+1}, if and only if lj≤xi<rjl_j \le x_i < r_j. Hence c(x)=countc(x) = count for every xx in the gap.

Step 2: the invariant. For 1≤i≤2n1 \le i \le 2n let Ti=λ(Sk∩(−∞,xi])T_i = \lambda(S_k \cap (-\infty, x_i]). We show:

(I) Just before line L6 processes ei+1e_{i+1}, for 1≤i<2n1 \le i < 2n, and also after the loop for i=2ni = 2n: total=Titotal = T_i and prev=xiprev = x_i.

Initialization. Before e1e_1, count=0<kcount = 0 < k, so line L6 adds nothing, and line L8 sets prev=x1prev = x_1. Every point x<x1x < x_1 is in no interval, and the point x1x_1 has measure 0, so T1=0=totalT_1 = 0 = total. So (I) holds for i=1i = 1.

Maintenance. Assume total=Titotal = T_i and prev=xiprev = x_i before line L6 handles ei+1e_{i+1}.

  • If xi+1=xix_{i+1} = x_i, line L6 adds 00 or nothing, so total=Ti=Ti+1total = T_i = T_{i+1}, since (−∞,xi+1]=(−∞,xi](-\infty, x_{i+1}] = (-\infty, x_i].
  • If xi+1>xix_{i+1} > x_i, then eie_i is the last event at xix_i, and by Step 1 the current countcount equals c(x)c(x) on the whole gap (xi,xi+1)(x_i, x_{i+1}). Line L6 adds xi+1−xi=λ((xi,xi+1))x_{i+1} - x_i = \lambda((x_i, x_{i+1})) if count≥kcount \ge k, that is, if the gap lies in SkS_k; otherwise the gap is disjoint from SkS_k and nothing is added. The point xi+1x_{i+1} has measure 0. So total=Ti+λ(Sk∩(xi,xi+1])=Ti+1total = T_i + \lambda(S_k \cap (x_i, x_{i+1}]) = T_{i+1}.

Lines L7 and L8 then set prev=xi+1prev = x_{i+1}.

Termination. The loop of line L5 processes each of the 2n2n events once, so it stops after 2n2n steps. Then total=T2ntotal = T_{2n}. Every point x>x2nx > x_{2n} is to the right of all end points and lies in no interval, so Sk⊆(−∞,x2n]S_k \subseteq (-\infty, x_{2n}] and T2n=λ(Sk)T_{2n} = \lambda(S_k). Line L9 returns it. ■\blacksquare

Tie order. The proof used the order of events with equal coordinates only through Step 1, which looks at the counter after the last event of a coordinate. That value does not depend on the order within the coordinate. So every tie order gives the same result.

Cost. Line L3 sorts 2n2n events; the loop does 2n2n steps of O(1)O(1) work each.

Back to Sweep for union length