SlideShare a Scribd company logo
1 of 40
Download to read offline
BITS Pilani
Pilani|Dubai|Goa|Hyderabad
1
DataStructuresandAlgorithmsDesign
DSECLZG519
BITS Pilani
Pilani|Dubai|Goa|Hyderabad
2
Contact Session #5
DSECCZG519 – Heaps & Heap Sort
• Heap is a special tree-based data structure, that satisfies
the following special heap properties:
– Shape Property
– Heap Property
Heap
3
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Shape Property
Heap data structure is always a complete binary tree,
which means all levels of the tree are fully filled till h-1
and at level h (last level), the nodes are filled from left
to right.
Complete Binary tree
4
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Heap Property
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
10
 All nodes are either [greater than or equal to] or [less than
or equal to] each of its children.
 If the parent nodes are greater than their children, then
such a heap is called: Max-Heap. So, The root of any sub-
tree holds the greatest value in the sub-tree
 If the parent nodes are smaller than their children, then
such a heap is called: Min-Heap. So, the root of any sub-tree
holds the least value in that sub-tree.
• Max-Heap
for every node v other than the root
element(parent(v)) ≥ element(v)
• Min-Heap
for every node v other than the root
element (parent(v)) ≤ element(v)
Examples of min-heap and max-heap
11
1
7
5
4
10
1
7
5
4
10
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Check :
These
are not
heaps!!
Min-Heap Max-Heap
Heap Representation
19 22 14 15
25
17
22
 Root is at index 1
 For any node at index i
o The left child is at index 2i
o The right child is at index 2i + 1
o Parent is at floor(i/2)
18 14 21 3 9 11
1 2 3 4 5 6 7 8 9 10 11 12 13
7
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 Since, a heap is always a complete binary tree, can we
represent heap as array easily and effectively ? YES
 Similar to array implementation of binary trees
25 22 17 19 22 14 15 18 14 21 3 9 11
Heapification (Max-Heapify)
 Now the Question is how to transform the above tree into a Heap ?
 Heapification !! Commonly referred as Max-Heapify()
4
10 12
7 9 5 11 3
6
 Before discussing the method for building heap of an arbitrary complete binary
tree, we discuss a simpler problem.
 Let us consider a binary tree in which left and right subtrees of the root satisfy
the heap property but not the root. See the following fig:
2
14 8
8
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
SequenceDepicting the Heapification
process
10
7
12 4
9 5 11 3
6
2
8
14
10
7
12 4
9 5 11 3
6
14
8
2
10 2 4 6
14
8
12
10
7
11
9 5
4
2 3
6
14
8
12
7 9 5 11 3 14
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
The time complexity
of max-Heapify is
O(log n)
*Since the complete
binary tree is perfectly
balanced, shifting up
a single node takes
O(log n) time.
Algorithm: Max-Heapify(B, s)
15
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 Heap building can be done efficiently with bottom up fashion.
 Given an arbitrary complete binary tree, we can assume each leaf
is a heap
 Start building the heap from the parents of these leaves i.e., Max-
Heapify subtrees rooted at the parents.
 The Heapify process continues till we reach the root of the tree.
Build Heap
All leaf nodes are from 𝑛/2 + 1 𝑡𝑜 𝑛
All non-leaf nodes are from 1 to 𝑛/2
2
1
9 7 16
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2
5
3
6
4 5
Build Heap
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
4 10
8 9 5
6 7
1
3
2
9 10
8 4 5
6 7
1
3
2
9 10
8 4 5
6 3
1
7
2
9 5 6 3
1
7
10
8 5 6 3
10
7
9
4
8 9
5
10
6 7
1
3
2
9 2 6 3
1
7
10
1 4 2 17
8 4 5 8 4 2
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Insertion into a Heap
Inserting an element e in the heap has
– Three steps
Find the insertion point z
oSo that we maintain complete binary tree property
Store e at insertion point z
Check if the heap follows heap-order property
oRestore the heap-order property by Up-heap bubbling
Example:
Insert the element 1 into the min-heap
2
5 6
9 7 13
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Insertion into a Heap
2
14
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
6
5
7
9
2
6
5
7
9 1
z
2
6
5
7
9 1
compare
z
2
1
5
7
9 6
swap
z
Insertion into a Heap
1
2
5
7
9 6
z
swap
1
5
7
9 6
z
2 compare
1
2
5
7
9 6
15
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
z
After the insertion of a new element e, the heap-order
property may be violated.
 Up-heap bubbling restores the heap-order property
– Compare and swap e along an upward path from the insertion
point
– Up-heap bubbling terminates when the element e reaches
• the root
• a node where the heap order property is satisfied
 Since the heap has a complete binary tree structure, its height = log n
(where n is no of elements). In the worst case (element inserted at the
bottom has to be swapped at every level from bottom to top up to the root
node to maintain the heap property), 1 swap is needed on every level.
Therefore, the maximum no of times this swap is performed is log n.
Hence, Insertion in a heap takes O(log n) time.
16
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Insertion into a Heap
Three steps
Remove the element at the root node from the heap
Fill the root node with the element from the last node
maintain Complete binary tree property
Check if the heap follows the heap-order property
Restore the heap-order property by down-heap bubbling
17
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Removal from a Heap
Removal from a Heap
Example: Perform a delete operation on the given min-heap
2
6
5
7
9
18
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Removal from a Heap
7
6
5
9
2
6
5
7
9
last node
Remove
5
6
7
9
Compare
&
swap
5
6
7
9
19
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 After replacing the root with the element from the last node e, the
heap-order property may be violated
 Down-heap bubbling restores the heap-order property
– Compare and swap e along a downward path from the root node
• Choose the eligible (min/max) child of e and swap it with e
– Down-heap terminates when the element e reaches
• Aleaf
• Anode where the heap order property is satisfied
 Here again, in worst case, we may have to perform down-heap
bubbling till the node reaches the leaf. Complexity is O(log n)
20
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Removal from a Heap
Min-Heap
 Illustrate the result of inserting the elements 35, 33, 42, 10, 14, 19 and
27 one at a time, into an initially empty binary min-heap in that order.
Draw the resulting min-heap after each insertion.
 Perform 2 delete operation for the min-heap constructed in the earlier
example.
Max Heap
 Illustrate the result of inserting the elements 35, 33, 42, 10, 14, 19 and
27 one at a time, into an initially empty binary max-heap in that
order. Draw the resulting max-heap after each insertion.
 Perform 2 delete operation for the max-heap constructed in the earlier
example.
21
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise 1
Exercise 2
22
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 Heap sort is one of the best sorting methods being in-place
and with no quadratic worst-case scenarios.
 Heap sort is divided into two basic parts :
o Creating a heap of the unsorted list
o Then a sorted array is created by repeatedly removing
the largest/smallest element form the heap and inserting
it into the array
o Heap is reconstructed after each removal
Heap-SortAlgorithm
29
In-Place: A sorting algorithm is said to be “in-place”
if it moves the items within the array itself and, thus,
requires only a small O(1) amount of extra storage.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 It is a well-known, traditional sorting algorithm you will
be expected to know!!
 Heapsort is always O(n log n)
Quicksort is usually O(n log n) but in the worst case
slows to O(n2)
Quicksort is generally faster, but Heapsort is better in
time-critical applications
 Heapsort is a really cool algorithm!
24
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Why study Heapsort?
Algorithm
•Time-complexity for buildMaxHeap() is O(n)
•Time complexity for each instance of heapify() is O(log n)
•Time complexity for heap sort is O(n log n)
•Building a max heap is dependent on how many times each node “trickles
down” at each level i.
•The run-time for heapify() depends directly on the height of the heap. This is
because you make 1 swap per level.
•Given set of elements: 16,14,10,8,7,9,3,2,4,1
Exercise 1:
• Implement Heap Sort by showing each step and the resultant
must be in increasing order. Hint: Create max-heap!
Exercise 2:
• Implement Heap Sort by showing each step and the resultant
must be in decreasing order. Hint: Create min-heap!
33
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise 3
 A priority queue is a data structure for maintaining a set S of
elements, each with an associated value called a key.
 There’s no “real” FIFO rule anymore.
34
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
 Two kinds : max-priority queues and min-priority
according to max-heaps and min-heaps.
 The key denotes the priority
 Max-heap is used to implement a max-Priority Queue
Always deletes an element with maximum priority
 Min-heap is used to implement a min-Priority Queue
Always deletes an element with minimum priority
queues,
Priority Queue
Max- Priority Queue Operations
Min-priority queues offer Insert, Minimum, Extract-Min, and Decrease-Key. 35
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Max – Priority Queue
36
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Max – Priority Queue
Applications
37
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example of Heap-Increase-Key
1
38
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2 3
4 5 6 7
8 9 10
 Heaps are used in heapsort!
 Priority Queues: Priority queues can be efficiently implemented
using Heaps
 Order statistics: The Heap data structure can be used to efficiently
find the kth smallest (or largest) element in an array.
 Heap Implemented priority queues are used in Graph algorithms
like Prim’s Algorithm and Dijkstra’s algorithm.
39
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Applications of Heap
Exercise 4
40
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956

More Related Content

Recently uploaded

Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 

Recently uploaded (20)

Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

DSECLZG519- Mathematical foundations Lec5

  • 2. BITS Pilani Pilani|Dubai|Goa|Hyderabad 2 Contact Session #5 DSECCZG519 – Heaps & Heap Sort
  • 3. • Heap is a special tree-based data structure, that satisfies the following special heap properties: – Shape Property – Heap Property Heap 3 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 4. Shape Property Heap data structure is always a complete binary tree, which means all levels of the tree are fully filled till h-1 and at level h (last level), the nodes are filled from left to right. Complete Binary tree 4 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 5. Heap Property BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 10  All nodes are either [greater than or equal to] or [less than or equal to] each of its children.  If the parent nodes are greater than their children, then such a heap is called: Max-Heap. So, The root of any sub- tree holds the greatest value in the sub-tree  If the parent nodes are smaller than their children, then such a heap is called: Min-Heap. So, the root of any sub-tree holds the least value in that sub-tree. • Max-Heap for every node v other than the root element(parent(v)) ≥ element(v) • Min-Heap for every node v other than the root element (parent(v)) ≤ element(v)
  • 6. Examples of min-heap and max-heap 11 1 7 5 4 10 1 7 5 4 10 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Check : These are not heaps!! Min-Heap Max-Heap
  • 7. Heap Representation 19 22 14 15 25 17 22  Root is at index 1  For any node at index i o The left child is at index 2i o The right child is at index 2i + 1 o Parent is at floor(i/2) 18 14 21 3 9 11 1 2 3 4 5 6 7 8 9 10 11 12 13 7 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956  Since, a heap is always a complete binary tree, can we represent heap as array easily and effectively ? YES  Similar to array implementation of binary trees 25 22 17 19 22 14 15 18 14 21 3 9 11
  • 8. Heapification (Max-Heapify)  Now the Question is how to transform the above tree into a Heap ?  Heapification !! Commonly referred as Max-Heapify() 4 10 12 7 9 5 11 3 6  Before discussing the method for building heap of an arbitrary complete binary tree, we discuss a simpler problem.  Let us consider a binary tree in which left and right subtrees of the root satisfy the heap property but not the root. See the following fig: 2 14 8 8 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 9. SequenceDepicting the Heapification process 10 7 12 4 9 5 11 3 6 2 8 14 10 7 12 4 9 5 11 3 6 14 8 2 10 2 4 6 14 8 12 10 7 11 9 5 4 2 3 6 14 8 12 7 9 5 11 3 14 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 10. The time complexity of max-Heapify is O(log n) *Since the complete binary tree is perfectly balanced, shifting up a single node takes O(log n) time. Algorithm: Max-Heapify(B, s) 15 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 11.  Heap building can be done efficiently with bottom up fashion.  Given an arbitrary complete binary tree, we can assume each leaf is a heap  Start building the heap from the parents of these leaves i.e., Max- Heapify subtrees rooted at the parents.  The Heapify process continues till we reach the root of the tree. Build Heap All leaf nodes are from 𝑛/2 + 1 𝑡𝑜 𝑛 All non-leaf nodes are from 1 to 𝑛/2 2 1 9 7 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 2 5 3 6 4 5
  • 12. Build Heap 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 4 10 8 9 5 6 7 1 3 2 9 10 8 4 5 6 7 1 3 2 9 10 8 4 5 6 3 1 7 2 9 5 6 3 1 7 10 8 5 6 3 10 7 9 4 8 9 5 10 6 7 1 3 2 9 2 6 3 1 7 10 1 4 2 17 8 4 5 8 4 2 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 13. Insertion into a Heap Inserting an element e in the heap has – Three steps Find the insertion point z oSo that we maintain complete binary tree property Store e at insertion point z Check if the heap follows heap-order property oRestore the heap-order property by Up-heap bubbling Example: Insert the element 1 into the min-heap 2 5 6 9 7 13 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 14. Insertion into a Heap 2 14 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 6 5 7 9 2 6 5 7 9 1 z 2 6 5 7 9 1 compare z 2 1 5 7 9 6 swap z
  • 15. Insertion into a Heap 1 2 5 7 9 6 z swap 1 5 7 9 6 z 2 compare 1 2 5 7 9 6 15 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 z
  • 16. After the insertion of a new element e, the heap-order property may be violated.  Up-heap bubbling restores the heap-order property – Compare and swap e along an upward path from the insertion point – Up-heap bubbling terminates when the element e reaches • the root • a node where the heap order property is satisfied  Since the heap has a complete binary tree structure, its height = log n (where n is no of elements). In the worst case (element inserted at the bottom has to be swapped at every level from bottom to top up to the root node to maintain the heap property), 1 swap is needed on every level. Therefore, the maximum no of times this swap is performed is log n. Hence, Insertion in a heap takes O(log n) time. 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Insertion into a Heap
  • 17. Three steps Remove the element at the root node from the heap Fill the root node with the element from the last node maintain Complete binary tree property Check if the heap follows the heap-order property Restore the heap-order property by down-heap bubbling 17 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Removal from a Heap
  • 18. Removal from a Heap Example: Perform a delete operation on the given min-heap 2 6 5 7 9 18 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 19. Removal from a Heap 7 6 5 9 2 6 5 7 9 last node Remove 5 6 7 9 Compare & swap 5 6 7 9 19 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 20.  After replacing the root with the element from the last node e, the heap-order property may be violated  Down-heap bubbling restores the heap-order property – Compare and swap e along a downward path from the root node • Choose the eligible (min/max) child of e and swap it with e – Down-heap terminates when the element e reaches • Aleaf • Anode where the heap order property is satisfied  Here again, in worst case, we may have to perform down-heap bubbling till the node reaches the leaf. Complexity is O(log n) 20 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Removal from a Heap
  • 21. Min-Heap  Illustrate the result of inserting the elements 35, 33, 42, 10, 14, 19 and 27 one at a time, into an initially empty binary min-heap in that order. Draw the resulting min-heap after each insertion.  Perform 2 delete operation for the min-heap constructed in the earlier example. Max Heap  Illustrate the result of inserting the elements 35, 33, 42, 10, 14, 19 and 27 one at a time, into an initially empty binary max-heap in that order. Draw the resulting max-heap after each insertion.  Perform 2 delete operation for the max-heap constructed in the earlier example. 21 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Exercise 1
  • 22. Exercise 2 22 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 23.  Heap sort is one of the best sorting methods being in-place and with no quadratic worst-case scenarios.  Heap sort is divided into two basic parts : o Creating a heap of the unsorted list o Then a sorted array is created by repeatedly removing the largest/smallest element form the heap and inserting it into the array o Heap is reconstructed after each removal Heap-SortAlgorithm 29 In-Place: A sorting algorithm is said to be “in-place” if it moves the items within the array itself and, thus, requires only a small O(1) amount of extra storage. BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 24.  It is a well-known, traditional sorting algorithm you will be expected to know!!  Heapsort is always O(n log n) Quicksort is usually O(n log n) but in the worst case slows to O(n2) Quicksort is generally faster, but Heapsort is better in time-critical applications  Heapsort is a really cool algorithm! 24 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Why study Heapsort?
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 32. •Time-complexity for buildMaxHeap() is O(n) •Time complexity for each instance of heapify() is O(log n) •Time complexity for heap sort is O(n log n) •Building a max heap is dependent on how many times each node “trickles down” at each level i. •The run-time for heapify() depends directly on the height of the heap. This is because you make 1 swap per level.
  • 33. •Given set of elements: 16,14,10,8,7,9,3,2,4,1 Exercise 1: • Implement Heap Sort by showing each step and the resultant must be in increasing order. Hint: Create max-heap! Exercise 2: • Implement Heap Sort by showing each step and the resultant must be in decreasing order. Hint: Create min-heap! 33 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Exercise 3
  • 34.  A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.  There’s no “real” FIFO rule anymore. 34 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956  Two kinds : max-priority queues and min-priority according to max-heaps and min-heaps.  The key denotes the priority  Max-heap is used to implement a max-Priority Queue Always deletes an element with maximum priority  Min-heap is used to implement a min-Priority Queue Always deletes an element with minimum priority queues, Priority Queue
  • 35. Max- Priority Queue Operations Min-priority queues offer Insert, Minimum, Extract-Min, and Decrease-Key. 35 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 36. Max – Priority Queue 36 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 37. Max – Priority Queue Applications 37 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
  • 38. Example of Heap-Increase-Key 1 38 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 2 3 4 5 6 7 8 9 10
  • 39.  Heaps are used in heapsort!  Priority Queues: Priority queues can be efficiently implemented using Heaps  Order statistics: The Heap data structure can be used to efficiently find the kth smallest (or largest) element in an array.  Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s algorithm. 39 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Applications of Heap
  • 40. Exercise 4 40 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956