SlideShare a Scribd company logo
1 of 15
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 (20)

Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Linked List
Linked ListLinked List
Linked List
 
Queues
QueuesQueues
Queues
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Arrays
ArraysArrays
Arrays
 
Linked list
Linked listLinked list
Linked list
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Linked list
Linked listLinked list
Linked list
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 

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.pptCUO VEERANAN VEERANAN
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxssuser17c9c21
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
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 EngineeringRAJASEKHARV8
 
basics of queues
basics of queuesbasics of queues
basics of queuessirmanohar
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
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,queueRai University
 
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,queueRai University
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
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,queueRai 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
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
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
 
Queues
QueuesQueues
Queues
 
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 algorithmsjunnubabu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javajunnubabu
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in javajunnubabu
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in pythonjunnubabu
 
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 processingjunnubabu
 
TELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMSTELECOMMUNICATIONS SYSTEMS
TELECOMMUNICATIONS SYSTEMSjunnubabu
 
MEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLMEDIUM ACCESS CONTROL
MEDIUM ACCESS CONTROLjunnubabu
 
WIRELESS TRANSMISSION
WIRELESS TRANSMISSIONWIRELESS TRANSMISSION
WIRELESS TRANSMISSIONjunnubabu
 
MOBILE COMMUNICATION
MOBILE COMMUNICATIONMOBILE COMMUNICATION
MOBILE COMMUNICATIONjunnubabu
 
Location based reminder
Location based reminderLocation based reminder
Location based reminderjunnubabu
 

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

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

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))