Introduction:
Think of a priority queue in programming exactly the same as a priority queue on an airport where the boarding process requires people to be divided into groups. First group one will line up to board. Then the second etc etc. This grouping is done to prevent the line from becoming to long and that the process happens in phases or just so that multiple airport attendants can help at the same time to speed things up. If one person causes issues in one line. They just continue with the second line to prevent expensive delays. However, there is one priority queue. People with status (money) are often in this line. This line will be boarded first.
Now think of this concept in programming. There is a large batch of tasks (people) that needs to be taken care of, but there is a limited amount of server resources (airport attendants) to process them all. To prevent clogging the system the batch of tasks will be put into a queue by one server (mostly a webserver) in the form of ‘messages’. The other servers called workers will start processing the tasks picking them one by one of the queue. If one of the workers fails the others will just pick the rest up. However sometimes in large queues there is some important task that requires asap processing. These tasks will be put into a different queue. If tasks are in that queue the workers will first process those tasks and continue with the normal queue afterwards.
Content Cover the following...
ToggleDefinition – Priority Queue
Priority Queue is quite similar to queue data structure, but in this when we insert an element means when we enqueue an element we also enqueue an additional piece of information, namely priority. When we pop an element, an element with highest priority will be pop out first.
Priority queue assign different priority levels to items.
The following example illustrates a priority queue with an ordering imposed on the values from least to the greatest:
Here, A, B, etc. denotes the value of items while 1, 2, etc. denotes the priority of items. So the item with the highest priority in this example is C (with the priority of 1) that is removed first. And the lowest priority item, Q (with the priority of 19), will be removed at the end of the process. In this tutorial, from now on, we’ll use priority as the value of items since other information can be easily attached to the queue’s elements.
Operations on Priority Queue
The main operations on a priority queue include:
- add/EnQueue : adds an item to the queue along with priority.
- peek: returns the item in the queue with the highest priority without deleting the node
- remove/DeQueue: removes and returns the item in the queue with the highest priority
Characteristics of a Priority Queue
A priority queue is an extended version of a queue having following characteristics:
- Every element in a priority queue enqueue with priority value associated with it.
- The element with the higher priority will be removed/dequeue first.
- When two elements have same priority value, then dequeue by FIFO principle.
Implementation of Priority Queue
Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary search tree. Among these data structures, heap data structure provides an efficient implementation of priority queues.
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.
Comparative analysis
A comparative analysis of different implementations of priority queue is given below.
Operations | peek | insert | delete |
Linked List | O(1) | O(n) | O(1) |
Binary Heap | O(1) | O(log n) | O(log n) |
Binary Search Tree | O(1) | O(log n) | O(log n) |