Partial preview of the text
Download Pairing Heaps - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Pairing Heaps
Fibonacci Pairing
Insert O(1) O(1)
Remove min (or max) O(n) O(n)
Meld O(1) O(1)
Remove O(n) O(n)
Decrease key (or
increase)
O(n) O(1)
Pairing Heaps
Fibonacci Pairing
Insert O(1) O(log n)
Remove min (or max) O(log n) O(log n)
Meld O(1) O(log n)
Remove O(log n) O(log n)
Decrease key (or
increase)
O(1) O(log n)
Pairing Heaps
• Experimental results suggest that pairing
heaps are actually faster than Fibonacci
heaps.
Simpler to implement.
Smaller runtime overheads.
Less space per node.
Definition
• A min (max) pairing heap is a min (max) tree in
which operations are done in a specified manner.
Insert
• Create 1 -element max tree with new item
and meld with existing max pairing heap.
insert(2)
Insert
• Create 1 -element max tree with new item
and meld with existing max pairing heap.
insert(14)
• Actual cost = O(1).
Worst-Case Degree
• Insert 9 , 8 , 7 , …, 1 , in this order.
• Worst-case degree = n –1.
Worst-Case Height
• Insert 1 , 2 , 3 , …, n, in this order.
• Worst-case height = n.
IncreaseKey(theNode, theAmount)
Meld subtree with remaining tree.
IncreaseKey(theNode, theAmount)
• Actual cost = O(1).
Remove Max
• If empty => fail.
• Otherwise, remove tree root and meld
subtrees into a single max tree.
• How to meld subtrees?
Good way => O(log n) amortized complexity
for remove max.
Bad way => O(n) amortized complexity.
Bad Way To Meld Subtrees
• currentTree = first subtree.
• for (each of the remaining trees)
currentTree = compareLink(currentTree,
nextTree);
Good Ways To Meld Subtrees
• Two-pass scheme.
• Multipass scheme.
• Both have same asymptotic complexity.
• Two-pass scheme gives better observed
performance.
Two-Pass Scheme
• Pass 1.
Examine subtrees from left to right.
Meld pairs of subtrees, reducing the number of
subtrees to half the original number.
If # subtrees was odd, meld remaining original
subtree with last newly generated subtree.
• Pass 2.
Start with rightmost subtree of Pass 1. Call this
the working tree.
Meld remaining subtrees, one at a time, from
right to left, into the working tree.
Two-Pass Scheme – Example
T1 T2 T3 T4 T5 T6 T7 T
S1 S2 S3 S
Two-Pass Scheme – Example
S1 S2 S3 S
R
R
R
Multipass Scheme--Example
S1 S2 S
T7 T
S1 S2 S3 S
S3 S
R
R
R
Multipass Scheme--Example
R1 R
Q
• Actual cost = O(n).
Remove Nonroot Element
• Remove theNode from its sibling list.
• Meld children of theNode using either 2 -pass
or multipass scheme.
• Meld resulting tree with what’s left of
original tree.
Remove(theNode)
Remove theNode from its doubly-linked sibling list.
theNode
Remove(theNode)
• Actual cost = O(n).