data structures and algorithms › wp-content › uploads › cs210... · binary heap a complete...
TRANSCRIPT
![Page 1: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/1.jpg)
Data Structures and Algorithms (CS210A)
Lecture 28: • Heap : an important tree data structure
• Implementing some special binary tree using an array !
• Binary heap
1
![Page 2: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/2.jpg)
Heap Definition: a tree data structure where :
value stored in a node ? value stored in each of its children.
2
11
4
18 23
47 21
7
71 9 13
37
43
19
<
![Page 3: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/3.jpg)
Operations on a heap
Query Operations • Find-min: report the smallest key stored in the heap.
Update Operations • CreateHeap(H)
• Insert(x,H)
• Extract-min(H)
• Decrease-key(p, ∆, H)
• Merge(H1,H2)
3
: Create an empty heap H.
: Insert a new key with value x into the heap H.
: delete the smallest key from H.
: decrease the value of the key p by amount ∆.
: Merge two heaps H1 and H2.
![Page 4: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/4.jpg)
Can we implement a binary tree using an array ?
4
Yes. In some special cases
![Page 5: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/5.jpg)
A complete binary tree
A complete binary of 12 nodes.
5
![Page 6: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/6.jpg)
A complete binary tree
The label of the leftmost node at level 𝒊 = ??
The label of a node v at level 𝒊 occurring at 𝒌th place from left = ??
The label of the left child of v is= ??
The label of the right child of v is= ??
6
0 1
Level nodes
1 2
2 4
0
1 2
3 4 5 6
7 8 9 10 11
𝟐𝒊 − 𝟏
𝟐𝒊 + 𝒌 − 𝟐
𝟐𝒊+𝟏 + 𝟐𝒌 − 𝟑
𝟐𝒊+𝟏 + 𝟐𝒌 − 𝟐
𝟐𝒊+𝟏 − 𝟏+ ? 𝟐(𝒌 − 𝟏)
Can you see a relationship between label of a node
and labels of its children ?
![Page 7: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/7.jpg)
A complete binary tree
Let v be a node with label 𝒋.
Label of left child(v) =
Label of right child(v) =
Label of parent(v) = 7
0 1
Level nodes
1 2
2 4
0
1 2
3 4 5 6
7 8 9 10 11
2 𝒋 +1
2𝒋 +2
(𝒋 − 1)/2
![Page 8: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/8.jpg)
A complete binary tree and array
Question: What is the relation between a complete binary trees and an array ?
Answer: A complete binary tree can be implemented by an array.
8
41
17
57
91
9
1 33 70
37 25 88 35
41 17 9 57 33 1 70 91 37 25 88 35
0
1 2
3 4 5 6
7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
Advantages ? The most compact representation.
![Page 9: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/9.jpg)
Binary heap
9
![Page 10: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/10.jpg)
Binary heap a complete binary tree
10
4
14
17
91
9
21 23 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33 H
satisfying heap property at each node.
![Page 11: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/11.jpg)
Implementation of a Binary heap
𝒏 : the maximum number of keys at any moment of time,
then we keep
• H[]
• size
11
: an array of size 𝒏 used for storing the binary heap.
: a variable for the total number of keys currently in the heap.
![Page 12: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/12.jpg)
Find_min(H) Report H[0].
12
4
14
17
91
9
21 23 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33 H
![Page 13: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/13.jpg)
Extract_min(H) Think hard on designing efficient algorithm for this operation.
The challenge is:
how to preserve the complete binary tree structure as well as the heap property ?
13
4
14
17
91
9
21 23 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33 H
![Page 14: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/14.jpg)
Extract_min(H)
14
4
14
17
91
9
21 23 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33 H
![Page 15: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/15.jpg)
Extract_min(H)
15
33
14
17
91
9
21 23 29
37 25 88 4
33 14 9 17 23 21 29 91 37 25 88 H 4
![Page 16: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/16.jpg)
Extract_min(H)
16
9
14
17
91
33
21 23 29
37 25 88
9 14 33 17 23 21 29 91 37 25 88 H
![Page 17: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/17.jpg)
Extract_min(H) We are done.
The no. of operations performed = O(no. of levels in binary heap)
= O(log 𝑛) …show it as an homework exercise.
17
9
14
17
91
21
33 23 29
37 25 88
9 14 21 17 23 33 29 91 37 25 88 H
![Page 18: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/18.jpg)
Insert(x,H)
18
9
14
17
71
21
33 23 29
37 25 88 41 52 32 76
98 85 47 57 11
9 14 21 17 23 33 29 71 37 25 88 41 52 32 76 98 85 47 57 H 11
![Page 19: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/19.jpg)
Insert(x,H)
19
9
14
17
71
21
33 23 29
37 25 88 41 52 32 76
98 85 47 57 11
9 14 21 17 23 33 29 71 37 25 88 41 52 32 76 98 85 47 57 H 11
![Page 20: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/20.jpg)
Insert(x,H)
20
9
14
17
71
21
33 23 29
37 11 88 41 52 32 76
98 85 47 57 25
9 14 21 17 23 33 29 71 37 11 88 41 52 32 76 98 85 47 57 25 H
![Page 21: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/21.jpg)
Insert(x,H)
21
9
14
17
71
21
33 11 29
37 23 88 41 52 32 76
98 85 47 57 25
9 14 21 17 11 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H
![Page 22: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/22.jpg)
Insert(x,H)
22
9
11
17
71
21
33 14 29
37 23 88 41 52 32 76
98 85 47 57 25
9 11 21 17 14 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H
![Page 23: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/23.jpg)
Insert(x,H)
Insert(x,H)
{ 𝒊 size(H);
H(size) x;
size(H) size(H) + 1;
While( ?? and ?? )
{
??
??
}
}
Time complexity: O(log n)
23
H(𝒊) < H( (𝒊 − 1)/2 )
H(𝒊) ↔ H( (𝒊 − 1)/2 );
𝒊 > 0
𝒊 (𝒊 − 1)/2 ;
![Page 24: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/24.jpg)
The remaining operations on Binary heap
• Decrease-key(p, ∆, H): decrease the value of the key p by amount ∆. – Similar to Insert(x,H).
– O(log 𝒏) time
– Do it as an exercise
• Merge(H1,H2): Merge two heaps H1 and H2. – O(𝒏) time where 𝒏 = total number of elements in H1 and H2
(This is because of the array implementation)
24
![Page 25: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/25.jpg)
Other heaps
Fibonacci heap : a link based data structure.
25
Binary heap Fibonacci heap
Find-min(H)
Insert(x,H)
Extract-min(H)
Decrease-key(p, ∆, H)
Merge(H1,H2)
Excited to study Fibonaccci heap ? We shall study it during CS345.
O(1)
O(log 𝑛)
O(1)
O(1)
O(1)
O(1)
O(log 𝑛)
O(log 𝑛)
O(log 𝑛)
O(𝑛)
![Page 26: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/26.jpg)
Building a Binary heap
26
![Page 27: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/27.jpg)
Building a Binary heap
Problem: Given n elements {𝑥0, …, 𝑥𝑛−1}, build a binary heap H storing them.
Trivial solution:
(Building the Binary heap incrementally)
CreateHeap(H);
For( 𝒊 = 0 to 𝒏 − 𝟏 )
Insert(𝑥𝑖,H);
27
What is the time complexity of this
algorithm?
![Page 28: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/28.jpg)
Building a Binary heap incrementally
28
9
11
17
71
21
33 14 29
37 23 88 41 52 32 76
98 85 47 57 25
9 11 21 17 14 33 29 71 37 23 88 41 52 32 76 98 85 47 57 25 H
![Page 29: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/29.jpg)
Time complexity
Theorem: Time complexity of building a binary heap incrementally is O(𝒏 log 𝒏).
29
A binary heap can be built in O(𝒏).
![Page 30: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/30.jpg)
A complete binary tree
30
![Page 31: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/31.jpg)
A complete binary tree
31
How many leaves are there in a complete
Binary tree of size 𝒏 ?
No. of Leaf nodes = No. of Internal nodes + 1
![Page 32: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/32.jpg)
A complete binary tree
32
How many leaves are there in a Complete
Binary tree of size 𝒏 ?
𝒏/𝟐
No. of Leaf nodes = No. of Internal nodes No. of Leaf nodes = No. of Internal nodes + 1 No. of Leaf nodes = No. of Internal nodes
![Page 33: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/33.jpg)
Building a Binary heap incrementally
The time complexity for inserting a leaf node = ?
# leaf nodes = 𝒏/𝟐 ,
Theorem: Time complexity of building a binary heap incrementally is O(𝒏 log 𝒏). 33
O(log 𝒏 )
What useful inference can you draw from
this Theorem ?
Top-down approach
![Page 34: Data Structures and Algorithms › wp-content › uploads › CS210... · Binary heap a complete binary tree 10 4 14 17 91 9 23 21 29 37 25 88 33 H 4 14 9 17 23 21 29 91 37 25 88](https://reader033.vdocument.in/reader033/viewer/2022052802/5f1ebd8098cc3b45d95e5397/html5/thumbnails/34.jpg)
Building a Binary heap incrementally
34
The O(𝒏) time algorithm must take O(1) time for each of the 𝒏/𝟐 leaves.
Top-down approach
Ponder over the design of the O(𝒏) algorithm based
on this insight.