Algorithm Search⌘K

Proofs: Interval piercing greedy

These are the human proofs of Interval piercing greedy, at full length.

The piercing greedy places the fewest points 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, with I[k]=⟨lk,rk⟩I[k] = \langle l_k, r_k \rangle. Let k1,k2,…,knk_1, k_2, \ldots, k_n be the order of line L1, so rk1≤rk2≤⋯≤rknr_{k_1} \le r_{k_2} \le \cdots \le r_{k_n}. For half-open and open intervals line L4 tests l≥pl \ge p in place of l>pl > p.

Claim. PIERCE stops after nn steps. For closed intervals it returns a set of points that pierces every interval, of the least possible size. For half-open and open intervals the same holds after each pp is replaced by the real point of its group described below.

Openers and groups. Call I[k]I[k] an opener if line L5 runs in its step, and let J1,…,JmJ_1, \ldots, J_m be the openers in order, with right ends q1≤q2≤⋯≤qmq_1 \le q_2 \le \cdots \le q_m. The group GiG_i is JiJ_i together with the intervals read after JiJ_i and before Ji+1J_{i+1}; in their steps p=qip = q_i.

Lemma 1 (each group has a common point). Every interval of GiG_i meets the point qiq_i in the sense of its type. That is, an interval ⟨l,r⟩∈Gi\langle l, r \rangle \in G_i has l≤qi≤rl \le q_i \le r for closed intervals, and l<qi≤rl < q_i \le r for half-open and open intervals.

Proof. Each interval of GiG_i comes at or after JiJ_i in the order, so r≥qir \ge q_i. For JiJ_i itself, l≤r=qil \le r = q_i, and l<r=qil < r = q_i for half-open and open intervals, which are nonempty. For any other interval of GiG_i the test of line L4 failed, so l≤qil \le q_i for closed intervals and l<qil < q_i for the other types. □\square

Real points. For closed intervals the point is qiq_i, which lies in every interval of GiG_i by Lemma 1. For half-open and open intervals let mim_i be the largest left end in GiG_i, so mi<qim_i < q_i by Lemma 1. For half-open intervals, mim_i lies in every [l,r)∈Gi[l, r) \in G_i, since l≤mi<qi≤rl \le m_i < q_i \le r. For open intervals, the midpoint (mi+qi)/2(m_i + q_i)/2 lies in every (l,r)∈Gi(l, r) \in G_i, since l≤mi<(mi+qi)/2<qi≤rl \le m_i < (m_i + q_i)/2 < q_i \le r. So in each case one point pierces the whole group, and the groups together contain all nn intervals. Hence the output pierces every interval and has mm points.

Lemma 2 (the openers are pairwise disjoint). For i<ji < j, Ji∩Jj=∅J_i \cap J_j = \emptyset.

Proof. When Ji+1J_{i+1} is read, p=qip = q_i and the test of line L4 holds: l(Ji+1)>qil(J_{i+1}) > q_i for closed intervals, and l(Ji+1)≥qil(J_{i+1}) \ge q_i for the other types. For j>ij > i, the openers come in order, so l(Jj)>qj−1≥qil(J_j) > q_{j-1} \ge q_i in the closed case and l(Jj)≥qj−1≥qil(J_j) \ge q_{j-1} \ge q_i otherwise. Every point of JiJ_i is at most qiq_i, and less than qiq_i for half-open and open intervals; every point of JjJ_j is at least l(Jj)l(J_j), and greater than it for open intervals. In each case every point of JiJ_i is less than every point of JjJ_j. □\square

Optimality. Let PP be any set of points that pierces every interval. Each opener contains a point of PP, and by Lemma 2 no point lies in two openers. So ∣P∣≥m|P| \ge m, and the output, with mm points, is optimal.

Termination. Line L1 sorts nn keys, and the loop of line L3 does one step of O(1)O(1) work for each index. The real points need one more value per group, the running maximum mim_i, which is updated in O(1)O(1) time per step. ■\blacksquare

Back to Interval piercing greedy