Algorithm Search⌘K

Proofs: Tournament tree

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

The winner tree and the loser tree give the winner after each update full

This proof shows: correctness.

The slots are 0,…,k−10, \dots, k - 1. Slot aa beats slot bb if key[a]<key[b]key[a] < key[b], or key[a]=key[b]key[a] = key[b] and a<ba < b, with +∞+\infty larger than every real key. This is a strict total order on slots, so every nonempty set SS of slots has a unique winner win(S)\mathrm{win}(S), which beats every other slot of SS. For a node jj of the tree, let S(j)S(j) be the set of slots whose leaves are below jj or at jj, and let w(j)=win(S(j))w(j) = \mathrm{win}(S(j)). For an internal node jj, S(j)=S(2j)∪S(2j+1)S(j) = S(2j) \cup S(2j + 1), a disjoint union, so

w(j)=the one of w(2j),w(2j+1) that beats the other.(1)w(j) = \text{the one of } w(2j), w(2j+1) \text{ that beats the other}. \tag{1}

Lemma 1 (winner tree). If t[k+i]=it[k + i] = i for each slot and t[j]t[j] is the winner of t[2j]t[2j] and t[2j+1]t[2j + 1] for each internal jj, then t[j]=w(j)t[j] = w(j) for every node.

Proof. By induction on the height of jj. At a leaf k+ik + i, S={i}S = \{i\} and t=it = i. At an internal node, t[2j]=w(2j)t[2j] = w(2j) and t[2j+1]=w(2j+1)t[2j + 1] = w(2j + 1) by induction, and (1) gives t[j]=w(j)t[j] = w(j). □\square

BUILD. Line L1 sets the leaves. Line L2 visits j=k−1,…,1j = k - 1, \dots, 1 in decreasing order, and both children 2j,2j+12j, 2j + 1 are larger than jj, so they are set before jj. Line L3 then sets t[j]t[j] as Lemma 1 needs. So after BUILD, t[1]=w(1)t[1] = w(1) is the overall winner.

UPDATE in the winner tree. A change of key[i]key[i] changes w(j)w(j) only for the nodes jj with i∈S(j)i \in S(j), which are the ancestors of leaf k+ik + i. Lines L4 to L7 visit these ancestors from the bottom up. When node jj is visited, its child on the path was visited before, and its other child is not an ancestor of the leaf, so both children satisfy the condition of Lemma 1. Line L6 then makes jj satisfy it. Nodes off the path are unchanged and still satisfy it. By Lemma 1, after the loop t[1]t[1] is the winner of the new keys.

Lemma 2 (loser tree, the path of the winner). Let the loser tree satisfy (L): l[j]l[j] is the loser of w(2j)w(2j) and w(2j+1)w(2j + 1) for each internal jj, and l[0]=w(1)l[0] = w(1). Let i=l[0]i = l[0]. Then for each internal ancestor jj of leaf k+ik + i, with cc its child on the path and c′c' the other child, w(c)=iw(c) = i and l[j]=w(c′)l[j] = w(c').

Proof. Slot ii is in S(c)S(c) and beats every slot of S(1)⊇S(c)S(1) \supseteq S(c), so w(c)=iw(c) = i. At node jj, the two players are w(c)=iw(c) = i and w(c′)w(c'), and ii wins, so the loser l[j]l[j] is w(c′)w(c'). □\square

LOSER-UPDATE. Let i=l[0]i = l[0], and let the key of ii change; all other keys stay. Let j1,j2,…,jd=1j_1, j_2, \dots, j_d = 1 be the internal ancestors of leaf k+ik + i from the bottom, and write crc_r for the child of jrj_r on the path and cr′c'_r for the other child. After the change, write w′w' for the new winners. Since S(cr′)S(c'_r) does not contain ii, w′(cr′)=w(cr′)=l[jr]w'(c'_r) = w(c'_r) = l[j_r] by Lemma 2.

We show by induction on rr the claim: when the loop reaches jrj_r, the variable ww holds w′(cr)w'(c_r), and each l[js]l[j_s] for s<rs < r holds the new loser at jsj_s. At r=1r = 1, c1c_1 is the leaf of ii and w=i=w′(c1)w = i = w'(c_1). At step rr, the two players at jrj_r are w′(cr)=ww'(c_r) = w and w′(cr′)=l[jr]w'(c'_r) = l[j_r]. Line L11 keeps the loser in l[jr]l[j_r] and puts the winner in ww, which by (1) is w′(jr)=w′(cr+1)w'(j_r) = w'(c_{r+1}). This proves the claim for r+1r + 1.

After the loop, every node on the path holds its new loser, w=w′(1)w = w'(1), and line L13 stores it in l[0]l[0]. Nodes off the path have no slot ii below them, so their players and losers are unchanged. So (L) holds for the new keys, and l[0]l[0] is the new winner. Each visited node costs one comparison, so the update costs d=⌊lg⁡(k+i)⌋d = \lfloor \lg(k + i) \rfloor comparisons.

Merge. In the kk-way merge, slot rr holds the smallest key not yet output from run rr, or +∞+\infty. The winner is the smallest of these, which is a smallest key not yet output of all runs, because each run is sorted. So each output is the next key in sorted order, the output stops when all slots hold +∞+\infty, and every key is output once. ■\blacksquare

Back to Tournament tree