Algorithm Search⌘K

Proofs: Sweep for maximum overlap

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

The sweep for maximum overlap returns the depth full

This proof shows: correctness, termination.

We use the pseudocode of the page. The input is n≥0n \ge 0 intervals IjI_j with end points lj,rjl_j, r_j, all closed [lj,rj][l_j, r_j] with lj≤rjl_j \le r_j, or all half-open [lj,rj)[l_j, r_j) with lj<rjl_j < r_j, or all open (lj,rj)(l_j, r_j) with lj<rjl_j < r_j. Let c(x)=∣{j:x∈Ij}∣c(x) = |\{j : x \in I_j\}| and d=max⁡xc(x)d = \max_x c(x), with d=0d = 0 for n=0n = 0.

Claim. SWEEP-MAX-OVERLAP returns best=dbest = d. For closed and half-open intervals, c(at)=dc(at) = d; for open intervals, c(y)=dc(y) = d for all yy in an open gap (at,at+ε)(at, at + \varepsilon) with ε>0\varepsilon > 0. For n=0n = 0 it returns (0,nil)(0, \mathrm{nil}).

Proof. If n=0n = 0 the loop does not run, and the result is (0,nil)(0, \mathrm{nil}). Let n≥1n \ge 1, and let e1,…,e2ne_1, \ldots, e_{2n} be the events in the order of line L3. Call an interval active after step tt if its start event is among e1,…,ete_1, \ldots, e_t and its end event is not. Each interval has its start event before its end event in the order of line L3: if lj<rjl_j < r_j this follows from the coordinates, and if lj=rjl_j = r_j (closed only) the start comes first by the tie rule.

Step 1 (the counter). By induction on tt, after line L6 of step tt the variable countcount equals the number AtA_t of intervals active after step tt. Before the loop, count=0=A0count = 0 = A_0. A +1+1 event makes one more interval active, and a −1-1 event makes one active interval inactive (its start came earlier). So countcount follows AtA_t.

Step 2 (no overshoot). Fix a step tt with event coordinate xx. Every active interval has its start among e1,…,ete_1, \ldots, e_t, so lj≤xl_j \le x, and its end not among them, so rj≥xr_j \ge x.

  • Closed: the active intervals all contain xx, so At≤c(x)≤dA_t \le c(x) \le d.
  • Half-open or open, ete_t a start event: all ends at xx come before all starts at xx, so each end at xx is already processed, and an active interval has rj>xr_j > x. An active interval with lj=xl_j = x started at this coordinate. Each active interval then contains every point of (x,x+ε)(x, x + \varepsilon) with ε=min⁡j(rj−x)>0\varepsilon = \min_j (r_j - x) > 0 over the active ones, because lj≤x<y<rjl_j \le x < y < r_j. So At≤dA_t \le d.
  • Half-open or open, ete_t an end event: no start at xx is processed yet, so each active interval has lj<x≤rjl_j < x \le r_j, and all contain the points of (x−ε,x)(x - \varepsilon, x) for ε=min⁡j(x−lj)>0\varepsilon = \min_j(x - l_j) > 0. So At≤dA_t \le d.

Hence best≤dbest \le d at all times, since bestbest is a maximum of values AtA_t or 0.

Step 3 (the depth is reached). Let yy be a point with c(y)=dc(y) = d, and let JJ be the set of intervals that contain yy; ∣J∣=d≥1|J| = d \ge 1. Let x=max⁡j∈Jljx = \max_{j \in J} l_j, so x≤yx \le y, and let ete_t be the last start event with coordinate xx.

  • Closed: each j∈Jj \in J has lj≤x≤y≤rjl_j \le x \le y \le r_j. Its start has coordinate at most xx, and it is processed by step tt, since the starts at xx are processed by the last of them. Its end has coordinate rj≥xr_j \ge x; if rj>xr_j > x it comes later, and if rj=xr_j = x it comes after all starts at xx by the tie rule. So all of JJ is active after step tt, and At≥dA_t \ge d.
  • Half-open or open: each j∈Jj \in J has lj≤xl_j \le x and rj>y≥xr_j > y \ge x (for half-open, y<rjy < r_j; for open, y<rjy < r_j too). Its start is processed by step tt, and its end has a coordinate greater than xx, so it is not processed. So At≥dA_t \ge d.

With Step 2, At=dA_t = d. Line L7 then sets best=dbest = d at step tt or has done so earlier, and it never sets bestbest above dd.

Step 4 (the witness). Let ss be the step at which line L7 last changes bestbest, to the value dd. A change needs countcount to grow, so ese_s is a start event; let xx be its coordinate. By Step 2 for a start event, the dd intervals active after step ss all contain xx in the closed case, and all contain (x,x+ε)(x, x + \varepsilon) in the half-open and open cases. For half-open intervals, each active one has lj≤x<rjl_j \le x < r_j, so it contains xx too. So c(at)=dc(at) = d for closed and half-open intervals, and c=dc = d on (at,at+ε)(at, at + \varepsilon) for open intervals.

Termination. The loop processes each of the 2n2n events once. ■\blacksquare

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

Back to Sweep for maximum overlap