Algorithm Search⌘K

Proofs: Interval containment by sorting

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

Interval containment by sorting keeps one copy of each maximal interval full

This proof shows: correctness, termination.

We use the pseudocode of the page. The intervals I[1],…,I[n]I[1], \ldots, I[n] are nonempty and of one type, so I[k]⊆I[j]I[k] \subseteq I[j] holds if and only if I[j].l≤I[k].lI[j].l \le I[k].l and I[k].r≤I[j].rI[k].r \le I[j].r. Write j≺kj \prec k when jj comes before kk in orderorder. By line L1, j≺kj \prec k if and only if I[j].l<I[k].lI[j].l < I[k].l, or the left ends are equal and I[j].r>I[k].rI[j].r > I[k].r, or both end points are equal and j<kj < k.

Claim. CONTAINMENT-SORT stops after nn steps. For each kk, if parent[k]=j≠parent[k] = j \ne nil, then j≠kj \ne k and I[k]⊆I[j]I[k] \subseteq I[j]. And $parent[k] = $ nil if and only if I[k]I[k] is maximal (no interval of the family properly contains it) and no index j<kj < k has I[j]=I[k]I[j] = I[k].

Lemma 1. If I[k]⊆I[j]I[k] \subseteq I[j] and j≠kj \ne k, then j≺kj \prec k, unless I[j]=I[k]I[j] = I[k] and j>kj > k.

Proof. We have I[j].l≤I[k].lI[j].l \le I[k].l. If I[j].l<I[k].lI[j].l < I[k].l, then j≺kj \prec k. Otherwise the left ends are equal and I[j].r≥I[k].rI[j].r \ge I[k].r. If I[j].r>I[k].rI[j].r > I[k].r, then j≺kj \prec k. Otherwise the intervals are equal, and j≺kj \prec k exactly when j<kj < k. □\square

Lemma 2. If j≺kj \prec k, then I[j].l≤I[k].lI[j].l \le I[k].l. So if also I[k].r≤I[j].rI[k].r \le I[j].r, then I[k]⊆I[j]I[k] \subseteq I[j].

Proof. The sort is by left end first. □\square

Invariant. Let PP be the set of indices processed so far. Before each step of the loop of line L3:

(I) If PP is empty, $best = $ nil. Otherwise best∈Pbest \in P, I[best].r=max⁡j∈PI[j].rI[best].r = \max_{j \in P} I[j].r, and $parent[best] = $ nil.

Initialization. Line L2 sets $best = $ nil and PP is empty.

Maintenance. Let kk be the next index. If $best = $ nil, then PP is empty, line L6 sets best←kbest \leftarrow k, and parent[k]parent[k] stays nil, so (I) holds for P∪{k}P \cup \{k\}. Otherwise, if I[k].r≤I[best].rI[k].r \le I[best].r, line L5 sets parent[k]parent[k], and bestbest keeps its value, which is still the maximum of the right ends over P∪{k}P \cup \{k\}; parent[best]parent[best] is unchanged, because best≠kbest \ne k. If I[k].r>I[best].rI[k].r > I[best].r, then I[k].rI[k].r is the new maximum, line L6 sets best←kbest \leftarrow k, and parent[k]parent[k] is nil. In both cases (I) holds.

Each step decides its interval. Consider the step for kk, with PP the set of indices before kk in orderorder.

If line L5 runs, then best∈Pbest \in P, so best≺kbest \prec k and best≠kbest \ne k, and I[k].r≤I[best].rI[k].r \le I[best].r. By Lemma 2, I[k]⊆I[best]I[k] \subseteq I[best].

If line L6 runs, then either PP is empty or I[k].r>max⁡j∈PI[j].rI[k].r > \max_{j \in P} I[j].r by (I). So no j≺kj \prec k has I[k].r≤I[j].rI[k].r \le I[j].r, and no j≺kj \prec k contains I[k]I[k]. Suppose I[j]I[j] properly contains I[k]I[k]. Then I[j]≠I[k]I[j] \ne I[k], so j≺kj \prec k by Lemma 1, a contradiction. Suppose I[j]=I[k]I[j] = I[k] with j<kj < k. Then j≺kj \prec k by the tie rule, and I[j]I[j] contains I[k]I[k], a contradiction. So I[k]I[k] is maximal and has no equal copy with a smaller index.

Conversely. If I[k]I[k] is properly contained in some I[j]I[j], then j≺kj \prec k by Lemma 1, so PP is not empty and I[best].r≥I[j].r≥I[k].rI[best].r \ge I[j].r \ge I[k].r by (I); line L5 runs. If I[j]=I[k]I[j] = I[k] for some j<kj < k, then j≺kj \prec k, and again I[best].r≥I[j].r=I[k].rI[best].r \ge I[j].r = I[k].r; line L5 runs. So in both cases parent[k]≠parent[k] \ne nil.

Termination. Line L1 is a sort of nn keys, and the loop of line L3 runs once for each of the nn indices. Each parent[k]parent[k] is written at most once, in the step for kk, so later steps do not change it.

Conclusion. The two parts above give the claim: every non-nil parent[k]parent[k] is a container of I[k]I[k] with a different index, and parent[k]parent[k] is nil exactly for the first copy, by index, of each maximal interval. ■\blacksquare

Back to Interval containment by sorting