Algorithm Search⌘K

Proofs: Hire-assistant

These are the human proofs of Hire-assistant, at full length.

Hire-assistant hires exactly the candidates who beat all earlier ones full

This proof shows: correctness.

We use the pseudocode of the page, with candidates C[1…n]C[1 \ldots n] and a dummy candidate C[0]C[0] that is worse than each real candidate. A position j≥1j \ge 1 is a record if C[j]C[j] is better than each of C[0],C[1],…,C[j−1]C[0], C[1], \ldots, C[j-1]. Since C[0]C[0] is worse than all, position 1 is always a record.

Claim. HIRE-ASSISTANT hires exactly the records, in increasing order of position, and at the end C[best]C[best] is a best candidate of C[1…n]C[1 \ldots n] when n≥1n \ge 1.

Proof. We use this invariant of the loop of line L2.

(I) Before the iteration for ii, with 1≤i≤n+11 \le i \le n + 1: (a) 0≤best≤i−10 \le best \le i - 1, and C[best]C[best] is at least as good as each of C[0…i−1]C[0 \ldots i-1]; (b) the hires so far are exactly the records j≤i−1j \le i - 1, in increasing order.

Initialization. Before the first iteration, i=1i = 1 and line L1 sets best=0best = 0. Part (a) holds because C[0…0]C[0 \ldots 0] is the dummy alone. Part (b) holds because no hire was made and no record j≤0j \le 0 exists.

Maintenance. Assume (I) for i≤ni \le n. By (a), C[best]C[best] is a maximum of C[0…i−1]C[0 \ldots i-1]. So C[i]C[i] is better than C[best]C[best] if and only if C[i]C[i] is better than each of C[0…i−1]C[0 \ldots i-1], that is, if and only if ii is a record. Line L3 does not change bestbest or the hires.

  • If ii is a record, line L4 is true. Line L5 sets best=ibest = i, and C[i]C[i] is better than each of C[0…i−1]C[0 \ldots i-1], so (a) holds for i+1i + 1. Line L6 hires ii, which is larger than each earlier hire, so (b) holds for i+1i + 1.
  • If ii is not a record, line L4 is false and nothing changes. C[i]C[i] is not better than C[best]C[best], so in a total order C[best]C[best] is at least as good as C[i]C[i], and (a) holds for i+1i + 1. No record was missed, so (b) holds for i+1i + 1.

Termination. The loop stops with i=n+1i = n + 1. By (b), the hires are exactly the records j≤nj \le n, in order. By (a), C[best]C[best] is at least as good as each of C[0…n]C[0 \ldots n]. If n≥1n \ge 1, position 1 is a record, so a real candidate was hired and best≥1best \ge 1; hence C[best]C[best] is a best candidate of C[1…n]C[1 \ldots n], and bestbest is the last hire. □\square

Cost. The loop runs nn times and each iteration does one comparison in line L4 and O(1)O(1) other work, so the time is Θ(n)\Theta(n). The number of hires mm is the number of records.

Expected number of hires. Let the order of C[1…n]C[1 \ldots n] be a uniform random permutation of distinct candidates, and let XiX_i be the indicator that ii is a record. The relative order of C[1…i]C[1 \ldots i] is uniform on the i!i! orders, so each of these ii positions holds the best of them with probability 1/i1/i, and Pr⁡[Xi=1]=1/i\Pr[X_i = 1] = 1/i. By linearity of expectation, E[m]=∑i=1nE[Xi]=Hn\mathbb{E}[m] = \sum_{i=1}^n \mathbb{E}[X_i] = H_n. For RANDOMIZED-HIRE-ASSISTANT, the shuffle makes the order uniform for each fixed input of distinct candidates, so the same computation gives E[m]=Hn\mathbb{E}[m] = H_n for each input. □\square

Back to Hire-assistant