Algorithm Search⌘K

Proofs: Skew heap

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

Skew heap operations take O(log n) amortized time full

This proof shows: time.amortized.

We prove that each merge of a top-down skew heap costs at most 2⌊lg⁡n1⌋+2⌊lg⁡n2⌋+42\lfloor \lg n_1 \rfloor + 2\lfloor \lg n_2 \rfloor + 4 amortized units, where n1n_1 and n2n_2 are the sizes of its inputs. This is O(log⁡n)O(\log n) for insert, delete-min and meld. The argument is Sleator and Tarjan's.

Cost. The actual cost of a merge is the number of calls of MERGE that reach line L2, which is the number of nodes on the merge path: the nodes whose children are swapped. All other work is O(1)O(1) per call or per operation.

Potential. For a node xx, let s(x)s(x) be the number of nodes in its subtree. Call xx heavy if s(x.right)>s(x.left)s(x.right) > s(x.left), and light otherwise, with s(NIL)=0s(\mathrm{NIL}) = 0. Let Φ\Phi be the number of heavy nodes in all heaps. Then Φ≥0\Phi \ge 0, and Φ=0\Phi = 0 when there are no nodes.

Lemma 1. On the right path of a tree with m≥1m \ge 1 nodes, at most ⌊lg⁡m⌋+1\lfloor \lg m \rfloor + 1 nodes are light.

Proof. If xx is light, then s(x.right)≤s(x.left)s(x.right) \le s(x.left) and s(x.right)+s(x.left)=s(x)−1s(x.right) + s(x.left) = s(x) - 1, so s(x.right)≤(s(x)−1)/2<s(x)/2s(x.right) \le (s(x) - 1)/2 < s(x)/2. Along the right path, sizes never grow, and they fall below half at each light node. If the light nodes are x1,…,xqx_1, \dots, x_q from the top, then 1≤s(xq)<m/2q−11 \le s(x_q) < m/2^{q-1}, so 2q−1<m2^{q-1} < m and q−1≤⌊lg⁡m⌋q - 1 \le \lfloor \lg m \rfloor when mm is not a power of 2, or q−1≤lg⁡m−1q - 1 \le \lg m - 1 when it is. In both cases q≤⌊lg⁡m⌋+1q \le \lfloor \lg m \rfloor + 1. □\square

Lemma 2. In one merge, every heavy node on the merge path becomes light, and no node off the merge path changes its type.

Proof. The merge path consists of nodes of the right paths of the two inputs. A node off the merge path keeps both of its subtrees as they were, because MERGE only changes the fields of the nodes on its path, and returns other subtrees unchanged. So its type does not change.

Let xx be on the merge path, with old subtrees LL (left) and RR (right). After the merge, its right subtree is LL and its left subtree is the result of merging RR with another tree, which has at least ∣R∣|R| nodes. If xx was heavy, then ∣R∣>∣L∣|R| > |L|, so now s(x.left)≥∣R∣>∣L∣=s(x.right)s(x.left) \ge |R| > |L| = s(x.right), and xx is light. □\square

Theorem. A merge of heaps with n1n_1 and n2n_2 nodes has amortized cost at most 2⌊lg⁡n1⌋+2⌊lg⁡n2⌋+42\lfloor \lg n_1 \rfloor + 2\lfloor \lg n_2 \rfloor + 4, with lg⁡0\lg 0 read as −1-1 when a heap is empty.

Proof. Let the merge path have hh heavy and ℓ\ell light nodes, measured before the merge. The actual cost is h+ℓh + \ell. By Lemma 2, the hh heavy nodes become light, at most the ℓ\ell light nodes become heavy, and no other node changes, so ΔΦ≤ℓ−h\Delta\Phi \le \ell - h. The amortized cost is at most h+ℓ+ℓ−h=2ℓh + \ell + \ell - h = 2\ell. The merge path is part of the right paths of the two inputs, so by Lemma 1, ℓ≤⌊lg⁡n1⌋+1+⌊lg⁡n2⌋+1\ell \le \lfloor \lg n_1 \rfloor + 1 + \lfloor \lg n_2 \rfloor + 1. □\square

The operations. Insert merges with a one-node heap, so its amortized cost is at most 2⌊lg⁡n⌋+42\lfloor \lg n \rfloor + 4. Delete-min removes the root; this can only remove a heavy node, and the subtrees of all other nodes are unchanged, so Φ\Phi does not grow. It then merges two heaps of total size n−1n - 1, at amortized cost at most 4lg⁡n+44 \lg n + 4. Meld of heaps of sizes n1n_1 and n2n_2 costs at most 2lg⁡n1+2lg⁡n2+4≤4lg⁡n+42 \lg n_1 + 2 \lg n_2 + 4 \le 4 \lg n + 4.

Since Φ\Phi starts at 00 and is never negative, the actual cost of any sequence of mm operations starting from empty heaps is at most the sum of the amortized costs, which is O(mlog⁡n)O(m \log n) for nn the largest heap size. ■\blacksquare

Skew merge keeps heap order and the keys full

This proof shows: correctness.

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.

Lemma. If AA and BB are heap-ordered trees with roots aa and bb, possibly empty, then MERGE(a,b)\mathrm{MERGE}(a, b) returns the root of a heap-ordered tree TT with K(T)=K(A)⊎K(B)K(T) = K(A) \uplus K(B).

Proof. By induction on the total number of nodes ∣A∣+∣B∣|A| + |B|. If aa or bb is NIL, line L1 returns the other tree, and the claim holds. Otherwise, after line L2, a.key≤b.keya.key \le b.key, and a.keya.key is at most every key of AA, by heap order and induction on depth. So a.keya.key is a smallest key of A∪BA \cup B.

Let RR be the old right subtree of aa and LL the old left subtree. Line L4 makes LL the right subtree of aa. The call at line L5 has fewer nodes in total, because aa is not in RR or BB, so by induction it returns a heap-ordered tree MM with K(M)=K(R)⊎K(B)K(M) = K(R) \uplus K(B), which becomes the left subtree of aa.

The tree TT with root aa, left subtree MM and right subtree LL is in heap order: LL and MM are heap-ordered, every key of LL and of RR is at least a.keya.key by heap order of AA, and every key of BB is at least b.key≥a.keyb.key \ge a.key. Its keys are {a.key}⊎K(L)⊎K(R)⊎K(B)=K(A)⊎K(B)\{a.key\} \uplus K(L) \uplus K(R) \uplus K(B) = K(A) \uplus K(B). □\square

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

Proof. A one-node tree is heap-ordered, so line L7 is correct by the lemma. In DELETE-MIN, the root holds a smallest key by heap order. Its two subtrees are heap-ordered and hold all other keys, so line L8 gives a heap-ordered tree of those keys by the lemma. Meld is one MERGE. The loop form in the implementation performs the same steps as the recursion: at each node of the merge path it moves the old left subtree to the right and continues the merge in the left field, and when the old right subtree is empty, MERGE(NIL, bb) returns bb unchanged. ■\blacksquare

Back to Skew heap