Professional Documents
Culture Documents
Week 1 - Heap and Priority Queue
Week 1 - Heap and Priority Queue
Week 1
Heaps and Priority Queue
o4d3khk
2
Course Outline
3
Heap?
4
Is it the same as heap memory ?
5
Is it the same as heap memory ?
6
Heap Data Structure
7
Heap Data Structure View as a Binary Tree
16
14 15
6 7 9 5
2 4 3
8
Heap Data Structure View as an Array
0
16
1 2
14 15
3 4 5 6
6 7 9 5
7
8 9
2 4 3
0 1 2 3 4 5 6 7 8 9
16 14 15 6 7 9 5 2 4 3
9
Heap Data Structure
10
Heap Properties
A[PARENT( i )] >= A[ i ] ;
that is, the value of a node is at most the value of its parent.
11
Heap Properties
A[PARENT( i )] <= A[ i ] ;
12
Heap
23 17 14 6 13 10 1 5 7 12
13
Heap Data Structure
14
Heap: Some Basic Procedures
15
Heap represented as a Binary tree and an array
0
16
1 2
14 15
6 7 9 5
2 4 3
0 1 2 3 4 5 6 7 8 9
16 14 15 6 7 9 5 2 4 3
16
Heap
For 0-based indexing of node numbers.
PARENT(i)
return ⌊(i-1)/2⌋
LEFT(i)
return 2i+1
RIGHT(i)
return 2i+2
17
Heap
For 1-based indexing of node numbers.
PARENT(i)
return ⌊i/2⌋
LEFT(i)
return 2i
RIGHT(i)
return 2i+1
19
MAX-HEAP Property!!
16
2 3
4 15
4 5 6
7
13 7 9 5
8 9 10
2 6 3
20
MAX-HEAP Property!!
16
2 3
4 15
4 5 6
7
13 7 9 5
8 9 10
2 6 3
21
MAX-HEAPIFY(A,2)
l = 4; r = 5
largest = 4 1
16
2 3
4 15
4 5 6
7
13 7 9 5
8 9 10
2 6 3
22
MAX-HEAPIFY(A,2)
l = 4; r = 5
largest = 4 1
16
2 3
13 15
4 5 6
7
4 7 9 5
8 9 10
2 6 3
23
MAX-HEAPIFY(A,4)
l = 8; r = 9
largest = 9 1
16
2 3
13 15
4 5 6
7
4 7 9 5
8 9 10
2 6 3
24
MAX-HEAPIFY(A,4)
l = 8; r = 9
largest = 9 1
16
2 3
13 15
4 5 6
7
6 7 9 5
8 9 10
2 4 3
25
MAX HEAP:
16
2 3
13 15
4 5 6
7
6 7 9 5
8 9 10
2 4 3
26
Building a Heap from a given array
● Array A[1..n]
, where n= A:length
27
Building a Heap from a given array
0 1 2 3 4 5 6 7 8 9 10
4 1 3 2 16 9 10 14 8 7
2 3
1 3
4 5 6
7
2 16 9 10
8 9 10
14 8 7
28
Building a Heap from a given array
29
Building a Heap from a given array
30
Building a Heap from a given array
31
Building a Heap from a given array
32
Building a Heap from a given array
33
Building a Heap from a given array
34
Building a Heap from a given array
10 5 25 40 30 35 20
35
Time Complexity of BUILD-MAX-HEAP
36
Time Complexity of BUILD-MAX-HEAP
Why is the upper bound not asymptotically tight?
37
Time Complexity of BUILD-MAX-HEAP
Why is the upper bound not asymptotically tight?
The time for MAX-HEAPIFY to run at a node varies with the height of the
node in the tree.
38
Time Complexity of BUILD-MAX-HEAP
we get,
39
Time Complexity of BUILD-MAX-HEAP
So the tighter upper bound
time complexity :
40
HEAP: Insertion Deletion
41
HEAP: Insertion
Insertion (O(logn)):
1. Insert in the leaf node and
Increase heap size
2. Maintain heap property by
exchanging with the parent
nodes(ancestors).
42
HEAP: Insertion (logn)
INSERT : 14
16
2 3
13 15
4 5 6
7
6 7 9 5
8 9 10
2 4 3
43
HEAP: Insertion (logn)
INSERT : 14
16
2 3
13 15
4 5 6
7
6 7 9 5
8 9 10
2 4 3 14 11
44
HEAP: Insertion (logn)
INSERT : 14
16
2 3
13 15
4 5 6
7
6 14 9 5
8 9 10
2 4 3 7 11
45
HEAP: Insertion (logn)
INSERT : 14
16
2 3
14 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 7 11
46
HEAP: Insertion (logn)
After INSERTION
16
2 3
14 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 7 11
47
HEAP: Deletion
Deletion (O(logn)):
1. Swap the node to be deleted
with the last leaf node.
2. Remove the last leaf node.
3. Maintain heap property
48
HEAP: Deletion
Deletion (O(logn)):
3. Maintain heap property
HEAP-DELETE(A, i)
if A[i] > A[A.heap-size]
swap(A[i], A[A.heap-size])
A.heap-size = A.heap-size - 1
MAX-HEAPIFY(A, i)
else
swap(A[i], A[A.heap-size])
A.heap-size = A.heap-size - 1
49
HEAP: Deletion (logn)
Delete 14 ,i=2
HEAP-DELETE(A, 2)
1
Here,
A[i] > A[Heapsize] 16
2 3
14 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 7 11
50
HEAP: Deletion (logn)
Delete 14 ,i=2 1. Swap the node to be deleted
with the last leaf node.
HEAP-DELETE(A, 2)
2. Remove the last leaf node.
1 3. Maintain heap property
16
2 3
14 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 7 11
51
HEAP: Deletion (logn)
Delete 14 ,i=2 1. Swap the node to be deleted
with the last leaf node.
HEAP-DELETE(A, 2)
2. Remove the last leaf node.
1 3. Maintain heap property
16
2 3
7 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 14 11
52
HEAP: Deletion (logn)
Delete 14 ,i=2 1. Swap the node to be deleted
with the last leaf node.
HEAP-DELETE(A, 2)
2. Remove the last leaf node.
1 3. Maintain heap property
16
2 3
7 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 14 11
53
HEAP: Deletion (logn)
Delete 14 ,i=2 1. Swap the node to be deleted
with the last leaf node.
HEAP-DELETE(A, 2)
2. Remove the last leaf node.
1 3. Maintain heap property
16
2 3
7 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 14
54
HEAP: Deletion (logn)
HEAP-DELETE(A, 2) 1. Swap the node to be deleted
with the last leaf node.
2. Remove the last leaf node.
As A[i] > A[Heapsize]
1 3. Maintain heap property
MAX-HEAPIFY(A, 2)
16
2 3
7 15
4 5 6
7
6 13 9 5
8 9 10
2 4 3 14
55
HEAP: Deletion (logn)
After Deleting 14
16
2 3
13 15
4 5 6
7
6 7 9 5
8 9 10
2 4 3
56
HEAP SORT
57
HEAP SORT
10 5 25 40 30 35 20
58
Priority Queue
59
Priority Queue
60
Priority Queue
DECREASE-KEY (S, x, k): decreases the value of element x’s key to the
new value k.
61
Priority Queue
62
Priority Queue Operations
HEAP-MAXIMUM (A)
return A[1]
63
Priority Queue Operations
64
Priority Queue Operations
65
Priority Queue Operations
66
Priority Queue Operations
67
Priority Queue Operations
68
Priority Queue Operations
69
Priority Queue Operations
70
Priority Queue Operations
71
Heap Exercises
72
73