Rank Pairing Heaps-Powerpoint

advertisement
Rank-Pairing Heaps
Bernhard Haeupler, Siddhartha Sen,
and Robert Tarjan, ESA 2009
1
Observation
Computer science is (still) a young field.
We often settle for the first (good) solution.
It may not be the best: the design space is rich.
2
Research Agenda
For fundamental problems, systematically explore the
design space to find the best solutions, seeking
elegance: “a quality of neatness and ingenious
simplicity in the solution of a problem (especially in
science or mathematics).”
 wordnet.princeton.edu/perl/webwn
Keep the design simple, allow complexity in the
analysis.
3
Heap (Priority Queue) Problem
Maintain a set (heap) of items, each with a real-valued key,
under the operations
Find minimum: find the item of minimum key in a heap
Insert: add a new item to a heap
Delete minimum : remove the item of minimum key
Meld: Combine two item-disjoint heaps into one
Decrease key: subtract a given positive amount from the key
of a given item in a known heap
Goal: O(log n) for delete min, O(1) for others
Related Work
Fibonacci heaps achieve the desired bounds (Fredman
& Tarjan, 1984); so do
•
•
•
•
•
•
Peterson’s heaps (1987)
Høyer’s heaps (1995)
Brodal’s heaps (1996), worst-case
Thin heaps (Kaplan & Tarjan, 2008)
Violation heaps (Elmasry, 2008)
Quake heaps (Chan, 2009)
Not pairing heaps:
(loglog n) time per key decrease, but good in practice
Heap-ordered
model
Half-ordered model
Half-ordered model
Half-ordered model
Half-ordered tree: binary tree, one item per node, each
item less than all items in left subtree
Half tree: half-ordered binary tree with no right subtree
Link two half trees:
10
5
5
+
A
10
B
A
O(1) time, preserves half order
B
Heap: a set (circular singly-linked list) of half trees, with
minimum root first on list; access via last on list
Find min: return minimum
Insert: Form a new one-node tree, combine with
current set of half trees, update the minimum
Meld: Combine sets of half trees, update the minimum
Delete min: Remove minimum root (forming new half
trees); Repeatedly link half trees, form a set of the
remaining trees
How to link? Use ranks: leaves have rank zero,
only link trees whose roots have equal rank,
increase winner’s rank by one:
k
k-1
k
+
k+1
k-1
Vuillemin’s binomial
queues!
k
k-1
k-1
All rank differences are 1: a half tree of rank k is a
perfect binary tree plus a root:
2k nodes, rank = lg n
Delete min
Each node on Delete
right path
minimum
becomes a new root
Delete min
Link half trees of equal rank
Array of buckets, at most one per rank
Delete min
“multipass”
Link half trees of equal rank
Form
new set
of halfone
trees
Array of
buckets,
at most
per rank
Delete min
Find Form
new minimum
new set ofinhalf
O(log
trees
n) time
Delete min: lazier linking
“one-pass”
Find minimum in O(log n)
time
Keep trackadditional
of minimum
during links
Delete min: lazier linking
Form new set of half trees
Amortized Analysis of Lazy Binomial
Queues
 = #trees
Link: O(1) time,  = -1, amortized time = 0
Insert: O(1) time,  = 1
Meld: O(1) time,  = 0
Delete min: if k trees after root removal, time is
O(k), potential decreases by k/2 – O(log n)
 O(log n) amortized time
Decrease key?
Application: Dijkstra’s shortest path algorithm, others
Method: To decrease key of x, detach its half tree,
restructure if necessary
u
x
x
y
u
y
(If x is the right child of u, no easy way to tell if half
order is violated)
How to maintain structure?
All previous methods, starting with Fibonacci
heaps, change ranks and restructure
Some, like Quake heaps (Chan, 2009) and Relaxed heaps
(Driscoll et al., 1988), do not restructure during key decrease,
but this just postpones restructuring
But all that is needed is rank changes:
Trees can have arbitrary structure!
Rank-Pairing Heaps = rp-heaps
Goal is
SIMPLICITY
Node Ranks
Each node has a non-negative integer rank
Convention: missing nodes have rank -1
(leaves have rank 0)
rank difference of a child =
rank of parent - rank of child
i-child: node of rank difference i
i,j-node: children have rank differences i and j
Convention: the child of a root is a 1-child
21
Rank Rules
Easy-to-analyze version (type 2):
All rank differences are non-negative
If rank difference exceeds 2, sibling has rank difference 0
If rank difference is 0, sibling has rank difference at least 2
1
1
2
1
1
2
≥2
0
0
≥2
Simpler but harder-to-analyze version (type 1):
If rank difference exceeds 1, sibling has rank difference 0
If rank difference is 0, sibling has rank difference ≥ 1
1
1
≥1
0
0
≥1
Tree Size
If nk is minimum number of descendants of a
node of rank k,
n0 = 1, nk = nk-1 + nk-2 : Fibonacci numbers
nk ≥ 
k,
=
k  log n
1 5
2
Decrease key
u k-1
k-1
21
1
u
1
6
5
10
u
106
16
u
60
028
u
1020
08
x
0
10
y
Detach
Restore
0-children
half
rank
block
tree
rule
propagation of rank
decreases from
sibling’s subtree
Amortized Analysis
Potential of node = sum of rank differences of children - 1
+1 if root (= 1)
-1 if 1,1-node (= 0)
Link is free:
1
(+1)
1
(+1)
+
1
One unit pays for link
Insert needs 1 unit, meld none
1
(0)
1
(+1)
Delete min
rank k
1
1
1 unit
needed
≥2
0
2
Each 1,1 needs potential 1,
adding at most k in total.
Delete min takes O(log n) amortized time
1
Decrease Key
Successive rank decreases are non-increasing
At most two 1,1’s occur on path of rank decreases –
1,1 becomes 0, j : prev decrease >1, next decrease = 1
1,1 becomes 1,2 : terminal
Give each 1,1 one extra unit of potential
Each rank decrease releases a unit to pay for decrease:
rank diffs of both children decrease by k, rank diff of
parent increases by k
Decrease key
2 k-1
1
1 k-1
21
1
Detach
Restorehalf
ranktree
rule
4
6
0
1
6
5
10
11
50
106
16
13
51
60
028
17
71
1020
08
9
0
10
Type-1 rp-heaps
Max k  lg n
Analysis requires a more elaborate potential
based on rank differences of children and
grandchildren
Same bounds as type-2 rp-heaps, provided we
preferentially link half trees from disassembly
Summary
Rank-pairing heaps combine performance guarantees of
Fibonacci heaps with simplicity approaching pairing heaps
Type-1 rp-heaps are especially simple, but not simple to analyze
Results build on prior work by Peterson, Høyer, and Kaplan
and Tarjan, and may be the natural conclusion of this work
Simpler methods of doing key decrease are not efficient
(see paper)
Preliminary experiments show rp-heaps are competitive
with pairing heaps on typical input sequences, and better on
worst-case sequences
Additional Questions
One-tree version of rp-heaps?
Yes, but unfair links add losers to the bottom of the
loser list instead of the top
Decrease key without cascading rank changes?
No, with O(log n) time per delete min
Maybe, with O(log m) time per delete min
need at least one rank decrease
Simplify Brodal’s heaps?
Download