Unit III - Priority Queue
Unit III - Priority Queue
Algorithms
The priority of the elements in a priority queue will determine the order in which elements
are removed from the priority queue.
The priority queue supports only comparable elements, which means that the elements
are either arranged in an ascending or descending order.
For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority
queue with an ordering imposed on the values is from least to the greatest.
Therefore, the 1 number would be having the highest priority while 22 will be having the
lowest priority.
Characteristics of priority queue
•Every element in a priority queue has some priority associated with it.
•An element with the higher priority will be deleted before the deletion of the lesser
priority.
•If two elements in a priority queue have the same priority, they will be arranged using the
FIFO principle.
Types of priority Queue
There are two types of priority queue:
Ascending order priority queue: In ascending order priority queue, a lower
priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in an ascending order like
1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest priority in a
priority queue.
Descending order priority queue: In descending order priority queue, a higher priority
number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3,
2, 1; therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
Representation of priority queue
Now, we will see how to represent the
priority queue through a one-way list.
We will create the priority queue by using
the list given below in which INFO list
contains the data elements, PRN list
contains the priority numbers of each
data element available in the INFO list,
and LINK basically contains the address
of the next node.
Step by step procedure
In the case of priority queue, lower priority number is considered the higher priority,
i.e., lower priority number = higher priority.
Step 1: In the list, lower priority number is 1, whose data value is 333, so it will be inserted in
the list as shown in the below diagram:
Step 2: After inserting 333, priority number 2 is having a higher priority, and data values
associated with this priority are 222 and 111. So, this data will be inserted based on the FIFO
principle; therefore 222 will be added first and then 111.
Step 3: After inserting the elements of priority 2, the next higher priority number is 4 and data
elements associated with 4 priority numbers are 444, 555, 777. In this case, elements would
be inserted based on the FIFO principle; therefore, 444 will be added first, then 555, and then
777.
Step 4: After inserting the elements of priority 4, the next higher priority number is 5, and the
value associated with priority 5 is 666, so it will be inserted at the end of the queue.
Implementation of Priority Queue
The priority queue can be implemented in four ways that include arrays, linked list, heap
data structure and binary search tree.
The heap data structure is the most efficient way of implementing the priority queue, so we
will implement the priority queue using a heap data structure in this topic.
Now, first we understand the reason why heap is the most efficient way among all the other
data structures.
Analysis of complexities using different implementations
What is Heap?
A heap is a tree-based data structure that forms a complete binary tree, and satisfies the
heap property.
If A is a parent node of B, then A is ordered with respect to the node B for all nodes A and
B in a heap.
It means that the value of the parent node could be more than or equal to the value of the
child node, or the value of the parent node could be less than or equal to the value of the
child node.
If the element is not in a correct place then it is compared with the parent
node; if it is found out of order, elements are swapped. This process
continues until the element is placed in a correct position.
Removing the minimum element from the priority queue
As we know that in a max heap, the maximum element is the root node.
When we remove the root node, it creates an empty slot. The last inserted element will be
added in this empty slot.
Then, this element is compared with the child nodes, i.e., left-child and right child, and swap
with the smaller of the two.
It keeps moving down the tree until the heap property is restored .
Applications of Priority queue
It is used in the Dijkstra's shortest path algorithm.
It is also used in operating system like priority scheduling, load balancing and
interrupt handling.
Min Heap - Example
Insert the value 5
Delete ‘5’
Program
#include <stdio.h>
#include <stdio.h>
int heap[40];
int size=-1;
return (i - 1) / 2;
}
int temp;
temp=heap[parent(i)];
heap[parent(i)]=heap[i];
heap[i]=temp;
}
// updating the value of i to i/2
i=i/2;
}
}
//
function to move the node down the tree in order to restore the heap prope
rty.
void moveDown(int k)
{
int index = k;
// move the node stored at ith location is shifted to the root node
moveUp(i);
insert(20);
insert(19);
insert(21);
insert(18);
insert(12);
insert(17);
insert(15);
insert(16);
insert(14);
int i=0;
printf("Elements in a priority queue are : ");
for(int i=0;i<=size;i++)
{
printf("%d ",heap[i]);
}
delete(2); // deleting the element whose index is 2.
printf("\nElements in a priority queue after deleting the element are : ");
for(int i=0;i<=size;i++)
{
printf("%d ",heap[i]);
}
int max=get_Max();
printf("\nThe element which is having the highest priority is %d: ",max);
int min=get_Min();
printf("\nThe element which is having the minimum priority is : %d",min);
return 0;
}
int parent(int i): This function returns the index of the parent node of a child node, i.e., i.
int left_child(int i): This function returns the index of the left child of a given index, i.e., i.
int right_child(int i): This function returns the index of the right child of a given index, i.e., i.
void moveUp(int i): This function will keep moving the node up the tree until the heap property is
restored.
void moveDown(int i): This function will keep moving the node down the tree until the heap property is
restored.
void removeMax(): This function removes the element which is having the highest priority.
void insert(int p): It inserts the element in a priority queue which is passed as an argument in a function.
void delete(int i): It deletes the element from a priority queue at a given index.
int get_Max(): It returns the element which is having the highest priority, and we know that in max heap,
the root node contains the element which has the largest value, and highest priority.
int get_Min(): It returns the element which is having the minimum priority, and we know that in max heap,
the last node contains the element which has the smallest value, and lowest priority.