Algorithm Search⌘K

Proofs: Interval covering greedy

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

The farthest-reach greedy finds a smallest cover full

This proof shows: correctness, termination.

We use the pseudocode of the page. The family is I[1],…,I[n]I[1], \ldots, I[n] with I[j]=⟨lj,rj⟩I[j] = \langle l_j, r_j \rangle, and the target has s<ts < t. All sets have one type. Define the prefix of length up to x≥sx \ge s:

P(x)=[s,x) for closed and half-open intervals,P(x)=(s,x) for open intervals.P(x) = [s, x) \text{ for closed and half-open intervals}, \qquad P(x) = (s, x) \text{ for open intervals}.

So P(s)=∅P(s) = \emptyset. For a set SS of intervals let U(S)=⋃j∈SI[j]U(S) = \bigcup_{j \in S} I[j] and ρ(S)=sup⁡{x≥s:P(x)⊆U(S)}\rho(S) = \sup \{ x \ge s : P(x) \subseteq U(S) \}. Since P(ρ)=⋃x<ρP(x)P(\rho) = \bigcup_{x < \rho} P(x), the supremum is attained: P(ρ(S))⊆U(S)P(\rho(S)) \subseteq U(S).

Covering the target. For half-open and open intervals the target is P(t)P(t). For closed intervals the target is [s,t][s, t], and [s,t]⊆U(S)[s, t] \subseteq U(S) if and only if P(t)=[s,t)⊆U(S)P(t) = [s, t) \subseteq U(S), because U(S)U(S) is a finite union of closed sets, hence closed, and tt is a limit of points of [s,t)[s, t). So in all cases, SS covers the target if and only if ρ(S)≥t\rho(S) \ge t.

The test. Call an interval available at xx when l≤xl \le x, except for open intervals with x>sx > s, where it means l<xl < x. Line L5 reads exactly the unread intervals available at reachreach.

Lemma 1 (extension). Let s≤xs \le x and let SS be a set with ρ(S)>x\rho(S) > x. Then some J∈SJ \in S is available at xx and has rJ>xr_J > x.

Proof. The points of the open interval (x,ρ(S))(x, \rho(S)) lie in P(ρ(S))⊆U(S)P(\rho(S)) \subseteq U(S). Let mm be the least left end among the intervals of SS with left end greater than xx, or m=ρ(S)m = \rho(S) if there is none, and let yy be a point with x<y<min⁡(m,ρ(S))x < y < \min(m, \rho(S)). Then yy lies in some J∈SJ \in S, with lJ≤xl_J \le x by the choice of yy, and rJ≥y>xr_J \ge y > x. If the intervals are not open, or if x=sx = s, then JJ is available at xx. If they are open and x>sx > s, then x∈P(ρ(S))x \in P(\rho(S)), so xx lies in some J′=(l′,r′)∈SJ' = (l', r') \in S, which has l′<x<r′l' < x < r'; take J′J'. □\square

Lemma 2 (the chosen prefix). After jj choices, P(reachj)⊆U(Cj)P(reach_j) \subseteq U(C_j), where CjC_j is the set of chosen intervals and reachjreach_j the value of reachreach.

Proof. By induction on jj; for j=0j = 0, P(s)=∅P(s) = \emptyset. At step jj the choice J=I[best]J = I[best] is available at reachj−1reach_{j-1} and has rJ=reachj>reachj−1r_J = reach_j > reach_{j-1}. For closed and half-open intervals lJ≤reachj−1l_J \le reach_{j-1}, so J⊇[reachj−1,reachj)J \supseteq [reach_{j-1}, reach_j), and P(reachj)=P(reachj−1)∪[reachj−1,reachj)P(reach_j) = P(reach_{j-1}) \cup [reach_{j-1}, reach_j). For open intervals with j=1j = 1, lJ≤sl_J \le s, so J⊇(s,reach1)J \supseteq (s, reach_1). For open intervals with j≥2j \ge 2, lJ<reachj−1l_J < reach_{j-1}, so J⊇[reachj−1,reachj)J \supseteq [reach_{j-1}, reach_j), and again the union of P(reachj−1)P(reach_{j-1}) and this set is P(reachj)P(reach_j). □\square

Lemma 3 (what line L5 sees). At step jj, the value farfar after line L6 is the largest right end among all intervals available at reachj−1reach_{j-1}, if this is more than reachj−1reach_{j-1}.

Proof. Line L1 sorts by left end, and availability at xx only depends on ll, so the unread available intervals form a contiguous run starting at ii, which line L5 reads. An interval read at an earlier step j′<jj' < j has right end at most far=reachj′≤reachj−1far = reach_{j'} \le reach_{j-1}. So the earlier reads cannot beat reachj−1reach_{j-1}, and line L6 sees every other available interval. □\square

Lemma 4 (stays ahead). If the greedy makes at least jj choices, then reachj≥ρ(S)reach_j \ge \rho(S) for every set SS of at most jj intervals.

Proof. By induction on jj. For j=0j = 0, S=∅S = \emptyset and ρ(∅)=s=reach0\rho(\emptyset) = s = reach_0. Let j≥1j \ge 1, ∣S∣≤j|S| \le j, and x=ρ(S)x = \rho(S). If x≤reachj−1x \le reach_{j-1}, then x<reachjx < reach_j. Otherwise x>reachj−1≥sx > reach_{j-1} \ge s. The points of P(x)P(x) just left of xx lie in U(S)U(S), and SS is finite, so some J∈SJ \in S contains points arbitrarily close to xx from the left; then lJ<x≤rJl_J < x \le r_J. Let S′=S∖{J}S' = S \setminus \{J\}, with ∣S′∣≤j−1|S'| \le j - 1.

Closed and half-open intervals. Each point of P(x)P(x) less than lJl_J is not in JJ, so it lies in U(S′)U(S'). If lJ>sl_J > s, then P(lJ)⊆U(S′)P(l_J) \subseteq U(S'), so ρ(S′)≥lJ\rho(S') \ge l_J, and by induction reachj−1≥lJreach_{j-1} \ge l_J. If lJ≤sl_J \le s, then lJ≤reachj−1l_J \le reach_{j-1} directly. So JJ is available at reachj−1reach_{j-1}.

Open intervals. If lJ≤sl_J \le s, then JJ is available at reachj−1reach_{j-1}, since either j=1j = 1 and the test is l≤sl \le s, or j≥2j \ge 2 and reachj−1>s≥lJreach_{j-1} > s \ge l_J. If lJ>sl_J > s, then the points of (s,lJ](s, l_J] are not in JJ and lie in P(x)P(x), so (s,lJ]⊆U(S′)(s, l_J] \subseteq U(S'). The set U(S′)U(S') is open, so it also contains (s,lJ+ε)(s, l_J + \varepsilon) for some ε>0\varepsilon > 0, and ρ(S′)>lJ\rho(S') > l_J. By induction reachj−1>lJreach_{j-1} > l_J, so JJ is available.

In both cases rJ≥x>reachj−1r_J \ge x > reach_{j-1}, so by Lemma 3 the greedy's farfar at step jj is at least rJr_J, and reachj≥xreach_j \ge x. □\square

Termination. Each step of the outer loop either returns at line L7 or chooses an interval with r>reachr > reach and sets reachreach to that right end, so reachreach strictly increases. The chosen interval was read in this step and ii moved past it, so no interval is chosen twice, and there are at most nn steps. The inner loop moves ii forward, at most nn times in total.

Correctness. If the greedy returns CC, it stopped because reach≥treach \ge t, so ρ(C)≥reach≥t\rho(C) \ge reach \ge t by Lemma 2, and CC covers the target. If a cover exists, let S∗S^* be a smallest one, with kk intervals. While reachj<t≤ρ(S∗)reach_{j} < t \le \rho(S^*), Lemma 1 with x=reachjx = reach_j gives an interval of S∗S^* available at reachjreach_j with right end above reachjreach_j; it has not been read in an earlier step, by the argument of Lemma 3, so line L6 sets bestbest, and line L7 does not fail. By Lemma 4, if the greedy makes kk choices then reachk≥ρ(S∗)≥treach_k \ge \rho(S^*) \ge t, and the loop stops. So the greedy returns a cover with at most kk intervals, which is optimal. If no cover exists, the greedy cannot return a list, since every returned list is a cover, so it returns "no cover" at line L7, by termination. ■\blacksquare

Back to Interval covering greedy