AfterAcademy Tech
•
18 Feb 2020

Priority Queues are similar to queues where we insert an element from the back and remove an element from the front, but with one difference that the logical order of elements in the priority queue depends on the priority of the elements. In this blog, we will discuss the priority queues, its properties, its various implementations, and its applications. We will also analyze and compare the various implementations of the Priority Queue. So let’s get started with the properties of Priority Queue.
Priority Queues are the extension of the queues with the following properties:

Priority Queue supports the following functionality
We can think of various ways of implementing the priority queues. We can choose various data structures like an array, linked list, BST, heaps to make the operations of the priority queues efficient.
The structure of the element of the priority queue will be
class Node {
int data
int priority
}
Implementation through Array
We will take an array that will store the elements of the priority queue. So, the enqueue operation will take place at the end of the array which is a constant time operation.
But, for the dequeue operation, we have to find the highest priority element in the array and delete it from the array. The deletion and the search operation in the array take linear time. Similarly, the top() will also take linear time to search for the highest priority element.
So, with an array
Now, if we maintain the order of the array with respect to priority, the enqueue() will take the linear time and dequeue() and top() will take the constant time.
Implementation through Linked List
The linked list can be created in such a way that the highest priority element should be at the front. The linked list can be arranged in the order of descending priority. So that the dequeue() operation can take constant time.
For enqueue() operation, we need to traverse the linked list and find the proper position to insert the node such that the overall order of priority queue is maintained. This will take linear time but the other two operations i.e. the dequeue() and top() can be done in constant time.
Implementation through BST
We can also implement the functionality of the priority queue using a Binary Search Tree. A typical BST takes O(logn) for both insertion and deletion operation. We can also use the self-balancing BST like AVL Tree, Red-Black Tree, etc to support the o(logn) complexity even in worst-case.
The time to find the top priority element can be done in constant time. We can keep an extra pointer to store the highest priority element and that can be updated when the insertion and deletion is performed. With deletion operation, we will update the extra pointer by finding the inorder successor.
Implementation through Heaps
Heaps are generally preferred for the implementation of a priority queue. In the heaps, the enqueue() and dequeue() can be performed in O(log n) time.
The top priority element will be present at the root Node. The heap can be built on the property that top priority element will be at the root Node so that the top priority element can be found in constant time.
The BST and heaps take the same time complexity to perform the operations of priority queue but heaps are prefered when priority queue is implemented. This is because of the following reasons:
Enqueue Operation
Inserting an element in the priority queue(max heap) is done as
-03eda2a57220b9f9.png)
-a98f283dfa65bb33.png)
Dequeue Operation
Deleting the maximum priority element is done as
-2079b3a85a4e8f32.png)
-d1657705f645771c.png)
-d0ec98ac05f13462.png)
Top Operation
The maximum priority element will be present as rootNode. So return the rootNode.
Pseudo Code
// arr will store the element's of the priority queue
void heapify(int arr[], int size, int i)
{
largest = i
leftChild = 2*i + 1
rightChild = 2*i + 1
if( leftChild < size and arr[i] < arr[leftChild])
largest = leftChild
if( rightChild < size and arr[largest] < arr[rightChild])
largest = rightChild
if( largest != i)
{
swap(arr[i],arr[largest])
heapify(arr,n,largest)
}
}
// This will return the size of the priority queue
int enqueue(int arr[], int size, int num) {
if(size == 0) {
arr.append(num)
}
else {
arr.append(num);
for ( i = size/2-1 to 0 )
heapify(arr,size,i)
}
return size + 1
}
// This will return the size of the priority queue
int dequeue(int arr[], int size)
{
swap(arr[0],arr[size-1])
size = size - 1 // decreasing the size
for( i = size/2 -1 to 0 )
heapify(arr,size,i)
}
// return top priority element
int top(){
return arr[0]
}
Happy coding! Enjoy Algorithms.
AfterAcademy Tech
Queue is a necessary addition in a programmer's toolkit. We will discuss the basic design structure of queue and what operations are performed on it. This blog also discusses the implementation of queue using arrays and linked lists

AfterAcademy Tech
Given an integer K and queue Q of integers. Write a program to reverse the order of the first K elements of the queue. The other elements will be in the same relative order.

AfterAcademy Tech
This is an interview problem asked in companies like Microsoft, Amazon, Adobe, Flipkart, etc. Here, we need to implement Queue data structure using another data structure Stack. This problem is meant for testing the data structure knowledge.

AfterAcademy Tech
In this blog, we will learn about various process scheduling algorithms used in Operating System. We will learn about FCFS, SJF, SRTF, Round-Robin, Priority-based, Highest Response Ratio Next, Multilevel Queue, and Multilevel Feedback Queue scheduling.
