SlideShare a Scribd company logo
1 of 17
What is a heap?
• Always keep the thing we
are most interested in
close to the top (and fast
to access).
• Like a binary search tree,
but less structured.
• No relationship between
keys at the same level
(unlike BST).
Types of heaps
• Min heap: priority 1 more important than 100
• Max heap: 100 more important than 1
– We are going to talk about max heaps.
• Max heap-order property
– Look at any node u, and its parent p.
– p.priority ≥ u.priority
p:7
u:3
Abstract data type (ADT)
• We are going to use max-heaps to implement the
(max) priority queue ADT
• A priority queue Q offers (at least) 2 operations:
– Extract-max(Q): returns the highest priority element
– Insert(Q, e): inserts e into Q
• Every time an Insert or Extract-max changes the
heap, it must restore the max-heap order
property. ( Prove by induction on the sequence
of inserts and extract-maxes that occur.)
What can we do with a heap
• Can do same stuff with a BST… why use a heap?
– BST extract-max is O(depth); heap is O(log n)!
• When would we use a BST?
– When we need to search for a particular key.
• Heaps are typically implemented with arrays.
• The array is just a level-order traversal of the heap.
• The children of the node at index i are at 2i and 2i+1.
Storing a heap in memory
Example time
• Interactive heap visualization
• Insert places a key in a new node that is the
last node in a level-order-traversal of the heap.
• The inserted key is then “bubbled” upwards until
the heap property is satisfied.
• Extract-max removes the last node in a level-
order-traversal and moves its key into the root.
• The new key at the root is then bubbled down
until the heap property is satisfied.
• Bubbling down is also called heapifying.
• Suppose we want to build a heap from an
unsorted array: 10, 2, 7, 8, 6, 5, 9, 4, 3, 11.
• We start by interpreting the array as a tree.
Building a max-heap in O(n) time
Building a heap: a helper function
Precondition: trees rooted at L and R are heaps
Postcondition: tree rooted at I is a heap
MaxHeapify(A,I):
L = LEFT(I)
R = RIGHT(I)
If L ≤ heap_size(A) and A[L] > A[I]
then max = L
else max = I
If R ≤ heap_size(A) and A[R] > A[max]
then max = R
If max is L or R then
swap(A[I],A[max])
MaxHeapify(A,max)
I:3
L:7 R:5
Case 1: max = L
Need to fix…
I:7
L:3 R:5
Case 2: max = I
Heap OK!
I:5
L:3 R:7
Case 3: max = R
Need to fix…
Proving MaxHeapify is correct
• How would you formally prove that MaxHeapify is correct?
• Goal: Prove MaxHeapify is correct for all inputs.
“Correct” means: “if the precondition is satisfied when MaxHeapify
is called, then the postcondition will be satisfied when it finishes.”
• How do we prove a recursive function correct?
– Define a problem size, and prove correctness by induction on
problem size.
– Base case: show function is correct for any input of the smallest
problem size.
– Inductive step: assume function is correct for problem size j;
show it is correct for problem size j+1.
Proving MaxHeapify is correct - 2
• Let’s apply this to MaxHeapify.
• Problem size: height of node I.
• Base case: Prove MaxHeapify is correct for every input
with height(I) = 0.
• Inductive step: Let A and I be any input parameters that
satisfy the precondition.
Assume MaxHeapify is correct when the problem size is j.
Prove MaxHeapify is correct when the problem size is j+1.
Proving MaxHeapify is correct - 3
Precondition: trees rooted at L and R are heaps
Postcondition: tree rooted at I is a heap
MaxHeapify(A,I):
L = LEFT(I)
R = RIGHT(I)
If L ≤ heap_size(A) and A[L] > A[I]
then max = L
else max = I
If R ≤ heap_size(A) and A[R] > A[max]
then max = R
If max is L or R then
swap(A[I],A[max])
MaxHeapify(A,max)
I:3
L:7 R:5
Case 1: max = L
Need to fix…
I:7
L:3 R:5
Case 2: max = I
Heap OK!
I:5
L:3 R:7
Case 3: max = R
Need to fix…
The main function
BUILD-MAX-HEAP(A):
for i = heap_size(A)/2 down to 1
MaxHeapify(A,i)
Analyzing worst-case complexity
Analyzing worst-case complexity
• ≤ 2d nodes at depth d
• Node at depth d has height ≤ h-d
• Cost to “heapify” one node at depth d is ≤ c(h-d)
– Don’t care about constants... Ignoring them below…
• Cost to heapify all nodes at depth d is ≤ 2d (h-d)
• So, cost to heapify all nodes over all depths is:
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx

More Related Content

Similar to week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx

Similar to week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx (20)

Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.ppt
 
Heaps
HeapsHeaps
Heaps
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Max priority queue
Max priority queueMax priority queue
Max priority queue
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Heap
HeapHeap
Heap
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
lecture 6
lecture 6lecture 6
lecture 6
 
Heap sort
Heap sort Heap sort
Heap sort
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 

week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx

  • 1. What is a heap? • Always keep the thing we are most interested in close to the top (and fast to access). • Like a binary search tree, but less structured. • No relationship between keys at the same level (unlike BST).
  • 2. Types of heaps • Min heap: priority 1 more important than 100 • Max heap: 100 more important than 1 – We are going to talk about max heaps. • Max heap-order property – Look at any node u, and its parent p. – p.priority ≥ u.priority p:7 u:3
  • 3. Abstract data type (ADT) • We are going to use max-heaps to implement the (max) priority queue ADT • A priority queue Q offers (at least) 2 operations: – Extract-max(Q): returns the highest priority element – Insert(Q, e): inserts e into Q • Every time an Insert or Extract-max changes the heap, it must restore the max-heap order property. ( Prove by induction on the sequence of inserts and extract-maxes that occur.)
  • 4. What can we do with a heap • Can do same stuff with a BST… why use a heap? – BST extract-max is O(depth); heap is O(log n)! • When would we use a BST? – When we need to search for a particular key.
  • 5. • Heaps are typically implemented with arrays. • The array is just a level-order traversal of the heap. • The children of the node at index i are at 2i and 2i+1. Storing a heap in memory
  • 6. Example time • Interactive heap visualization • Insert places a key in a new node that is the last node in a level-order-traversal of the heap. • The inserted key is then “bubbled” upwards until the heap property is satisfied. • Extract-max removes the last node in a level- order-traversal and moves its key into the root. • The new key at the root is then bubbled down until the heap property is satisfied. • Bubbling down is also called heapifying.
  • 7. • Suppose we want to build a heap from an unsorted array: 10, 2, 7, 8, 6, 5, 9, 4, 3, 11. • We start by interpreting the array as a tree. Building a max-heap in O(n) time
  • 8. Building a heap: a helper function Precondition: trees rooted at L and R are heaps Postcondition: tree rooted at I is a heap MaxHeapify(A,I): L = LEFT(I) R = RIGHT(I) If L ≤ heap_size(A) and A[L] > A[I] then max = L else max = I If R ≤ heap_size(A) and A[R] > A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) I:3 L:7 R:5 Case 1: max = L Need to fix… I:7 L:3 R:5 Case 2: max = I Heap OK! I:5 L:3 R:7 Case 3: max = R Need to fix…
  • 9. Proving MaxHeapify is correct • How would you formally prove that MaxHeapify is correct? • Goal: Prove MaxHeapify is correct for all inputs. “Correct” means: “if the precondition is satisfied when MaxHeapify is called, then the postcondition will be satisfied when it finishes.” • How do we prove a recursive function correct? – Define a problem size, and prove correctness by induction on problem size. – Base case: show function is correct for any input of the smallest problem size. – Inductive step: assume function is correct for problem size j; show it is correct for problem size j+1.
  • 10. Proving MaxHeapify is correct - 2 • Let’s apply this to MaxHeapify. • Problem size: height of node I. • Base case: Prove MaxHeapify is correct for every input with height(I) = 0. • Inductive step: Let A and I be any input parameters that satisfy the precondition. Assume MaxHeapify is correct when the problem size is j. Prove MaxHeapify is correct when the problem size is j+1.
  • 11. Proving MaxHeapify is correct - 3 Precondition: trees rooted at L and R are heaps Postcondition: tree rooted at I is a heap MaxHeapify(A,I): L = LEFT(I) R = RIGHT(I) If L ≤ heap_size(A) and A[L] > A[I] then max = L else max = I If R ≤ heap_size(A) and A[R] > A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) I:3 L:7 R:5 Case 1: max = L Need to fix… I:7 L:3 R:5 Case 2: max = I Heap OK! I:5 L:3 R:7 Case 3: max = R Need to fix…
  • 12. The main function BUILD-MAX-HEAP(A): for i = heap_size(A)/2 down to 1 MaxHeapify(A,i)
  • 15. • ≤ 2d nodes at depth d • Node at depth d has height ≤ h-d • Cost to “heapify” one node at depth d is ≤ c(h-d) – Don’t care about constants... Ignoring them below… • Cost to heapify all nodes at depth d is ≤ 2d (h-d)
  • 16. • So, cost to heapify all nodes over all depths is:

Editor's Notes

  1. Heaps are faster! =D
  2. Sketch the code, then walk them through the cases, then discuss the precondition and postcondition, then show them how, e.g., case three works (meaning the postcondition is satisfied if the precondition is satisfied).
  3. Base case: j = 0. Any node with height 0 is a heap, so the post condition is satisfied. Inductive step: Assume MaxHeapify is correct when its input I has height j. Show it is correct when I has height j+1. Explain case 3 as an example.
  4. Why do we iterate from heap_size(A)/2 and not from heap_size(A)? Why do we iterate backwards? (What do we know about the nodes from heap_size(A)/2 down to 1? Is there any point in calling max heapify on any of the other nodes?) We need to know that max heapify’s precondition is satisfied, in order for us to call it… Show how, this way, the precondition is satisfied, so the postcondition guarantees that we are always building larger and larger heaps, as we move up.