Algorithm Search⌘K

Proofs: Randomized heap

These are the human proofs of Randomized heap, at full length.

Randomized meld is correct and takes O(log n) expected time full

This proof shows: correctness, time.expected.

A tree is in heap order if the key of each node is at least the key of its parent. Write K(T)K(T) for the multiset of keys of TT and ∣T∣|T| for its number of nodes.

Lemma 1 (correctness). For every result of the coins, if AA and BB are heap-ordered trees with roots aa and bb, possibly empty, then MELD(a,b)\mathrm{MELD}(a, b) returns a heap-ordered tree TT with K(T)=K(A)⊎K(B)K(T) = K(A) \uplus K(B).

Proof. By induction on ∣A∣+∣B∣|A| + |B|. If one tree is empty, line L1 returns the other. Otherwise, after line L2, a.key≤b.keya.key \le b.key, and a.keya.key is a smallest key of A∪BA \cup B, because every key of AA is at least a.keya.key and every key of BB is at least b.keyb.key. Let CC be the subtree a.ca.c chosen at line L3. The call at line L4 has fewer nodes, since aa is not in CC or BB, so by induction it returns a heap-ordered tree MM with K(M)=K(C)⊎K(B)K(M) = K(C) \uplus K(B), every key of which is at least a.keya.key. The other subtree of aa is unchanged. So the tree with root aa is heap-ordered and holds K(A)⊎K(B)K(A) \uplus K(B). □\square

Theorem 1. INSERT, DELETE-MIN and MELD keep heap order and the right multiset, and DELETE-MIN returns a smallest key.

Proof. As for any heap-ordered tree: insert melds with a one-node tree, and delete-min removes the root, which holds a smallest key, and melds its two subtrees, which hold all other keys. Lemma 1 gives the rest. □\square

Lemma 2 (random walk). For a binary tree TT, let W(T)W(T) be the number of nodes visited by a walk that starts at the root, and at each node moves to the left or the right child with probability 1/21/2 each, independently, until it reaches an empty subtree. Then E[W(T)]≤lg⁡(∣T∣+1)\mathbb{E}[W(T)] \le \lg(|T| + 1).

Proof. By induction on ∣T∣|T|. If TT is empty, W=0=lg⁡1W = 0 = \lg 1. Otherwise let TT have subtrees LL and RR with ℓ\ell and rr nodes, so ∣T∣=ℓ+r+1|T| = \ell + r + 1. Then

E[W(T)]=1+12E[W(L)]+12E[W(R)]≤1+12lg⁡(ℓ+1)+12lg⁡(r+1).\mathbb{E}[W(T)] = 1 + \tfrac12 \mathbb{E}[W(L)] + \tfrac12 \mathbb{E}[W(R)] \le 1 + \tfrac12 \lg(\ell + 1) + \tfrac12 \lg(r + 1).

Because lg⁡\lg is concave, 12lg⁡u+12lg⁡v≤lg⁡u+v2\tfrac12 \lg u + \tfrac12 \lg v \le \lg \frac{u + v}{2} for u,v>0u, v > 0. With u=ℓ+1u = \ell + 1 and v=r+1v = r + 1,

E[W(T)]≤1+lg⁡ℓ+r+22=lg⁡(ℓ+r+2)=lg⁡(∣T∣+1).□\mathbb{E}[W(T)] \le 1 + \lg \frac{\ell + r + 2}{2} = \lg(\ell + r + 2) = \lg(|T| + 1). \quad \square

Lemma 3 (the meld path). Let the shapes of AA and BB be fixed before the meld, and let the coins of the meld be fresh. Then the number of calls of MELD that reach line L2 is at most XA+XBX_A + X_B, where XAX_A has the distribution of a prefix of W(A)W(A) and XBX_B of a prefix of W(B)W(B). So the expected number of calls, with the last call at line L1, is at most lg⁡(∣A∣+1)+lg⁡(∣B∣+1)+1\lg(|A| + 1) + \lg(|B| + 1) + 1.

Proof. Each call that reaches line L2 has two nonempty roots. One of them, the smaller one aa, is the node at which this call flips its coin; call it the node charged to the call. Each node is charged at most once, because after its call the path goes into a subtree of it. Consider the nodes of AA that are charged, in the order of the calls. The first one, if any, is the root of AA. If uu is a charged node of AA, the call at uu chooses a child u.cu.c by its coin. The next call involves the subtree u.cu.c with its original root: if that subtree is not empty, its root is either charged in this next call or, if it loses the comparison, it is passed on unchanged as the second argument, and every later call keeps the root of this subtree as one of its two arguments until that root is charged or the meld ends. So the charged nodes of AA are a prefix of the walk in AA driven by the coins flipped at nodes of AA. These coins are independent and fair. The same holds for BB. Every call that reaches line L2 charges a node of AA or of BB, so the number of such calls is at most XA+XBX_A + X_B, where XA≤W(A)X_A \le W(A) and XB≤W(B)X_B \le W(B) for walks with these coins. Lemma 2 and linearity of expectation give the bound. □\square

Theorem 2. Against a sequence of operations fixed in advance, each meld of heaps of sizes n1n_1 and n2n_2 makes at most lg⁡(n1+1)+lg⁡(n2+1)+1\lg(n_1 + 1) + \lg(n_2 + 1) + 1 calls in expectation, insert at most lg⁡(n+1)+2\lg(n + 1) + 2, and delete-min at most 2lg⁡n+12 \lg n + 1, where nn is the size before the operation.

Proof. The shapes before an operation depend only on earlier coins, and the coins of the operation are independent of them, so Lemma 3 applies conditioned on the shapes, and then also without the condition. Insert melds with one node: lg⁡(n+1)+lg⁡2+1\lg(n + 1) + \lg 2 + 1. Delete-min melds two subtrees of total size n−1n - 1; with n1+n2=n−1n_1 + n_2 = n - 1, concavity gives lg⁡(n1+1)+lg⁡(n2+1)≤2lg⁡n+12≤2lg⁡n\lg(n_1 + 1) + \lg(n_2 + 1) \le 2 \lg \frac{n + 1}{2} \le 2 \lg n for n≥1n \ge 1, so the bound is 2lg⁡n+12 \lg n + 1. Each call does O(1)O(1) work, so each operation takes O(log⁡n)O(\log n) expected time. ■\blacksquare

Back to Randomized heap