






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of binary and binomial heaps, two essential data structures used for implementing priority queues. The slides cover the definition, properties, operations, and applications of binary and binomial heaps, including dijkstra's shortest path algorithm, prim's mst algorithm, and heapsort. The document also discusses the differences between binary and binomial heaps and their respective advantages.
Typology: Slides
1 / 11
This page cannot be seen from the preview
Don't miss anything!







These lecture slides are adaptedfrom CLRS, Chapters 6, 19.
Supports the following operations.
n^
Insert element x. n^
Return min element. n^
Return and delete minimum element. n^
Decrease key of element x to k.
Applications.
n^
Dijkstra’s shortest path algorithm. n^
Prim’s MST algorithm. n^
Event-driven simulation. n^
Huffman encoding. n^
Heapsort. n^
3
PQinit()for each v
key(v)
PQinsert(v)
key(s)
while (!PQisempty())
v = PQdelmin()for each w
Q s.t (v,w)
if
π
(w) >
π
(v) + c(v,w)
PQdecrease(w,
π
(v) + c(v,w))
Dijkstra’s Shortest Path Algorithm
Dijkstra/Prim1 make-heap|V| insert|V| delete-min|E| decrease-key
Operationmake-heap
insert find-min delete-min
union
decrease-key
delete
Binarylog N
log N
log Nlog N
Binomial
log Nlog Nlog Nlog Nlog Nlog N
Fibonacci *
log N
log N
Relaxed
log N
log N
Linked List
is-empty
Heaps
O(|E| + |V| log |V|)
O(|E| log |V|)
5
Binary heap.
n^
Almost complete binary tree.
-^
filled on all levels, except last, where filled from left to right
n^
Min-heap ordered.
-^
every child greater than (or equal to) parent
06
14
78
18
81
77
91
45
53
47
64
84
99
83
Properties.
n^
Min element is in root. n^
Heap with N elements has height =
log
2
06
14
78
18
81
77
91
45
53
47
64
84
99
83
N = 14Height = 3
7
Implementing binary heaps.
n^
Use an array: no need for explicit parent or child pointers.
-^
Parent(i) =
i/
-^
Left(i)
= 2i
-^
Right(i)
= 2i + 1
06
14
78
18
81
77
91
45
53
47
64
84
99
83
Insert element x into heap.
n^
Insert into next available slot. n^
Bubble up until it’s heap ordered.
-^
Peter principle: nodes rise to level of incompetence
06
14
78
18
81
77
91
45
53
47
64
84
99
83
42
next free slot
13
Delete minimum element from heap.
n^
Exchange root with rightmost leaf. n^
Bubble root down until it’s heap ordered.
-^
power struggle principle: better subordinate is promoted
06
14
78
18
81
77
91
42
45
47
64
84
99
83
53
Delete minimum element from heap.
n^
Exchange root with rightmost leaf. n^
Bubble root down until it’s heap ordered.
-^
power struggle principle: better subordinate is promoted
53
14
78
18
81
77
91
42
45
47
64
84
99
83
06
15
Delete minimum element from heap.
n^
Exchange root with rightmost leaf. n^
Bubble root down until it’s heap ordered.
-^
power struggle principle: better subordinate is promoted
53
14
78
18
81
77
91
42
45
47
64
84
99
83
exchange with left child
Delete minimum element from heap.
n^
Exchange root with rightmost leaf. n^
Bubble root down until it’s heap ordered.
-^
power struggle principle: better subordinate is promoted
14
53
78
18
81
77
91
42
45
47
64
84
99
83
exchange with right child
17
Delete minimum element from heap.
n^
Exchange root with rightmost leaf. n^
Bubble root down until it’s heap ordered.
-^
power struggle principle: better subordinate is promoted
n^
O(log N) operations.
14
18
78
53
81
77
91
42
45
47
64
84
99
83
stop: heap ordered
Heapsort.
n^
Insert N items into binary heap. n^
Perform N delete-min operations. n^
O(N log N) sort. n^
No extra storage.
19
1
2
14
78
18
81
77
91
11
62
53
64
84
99
41
Union.
n^
Combine two binary heaps H
1
and H
2
into a single heap.
n^
No easy solution.
-^
(N) operations apparently required
n^
Can support fast union with fancier heaps.
Operationmake-heap
insert find-min delete-min
union
decrease-key
delete
Binarylog N
log N
log Nlog N
Binomial
log Nlog Nlog Nlog Nlog Nlog N
Fibonacci *
log N
log N
Relaxed
log N
log N
Linked List
is-empty
Heaps
25
Implementation.
n^
Represent trees using left-child, right sibling pointers.
-^
three links per node (parent, left, right)
n^
Roots of trees connected with singly linked list.
-^
degrees of trees strictly decreasing from left to right
48 50
31
17
44
10
6
(^337)
18
29
6
37
3
18
48
31
50
10
44
17
heap
29
Leftist Power-of-2 Heap
Binomial Heap
Parent
Left
Right
Properties of N-node binomial heap.
n^
Min key contained in root of B
.k
n^
Contains binomial tree B
i^
iff b
= 1 where bi^
⋅n
b
b 2
b 1
0
is binary
representation of N. n^
At most
log
2
n^
Height
log
2
4
0
1
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^337)
18
N = 19# trees = 3height = 4binary = 10011
27
Create heap H that is union of heaps H’ and H’’.
n^
"Mergeable heaps." n^
Easy if H’ and H’’ are each order k binomial trees.
-^
connect roots of H’ and H’’
-^
choose smaller key to be root of H
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^3 )
18
41
33
28
15
(^7 )
12
29
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^3 )
18
41
33
28
15
(^7 )
12
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^337)
41
33
28
15
(^725)
12 18
18 12
31
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^337)
41
33
28
15
(^725)
12 18
25
37
7
3
12 18
18 12
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
6
(^337)
41
33
28
15
12 7 25
18
25
37
7
3
28 41
33
25
37
15
7
3
12 18
18 12
37
Delete node with minimum key in binomial heap H.
n^
Find root x with min key in root list of H, and delete n^
broken binomial trees
n^
Union(H’, H)
Running time. O(log N)
45 55
30 32
23 24
22
48 50
31
17
(^637)
18
44
8
29
10
3
(^637)
18
x 55
30 32
23 24
22
48 50
31
17
44
8
29
10
Decrease key of node x in binomial heap H.
n^
Suppose x is in binomial tree B
.k
n^
Bubble node x up the tree if x is too small.
Running time. O(log N)
n^
Proportional to depth of node x
log
2
depth = 3
39
Delete node x in binomial heap H.
n^
Decrease key of x to -
n^
Delete min.
Running time. O(log N)
Insert a new node x into binomial heap H.
n^
MakeHeap(x)
n^
Union(H’, H)
Running time. O(log N)
3
(^637)
18
45 55
30 32
23 24
22
48 50
31
17
44
8
29
10
x H’
41
Insert a new node x into binomial heap H.
n^
If N =
, then only 1 steps.
n^
If N =
, then only 2 steps.
n^
If N =
, then only 3 steps.
n^
If N =
, then only 4 steps.
Inserting 1 item can take
(log N) time.
n^
If N =
, then log
2
N steps.
But, inserting sequence of N items takes O(N) time!
n^
n^
Amortized analysis. n^
Basis for getting most operationsdown to constant time.
48 50
31
17
44
29
10
3
(^637)
x
1
1
−
=^
N
N
N n^
n
Operationmake-heap
insert find-min delete-min
union
decrease-key
delete
Binarylog N
log N
log Nlog N
Binomial
log Nlog Nlog Nlog Nlog Nlog N
Fibonacci *
log N
log N
Relaxed
log N
log N
Linked List
is-empty
Heaps
just did this