Priority Queue
A priority queue is a special kind of queue where each element has a priority assigned to it. Unlike a normal queue, where elements are processed in the order they arrive. They follow First in First Out principle and in a priority queue, the element with the highest priority is served first. If two elements have the same priority, they are processed in the order they were added, just like a normal queue. Priority queues are useful in situations where some tasks or elements need to be processed before others, such as CPU task scheduling, printer job management, or emergency room patient handling. This way, important elements are always handled first while still keeping the relative order for elements with the same priority.
How priority queue is different from the normal queue?
The difference between Priority Queue and Normal Queue is Normal queue Follows FIFO (first-in-first-out) principle whereas, in a priority queue, the values are removed on the basis of priority which is assigned to the elements. The element with the highest priority will be removed first.
There are two types of Priority Queues.
1. Ascending Priority Queue
2. Descending priority Queue
Ascending Priority Queue
An Ascending Priority Queue is a type of priority queue where elements can be inserted in any order, but when we remove elements then the smallest element is always removed first which is given the highest priority. For example, if we insert the numbers 4, 2, and 8 and they are also stored as 4, 2, 8 in the queue. However, when we start deleting the elements, they are deleted in ascending order: 2 is deleted first, then 4, and finally 8. This ensures that the element with the highest priority is always served before the others.
Descending priority Queue
A Descending Priority Queue is a type of priority queue where elements can be inserted in any order, but when we remove elements then the largest element is always removed first which is given the highest priority. For example, if we insert the numbers 4, 2, and 8 and they are also stored as 4,2,8 in the queue. However, when we start deleting the elements, they are deleted in descending order: 8 is deleted first, then 4, and finally 2. This ensures that the element with the highest priority is always served before the others.
Ascending Priority Queue vs Descending Priority Queue
| Feature | Ascending Priority Queue | Descending Priority Queue |
|---|---|---|
| Definition | A queue where elements can be inserted in any order, but the smallest element is removed first. | A queue where elements can be inserted in any order, but the largest element is removed first. |
| Insertion | Elements can be inserted arbitrarily without considering their value. | Elements can be inserted arbitrarily without considering their value. |
| Deletion | Always deletes the minimum/smallest value first. | Always deletes the maximum/largest value first. |
| Order of Removal | Elements come out in ascending order (small → large). | Elements come out in descending order (large → small). |
| Example | Insert: 4, 2, 8 → Deletion order: 2, 4, 8 | Insert: 4, 2, 8 → Deletion order: 8, 4, 2 |
| Use Case | Useful when we need to process the lowest priority first, like task scheduling with minimum cost. | Useful when we need to process the highest priority first, like serving urgent tasks or maximum value jobs. |
Application of priority queue
- Dijkstra’s algorithm
- Stack implementation
- In CPU scheduling algorithms
- For load balancing and interrupt handling in an operating system
- In Huffman code for data compression
Conclusion
In short, a Priority Queue helps organize data so that important tasks are handled first. It works by giving each element a priority, and elements are processed according to that priority. With types like ascending and descending, it makes many real-life processes — like task scheduling, network management, and pathfinding — faster and more efficient.