Data Structures

Priority Queue in Data Structure

Priority Queue is a linear data structure that has the priority associated with each element. So, the elements are served in the order of highest to lowest priority. If multiple elements have the same priority, they are served as per their original order.

Representation

Ordered Queue (for Faster Deletion)

The elements are enqueued in the order of 10, 20, 30, 40, and 50. They are sorted as per their priority during the Enqueue operation. So, the highest priority element is quickly served by the Dequeue operation from the Front side.

Priority Queue Representation (Ordered)
Priority Queue Representation (Ordered)
Unordered Queue (for Faster Insertion)

The enqueue order 10, 20, 30, 40, and 50 is retained in the Unordered queue as shown below. The dequeue operation serves the highest priority element by searching the entire Queue. So, the dequeue operation is costlier than the enqueue operation.

Priority Queue Representation (Unordered)
Priority Queue Representation (Unordered)
Properties
  • Front: Front side of the queue
  • Rear: Rear side of the queue
  • Element: The actual data (represented as 50, 20, 40, 10, and 30)
  • Priority: Priority of the element (represented as 1-High, 2-Mid, and 3-Low)
  • Index: Array index

Priority Queue Operations and Time Complexity

OperationShort DescriptionTime
Complexity
Ordered
Time
Complexity
Unordered
Space
Complexity
EnqueueInsert a new elementO(n)O(1) O(n)
DequeueDelete the elementO(1)O(n) O(n)
PeekReturns the front element without removingO(1)O(n)O(n)
IsFullCheck if the Queue is fullO(1)O(1)O(n)
IsEmptyCheck if the Queue is emptyO(1)O(1)O(n)
SearchSearch the element by valueO(n)O(n)O(n)
Time and Space complexity of Priority Queue operations

Implementations of Priority Queue

Priority Queue can be implemented using Array, Linked List, or Heap. However, we need to choose the right implementation based on the requirements.

ImplementationWhen to Use
Priority Queue Implementation using ordered ArrayPrefer to work with a fixed amount of data.
Also to support faster retrieval or dequeue operation.
Priority Queue Implementation using unordered ArrayTo work with a fixed amount of data.
Faster to enqueue the elements but slower to dequeue.
Priority Queue Implementation using ordered Linked ListAlternatively, you can prefer linked list implementation to work with large and dynamic data. Use an ordered linked list for faster dequeue operation.
Priority Queue Implementation using unordered Linked ListTo work with large and dynamic data.
Faster to enqueue the elements but slower to dequeue.

Advantages and Disadvantages

Advantages

  • Priority Queue is unique and can serve the elements based on their priority.

Disadvantages

  • The unordered implementation always requires the full search to dequeue the highest priority element.
  • Array implementation requires swapping the elements for enqueue or dequeue operation.

Applications of Priority Queue in Data Structure

When to use Priority Queues

Prefer priority queues when your Application wants to provide preference to certain tasks. Also, use priority queues when you have limited resources shared by multiple tasks.

Realtime Applications

  • CPU Scheduling & Interrupt Handling – Operating systems use priority queues to execute high-priority tasks first. So, the interrupt requests are enqueued with high priority.
  • Load Balancing is another application of priority queues. This helps to distribute the load based on priorities.
  • Huffman Coding – A data compression algorithm that requires a priority queue to work.
Copyright copyright 2024 Simplerize