Algorithm Search⌘K

Proofs: Tournament method

These are the human proofs of Tournament method, at full length.

The tournament method returns the largest and the second largest full

This proof shows: correctness, termination.

Let b1≤⋯≤bnb_1 \le \cdots \le b_n be the items of AA in sorted order, with n≥2n \ge 2. The claim is: TOURNAMENT-SECOND(A)(A) stops and returns (bn,bn−1)(b_n, b_{n-1}), after at most n+⌈lg⁡n⌉−2n + \lceil \lg n \rceil - 2 comparisons.

Termination and the shape of the rounds. A round on p≥2p \ge 2 players makes ⌊p/2⌋≥1\lfloor p/2 \rfloor \ge 1 matches and leaves ⌈p/2⌉<p\lceil p/2 \rceil < p players (lines L4 to L9). So the number of players drops in each round and the loop of line L2 stops with one player. A round never adds a player, and each match removes exactly one, the loser, so the tournament makes exactly n−1n - 1 matches, and each index other than the champion mm is the loser ll of exactly one match. Lines L11 and L12 scan a finite list, so the algorithm stops.

Lemma 1 (the beaten lists). If ll is appended to beaten[w][w] in line L7, then A[w]≥A[l]A[w] \ge A[l], and ww is still a player after the round in which ll leaves.

Proof. Line L6 sets (w,l)=(u,v)(w, l) = (u, v) only if A[u]≥A[v]A[u] \ge A[v], and (w,l)=(v,u)(w, l) = (v, u) only if A[u]<A[v]A[u] < A[v]. In both cases A[w]≥A[l]A[w] \ge A[l]. Line L7 appends ww to QQ, the players of the next round. □\square

Lemma 2 (the champion). A[m]=bnA[m] = b_n, and for each index i≠mi \ne m there is an index jj in beaten[m][m] with A[j]≥A[i]A[j] \ge A[i].

Proof. Let i≠mi \ne m. Then ii lost exactly one match, to some w1w_1; write i=w0i = w_0. If w1≠mw_1 \ne m, then w1w_1 also lost exactly one match, to some w2w_2, in a later round than the one in which w0w_0 lost, by Lemma 1. The rounds are finite, so the chain w0,w1,w2,…w_0, w_1, w_2, \ldots reaches the only index that never lost, mm. Let wt=mw_t = m. Then wt−1w_{t-1} is in beaten[m][m], and by Lemma 1, A[wt−1]≥A[wt−2]≥⋯≥A[w0]=A[i]A[w_{t-1}] \ge A[w_{t-2}] \ge \cdots \ge A[w_0] = A[i]. Take j=wt−1j = w_{t-1}. Also A[m]≥A[j]≥A[i]A[m] \ge A[j] \ge A[i] for each i≠mi \ne m, so A[m]A[m] is a largest item, bnb_n. □\square

Lemma 3 (the scan). Let BB be beaten[m][m]. After line L12, ss is in BB and A[s]≥A[i]A[s] \ge A[i] for each ii in BB.

Proof. The list BB is not empty, since n≥2n \ge 2 and so mm won at least one match: the last round has two players. Line L11 sets ss to the first entry. Line L12 keeps the invariant "A[s]A[s] is the largest value among the entries of BB seen so far, and ss is one of them": an entry ii with A[i]>A[s]A[i] > A[s] replaces ss, and other entries are at most A[s]A[s]. □\square

Conclusion. By Lemma 2, A[m]=bnA[m] = b_n. Remove the index mm from AA; the other items, sorted, are b1,…,bn−1b_1, \ldots, b_{n-1}, so their largest is bn−1b_{n-1}. By Lemmas 2 and 3, A[s]≥A[j]≥A[i]A[s] \ge A[j] \ge A[i] for each i≠mi \ne m, where jj is the index of Lemma 2, and s≠ms \ne m is one of these indices. So A[s]A[s] is the largest item other than A[m]A[m], which is bn−1b_{n-1}.

The count. The tournament makes n−1n - 1 comparisons. After rr rounds at most ⌈n/2r⌉\lceil n / 2^r \rceil players are left, so there are at most ⌈lg⁡n⌉\lceil \lg n \rceil rounds, and mm plays at most one match in each, so ∣B∣≤⌈lg⁡n⌉|B| \le \lceil \lg n \rceil. Line L12 makes ∣B∣−1|B| - 1 comparisons. The total is at most n−1+⌈lg⁡n⌉−1=n+⌈lg⁡n⌉−2n - 1 + \lceil \lg n \rceil - 1 = n + \lceil \lg n \rceil - 2. ■\blacksquare

Back to Tournament method