SlideShare a Scribd company logo
1 of 17
PRIORITY QUEUE
PRESENTED BY
Maneesh Kumar Jamwal
PRIORITY QUEUE
• Definition
• Simple Implementation
• Binary Heap-Structure Property
• Heap Order Property
• Basic Heap Operation
• Application Of Priority Queues
• d-Heaps
• References
Def:- A Priority Queue is a collection of Elements such that
Each element has been assigned a priority and such that is
the order in which the elements are deleted and processed
From the following rules:-
• An Element of higher priority is processed before any
element of lower priority.
• Two elements with the same priority are processed
According to the order in which they were added to the
queue.
• Any Data Structure that supports the operations of
search of min or max, insert and delete min or max
respectively is called a priority queue.
• Example:-Suppose that we are selling the services of a
machine. Each user pays a fixed amount per use.
However, the time needed by each user is different. We
wish to maximize the returns from this machine under
the assumption that the machine is not to be kept idle
unless no user is available. This can be done by
maintaining a priority queue of all persons waiting to
use the machine. Whenever the machine is available,
the user with the smallest time requirement will be
selected. Hence a priority queue that supports delete
min is required. When a new user requests the
machine, his or her request is put into the priority
queue.
If each user needs the same amount of time on the
machine but people are willing to pay different amounts for
the service, then the priority queue based on the
amount of payment can be maintained. Whenever the
machine becomes available, the user willing to pay the
most is selected. This requires a delete max operation.
Model of Priority Queue:-
Simple general diagram of the
Priority queue is as shown.
Whenever an element is inserted
into queue, priority Queue inserts
the item according to its order.
Here we're assuming that data
with high value has
low priority
Basic Operations on priority Queues:-
• Insert or enqueue:−This operation will add an
item to the rear of the queue.
• Delete or Dequeue:-This operation will remove
an item from the front of the queue.
• Peek − This operation will performed to get the
element at front of the queue.
• isFull − check if queue is full.
• isEmpty − check if queue is empty.
Binary Heap
A binary heap is a heap data structure that takes the
form of a binary tree. Binary heaps are a common
way of implementing priority queues.
A binary heap is defined as a binary tree with two
additional constraints:
1.Shape property:- a binary heap is a complete
binary tree that is, all levels of the tree, except
possibly the last one (deepest) are fully filled,
and, if the last level of the tree is not complete,
the nodes of that level are filled from left to right.
2.Heap property: the key stored in each node is
either greater than or equal to (≥) or less than or
equal to (≤) the keys in the node's children,
according to some total order.
Heaps where the parent key is greater than or
equal to (≥) the child keys are called max-heaps;
those where it is less than or equal to (≤) are
called min-heaps.
Figure shows :- Example of a complete binary max heap
Example of a complete binary min heap
QN:-What is heap order property?
Ans:-A binary heap is a complete binary tree
which satisfies the heap ordering property.
The ordering can be one of two types:
1 min-heap property: the value of each node
is greater than or equal to the value of its
parent, with the minimum-value element at
the root.
2. max-heap property: the value of each node
is less than or equal to the value of its parent,
with the maximum-value element at the root.
Basic Heap Operation
The common operations are:
• Basic operations:-
 Find-max (or find-min): find a maximum item of a
max-heap, or a minimum item of a min-heap,
respectively(peek operation) .
 insert: adding a new key to the heap (push ).
 extract-max (or extract-min): returns the node of
maximum value from a max heap [or minimum value
from a min heap] after removing it from the heap (pop
operation).
 delete-max (or delete-min): removing the root
node of a max heap (or min heap), respectively.
replace: pop root and push a new key. More efficient than
pop followed by push, since only need to balance once, not
twice, and appropriate for fixed-size heaps.
• Creation operations:-
create-heap: This operation creates an empty heap.
merge (union):This operation joining two heaps to
form a valid new heap containing all the elements of both,
preserving the original heaps.
heapify: create a heap out of given array of elements.
• Inspection operations:-
 size: return the number of items in the heap.
 is-empty: return true if the heap is empty,
 false otherwise.
• Internal operations:-
 increase-key or decrease-key: updating a key within
a max- or min-heap, respectively.
 delete: delete an arbitrary node.
 sift-up: move a node up in the tree, as long as needed;
used to restore heap condition after insertion.
sift-down: move a node down in the tree, similar to
sift-up; used to restore heap condition after deletion or
replacement.
Application Of Priority Queues:-
 A* search algorithm implementation can be done using
Priority queues.
 Priority queues are used for load balancing and
interrupt handling.
 Priority queues are used for Huffman codes for data
compression.
 In traffic light, depending upon the traffic, the color will
be given priority.
 Dijkstra’s shortest path algorithm using priority queue
when the graph is stored in the form of adjacency list or
matrix, priority queue can be used to extract minimum
efficiency when implementing Dijkstra’s algorithms.
 Prims algorithms implementation can be done using
Priority queue & also used to sort heaps.
d-array heap:-The d-ary heap or d-heap is a priority
queue data structure, a generalization of the binary heap in
which the nodes have d children instead of 2. Thus, a binary
heap is a 2-heap, and a ternary heap is a 3-heap.
References:-
1.wikipedia.
2.Data Structures by Seymour lipschutz.
3.Fundamentals of Data Structures by Sartaj Sahini
4.World wide web.
Thank You

More Related Content

What's hot

In class, we discussed min-heaps. In a min-heap the element of the heap with ...
In class, we discussed min-heaps. In a min-heap the element of the heap with ...In class, we discussed min-heaps. In a min-heap the element of the heap with ...
In class, we discussed min-heaps. In a min-heap the element of the heap with ...
licservernoida
 
Climate data in r with the raster package
Climate data in r with the raster packageClimate data in r with the raster package
Climate data in r with the raster package
Alberto Labarga
 

What's hot (20)

Heap sort
Heap sortHeap sort
Heap sort
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
In class, we discussed min-heaps. In a min-heap the element of the heap with ...
In class, we discussed min-heaps. In a min-heap the element of the heap with ...In class, we discussed min-heaps. In a min-heap the element of the heap with ...
In class, we discussed min-heaps. In a min-heap the element of the heap with ...
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Heap
HeapHeap
Heap
 
NUS-ISS Learning Day 2019-Pandas in the cloud
NUS-ISS Learning Day 2019-Pandas in the cloudNUS-ISS Learning Day 2019-Pandas in the cloud
NUS-ISS Learning Day 2019-Pandas in the cloud
 
Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)
Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)
Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Climate data in r with the raster package
Climate data in r with the raster packageClimate data in r with the raster package
Climate data in r with the raster package
 
MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)MBrace: Large-scale cloud computation with F# (CUFP 2014)
MBrace: Large-scale cloud computation with F# (CUFP 2014)
 
MBrace: Cloud Computing with F#
MBrace: Cloud Computing with F#MBrace: Cloud Computing with F#
MBrace: Cloud Computing with F#
 
Snabb - A toolkit for user-space networking (FOSDEM 2018)
Snabb - A toolkit for user-space networking (FOSDEM 2018)Snabb - A toolkit for user-space networking (FOSDEM 2018)
Snabb - A toolkit for user-space networking (FOSDEM 2018)
 
Python library
Python libraryPython library
Python library
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
Flux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul DixFlux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul Dix
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Bloom filters
Bloom filtersBloom filters
Bloom filters
 
150970116028 2140705
150970116028 2140705150970116028 2140705
150970116028 2140705
 
5 Coding Hacks to Reduce GC Overhead
5 Coding Hacks to Reduce GC Overhead5 Coding Hacks to Reduce GC Overhead
5 Coding Hacks to Reduce GC Overhead
 

Similar to Priority queue

Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
americanopticalscbe
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
Mehedi Hasan
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
Machine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester ElectiveMachine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester Elective
MayuraD1
 

Similar to Priority queue (20)

Stacks
StacksStacks
Stacks
 
Heaps
HeapsHeaps
Heaps
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptx
 
Queues
QueuesQueues
Queues
 
Mathemetics module
Mathemetics moduleMathemetics module
Mathemetics module
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
6 module 4
6 module 46 module 4
6 module 4
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
Data Structures
Data StructuresData Structures
Data Structures
 
UNIT-2.pptx
UNIT-2.pptxUNIT-2.pptx
UNIT-2.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
Machine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester ElectiveMachine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester Elective
 

Recently uploaded

Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 

Recently uploaded (20)

Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 

Priority queue

  • 2. PRIORITY QUEUE • Definition • Simple Implementation • Binary Heap-Structure Property • Heap Order Property • Basic Heap Operation • Application Of Priority Queues • d-Heaps • References
  • 3. Def:- A Priority Queue is a collection of Elements such that Each element has been assigned a priority and such that is the order in which the elements are deleted and processed From the following rules:- • An Element of higher priority is processed before any element of lower priority. • Two elements with the same priority are processed According to the order in which they were added to the queue. • Any Data Structure that supports the operations of search of min or max, insert and delete min or max respectively is called a priority queue.
  • 4. • Example:-Suppose that we are selling the services of a machine. Each user pays a fixed amount per use. However, the time needed by each user is different. We wish to maximize the returns from this machine under the assumption that the machine is not to be kept idle unless no user is available. This can be done by maintaining a priority queue of all persons waiting to use the machine. Whenever the machine is available, the user with the smallest time requirement will be selected. Hence a priority queue that supports delete min is required. When a new user requests the machine, his or her request is put into the priority queue. If each user needs the same amount of time on the machine but people are willing to pay different amounts for
  • 5. the service, then the priority queue based on the amount of payment can be maintained. Whenever the machine becomes available, the user willing to pay the most is selected. This requires a delete max operation. Model of Priority Queue:- Simple general diagram of the Priority queue is as shown. Whenever an element is inserted into queue, priority Queue inserts the item according to its order. Here we're assuming that data with high value has low priority
  • 6. Basic Operations on priority Queues:- • Insert or enqueue:−This operation will add an item to the rear of the queue. • Delete or Dequeue:-This operation will remove an item from the front of the queue. • Peek − This operation will performed to get the element at front of the queue. • isFull − check if queue is full. • isEmpty − check if queue is empty.
  • 7. Binary Heap A binary heap is a heap data structure that takes the form of a binary tree. Binary heaps are a common way of implementing priority queues. A binary heap is defined as a binary tree with two additional constraints: 1.Shape property:- a binary heap is a complete binary tree that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
  • 8. 2.Heap property: the key stored in each node is either greater than or equal to (≥) or less than or equal to (≤) the keys in the node's children, according to some total order. Heaps where the parent key is greater than or equal to (≥) the child keys are called max-heaps; those where it is less than or equal to (≤) are called min-heaps.
  • 9. Figure shows :- Example of a complete binary max heap
  • 10. Example of a complete binary min heap
  • 11. QN:-What is heap order property? Ans:-A binary heap is a complete binary tree which satisfies the heap ordering property. The ordering can be one of two types: 1 min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root. 2. max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.
  • 12. Basic Heap Operation The common operations are: • Basic operations:-  Find-max (or find-min): find a maximum item of a max-heap, or a minimum item of a min-heap, respectively(peek operation) .  insert: adding a new key to the heap (push ).  extract-max (or extract-min): returns the node of maximum value from a max heap [or minimum value from a min heap] after removing it from the heap (pop operation).  delete-max (or delete-min): removing the root node of a max heap (or min heap), respectively.
  • 13. replace: pop root and push a new key. More efficient than pop followed by push, since only need to balance once, not twice, and appropriate for fixed-size heaps. • Creation operations:- create-heap: This operation creates an empty heap. merge (union):This operation joining two heaps to form a valid new heap containing all the elements of both, preserving the original heaps. heapify: create a heap out of given array of elements.
  • 14. • Inspection operations:-  size: return the number of items in the heap.  is-empty: return true if the heap is empty,  false otherwise. • Internal operations:-  increase-key or decrease-key: updating a key within a max- or min-heap, respectively.  delete: delete an arbitrary node.  sift-up: move a node up in the tree, as long as needed; used to restore heap condition after insertion. sift-down: move a node down in the tree, similar to sift-up; used to restore heap condition after deletion or replacement.
  • 15. Application Of Priority Queues:-  A* search algorithm implementation can be done using Priority queues.  Priority queues are used for load balancing and interrupt handling.  Priority queues are used for Huffman codes for data compression.  In traffic light, depending upon the traffic, the color will be given priority.  Dijkstra’s shortest path algorithm using priority queue when the graph is stored in the form of adjacency list or matrix, priority queue can be used to extract minimum efficiency when implementing Dijkstra’s algorithms.  Prims algorithms implementation can be done using Priority queue & also used to sort heaps.
  • 16. d-array heap:-The d-ary heap or d-heap is a priority queue data structure, a generalization of the binary heap in which the nodes have d children instead of 2. Thus, a binary heap is a 2-heap, and a ternary heap is a 3-heap. References:- 1.wikipedia. 2.Data Structures by Seymour lipschutz. 3.Fundamentals of Data Structures by Sartaj Sahini 4.World wide web.