Professional Documents
Culture Documents
Applications.
These lecture slides are adapted
from CLRS, Chapters 6, 19. ■ Dijkstra’s shortest path algorithm.
■ Prim’s MST algorithm.
■ Event-driven simulation.
■ Huffman encoding.
■ Heapsort.
■ ...
Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne 2
06 06
N = 14
Height = 3
14 45 14 45
78 18 47 53 78 18 47 53
83 91 81 77 84 99 64 83 91 81 77 84 99 64
5 6
06 06
1
14 45 14 45
2 3
78 18 47 53 78 18 47 53
4 5 6 7
14 45 14 45
78 18 47 53 78 18 47 42
83 91 81 77 84 99 64 42 83 91 81 77 84 99 64 53
42
9 10
14 42 14 42
78 18 47 45 78 18 47 45
83 91 81 77 84 99 64 53 83 91 81 77 84 99 64 53
11 12
Binary Heap: Delete Min Binary Heap: Delete Min
Delete minimum element from heap. Delete minimum element from heap.
■ Exchange root with rightmost leaf. ■ Exchange root with rightmost leaf.
■ Bubble root down until it’s heap ordered. ■ Bubble root down until it’s heap ordered.
– power struggle principle: better subordinate is promoted – power struggle principle: better subordinate is promoted
06 53
14 42 14 42
78 18 47 45 78 18 47 45
83 91 81 77 84 99 64 53 83 91 81 77 84 99 64 06
13 14
14 42 53 42
78 18 47 45 78 18 47 45
83 91 81 77 84 99 64 83 91 81 77 84 99 64
15 16
Binary Heap: Delete Min Binary Heap: Heapsort
Delete minimum element from heap. Heapsort.
■ Exchange root with rightmost leaf. ■ Insert N items into binary heap.
■ Bubble root down until it’s heap ordered. ■ Perform N delete-min operations.
– power struggle principle: better subordinate is promoted ■ O(N log N) sort.
■ O(log N) operations. ■ No extra storage.
18 42
78 53 47 45
83 91 81 77 84 99 64
17 18
41 91 81 77 84 99 64
19 20
Binomial Tree Binomial Tree
Binomial tree. Useful properties of order k binomial tree Bk.
■ Recursive definition: ■ Number of nodes = 2k.
B0 Bk
■ Height = k.
Bk+1
■ Degree of root = k.
■ Deleting root yields binomial
Bk-1
trees Bk-1, … , B0.
B1 B0
Bk-1 B2
Proof. Bk
■ By induction on k.
B0 B1 B2 B3 B4 B0 B1 B2 B3 B4
21 22
4
=6
2
depth 0 6 3 18
depth 1
8 29 10 44 37
depth 2
30 23 22 48 31 17
depth 3
45 32 24 50
depth 4 B4 55
B4 B1 B0
23 24
Binomial Heap: Implementation Binomial Heap: Properties
Implementation. Properties of N-node binomial heap.
■ Represent trees using left-child, right sibling pointers. ■ Min key contained in root of B0, B1, . . . , Bk.
– three links per node (parent, left, right) ■ Contains binomial tree Bi iff bi = 1 where bn⋅ b2b1b0 is binary
■ Roots of trees connected with singly linked list. representation of N.
– degrees of trees strictly decreasing from left to right ■ At most log2 N + 1 binomial trees.
heap ■ Height ≤ log2 N.
6 3 18 6 3 18
6 3 18
nt
are
P
37 37 8 29 10 44 37
29 10 44 29
Ri
ft
gh
Le
t
30 23 22 48 31 17 N = 19
48 31 17 48 10 # trees = 3
height = 4
45 32 24 50
binary = 10011
50 50 31 17 44
55
Binomial Heap Leftist Power-of-2 Heap B4 B1 B0
25 26
30 23 22 48 31 17
15 7 12
6 45 32 24 50
28 33 25
55
8 29 10 44
+ 41
30 23 22 48 31 17
1 1 1
1 0 0 1 1
45 32 24 50
19 + 7 = 26 + 0 0 1 1 1
55 1 1 0 1 0
H’ H’’
27 28
Binomial Heap: Union Binomial Heap: Union 12
18
6 3 18 6 3 18
8 29 10 44 37 8 29 10 44 37
30 23 22 48 31 17 30 23 22 48 31 17
15 7 12 15 7 12
45 32 24 50 45 32 24 50
28 33 25 28 33 25
55 55
+ 41 + 41
29 30
3 12 3 3 12
7 37 18 15 7 37 7 37 18
25 28 33 25 25
41
6 3 18 6 3 18
8 29 10 44 37 8 29 10 44 37
30 23 22 48 31 17 30 23 22 48 31 17
15 7 12 15 7 12
45 32 24 50 45 32 24 50
28 33 25 28 33 25
55 55
+ 41 + 41
12 12
18 18
31 32
3 3 12 3 3 12
15 7 37 7 37 18 15 7 37 7 37 18
28 33 25 25 28 33 25 25
41 41
6 3 18 6 3 18
8 29 10 44 37 8 29 10 44 37
30 23 22 48 31 17 30 23 22 48 31 17
15 7 12 15 7 12
45 32 24 50 45 32 24 50
28 33 25 28 33 25
55 55
+ 41 + 41
3 12 6 3 12
15 7 37 18 8 29 10 44 15 7 37 18
28 33 25 30 23 22 48 31 17 28 33 25
41 45 32 24 50 41
33
55 34
3 6 18
8 29 10 44 37
1 1 1 30 23 22 48 31 17 H
1 0 0 1 1
+ 0 0 1 1 1 45 32 24 50
19 + 7 = 26
1 1 0 1 0
55
35 36
Binomial Heap: Delete Min Binomial Heap: Decrease Key
Delete node with minimum key in binomial heap H. Decrease key of node x in binomial heap H.
■ Find root x with min key in root list of H, and delete ■ Suppose x is in binomial tree Bk.
■ H’ ← broken binomial trees ■ Bubble node x up the tree if x is too small.
■ H ← Union(H’, H)
Running time. O(log N)
Running time. O(log N) ■ Proportional to depth of node x ≤ log2 N .
6 18 3 6 18
8 29 10 44 37 depth = 3 8 29 10 44 37
30 23 22 48 31 17 H 30 23 22 48 31 17 H
45 32 24 50 x 32 24 50
55 55
H’ 37 38
3 6 18 x
8 29 10 44 37
30 23 22 48 31 17 H H’
45 32 24 50
55
39 40
Binomial Heap: Sequence of Inserts Priority Queues
Insert a new node x into binomial heap H.
■ If N = .......0, then only 1 steps. 3 6 x