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] with I[j]=⟨lj,rj⟩, and the target has s<t. All sets have one type. Define the prefix of length up to x≥s:
P(x)=[s,x) for closed and half-open intervals,P(x)=(s,x) for open intervals. So P(s)=∅. For a set S of intervals let U(S)=⋃j∈SI[j] and ρ(S)=sup{x≥s:P(x)⊆U(S)}. Since P(ρ)=⋃x<ρP(x), the supremum is attained: P(ρ(S))⊆U(S).
Covering the target. For half-open and open intervals the target is P(t). For closed intervals the target is [s,t], and [s,t]⊆U(S) if and only if P(t)=[s,t)⊆U(S), because U(S) is a finite union of closed sets, hence closed, and t is a limit of points of [s,t). So in all cases, S covers the target if and only if ρ(S)≥t.
The test. Call an interval available at x when l≤x, except for open intervals with x>s, where it means l<x. Line L5 reads exactly the unread intervals available at reach.
Lemma 1 (extension). Let s≤x and let S be a set with ρ(S)>x. Then some J∈S is available at x and has rJ>x.
Proof. The points of the open interval (x,ρ(S)) lie in P(ρ(S))⊆U(S). Let m be the least left end among the intervals of S with left end greater than x, or m=ρ(S) if there is none, and let y be a point with x<y<min(m,ρ(S)). Then y lies in some J∈S, with lJ≤x by the choice of y, and rJ≥y>x. If the intervals are not open, or if x=s, then J is available at x. If they are open and x>s, then x∈P(ρ(S)), so x lies in some J′=(l′,r′)∈S, which has l′<x<r′; take J′. □
Lemma 2 (the chosen prefix). After j choices, P(reachj)⊆U(Cj), where Cj is the set of chosen intervals and reachj the value of reach.
Proof. By induction on j; for j=0, P(s)=∅. At step j the choice J=I[best] is available at reachj−1 and has rJ=reachj>reachj−1. For closed and half-open intervals lJ≤reachj−1, so J⊇[reachj−1,reachj), and P(reachj)=P(reachj−1)∪[reachj−1,reachj). For open intervals with j=1, lJ≤s, so J⊇(s,reach1). For open intervals with j≥2, lJ<reachj−1, so J⊇[reachj−1,reachj), and again the union of P(reachj−1) and this set is P(reachj). □
Lemma 3 (what line L5 sees). At step j, the value far after line L6 is the largest right end among all intervals available at reachj−1, if this is more than reachj−1.
Proof. Line L1 sorts by left end, and availability at x only depends on l, so the unread available intervals form a contiguous run starting at i, which line L5 reads. An interval read at an earlier step j′<j has right end at most far=reachj′≤reachj−1. So the earlier reads cannot beat reachj−1, and line L6 sees every other available interval. □
Lemma 4 (stays ahead). If the greedy makes at least j choices, then reachj≥ρ(S) for every set S of at most j intervals.
Proof. By induction on j. For j=0, S=∅ and ρ(∅)=s=reach0. Let j≥1, ∣S∣≤j, and x=ρ(S). If x≤reachj−1, then x<reachj. Otherwise x>reachj−1≥s. The points of P(x) just left of x lie in U(S), and S is finite, so some J∈S contains points arbitrarily close to x from the left; then lJ<x≤rJ. Let S′=S∖{J}, with ∣S′∣≤j−1.
Closed and half-open intervals. Each point of P(x) less than lJ is not in J, so it lies in U(S′). If lJ>s, then P(lJ)⊆U(S′), so ρ(S′)≥lJ, and by induction reachj−1≥lJ. If lJ≤s, then lJ≤reachj−1 directly. So J is available at reachj−1.
Open intervals. If lJ≤s, then J is available at reachj−1, since either j=1 and the test is l≤s, or j≥2 and reachj−1>s≥lJ. If lJ>s, then the points of (s,lJ] are not in J and lie in P(x), so (s,lJ]⊆U(S′). The set U(S′) is open, so it also contains (s,lJ+ε) for some ε>0, and ρ(S′)>lJ. By induction reachj−1>lJ, so J is available.
In both cases rJ≥x>reachj−1, so by Lemma 3 the greedy's far at step j is at least rJ, and reachj≥x. □
Termination. Each step of the outer loop either returns at line L7 or chooses an interval with r>reach and sets reach to that right end, so reach strictly increases. The chosen interval was read in this step and i moved past it, so no interval is chosen twice, and there are at most n steps. The inner loop moves i forward, at most n times in total.
Correctness. If the greedy returns C, it stopped because reach≥t, so ρ(C)≥reach≥t by Lemma 2, and C covers the target. If a cover exists, let S∗ be a smallest one, with k intervals. While reachj<t≤ρ(S∗), Lemma 1 with x=reachj gives an interval of S∗ available at reachj with right end above reachj; it has not been read in an earlier step, by the argument of Lemma 3, so line L6 sets best, and line L7 does not fail. By Lemma 4, if the greedy makes k choices then reachk≥ρ(S∗)≥t, and the loop stops. So the greedy returns a cover with at most k 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. ■