Today’s Outline CSE 326: Data Structures • Binary Heaps: average runtime of insert • Leftist Heaps: re-do proof of property #1 Topic #5: Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs
2
Binary Heaps: Average runtime of Insert Right Path in a Leftist Tree is Short (#1) Recall: Insert-in-Binary-Heap(x) { Claim: The right path is as short as any in the tree. Put x in the next available position Proof: (By contradiction) percolateUp(last node) Pick a shorter path: D1 < D2 } Say it diverges from right path at x x ≤
How long does this percolateUp(last node) take? npl(L) D1-1 because of the path of
– Worst case: (tree height), i.e. (log n) length D1-1 to null L R D – Average case: (1) Why?? 1 D2 ≥ npl(R) D2-1 because every node on right path is leftist
Average runtime of insert in binary heap = ¡ (1) 3 Leftist property at x violated! 4
A Twist in Complexity Analysis: Skew Heaps The Amortized Case Problems with leftist heaps If a sequence of M operations takes O(M f(n)) time, – extra storage for npl we say the amortized runtime is O(f(n)). – extra complexity/logic to maintain and check npl – two pass iterative merge (requires stack!) • Worst case time per operation can still be large, say O(n) – right side is “often” heavy and requires a switch • Worst case time for any sequence of M operations is O(M f(n)) Solution: skew heaps • Average time per operation for any sequence is O(f(n)) – blind adjusting version of leftist heaps Is this the same as average time? – merge always switches children when fixing right path – iterative method has only one pass
– amortized time for merge, insert, and deleteMin is (log n)
5 – however, worst case time for all three is (n) 6
1 Merging Two Skew Heaps Example merge merge 5 3 3 merge T1 a a 7 5 7 merge 10 12 5 merge 3 14 12 10 14 L1 R1 L1 10 12 a < b R1 8 7 8 T 8 2 b b 3 14 5 7 L R 2 2 L2 R2 8 10 14 Only one step per iteration, with children always switched 7 12 8
Runtime Analysis: Skew Heap Code Worst-case and Amortized void merge(heap1, heap2) { case { • No worst case guarantee on right path length! heap1 == NULL: return heap2; • All operations rely on merge heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): Ω worst case complexity of all ops = temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); • Will do amortized analysis later in the course return heap1; (see chapter 11 if curious) otherwise: return merge(heap2, heap1); • Result: M merges take time M log n } } Ω amortized complexity of all ops = 9 10
Yet Another Data Structure: ATaB: Comparing Heaps Binomial Queues • Binary Heaps • Leftist Heaps • Structural property – Forest of binomial trees with at most one tree of any height • d-Heaps • Skew Heaps What’s a forest?
What’s a binomial tree? • Order property – Each binomial tree has the heap-order property
Still scope for improvement! 11 My opinion: Beautiful and elegant! 12
2 The Binomial Tree, Bh Binomial Q with n elements
h • Bh has height h and exactly 2 nodes Binomial Q with n elements has a unique structural
• Bh is formed by making Bh-1 a child of another Bh-1 representation in terms of binomial trees! • Root has exactly h children ≈ h ’ Write n in binary: n = 1101 = 13 • Number of nodes at depth d is binomial coeff. ∆ ÷ (base 2) (base 10) «d ◊ – Hence the name; we will not use this last property
1 B3 1 B2 No B1 1 B0 B0 B1 B2 B3
13 14
Properties of Binomial Q Operations on Binomial Q
• At most one binomial tree of any height • Will again define merge as the base operation – insert, deleteMin, buildBinomialQ will use merge • n nodes Ω binary representation is of size ? Ω deepest tree has height ? • Can we do increaseKey efficiently? Ω number of trees is ? decreaseKey?
Define: height(forest F) = maxtree T in F { height(T) } • What about findMin?
Binomial Q with n nodes has height ¡ (log n)
15 16
Merging Two Binomial Qs Complexity of Merge
Essentially like adding two binary numbers! Constant time for each height Max height is log n 1. Combine the two forests 2. For k from 1 to maxheight {
← Ω worst case running time = ( ) a. m total number of Bk’s in the two BQs # of 1’s b. if m=0: continue; 0+0 = 0 c. if m=1: continue; 1+0 = 1 d. if m=2: combine the two Bk’s to form a Bk+1 1+1 = 1+c e. if m=3: retain one Bk and 1+1+c = 1+c combine the other two to form a Bk+1 } Claim: When this process ends, the forest has at most one tree of any height 17 18
3 Insert in a Binomial Q deleteMin in Binomial Q
Insert(x): Similar to leftist or skew heap deleteMin: Similar to leftist and skew heaps A tiny bit more complicated runtime Worst case complexity: same as merge
( )
Average case complexity: (1) Why?? Hint: Think of adding 1 to 1101
19 20
deleteMin: Example deleteMin: Example
BQ 7 3 4 Result: 7 8 5 4 7 8 5
find and delete 7 smallest root
3 merge without the shaded part 8 5 BQ’ runtime: 7 21 22
buildBinomialQ To Do
Call insert n times on an initially empty BQ • Project #1 due tonight! – Bring printout to section tomorrow
runtime: naïve O(n log n) • Written homework #1 – will be out later today; I’ll send an email
careful analysis (n) idea: count the number of times one needs to combine trees • Revise binary search tree basics • Begin reading chapter 4 in the book
23 24
4