Date post: | 03-Jun-2018 |
Category: |
Documents |
Upload: | moazzam-hussain |
View: | 216 times |
Download: | 0 times |
of 33
8/11/2019 Lecture-26-CS210-2012 (1).pptx
1/33
Data Structures and Algorithms
(CS210/ESO207/ESO211)
Lecture 25 Binary Heap : Operations: Insert(), DecreaseKey(), Merge()
Buildinga Binary heap on n elements in O(n) time.
Applications of Binary heap : sorting
1
8/11/2019 Lecture-26-CS210-2012 (1).pptx
2/33
Recap from the last lecture
2
8/11/2019 Lecture-26-CS210-2012 (1).pptx
3/33
Heap
Definition:a tree data structure satisfying the following property:
value stored in a node is smaller than the value stored in its children.
3
11
4
18 23
4721
7
71 913
37
43
19
8/11/2019 Lecture-26-CS210-2012 (1).pptx
4/33
Operations on a heap
Query Operations
Find-min: report the smallest key stored in the heap.
Update Operations CreateHeap(H): Create an empty heap H.
Insert(x,H): Insert a new key with value xinto the heap H.
Extract-min(H): delete the smallest key from H.
Decrease-key(p, , H): decrease the value of the key pby amount .
Merge(H1,H2): Merge two heaps H1and H2.
4
8/11/2019 Lecture-26-CS210-2012 (1).pptx
5/33
A complete binary tree
Let vbe a node with label.
Label of left child(v) =
Label of right child(v) =
Label of parent(v) = 5
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
# leaf nodes = /
8/11/2019 Lecture-26-CS210-2012 (1).pptx
6/33
8/11/2019 Lecture-26-CS210-2012 (1).pptx
7/33
Binary heap
a complete binary tree satisfyingheap property at each node.
7
4
14
17
91
9
2123 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33
H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
8/33
Implementation of a Binary heap
8
If nis the maximum number of keys in the binary heap at any moment of
time, then we keep
H : an array of size nused for storing the binary heap. size: a variable for the total number of keys currently in the heap.
8/11/2019 Lecture-26-CS210-2012 (1).pptx
9/33
8/11/2019 Lecture-26-CS210-2012 (1).pptx
10/33
Insert(x,H)
10
9
14
17
71
21
3323 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 57H 11
8/11/2019 Lecture-26-CS210-2012 (1).pptx
11/33
Insert(x,H)
11
9
14
17
71
21
3323 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 57H 11
8/11/2019 Lecture-26-CS210-2012 (1).pptx
12/33
Insert(x,H)
12
9
14
17
71
21
3323 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 25H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
13/33
Insert(x,H)
13
9
14
17
71
21
3311 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 25H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
14/33
Insert(x,H)
14
9
11
17
71
21
3314 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 25H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
15/33
Insert(x,H)
Insert(x,H)
{ size(H);
H()x;
size(H)size(H)+ 1;
While( ?? and ?? )
{
??
??
}}
Time complexity: O(log n)
15
H() 0
( 1)/2 ;
8/11/2019 Lecture-26-CS210-2012 (1).pptx
16/33
8/11/2019 Lecture-26-CS210-2012 (1).pptx
17/33
Other heaps
There are heaps which achieve much better time complexities than Binary
heap. One such heap is Fibonacci heap. Fibonacci heap is a link based data
structure.
17
Binary heap Fibonacciheap
Find-min(H) O(1) O(1)
Insert(x,H) O(logn) O(1)
Extract-min(H) O(log n) O(log n)
Decrease-key(p, , H) O(log n) O(1)Merge(H1,H2) O(n) O(1)
Looking at the amazing bounds in the 3rdcolumn, dont
you feel excited to study Fibonaccci heap ?
We shall study it during CS345.
8/11/2019 Lecture-26-CS210-2012 (1).pptx
18/33
Building a Binary heap
18
8/11/2019 Lecture-26-CS210-2012 (1).pptx
19/33
Building a Binary heap
Problem:Given nelements {0, , }, build a binary heap Hstoring them.
Trivial solution: (Building the Binary heap incrementally)
CreateHeap(H);
For(= 0to )
Insert(,H);
19
8/11/2019 Lecture-26-CS210-2012 (1).pptx
20/33
Building a Binary heap incrementally
20
9
11
17
71
21
3314 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 25H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
21/33
Time complexity of incrementally building a Binary heap
Theorem: Time complexity of building a binary heap incrementally is O(nlogn).
21
8/11/2019 Lecture-26-CS210-2012 (1).pptx
22/33
Exploring the way to build a Binary heap in O(n) time
Question1:what is the cause of O( log)time complexity for incrementally building heap ?
Answer: # leaf nodes = / , the time complexity for inserting each of them takes O(log )
time.
Aim:an algorithm where leaf nodes take O(1) time.
Hint:Revisit the heap property: Every node stores value smaller than its children
Question2: In a complete binary tree (not necessary a heap), how many nodes satisfy heap
property ?Answer: all leaf nodes(since none of them has any child).
22
What approach for building a heap comes to your mind based
on the above questions and their answers ? Ponder over it
8/11/2019 Lecture-26-CS210-2012 (1).pptx
23/33
A new approach to build binary heap
1. Just copy the givenelements {0, , } into an array H.
2. The heap property holds for all the leaf nodes in the corresponding
complete binary tree.
3. Leaving all the leaf nodes, process the elements in the decreasing order
of their numbering and set the heap property for each of them.
23
8/11/2019 Lecture-26-CS210-2012 (1).pptx
24/33
A new approach to build binary heap
24
98
14
37
85
33
5211 32
17 25 88 41 21 29 76
47 75 9 57 23
98 14 33 37 11 52 32 47 17 23 88 41 21 29 76 85 75 9 57 25H
The first node to beprocessed.
8/11/2019 Lecture-26-CS210-2012 (1).pptx
25/33
A new approach to build binary heap
25
98
14
37
85
33
5211 32
17 23 88 41 21 29 76
47 75 9 57 25
98 14 33 37 11 52 32 47 17 23 88 41 21 29 76 85 75 9 57 25H
The second node tobe processed.
8/11/2019 Lecture-26-CS210-2012 (1).pptx
26/33
A new approach to build binary heap
26
98
14
37
85
33
5211 32
9 23 88 41 21 29 76
47 75 17 57 25
98 14 33 37 11 52 32 47 17 23 88 41 21 29 76 85 75 9 57 25H
The third node tobe processed.
8/11/2019 Lecture-26-CS210-2012 (1).pptx
27/33
A new approach to build binary heap
27
v
Let v be a node corresponding to index iin H.The process of restoring heap property at icalled Heapify(i,H).
98
54
9
47
21
3311 29
17 23 88 41 52 32 76
85 75 37 57 25
98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25H
8/11/2019 Lecture-26-CS210-2012 (1).pptx
28/33
Heapify(,H)
Heapify(,H)
{ size(H) -1;
While( ?? )
{ ;
If(H[]>H[ + ]) + ;
If( + and H[]>H[ + ]) + ;
If( )
H()H();
elseFlag false;
;
}
}28
and Flag= true (1)/2
Flag true;
8/11/2019 Lecture-26-CS210-2012 (1).pptx
29/33
Building Binary heap in O(n) time
29
98
54
9
47
21
3311 29
17 23 88 41 52 32 76
85 75 37 57 25
98 54 21 9 11 33 29 47 17 23 88 41 52 32 76 85 75 37 57 25H
Time to heapify node v ?
v
Height(v)
8/11/2019 Lecture-26-CS210-2012 (1).pptx
30/33
Building Binary heap in O(n) time
Time complexity of heapifying a node v= height(v)
Question:How many nodes of height are there in a Binary heap of size n ?
Answer: Let v be a node of height in the binary heap of size n.
The subtree(v) is surely a complete binary tree of height .
So subtree(v) must store > elements.
All subtrees of height are mutually disjoint. So the total number of elements
stored in all of them is bounded by .
Hence the number of subtrees of height is bounded by
.
Time complexity to build the heap =
()log
=
=
log
=
= O()
30
As an exercise (using knowledge from your JEE
preparation days), show that
log = is bounded by 2
8/11/2019 Lecture-26-CS210-2012 (1).pptx
31/33
Sorting using a Binary heap
31
8/11/2019 Lecture-26-CS210-2012 (1).pptx
32/33
Sorting using heap
Build heap H on the given nelements;
While(His not empty)
{ xExtract-min(H);print x;
}
This is HEAP SORT algorithm
32
8/11/2019 Lecture-26-CS210-2012 (1).pptx
33/33
Heap sort
Advantages over merge sort and quick sort:
A non-recursive algorithm.
No extra space.
Disadvantage:
Average case number of comparisons performed during heap sort is more (by a
constant factor) than average number of comparisons performed during quick sort. Soquick sort outperformsheap sort in real applications.
33