Algorithm Search⌘K

Proofs: Interval merging by sorting

These are the human proofs of Interval merging by sorting, at full length.

Interval merging by sorting returns the components of the union full

This proof shows: correctness, termination.

We use the pseudocode of the page. Let I1=[l1,r1],…,In=[ln,rn]I_1 = [l_1, r_1], \ldots, I_n = [l_n, r_n] be the intervals in the order that line L1 makes, so l1≤l2≤⋯≤lnl_1 \le l_2 \le \cdots \le l_n. Let Ui=I1∪⋯∪IiU_i = I_1 \cup \cdots \cup I_i, with U0=∅U_0 = \emptyset, and let Ri=max⁡(r1,…,ri)R_i = \max(r_1, \ldots, r_i).

Claim. For closed intervals, MERGE-INTERVALS returns the list of the connected components of UnU_n, sorted from left to right. With the test l≥out.last.rl \ge out.last.r in line L4, it does the same for open intervals (lj,rj)(l_j, r_j) with lj<rjl_j < r_j; with the test of the page, it does the same for half-open intervals [lj,rj)[l_j, r_j) with lj<rjl_j < r_j.

Proof for closed intervals. We prove this invariant of the loop of line L3.

(I) After ii steps, with 0≤i≤n0 \le i \le n: out=(C1,…,Ck)out = (C_1, \ldots, C_k) is the list of the components of UiU_i, sorted, and if i≥1i \ge 1 then the last component is Ck=[ak,Ri]C_k = [a_k, R_i] with ak≤lia_k \le l_i.

Each component of a finite union of closed intervals is a closed interval, because it is the union of the finitely many input intervals that lie in it, and a finite union of closed intervals that is connected is a closed interval.

Initialization. With i=0i = 0, line L2 makes outout empty, and U0U_0 has no components.

Maintenance. Assume (I) after ii steps, i<ni < n, and let [l,r]=Ii+1[l, r] = I_{i+1}.

  1. If outout is empty, then i=0i = 0, line L5 makes out=([l,r])out = ([l, r]), and [l,r][l, r] is the only component of U1U_1. Also R1=rR_1 = r and a1=l=l1a_1 = l = l_1.
  2. If l>Ril > R_i, line L5 appends [l,r][l, r]. Each point of UiU_i is at most Ri<lR_i < l, so the open gap (Ri,l)(R_i, l) is not in Ui+1U_{i+1}, and Ui+1U_{i+1} is the disjoint union of UiU_i and [l,r][l, r] with this gap between them. So the components of Ui+1U_{i+1} are C1,…,CkC_1, \ldots, C_k and [l,r][l, r], in this order. Also Ri+1=max⁡(Ri,r)=rR_{i+1} = \max(R_i, r) = r, since r≥l>Rir \ge l > R_i, and the new last component starts at l=li+1l = l_{i+1}.
  3. If l≤Ril \le R_i, line L6 sets the right end of CkC_k to max⁡(Ri,r)=Ri+1\max(R_i, r) = R_{i+1}. By (I) and the sort, ak≤li≤l≤Ria_k \le l_i \le l \le R_i, so l∈Ckl \in C_k and [l,r][l, r] meets CkC_k. Hence Ck∪[l,r]=[ak,max⁡(Ri,r)]C_k \cup [l, r] = [a_k, \max(R_i, r)] is an interval. For j<kj < k, the component CjC_j ends before aka_k with a gap, because CjC_j and CkC_k are different components of UiU_i and CjC_j is to the left. Since [l,r][l, r] starts at l≥akl \ge a_k, it does not reach CjC_j or the gap after it. So the components of Ui+1U_{i+1} are C1,…,Ck−1C_1, \ldots, C_{k-1} and [ak,Ri+1][a_k, R_{i+1}], and ak≤l=li+1a_k \le l = l_{i+1}.

In each case (I) holds after i+1i + 1 steps.

Termination. The loop of line L3 runs exactly nn times, once for each interval, because each step consumes one element of the finite sorted list. After the last step, (I) with i=ni = n says that outout is the sorted list of the components of UnU_n, and line L7 returns it. □\square

Half-open intervals. For [lj,rj)[l_j, r_j) with lj<rjl_j < r_j, replace closed by half-open in (I) and let Ck=[ak,Ri)C_k = [a_k, R_i). In case 2, l>Ril > R_i leaves the nonempty gap [Ri,l)[R_i, l) uncovered. In case 3, l≤Ril \le R_i gives [ak,Ri)∪[l,r)=[ak,max⁡(Ri,r))[a_k, R_i) \cup [l, r) = [a_k, \max(R_i, r)), an interval, also when l=Ril = R_i: the point RiR_i is in [l,r)[l, r) because l=Ri<rl = R_i < r. The rest of the proof is the same.

Open intervals. For (lj,rj)(l_j, r_j) with lj<rjl_j < r_j and line L4 changed to l≥out.last.rl \ge out.last.r, let Ck=(ak,Ri)C_k = (a_k, R_i). In case 2, now l≥Ril \ge R_i, and the point RiR_i is in no interval I1,…,IiI_1, \ldots, I_i (each ends at most at RiR_i and excludes its end) and not in (l,r)(l, r), since l≥Ril \ge R_i. So the components are separated, and the new one is (l,r)(l, r). In case 3, now l<Ril < R_i, and (ak,Ri)∪(l,r)=(ak,max⁡(Ri,r))(a_k, R_i) \cup (l, r) = (a_k, \max(R_i, r)) is an interval because ak≤l<Ria_k \le l < R_i and the two open intervals share the points just above ll. The rest is the same. ■\blacksquare

Cost. Line L1 sorts nn items. Each of the nn steps of the loop does one comparison in line L4, at most one maximum in line L6, and O(1)O(1) other work, so the scan costs Θ(n)\Theta(n) time.

Back to Interval merging by sorting