BinomialHeaps 207
BinomialHeaps 207
‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
2
Priority queue applications
Applications.
・A* search.
・Heapsort.
・Online median.
・Huffman encoding.
・Prim’s MST algorithm.
・Discrete event-driven simulation.
・Network bandwidth management.
・Dijkstra’s shortest-paths algorithm.
・…
http://younginc.site11.com/source/5895/fos0092.html
3
P RIORITY Q UEUES
‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
SECTION 2.4
Complete binary tree
Binary tree. Empty or node with links to two disjoint binary trees.
5
A complete binary tree in nature
6
Binary heap
Heap-ordered tree. For each child, the key in child ≥ key in parent.
parent
1
8
0
1 1 1 2
child child
2 8 1 5
2 1 1
1 7 9
7
Explicit binary heap
Pointer representation. Each node has a pointer to parent and two children.
・Maintain number of elements n.
・Maintain pointer to root node.
・Can find pointer to last node or next node in O(log n) time.
root
1
8
0
1 1 1 2
2 8 1 5
2 1 1
1 7 9
last next
8
Implicit binary heap
2 3
1
8
0
4 5 6 7
1 1 1 2
2 8 1 5
8 9 10
2 1 1
1 7 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
6 10 8 12 18 11 25 21 17 19
9
Binary heap demo
heap ordered
1
8
0
1 1 1 2
2 8 1 5
2 1 1
1 7 9
10
Binary heap: insert
Insert. Add element in new node at end; repeatedly exchange new element
with element in its parent until heap order is restored.
6
1
8
0
1 1 1 2
2 8 1 5
7 8
1 1 1 2
2 0 1 5
2 1 1 1
1 7 9 8 swim up
11
Binary heap: extract the minimum
Extract min. Exchange element in root node with last node; repeatedly
exchange element in root with its smaller child until heap order is restored.
6 element to
remove
7 8
1 1 1 2
2 0 1 5
2 1 1 1 exchange
1 7 9 8
with root
sink down
1 violates 7
8
heap order
1
7 8 8
0
1 1 1 2 1 1 1 2
2 0 1 5 2 8 1 5
2 1 1 remove 2 1 1
6 6
1 7 9 1 7 9
from heap
12
Binary heap: decrease key
Decrease key. Given a handle to node, repeatedly exchange element with its
parent until heap order is restored.
1
8
0
1 1 1 2
2 8 1 5
2 1 1
1 7 9
13
Binary heap: analysis
14
Binary heap: find-min
root
1
7
0
1 1 2
8
2 1 5
2 1
9
1 7
15
Binary heap: delete
Delete. Given a handle to a node, exchange element in node with last node;
either swim down or sink up the node until heap order is restored.
delete node x or y
1
x 7
0
1 1 2
8
2 1 5
y
2 1
9
1 7
last
16
Binary heap: meld
Meld. Given two binary heaps H1 and H2, merge into a single binary heap.
H1 H2
1
7
0
1 1 2
8
2 1 5
2 1
9
1 7
17
Binary heap: heapify
1 8
2 1 3 9
2
4 5 2 6 7 2
7 3
2 6
8 1 9 1 10 1 11 2
4 1 5 2
8 12 9 7 22 3 26 14 11 15 22
1 2 3 4 5 6 7 8 9 10 11
18
Binary heap: heapify
19
Priority queues performance cost summary
20
Priority queues performance cost summary
† amortized
21
P RIORITY Q UEUES
‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
SECTION 2.4
Complete d-ary tree
Fact. The height of a complete d-ary tree with n nodes is ≤ ⎡logd n⎤.
23
d-ary heap
3 1 2
0 0 0
8 3 3 5 2 3 4 2 4
0 2 4 5 2 4 6 0 0
8 9
2 0
24
d-ary heap: insert
Insert. Add node at end; repeatedly exchange element in child with element
in parent until heap order is restored.
3 1 2
0 0 0
8 3 3 5 2 3 4 2 4
0 2 4 5 2 4 6 0 0
8 9
2 0
25
d-ary heap: extract the minimum
Extract min. Exchange root node with last node; repeatedly exchange
element in parent with element in largest child until heap order is restored.
3 1 2
0 0 0
8 3 3 5 2 3 4 2 4
0 2 4 5 2 4 6 0 0
8 9
2 0
26
d-ary heap: decrease key
3 1 2
0 0 0
8 3 3 5 2 3 4 2 4
0 2 4 5 2 4 6 0 0
8 9
2 0
27
Priority queues performance cost summary
28
P RIORITY Q UEUES
‣ binary heaps
‣ d-ary heaps
‣ binomial heaps
‣ Fibonacci heaps
mergeable heap 30
Binomial heaps
31
Binomial tree
B0 Bk
Bk-1
Bk-1
B0 B1 B2 B3 B4
32
Binomial tree properties
Bk+1
B1 B0
B2
Bk
B4
33
Binomial heap
6 3 18
8 29 10 44 37
30 23 22 48 31 17
45 32 24 50
55
B4 B1 B0
34
Binomial heap representation
root
6 3 18 6 3 18
37
29 10 44 37 29
48 31 17 48 10
50 50 31 17 44
8 29 10 44 37
n = 19
# trees = 3
30 23 22 48 31 17 height = 4
binary = 10011
45 32 24 50
55
B4 B1 B0
36
Binomial heap: meld
8 29 10 44
30 23 22 48 31 17
45 32 24 50
55
H1 H2
37
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
12
18
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
3 12
7 37 18
25
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
12
18
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
12
18
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
3 12
15 7 37 18
28 33 25
41
3 3 12
15 7 37 7 37 18
28 33 25 25
41
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
6 3 12
8 29 10 44 15 7 37 18
30 23 22 48 31 17 28 33 25
45 32 24 50 41
55
6 3 18
8 29 10 44 37
30 23 22 48 31 17
15 7 12
45 32 24 50
28 33 25
+
55
41
1 1 1
1 0 0 1 1
19 + 7 = 26 + 0 0 1 1 1
1 1 0 1 0
Binomial heap: meld
1 1 1
1 0 0 1 1
19 + 7 = 26 + 0 0 1 1 1
1 1 0 1 0
45
Binomial heap: extract the minimum
3 6 18
8 29 10 44 37
30 23 22 48 31 17
H
45 32 24 50
55
46
Binomial heap: extract the minimum
6 18
8 29 10 44 37
30 23 22 48 31 17
H
45 32 24 50
H′
55
47
Binomial heap: decrease key
3 6 18
8 29 10 44 37
30 23 22 48 31 17
H
x 32 24 50
55
48
Binomial heap: delete
3 6 18
8 29 10 44 37
30 23 22 48 31 17
H
45 32 24 50
55
49
Binomial heap: insert
3 6 18 x
8 29 10 44 H′
37
30 23 22 48 31 17
H
45 32 24 50
55
50
Binomial heap: sequence of insertions
50
if n = 11...111
51
Binomial heap: amortized analysis
Theorem. In a binomial heap, the amortized cost of INSERT is O(1) and the
worst-case cost of EXTRACT-MIN and DECREASE-KEY is O(log n).
Pf. Define potential function Φ(Hi) = trees(Hi) = # trees in binomial heap Hi.
・Φ(H0) = 0.
・Φ(Hi) ≥ 0 for each binomial heap Hi.
Case 1. [INSERT]
・Actual cost ci = number of trees merged + 1.
・∆Φ = Φ(Hi) – Φ(Hi–1) = 1 – number of trees merged.
・Amortized cost = ĉi = ci + Φ(Hi) – Φ(Hi–1) = 2.
52
Binomial heap: amortized analysis
Theorem. In a binomial heap, the amortized cost of INSERT is O(1) and the
worst-case cost of EXTRACT-MIN and DECREASE-KEY is O(log n).
Pf. Define potential function Φ(Hi) = trees(Hi) = # trees in binomial heap Hi.
・Φ(H0) = 0.
・Φ(Hi) ≥ 0 for each binomial heap Hi.
Case 2. [ DECREASE-KEY ]
・Actual cost ci = O(log n).
・∆Φ = Φ(Hi) – Φ(Hi–1) = 0.
・Amortized cost = ĉi = ci = O(log n).
53
Binomial heap: amortized analysis
Theorem. In a binomial heap, the amortized cost of INSERT is O(1) and the
worst-case cost of EXTRACT-MIN and DECREASE-KEY is O(log n).
Pf. Define potential function Φ(Hi) = trees(Hi) = # trees in binomial heap Hi.
・Φ(H0) = 0.
・Φ(Hi) ≥ 0 for each binomial heap Hi.
Case 3. [ EXTRACT-MIN or DELETE ]
・Actual cost ci = O(log n).
・∆Φ = Φ(Hi) – Φ(Hi–1) ≤ Φ(Hi) ≤ ⎣log2 n⎦.
・Amortized cost = ĉi = ci + Φ(Hi) – Φ(Hi–1) = O(log n). ▪
54
Priority queues performance cost summary
† amortized
55