SlideShare a Scribd company logo
Priority Queue:
Priority Queue is similar to queue where we insert an element from the back and remove
an element from front, but with a one difference that the logical order of elements in the
priority queue depends on the priority of the elements. The element with highest priority
will be moved to the front of the queue and one with lowest priority will move to the back
of the queue. Thus it is possible that when you enqueue an element at the back in the
queue, it can move to front because of its highest priority.
Example:
Let’s say we have an array of 5 elements : {4, 8, 1, 7, 3} and we have to insert all the
elements in the max-priority queue.
First as the priority queue is empty, so 4 will be inserted initially.
Now when 8 will be inserted it will moved to front as 8 is greater than 4.
While inserting 1, as it is the current minimum element in the priority queue, it will
remain in the back of priority queue.
Now 7 will be inserted between 8 and 4 as 7 is smaller than 8.
Now 3 will be inserted before 1 as it is the 2nd minimum element in the priority queue. All
the steps are represented in the diagram below:
Naïve Approach:
Suppose we have N elements and we have to insert these elements in the priority
queue. We can use list and can insert elements in O(N) time and can sort them to
maintain a priority queue in O(N logN ) time.
Efficient Approach:
We can use heaps to implement the priority queue. It will take O(log N) time to insert
and delete each element in the priority queue.
Based on heap structure, priority queue also has two types max- priority queue and min
- priority queue. Max Priority Queue is based on the structure of max heap and can
perform following operations: maximum(Arr) : It returns maximum element from the Arr.
extract_maximum (Arr) - It removes and return the maximum element from the Arr.
increase_val (Arr, i , val) - It increases the key of element stored at index i in Arr to new
value val. insert_val (Arr, val ) - It inserts the element with value val in Arr.
Implementation:
length = number of elements in Arr.
Maximum :
int maximum(int Arr[ ])
{
return Arr[ 1 ]; //as the maximum element is the root element in
the max heap.
}
Complexity: O(1)
Extract Maximum: In this operation, the maximum element will be returned and the
last element of heap will be placed at index 1 and max_heapify will be performed on
node 1 as placing last element on index 1 will violate the property of max-heap.
int extract_maximum (int Arr[ ])
{
if(length == 0)
{
cout<< “Can’t remove element as queue is empty”;
return -1;
}
int max = Arr[1];
Arr[1] = Arr[length];
length = length -1;
max_heapify(Arr, 1);
return max;
}
Complexity: O(logN).
Increase Value: In case increasing value of any node, may violate the property of
max-heap, so we will swap the parent’s value with the node’s value until we get a larger
value on parent node.
void increase_value (int Arr[ ], int i, int val)
{
if(val < Arr[ i ])
{
cout<<”New value is less than current value, can’t be inserted”
<<endl;
return;
}
Arr[ i ] = val;
while( i > 1 and Arr[ i/2 ] < Arr[ i ])
{
swap|(Arr[ i/2 ], Arr[ i ]);
i = i/2;
}
}
Complexity : O(log N).
Insert Value :
void insert_value (int Arr[ ], int val)
{
length = length + 1;
Arr[ length ] = -1; //assuming all the numbers greater than 0 are to be
inserted in queue.
increase_val (Arr, length, val);
}
Complexity: O(log N).
Example:
Initially there are 5 elements in priority queue.
Operation: Insert Value(Arr, 6)
In the diagram below, inserting another element having value 6 is violating the property
of max-priority queue, so it is swapped with its parent having value 4, thus maintaining
the max priority queue.
Operation: Extract Maximum:
In the diagram below, after removing 8 and placing 4 at node 1, violates the property of
max-priority queue. So max_heapify(Arr, 1) will be performed which will maintain the
property of max - priority queue.
As discussed above, like heaps we can use priority queues in scheduling of jobs. When
there are N jobs in queue, each having its own priority. If the job with maximum priority
will be completed first and will be removed from the queue, we can use priority queue’s
operation extract_maximum here. If at every instant we have to add a new job in the
queue, we can use insert_value operation as it will insert the element in O(log N) and
will also maintain the property of max heap.

More Related Content

PPTX
Insertion sort
PPT
Priority queues
PPTX
Insertion sort
PPTX
queue & its applications
PPT
Queue Data Structure
PPT
Queue Data Structure
PDF
Array linear data_structure_2 (1)
PPT
Algorithm
Insertion sort
Priority queues
Insertion sort
queue & its applications
Queue Data Structure
Queue Data Structure
Array linear data_structure_2 (1)
Algorithm

What's hot (20)

PDF
Queue as data_structure
PPTX
PPT
Priority queues
PDF
Insertion sort
PDF
Functional programming 101
PDF
Java ArrayList Tutorial | Edureka
PDF
Concept of Stream API Java 1.8
PDF
Arguments Object in JavaScript
PDF
Functional programming java
PPT
basics of queues
PDF
Heapsort quick sort
PPT
Notes DATA STRUCTURE - queue
PPTX
Quicksort Algorithm..simply defined through animations..!!
PPT
3.8 quick sort
PPT
PPT
Array 2
PDF
Dsa circular queue
Queue as data_structure
Priority queues
Insertion sort
Functional programming 101
Java ArrayList Tutorial | Edureka
Concept of Stream API Java 1.8
Arguments Object in JavaScript
Functional programming java
basics of queues
Heapsort quick sort
Notes DATA STRUCTURE - queue
Quicksort Algorithm..simply defined through animations..!!
3.8 quick sort
Array 2
Dsa circular queue
Ad

Similar to Priorty queue (20)

PPT
GAC DS Priority Queue Presentation 2022.ppt
PPTX
PriorityqDhruvBaswal.pptx
PPTX
CHAPTER 4 Learning QUEUE data structure.pptx
PDF
Chapter11
PDF
5.1 Priority Queues.pdf 5.1 Priority Queues.pdf5.1 Priority Queues.pdf5.1 Pri...
PPTX
Priority queue in DSA
PPTX
DS UNIT2QUEUES.pptx
PPTX
My lectures circular queue
PDF
Basic Terminologies of Queue...Basic operations on Queue
PPT
Priority Queue
PPTX
Priority queue
PDF
5-Queue-----------------------------in c++
PDF
4 heapsort pq
PPTX
Thisismyroughkjhfdyhbfhjbgijggjnvkppt.pptx
PPTX
ADS UNIT II -Priority queue.pptx
PPTX
The presention is about the queue data structure
PDF
LEC4-DS ALGO.pdf
PDF
10 chapter6 heaps_priority_queues
PDF
Priority Queue using HEAP. ( Using C in data structure )
GAC DS Priority Queue Presentation 2022.ppt
PriorityqDhruvBaswal.pptx
CHAPTER 4 Learning QUEUE data structure.pptx
Chapter11
5.1 Priority Queues.pdf 5.1 Priority Queues.pdf5.1 Priority Queues.pdf5.1 Pri...
Priority queue in DSA
DS UNIT2QUEUES.pptx
My lectures circular queue
Basic Terminologies of Queue...Basic operations on Queue
Priority Queue
Priority queue
5-Queue-----------------------------in c++
4 heapsort pq
Thisismyroughkjhfdyhbfhjbgijggjnvkppt.pptx
ADS UNIT II -Priority queue.pptx
The presention is about the queue data structure
LEC4-DS ALGO.pdf
10 chapter6 heaps_priority_queues
Priority Queue using HEAP. ( Using C in data structure )
Ad

Recently uploaded (20)

PDF
A comparative analysis of optical character recognition models for extracting...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Approach and Philosophy of On baking technology
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Mushroom cultivation and it's methods.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
A comparative analysis of optical character recognition models for extracting...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
TLE Review Electricity (Electricity).pptx
WOOl fibre morphology and structure.pdf for textiles
cloud_computing_Infrastucture_as_cloud_p
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
A novel scalable deep ensemble learning framework for big data classification...
Approach and Philosophy of On baking technology
Zenith AI: Advanced Artificial Intelligence
Hindi spoken digit analysis for native and non-native speakers
1 - Historical Antecedents, Social Consideration.pdf
A Presentation on Artificial Intelligence
A comparative study of natural language inference in Swahili using monolingua...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
1. Introduction to Computer Programming.pptx
Enhancing emotion recognition model for a student engagement use case through...
Mushroom cultivation and it's methods.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf

Priorty queue

  • 1. Priority Queue: Priority Queue is similar to queue where we insert an element from the back and remove an element from front, but with a one difference that the logical order of elements in the priority queue depends on the priority of the elements. The element with highest priority will be moved to the front of the queue and one with lowest priority will move to the back of the queue. Thus it is possible that when you enqueue an element at the back in the queue, it can move to front because of its highest priority. Example: Let’s say we have an array of 5 elements : {4, 8, 1, 7, 3} and we have to insert all the elements in the max-priority queue. First as the priority queue is empty, so 4 will be inserted initially. Now when 8 will be inserted it will moved to front as 8 is greater than 4. While inserting 1, as it is the current minimum element in the priority queue, it will remain in the back of priority queue. Now 7 will be inserted between 8 and 4 as 7 is smaller than 8. Now 3 will be inserted before 1 as it is the 2nd minimum element in the priority queue. All the steps are represented in the diagram below: Naïve Approach: Suppose we have N elements and we have to insert these elements in the priority queue. We can use list and can insert elements in O(N) time and can sort them to maintain a priority queue in O(N logN ) time.
  • 2. Efficient Approach: We can use heaps to implement the priority queue. It will take O(log N) time to insert and delete each element in the priority queue. Based on heap structure, priority queue also has two types max- priority queue and min - priority queue. Max Priority Queue is based on the structure of max heap and can perform following operations: maximum(Arr) : It returns maximum element from the Arr. extract_maximum (Arr) - It removes and return the maximum element from the Arr. increase_val (Arr, i , val) - It increases the key of element stored at index i in Arr to new value val. insert_val (Arr, val ) - It inserts the element with value val in Arr. Implementation: length = number of elements in Arr. Maximum : int maximum(int Arr[ ]) { return Arr[ 1 ]; //as the maximum element is the root element in the max heap. } Complexity: O(1) Extract Maximum: In this operation, the maximum element will be returned and the last element of heap will be placed at index 1 and max_heapify will be performed on node 1 as placing last element on index 1 will violate the property of max-heap. int extract_maximum (int Arr[ ]) { if(length == 0) { cout<< “Can’t remove element as queue is empty”; return -1; } int max = Arr[1]; Arr[1] = Arr[length]; length = length -1; max_heapify(Arr, 1); return max; } Complexity: O(logN).
  • 3. Increase Value: In case increasing value of any node, may violate the property of max-heap, so we will swap the parent’s value with the node’s value until we get a larger value on parent node. void increase_value (int Arr[ ], int i, int val) { if(val < Arr[ i ]) { cout<<”New value is less than current value, can’t be inserted” <<endl; return; } Arr[ i ] = val; while( i > 1 and Arr[ i/2 ] < Arr[ i ]) { swap|(Arr[ i/2 ], Arr[ i ]); i = i/2; } } Complexity : O(log N). Insert Value : void insert_value (int Arr[ ], int val) { length = length + 1; Arr[ length ] = -1; //assuming all the numbers greater than 0 are to be inserted in queue. increase_val (Arr, length, val); } Complexity: O(log N). Example: Initially there are 5 elements in priority queue. Operation: Insert Value(Arr, 6) In the diagram below, inserting another element having value 6 is violating the property of max-priority queue, so it is swapped with its parent having value 4, thus maintaining the max priority queue.
  • 4. Operation: Extract Maximum: In the diagram below, after removing 8 and placing 4 at node 1, violates the property of max-priority queue. So max_heapify(Arr, 1) will be performed which will maintain the property of max - priority queue. As discussed above, like heaps we can use priority queues in scheduling of jobs. When there are N jobs in queue, each having its own priority. If the job with maximum priority will be completed first and will be removed from the queue, we can use priority queue’s operation extract_maximum here. If at every instant we have to add a new job in the queue, we can use insert_value operation as it will insert the element in O(log N) and will also maintain the property of max heap.