Algorithm Search⌘K

Proofs: Earliest finish time first

These are the human proofs of Earliest finish time first, at full length.

Earliest finish time first returns a maximum set of compatible activities full

This proof shows: correctness, termination.

We use the pseudocode of the page. The input is a finite set SS of n≥0n \ge 0 activities, each with s(a)<f(a)s(a) < f(a) and the half-open interval [s(a),f(a))[s(a), f(a)). Two distinct activities a,ba, b are compatible when f(a)≤s(b)f(a) \le s(b) or f(b)≤s(a)f(b) \le s(a). Line L1 gives the order a1,…,ana_1, \ldots, a_n with f(a1)≤⋯≤f(an)f(a_1) \le \cdots \le f(a_n); ties are in any order. Let i1,i2,…,iki_1, i_2, \ldots, i_k be the activities appended in line L6, in the order of appending.

Claim. The algorithm stops after nn iterations, and the returned list A=(i1,…,ik)A = (i_1, \ldots, i_k) is a set of pairwise compatible activities of the largest possible size.

Termination. The loop of line L4 runs for i=1,…,ni = 1, \ldots, n, and each iteration does a bounded number of steps. So the algorithm stops.

Step 1: the invariant. Before the iteration for ii, with 1≤i≤n+11 \le i \le n + 1:

(I) the activities in AA are pairwise compatible; they appear in AA in non-decreasing order of finish time; and last=−∞last = -\infty if AA is empty, else lastlast is the finish time of the last activity of AA, which is the largest finish time in AA.

Initialization. Before the first iteration, AA is empty and last=−∞last = -\infty by lines L2 and L3. All three parts hold.

Maintenance. Assume (I) before the iteration for i≤ni \le n. If the test of line L5 fails, nothing changes and (I) holds before the next iteration. If it passes, then s(ai)≥lasts(a_i) \ge last. Each activity bb in AA has f(b)≤lastf(b) \le last by (I), so f(b)≤s(ai)f(b) \le s(a_i), and bb is compatible with aia_i. The activities of AA come from a1,…,ai−1a_1, \ldots, a_{i-1}, so aia_i is not one of them and each has finish time at most f(ai)f(a_i). After lines L6 and L7, AA is pairwise compatible, it is in non-decreasing order of finish time, and last=f(ai)last = f(a_i) is the finish time of its last activity and its largest finish time.

Termination of the loop. After the iteration for i=ni = n, (I) holds, so AA is a set of pairwise compatible activities.

Step 2: what the scan sees between two choices. Let i0i_0 be a dummy with f(i0)=−∞f(i_0) = -\infty. For 1≤r≤k1 \le r \le k, while the loop processes the activities strictly after ir−1i_{r-1} in the sorted order (from the start if r=1r = 1) and strictly before iri_r, the value of lastlast is f(ir−1)f(i_{r-1}), and none of these activities passes line L5. After iki_k, the value of lastlast is f(ik)f(i_k), and no later activity passes line L5. Both statements hold because lastlast changes only in line L7, which runs exactly when an activity is appended.

So: (S) if an activity xx comes after ir−1i_{r-1} in the sorted order and s(x)≥f(ir−1)s(x) \ge f(i_{r-1}), then x=irx = i_r or xx comes after iri_r; and if r−1=kr - 1 = k, no such xx exists.

Step 3: the greedy stays ahead. Let BB be any set of pairwise compatible activities, and list it as j1,…,jmj_1, \ldots, j_m in non-decreasing order of finish time. For distinct compatible jr−1,jrj_{r-1}, j_r with f(jr−1)≤f(jr)f(j_{r-1}) \le f(j_r), the case f(jr)≤s(jr−1)f(j_r) \le s(j_{r-1}) would give f(jr)≤s(jr−1)<f(jr−1)f(j_r) \le s(j_{r-1}) < f(j_{r-1}), which is false; so f(jr−1)≤s(jr)f(j_{r-1}) \le s(j_r).

Lemma. For each rr with 1≤r≤min⁡(k,m)1 \le r \le \min(k, m), f(ir)≤f(jr)f(i_r) \le f(j_r).

Proof by induction on rr. Set f(j0)=−∞f(j_0) = -\infty, so the hypothesis f(ir−1)≤f(jr−1)f(i_{r-1}) \le f(j_{r-1}) holds for r=1r = 1. Take r≥1r \ge 1 and assume f(ir−1)≤f(jr−1)f(i_{r-1}) \le f(j_{r-1}). Then s(jr)≥f(jr−1)≥f(ir−1)s(j_r) \ge f(j_{r-1}) \ge f(i_{r-1}) (for r=1r = 1 this is s(j1)≥−∞s(j_1) \ge -\infty). Suppose, for a contradiction, that f(jr)<f(ir)f(j_r) < f(i_r). Then jrj_r comes before iri_r in the sorted order, because the sort is by finish time and the inequality is strict. Also f(jr)>s(jr)≥f(ir−1)f(j_r) > s(j_r) \ge f(i_{r-1}), so jrj_r comes after ir−1i_{r-1} in the sorted order (for r=1r = 1, after the start). So jrj_r is processed strictly between ir−1i_{r-1} and iri_r, with s(jr)≥f(ir−1)s(j_r) \ge f(i_{r-1}). By (S), jr=irj_r = i_r or jrj_r comes after iri_r, which contradicts that jrj_r comes before iri_r. So f(ir)≤f(jr)f(i_r) \le f(j_r). □\square

Step 4: optimality. Suppose m>km > k. Then jk+1j_{k+1} exists. If k=0k = 0, then n≥m≥1n \ge m \ge 1, and a1a_1 passes line L5 because last=−∞last = -\infty, so k≥1k \ge 1; this is a contradiction. So k≥1k \ge 1, and by the lemma f(ik)≤f(jk)≤s(jk+1)f(i_k) \le f(j_k) \le s(j_{k+1}). Also f(jk+1)>s(jk+1)≥f(ik)f(j_{k+1}) > s(j_{k+1}) \ge f(i_k), so jk+1j_{k+1} comes after iki_k in the sorted order, and s(jk+1)≥f(ik)s(j_{k+1}) \ge f(i_k). By (S) with r−1=kr - 1 = k, no such activity exists. This is a contradiction. So m≤km \le k for every compatible set BB.

By Step 1, AA is compatible, and by Step 4 no compatible set is larger. So AA is a maximum set of pairwise compatible activities. ■\blacksquare

Cost. Line L1 is one sort of nn keys. Each of the nn iterations of lines L4 to L7 does one comparison and O(1)O(1) other work. So the time is the time of the sort plus Θ(n)\Theta(n).

Back to Earliest finish time first