SlideShare a Scribd company logo
Priority Queue
N.JunnuBabu asst prof.
MITS.
Priority Queue
 Definition:
 The priority queue is an abstract data-type similar to a regular queue or stack
data structure in which each element additionally has a "priority" associated with it.
In a priority queue, an element with high priority is served before an element with
low priority.
 A priority queue ADT is a data structure that supports the operations Insert and
Delete Min (which returns and removes the minimum element) or Delete Max
(which returns and removes the maximum element) and peeking the element(find
min or find max).
 Difference between Priority Queue and Normal Queue:
 In a queue, the first-in-first-out rule is implemented whereas, in a priority queue,
the values are removed on the basis of priority. The element with the highest
priority is removed first.
Types of Priority Queue:
 There are two types in Priority Queue:
1. Ascending Order
2. Descending order
1. Ascending Order Priority Queue:
 An ascending order priority queue gives the highest priority to the lower number in that
queue.
Example:
 you have six numbers in the priority queue that are 15, 35, 10, 40, 6, 5. Firstly, you will
arrange these numbers in ascending order.
The new list is as follows:
 5, 6, 10, 15, 35, 40. In this list, 5 is the smallest number. Hence, the ascending order
priority queue treats number 5 as the highest priority.
 Before applying of ascending order table like this.
 After applying of ascending order table like this.
 In the above table 5 has the highest priority, and 40 has the lowest priority.
15 35 10 40 6 5
5 6 10 15 35 40
Continue…
2. Descending Order Priority Queue:
 A descending order priority queue gives the highest priority to the highest number
in that queue.
Example:
 you have six numbers in the priority queue that are 15, 35, 10, 40, 6, 5. Firstly, you
will arrange these numbers in ascending order.
The new list is as follows:
 40, 35, 15, 10, 6, 5. In this list, 40 is the highest number. Hence, the descending
order priority queue treats number 40 as the highest priority.
 Before applying of ascending order table like this.
 After applying of ascending order table like this.
 In the above table 40 has the highest priority, and 5 has the lowest priority.
15 35 10 40 6 5
40 35 15 10 6 5
Applications of Priority Queue:
 Prim's algorithm implementation can be done using priority queues.
 Dijkstra's shortest path algorithm implementation can be done using priority
queues.
 A* Search algorithm implementation can be done using priority queues.
 Priority queues are used to sort heaps.
 Priority queues are used in operating system for load balancing and interrupt
handling.
 Priority queues are used in huffman codes for data compression.
 In traffic light, depending upon the traffic, the colors will be given priority.
Characteristics of a Priority Queue:
 A queue is termed as a priority queue if it has the following characteristics:
 Each item has some priority associated with it.
 An item with the highest priority is moved at the front and deleted first.
 If two elements share the same priority value, then the priority queue follows the first-in-
first-out principle for de queue operation.
 Implementation of the Priority Queue in Data Structure:
 You can implement the priority queues in one of the following ways:
1. Linked list.
2. Binary heap.
1. Min heap.
2. Max heap.
3. Arrays.
1. Unordered Array.
2. Ordered Array.
4. Binary search tree.
Priority Queue using Linked list:
Algorithm for Implementing Priority Queue
using linked list in C :(enqueue(int))
enqueue(int d)
Create a new node of structure node type.
new_node->data = entered data.
new_node->Priority = priority.
IF(Front == NULL)||(Entered priorityPriority)
new_node->next = Front.
Front = new_node.
End IF.
ELSE
Temp = Front
WHILE((Temp->next!=NULL)&&(Temp->Priority<=Entered Priority)
Temp = Temp->next
new_node->next = temp->next
temp->next = new_node
End ELSE
Algorithm for Implementing Priority Queue
using linked list in C :(dequeue())
dequeue()
Create a temporary pointer temp of node structure type
IF(Front == NULL)
Print ‘Underflow Condition’
End IF
ELSE
Temp = Front
Front = Front->Next
Print(data and priority)
Free(temp)
End ELSE
Algorithm for Implementing Priority Queue
using linked list in C :(print())
STRUCT NODE* TEMP
IF((FRONT == NULL)&&(REAR == NULL))
RETURN
ELSE
TEMP = FRONT
WHILE(TEMP)
RETURN TEMP->DATA
TEMP = TEMP->NEXT
Priority Queue using unordered Array :
Priority Queue using ordered Array :
Algorithm for Implementing Priority Queue
using Array in C :(enqueue(int))
Enqueue()
IF((Front == 0)&&(Rear == N-1))
PRINT “Overflow Condition”
Else
IF(Front == -1)
Front = Rear =0
Queue[Rear] = Data
Priority[Rear] = Priority
ELSE IF(Rear ==N-1)
FOR (i=Front;i<=Rear;i++)
FOR(i=Front;i<=Rear;i++)
Q[i-Front] =Q[i]
Pr[i-Front] = Pr[i]
Rear = Rear-Front
Front = 0
FOR(i = r;i>f;i–)
IF(p>Pr[i])
Q[i+1] = Q[i] Pr[i+1] = Pr[i]
ELSE
Q[i+1] = data Pr[i+1] = p
Rear++
Algorithm for Implementing Priority Queue
using Array in C :(dequeue(),print())
Dequeue()
IF(Front == -1)
PRINT “Queue Under flow condition”
ELSE
PRINT”Q[f],Pr[f]”
IF(Front==Rear)
Front = Rear = -1
ELSE
FRONT++
Print()
FOR(i=Front;i<=Rear;i++)
PRINT(Q[i],Pr[i])
Comparative analysis of priority queue:
Operation
Unordered
Array
Ordered
Array
Linked list Binary Heap Binary Search Tree
Insert 0(1) 0(N) 0(1) 0(log(N)) 0(log(N))
Peek 0(N) 0(1) 0(N) 0(1) 0(1)
Delete 0(N) 0(1) 0(1) 0(log (N)) 0(log(N))

More Related Content

What's hot

Linked List
Linked ListLinked List
Linked List
RaaviKapoor
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
MdSaiful14
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
linked list
linked list linked list
linked list
Mohaimin Rahat
 
Circular Queue data structure
Circular Queue data structureCircular Queue data structure
Circular Queue data structure
Dhananjaysinh Jhala
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 

What's hot (20)

Linked List
Linked ListLinked List
Linked List
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
stack & queue
stack & queuestack & queue
stack & queue
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
linked list
linked list linked list
linked list
 
Circular Queue data structure
Circular Queue data structureCircular Queue data structure
Circular Queue data structure
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 

Similar to Priority queue in DSA

GAC DS Priority Queue Presentation 2022.ppt
GAC DS Priority Queue Presentation 2022.pptGAC DS Priority Queue Presentation 2022.ppt
GAC DS Priority Queue Presentation 2022.ppt
CUO VEERANAN VEERANAN
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
VeerannaKotagi1
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
maneesh boddu
 
Queues
Queues Queues
Queues
nidhisatija1
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptx
ssuser17c9c21
 
Data Structures
Data StructuresData Structures
Data Structures
Dr.Umadevi V
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
Dr.Umadevi V
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
2 b queues
2 b queues2 b queues
2 b queues
Nguync91368
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Queue
QueueQueue
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
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
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 

Similar to Priority queue in DSA (20)

GAC DS Priority Queue Presentation 2022.ppt
GAC DS Priority Queue Presentation 2022.pptGAC DS Priority Queue Presentation 2022.ppt
GAC DS Priority Queue Presentation 2022.ppt
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Queues
Queues Queues
Queues
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptx
 
Data Structures
Data StructuresData Structures
Data Structures
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
2 b queues
2 b queues2 b queues
2 b queues
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Queue
QueueQueue
Queue
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
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)
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 

More from junnubabu

Data struchers and algorithms
Data struchers and algorithmsData struchers and algorithms
Data struchers and algorithms
junnubabu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
junnubabu
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
junnubabu
 
Mobile transport layer .
Mobile transport layer .Mobile transport layer .
Mobile transport layer .
junnubabu
 
Internet protocol (ip)
Internet protocol (ip)Internet protocol (ip)
Internet protocol (ip)
junnubabu
 
Data pre processing
Data pre processingData pre processing
Data pre processing
junnubabu
 
TELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMSTELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMS
junnubabu
 
MEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLMEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROL
junnubabu
 
WIRELESS TRANSMISSION
WIRELESS TRANSMISSIONWIRELESS TRANSMISSION
WIRELESS TRANSMISSION
junnubabu
 
MOBILE COMMUNICATION
MOBILE COMMUNICATIONMOBILE COMMUNICATION
MOBILE COMMUNICATION
junnubabu
 
Location based reminder
Location based reminderLocation based reminder
Location based reminder
junnubabu
 

More from junnubabu (12)

Data struchers and algorithms
Data struchers and algorithmsData struchers and algorithms
Data struchers and algorithms
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Mobile transport layer .
Mobile transport layer .Mobile transport layer .
Mobile transport layer .
 
Internet protocol (ip)
Internet protocol (ip)Internet protocol (ip)
Internet protocol (ip)
 
Data pre processing
Data pre processingData pre processing
Data pre processing
 
TELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMSTELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMS
 
MEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLMEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROL
 
WIRELESS TRANSMISSION
WIRELESS TRANSMISSIONWIRELESS TRANSMISSION
WIRELESS TRANSMISSION
 
MOBILE COMMUNICATION
MOBILE COMMUNICATIONMOBILE COMMUNICATION
MOBILE COMMUNICATION
 
Location based reminder
Location based reminderLocation based reminder
Location based reminder
 

Recently uploaded

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 

Recently uploaded (20)

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 

Priority queue in DSA

  • 2. Priority Queue  Definition:  The priority queue is an abstract data-type similar to a regular queue or stack data structure in which each element additionally has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.  A priority queue ADT is a data structure that supports the operations Insert and Delete Min (which returns and removes the minimum element) or Delete Max (which returns and removes the maximum element) and peeking the element(find min or find max).  Difference between Priority Queue and Normal Queue:  In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the values are removed on the basis of priority. The element with the highest priority is removed first.
  • 3. Types of Priority Queue:  There are two types in Priority Queue: 1. Ascending Order 2. Descending order 1. Ascending Order Priority Queue:  An ascending order priority queue gives the highest priority to the lower number in that queue. Example:  you have six numbers in the priority queue that are 15, 35, 10, 40, 6, 5. Firstly, you will arrange these numbers in ascending order. The new list is as follows:  5, 6, 10, 15, 35, 40. In this list, 5 is the smallest number. Hence, the ascending order priority queue treats number 5 as the highest priority.  Before applying of ascending order table like this.  After applying of ascending order table like this.  In the above table 5 has the highest priority, and 40 has the lowest priority. 15 35 10 40 6 5 5 6 10 15 35 40
  • 4. Continue… 2. Descending Order Priority Queue:  A descending order priority queue gives the highest priority to the highest number in that queue. Example:  you have six numbers in the priority queue that are 15, 35, 10, 40, 6, 5. Firstly, you will arrange these numbers in ascending order. The new list is as follows:  40, 35, 15, 10, 6, 5. In this list, 40 is the highest number. Hence, the descending order priority queue treats number 40 as the highest priority.  Before applying of ascending order table like this.  After applying of ascending order table like this.  In the above table 40 has the highest priority, and 5 has the lowest priority. 15 35 10 40 6 5 40 35 15 10 6 5
  • 5. Applications of Priority Queue:  Prim's algorithm implementation can be done using priority queues.  Dijkstra's shortest path algorithm implementation can be done using priority queues.  A* Search algorithm implementation can be done using priority queues.  Priority queues are used to sort heaps.  Priority queues are used in operating system for load balancing and interrupt handling.  Priority queues are used in huffman codes for data compression.  In traffic light, depending upon the traffic, the colors will be given priority.
  • 6. Characteristics of a Priority Queue:  A queue is termed as a priority queue if it has the following characteristics:  Each item has some priority associated with it.  An item with the highest priority is moved at the front and deleted first.  If two elements share the same priority value, then the priority queue follows the first-in- first-out principle for de queue operation.  Implementation of the Priority Queue in Data Structure:  You can implement the priority queues in one of the following ways: 1. Linked list. 2. Binary heap. 1. Min heap. 2. Max heap. 3. Arrays. 1. Unordered Array. 2. Ordered Array. 4. Binary search tree.
  • 7. Priority Queue using Linked list:
  • 8. Algorithm for Implementing Priority Queue using linked list in C :(enqueue(int)) enqueue(int d) Create a new node of structure node type. new_node->data = entered data. new_node->Priority = priority. IF(Front == NULL)||(Entered priorityPriority) new_node->next = Front. Front = new_node. End IF. ELSE Temp = Front WHILE((Temp->next!=NULL)&&(Temp->Priority<=Entered Priority) Temp = Temp->next new_node->next = temp->next temp->next = new_node End ELSE
  • 9. Algorithm for Implementing Priority Queue using linked list in C :(dequeue()) dequeue() Create a temporary pointer temp of node structure type IF(Front == NULL) Print ‘Underflow Condition’ End IF ELSE Temp = Front Front = Front->Next Print(data and priority) Free(temp) End ELSE
  • 10. Algorithm for Implementing Priority Queue using linked list in C :(print()) STRUCT NODE* TEMP IF((FRONT == NULL)&&(REAR == NULL)) RETURN ELSE TEMP = FRONT WHILE(TEMP) RETURN TEMP->DATA TEMP = TEMP->NEXT
  • 11. Priority Queue using unordered Array :
  • 12. Priority Queue using ordered Array :
  • 13. Algorithm for Implementing Priority Queue using Array in C :(enqueue(int)) Enqueue() IF((Front == 0)&&(Rear == N-1)) PRINT “Overflow Condition” Else IF(Front == -1) Front = Rear =0 Queue[Rear] = Data Priority[Rear] = Priority ELSE IF(Rear ==N-1) FOR (i=Front;i<=Rear;i++) FOR(i=Front;i<=Rear;i++) Q[i-Front] =Q[i] Pr[i-Front] = Pr[i] Rear = Rear-Front Front = 0 FOR(i = r;i>f;i–) IF(p>Pr[i]) Q[i+1] = Q[i] Pr[i+1] = Pr[i] ELSE Q[i+1] = data Pr[i+1] = p Rear++
  • 14. Algorithm for Implementing Priority Queue using Array in C :(dequeue(),print()) Dequeue() IF(Front == -1) PRINT “Queue Under flow condition” ELSE PRINT”Q[f],Pr[f]” IF(Front==Rear) Front = Rear = -1 ELSE FRONT++ Print() FOR(i=Front;i<=Rear;i++) PRINT(Q[i],Pr[i])
  • 15. Comparative analysis of priority queue: Operation Unordered Array Ordered Array Linked list Binary Heap Binary Search Tree Insert 0(1) 0(N) 0(1) 0(log(N)) 0(log(N)) Peek 0(N) 0(1) 0(N) 0(1) 0(1) Delete 0(N) 0(1) 0(1) 0(log (N)) 0(log(N))