SlideShare a Scribd company logo
Heaps & Adaptable Priority
Queues
By – Priyanka Rana
Session 2 review
In list-based implementation of priority queue:
If we use unsorted lists:
 insertion takes O(1) time
 deletion takes O(n) time.
If we use sorted lists:
 insertion takes O(n) time
 deletion takes O(1) time.
Session 2 Review
Selection-sort:
Phase 1 -> fast running time.
Phase 2 -> slow running time.
Insertion-sort:
Phase 1 -> slow running time.
Phase 2 -> fast running time.
Balancing the running time
Heap
Heap Data structure
Is a binary tree T that stores a collection of entries at its nodes
and satisfies two additional properties:
Order Property
Structure Property
Heap properties
Order Property:
For every internal node v, other than the root, key(v) ≥
key(parent(v)).
Structure Property:
A heap T with height h is a complete binary tree, i.e.:
for depth/level i < h, there are 2i nodes.
at depth h−1, the internal nodes are to the left of the external
nodes.
Heap data structure
The last node of a heap is the rightmost node of the
maximum depth.
2
65
89 Last node
Height of heap
A heap storing n keys has height h = [lg n]
For each depth i < h , there are 2i nodes and
at least one node at depth h. Thus,
n ≥ 20 + 21+ 22 + ... + 2h-1 + 1
≥ 1 + 2 + 4 + ... + 2h-1 + 1
≥ 2h
Which gives, h ≤ lgn.
Array list representation of heap
For the node at rank i
the left child is at rank 2i.
the right child is at rank 2i+1.
The cell at rank 0 is not used
A heap with n keys is represented by means of a array size n+1.
2
65
79
0 1 2 3 4 5
2 5 6 79
Insertion Algorithm
The insertion algorithm consists of three steps:
Find the insertion node z (i.e. the new last node).
Store k at z. (Here k=1)
Restore the heap-order property (discussed next).
2
65
79 2
5
79
z
Insertion Node
6
1
Up-heap bubbling
1
5
79
2
6
2
5
79
1
6
Removal from a heap
The removal algorithm consists of three steps:
 Replace the root key with the key of the last node
w.
 Remove w.
 Restore the heap-order property (discussed next).
2
65
79 7
5
9
w
Last Node
6
New Last Node
Down-heap bubbling
5
7
9
6
7
5
w9
6
Heap-sort
A heap-based priority queue, sorts a sequence of n
elements in O(nlgn) time.
performs n series of removeMin() operation.
Since each call of removeMin() takes O(lgn) time, sorting a
heap with n elements takes O(nlgn) time.
Merging two heaps
2
6
3
8 5
72
47
4
Exercise
At which nodes of a heap can an entry with the
largest key be stored?
Adaptable Priority Queues
Priority queues which are adaptable.
When priority queue methods are not sufficient.
For example, in a standby airline passenger application.
Adaptable Priority Queue ADT
remove(e):
Remove from P and return e.
replaceKey(e, k):
Replace with k and return the key of entry e of P.
replaceValue(e, x):
 Replace with x and return the value of entry e of P.
Exercise
Operation Output
Insert(5,A) e1
Insert(3,B) e2
Insert(7,C) e3
min() e2
Key(e2) 3
remove(e1) e1
replaceKey(e2,9) 3
replaceValue(e3,D
)
C
Remove(e2) e2
isEmpty() “false”
(3,B)
,
(5,A),(7,C),(9,B),(7,D)
Location aware entry
mechanism for finding the position of entry e of Priority Queue P.
This position is called the location of the entry
implementation of an entry that keeps track of its position is called
location-aware entry.
List implementation of an adaptable
PQ
A location-aware list entry is an object storing
key
value
position (or rank) of the entry in the list.
Heap Implementation of an Adaptable PQ
A location-aware list entry is an object storing
key
value
position of the entry in the heap.
Performance of Adaptable PQ Implementations
Method Unsorted List Sorted List Heap
Size, isEmpty() O(1) O(1) O(1)
insert O(1) O(n) O(n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(lg n)
remove O(1) O(1) O(lg n)
replaceKey O(1) O(n) O(lg n)
replaceValue O(1) O(1) O(1)
THANK YOU

More Related Content

What's hot

Propositional Logic and Pridicate logic
Propositional Logic and Pridicate logicPropositional Logic and Pridicate logic
Propositional Logic and Pridicate logic
Roorkee College of Engineering, Roorkee
 
Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Fundamentals of algorithms
Fundamentals of algorithmsFundamentals of algorithms
Fundamentals of algorithms
Amit Kumar Rathi
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
Sukrit Gupta
 
Find Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's AlgorithmFind Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's Algorithm
Safayet Hossain
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
Ms.SHANTHI.S CSE
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
Rishabh Soni
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
Mohit Tare
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
HarisonFekadu
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
deepinderbedi
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
Dining philosopher
Dining philosopherDining philosopher
Dining philosopher
.AIR UNIVERSITY ISLAMABAD
 

What's hot (20)

Propositional Logic and Pridicate logic
Propositional Logic and Pridicate logicPropositional Logic and Pridicate logic
Propositional Logic and Pridicate logic
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Quick sort
Quick sortQuick sort
Quick sort
 
Fundamentals of algorithms
Fundamentals of algorithmsFundamentals of algorithms
Fundamentals of algorithms
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
First order logic
First order logicFirst order logic
First order logic
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Find Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's AlgorithmFind Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's Algorithm
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
First() and Follow()
First() and Follow()First() and Follow()
First() and Follow()
 
Dining philosopher
Dining philosopherDining philosopher
Dining philosopher
 

Similar to Heaps & Adaptable priority Queues

Sorting
SortingSorting
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
Nash229987
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
Computer Network Assignment Help
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
B.Kirron Reddi
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
PJS KUMAR
 
Lec3
Lec3Lec3
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
Deepak John
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
Programming Exam Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
Programming Homework Help
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
Education Front
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
RUHULAMINHAZARIKA
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Queue
QueueQueue
Lecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptxLecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptx
JosephKariuki46
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
kalyanineve
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 

Similar to Heaps & Adaptable priority Queues (20)

Sorting
SortingSorting
Sorting
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
lecture 6
lecture 6lecture 6
lecture 6
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
Lec3
Lec3Lec3
Lec3
 
Lec3
Lec3Lec3
Lec3
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Review session2
Review session2Review session2
Review session2
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Queue
QueueQueue
Queue
 
Lecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptxLecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptx
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 

More from Priyanka Rana

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
Priyanka Rana
 
Scrum values
Scrum valuesScrum values
Scrum values
Priyanka Rana
 
Usability testing
Usability testing  Usability testing
Usability testing
Priyanka Rana
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
Priyanka Rana
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
Priyanka Rana
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
Priyanka Rana
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
Priyanka Rana
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
Priyanka Rana
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
Priyanka Rana
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
Priyanka Rana
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
Priyanka Rana
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
Priyanka Rana
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
Priyanka Rana
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
Priyanka Rana
 
Priority queues
Priority queuesPriority queues
Priority queues
Priyanka Rana
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
Priyanka Rana
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Priyanka Rana
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
Priyanka Rana
 
Linked list
Linked listLinked list
Linked list
Priyanka Rana
 

More from Priyanka Rana (19)

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
 
Scrum values
Scrum valuesScrum values
Scrum values
 
Usability testing
Usability testing  Usability testing
Usability testing
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 

Recently uploaded (20)

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 

Heaps & Adaptable priority Queues

  • 1. Heaps & Adaptable Priority Queues By – Priyanka Rana
  • 2. Session 2 review In list-based implementation of priority queue: If we use unsorted lists:  insertion takes O(1) time  deletion takes O(n) time. If we use sorted lists:  insertion takes O(n) time  deletion takes O(1) time.
  • 3. Session 2 Review Selection-sort: Phase 1 -> fast running time. Phase 2 -> slow running time. Insertion-sort: Phase 1 -> slow running time. Phase 2 -> fast running time.
  • 5. Heap Data structure Is a binary tree T that stores a collection of entries at its nodes and satisfies two additional properties: Order Property Structure Property
  • 6. Heap properties Order Property: For every internal node v, other than the root, key(v) ≥ key(parent(v)). Structure Property: A heap T with height h is a complete binary tree, i.e.: for depth/level i < h, there are 2i nodes. at depth h−1, the internal nodes are to the left of the external nodes.
  • 7. Heap data structure The last node of a heap is the rightmost node of the maximum depth. 2 65 89 Last node
  • 8. Height of heap A heap storing n keys has height h = [lg n] For each depth i < h , there are 2i nodes and at least one node at depth h. Thus, n ≥ 20 + 21+ 22 + ... + 2h-1 + 1 ≥ 1 + 2 + 4 + ... + 2h-1 + 1 ≥ 2h Which gives, h ≤ lgn.
  • 9. Array list representation of heap For the node at rank i the left child is at rank 2i. the right child is at rank 2i+1. The cell at rank 0 is not used A heap with n keys is represented by means of a array size n+1. 2 65 79 0 1 2 3 4 5 2 5 6 79
  • 10. Insertion Algorithm The insertion algorithm consists of three steps: Find the insertion node z (i.e. the new last node). Store k at z. (Here k=1) Restore the heap-order property (discussed next). 2 65 79 2 5 79 z Insertion Node 6 1
  • 12. Removal from a heap The removal algorithm consists of three steps:  Replace the root key with the key of the last node w.  Remove w.  Restore the heap-order property (discussed next). 2 65 79 7 5 9 w Last Node 6 New Last Node
  • 14. Heap-sort A heap-based priority queue, sorts a sequence of n elements in O(nlgn) time. performs n series of removeMin() operation. Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time.
  • 16. Exercise At which nodes of a heap can an entry with the largest key be stored?
  • 17. Adaptable Priority Queues Priority queues which are adaptable. When priority queue methods are not sufficient. For example, in a standby airline passenger application.
  • 18. Adaptable Priority Queue ADT remove(e): Remove from P and return e. replaceKey(e, k): Replace with k and return the key of entry e of P. replaceValue(e, x):  Replace with x and return the value of entry e of P.
  • 19. Exercise Operation Output Insert(5,A) e1 Insert(3,B) e2 Insert(7,C) e3 min() e2 Key(e2) 3 remove(e1) e1 replaceKey(e2,9) 3 replaceValue(e3,D ) C Remove(e2) e2 isEmpty() “false” (3,B) , (5,A),(7,C),(9,B),(7,D)
  • 20. Location aware entry mechanism for finding the position of entry e of Priority Queue P. This position is called the location of the entry implementation of an entry that keeps track of its position is called location-aware entry.
  • 21. List implementation of an adaptable PQ A location-aware list entry is an object storing key value position (or rank) of the entry in the list.
  • 22. Heap Implementation of an Adaptable PQ A location-aware list entry is an object storing key value position of the entry in the heap.
  • 23. Performance of Adaptable PQ Implementations Method Unsorted List Sorted List Heap Size, isEmpty() O(1) O(1) O(1) insert O(1) O(n) O(n) min O(n) O(1) O(1) removeMin O(n) O(1) O(lg n) remove O(1) O(1) O(lg n) replaceKey O(1) O(n) O(lg n) replaceValue O(1) O(1) O(1)

Editor's Notes

  1. &amp;lt;number&amp;gt;
  2. &amp;lt;number&amp;gt;
  3. &amp;lt;number&amp;gt;
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;
  6. Order property is about every internal node whose key value must be greater than the key value of its parent. Depth: number of ancestors a node has Height: the height of a tree is given as the maximum depth of any node // structure property considers a heap of height &amp;quot;h&amp;quot; a complete binary tree when: its depth is less than its height and there are 2 raise to power of i nodes. another condition is that at height h-1 , internal nodes are to the left of the external nodes that // also means in a way that last node of a heap is the rightmost node of the maximum depth. &amp;lt;number&amp;gt;
  7. &amp;lt;number&amp;gt;
  8. this is about height of the heap, which is said to be as log n where n is the number of entries, now here we consider //the structural property of heap which says that at depth i ,// heap has 2 raise to power of i nodes. and //hence using mathematical operations// and in the end taking// log on both sides it is easily proved that height is log of number of entries. &amp;lt;number&amp;gt;
  9. We store a (key and value) for each entry item at each internal node, and keep track of the position of the last node. &amp;lt;number&amp;gt;
  10. Insertion into heap has three steps. // // // After the insertion of a new node, the heap-order property may be violated. we need to restore it by some swappings of the values stored at z with the entry stored in its ancestor nodes.// &amp;lt;number&amp;gt;
  11. The upward movement of the newly inserted entry //by means of swaps is called up-heap bubbling. Since the heap has height lgn, up-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  12. removal from a heap has three steps as well, // // // After replacing the root key with key k of the last node, again the heap-order property gets violated. Then we need to restore the heap-order by swapping the entries stored at root with the entries stored in its descendent nodes.// here The downward movement of the root entry by means of swaps is call down-heap bubbling. as the heap has height lgn, down-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  13. Using a heap-based priority queue, we can sort a sequence of n elements in O(nlgn) time, we have already covered sorting in detail, heap sort is also similar. //The algorithm simply performs n series of removeMin() operation. //Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time. Traaversing takes O(logn) time --. Sorting takes O(nlog(n)) time &amp;lt;number&amp;gt;
  14. lets see how two are merged together, lets say We are given two heaps and a key k. //We create a new heap with the root node storing k and with the two heaps as subtrees.// We perform down-heap bubbling to restore the heap-order property. &amp;lt;number&amp;gt;
  15. &amp;lt;number&amp;gt;
  16. APQ are the priority queues which are adaptable, //There are some situations where the basic methods of the priority queue ADT (given in previous section) are not sufficient. //For example, in a standby airline passenger application: A passenger may request to be removed from the waiting list.A passenger presents a document (e.g. gold frequent flyer card) that implies to update his/her order in the waiting list.a passenger notices her name is misspelled on the ticket and asks it to be corrected. So, we need the priority queues be adaptable. &amp;lt;number&amp;gt;
  17. &amp;lt;number&amp;gt;
  18. &amp;lt;number&amp;gt;
  19. &amp;lt;number&amp;gt;
  20. &amp;lt;number&amp;gt;