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 of activities, each with and the half-open interval . Two distinct activities are compatible when or . Line L1 gives the order with ; ties are in any order. Let be the activities appended in line L6, in the order of appending.
Claim. The algorithm stops after iterations, and the returned list is a set of pairwise compatible activities of the largest possible size.
Termination. The loop of line L4 runs for , and each iteration does a bounded number of steps. So the algorithm stops.
Step 1: the invariant. Before the iteration for , with :
(I) the activities in are pairwise compatible; they appear in in non-decreasing order of finish time; and if is empty, else is the finish time of the last activity of , which is the largest finish time in .
Initialization. Before the first iteration, is empty and by lines L2 and L3. All three parts hold.
Maintenance. Assume (I) before the iteration for . If the test of line L5 fails, nothing changes and (I) holds before the next iteration. If it passes, then . Each activity in has by (I), so , and is compatible with . The activities of come from , so is not one of them and each has finish time at most . After lines L6 and L7, is pairwise compatible, it is in non-decreasing order of finish time, and is the finish time of its last activity and its largest finish time.
Termination of the loop. After the iteration for , (I) holds, so is a set of pairwise compatible activities.
Step 2: what the scan sees between two choices. Let be a dummy with . For , while the loop processes the activities strictly after in the sorted order (from the start if ) and strictly before , the value of is , and none of these activities passes line L5. After , the value of is , and no later activity passes line L5. Both statements hold because changes only in line L7, which runs exactly when an activity is appended.
So: (S) if an activity comes after in the sorted order and , then or comes after ; and if , no such exists.
Step 3: the greedy stays ahead. Let be any set of pairwise compatible activities, and list it as in non-decreasing order of finish time. For distinct compatible with , the case would give , which is false; so .
Lemma. For each with , .
Proof by induction on . Set , so the hypothesis holds for . Take and assume . Then (for this is ). Suppose, for a contradiction, that . Then comes before in the sorted order, because the sort is by finish time and the inequality is strict. Also , so comes after in the sorted order (for , after the start). So is processed strictly between and , with . By (S), or comes after , which contradicts that comes before . So .
Step 4: optimality. Suppose . Then exists. If , then , and passes line L5 because , so ; this is a contradiction. So , and by the lemma . Also , so comes after in the sorted order, and . By (S) with , no such activity exists. This is a contradiction. So for every compatible set .
By Step 1, is compatible, and by Step 4 no compatible set is larger. So is a maximum set of pairwise compatible activities.
Cost. Line L1 is one sort of keys. Each of the iterations of lines L4 to L7 does one comparison and other work. So the time is the time of the sort plus .