Back to Data Structures & Algorithms
2026-03-036 min read

Priority Queue (Data Structures & Algorithms)

Learn Priority Queue (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on Priority Queues, an essential data structure and algorithm concept that plays a crucial role in many programming scenarios. In this lesson, we will delve into the world of priority queues using Python as our primary language.

The Importance of Priority Queues

In numerous situations, handling tasks or items based on their importance or urgency is vital. For instance, when processing multiple jobs in a computer operating system, it's essential to execute high-priority tasks before low-priority ones. Priority queues help manage such scenarios efficiently by maintaining an ordered collection of elements where each element has a unique priority level.

Prerequisites

Before diving into the core concept, ensure you have a strong understanding of the following topics:

  1. Basic Python syntax and control structures (if-else statements, for loops, while loops)
  2. Data Structures: Lists, Tuples, Dictionaries
  3. Functions and Methods in Python
  4. Understanding of Big O Notation
  5. Familiarity with basic sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort
  6. Knowledge of heap data structures (max-heap and min-heap)
  7. Understanding of recursive functions
  8. Familiarity with the concept of binary trees

Core Concept

A Priority Queue is a data structure that follows the queue principle (First-In-First-Out - FIFO) but maintains an additional property: elements are ordered based on their priority values. In a priority queue, higher priority items are processed before lower priority ones.

Implementing a Priority Queue in Python

To create a priority queue in Python, we can use the heapq module, which provides efficient implementations of heap data structures. Here's an example implementation:

import heapq

def create_priority_queue():
return []

def enqueue(pq, item, priority):
heapq.heappush(pq, (-priority, item))

def dequeue(pq):
return heapq.heappop(pq)[1]

def size(pq):
return len(pq)

def update_priority(pq, item, new_priority):
for i, (_, current_item) in enumerate(pq):
if current_item == item:
heapq.heapreplace(pq, (-new_priority, item), pq.pop(i))
break

def contains(pq, item):
return any((current_item == item) for _, current_item in pq)

In this implementation, we create a priority queue as an empty list and use the heapq.heappush() function to add items with their respective priorities. The negative sign before the priority ensures that higher priority items are at the beginning of the list (i.e., lower indexes). To dequeue an item, we use heapq.heappop(), which removes and returns the highest priority item. We also provide functions to update an item's priority and check if a specific item is present in the queue.

Priority Queue Operations

  • enqueue(pq, item, priority): Adds an item with a specific priority to the priority queue.
  • dequeue(pq): Removes and returns the highest priority item from the priority queue.
  • size(pq): Returns the number of items in the priority queue.
  • update_priority(pq, item, new_priority): Updates an item's priority in the priority queue.
  • contains(pq, item): Checks if a specific item is present in the priority queue.

Worked Example

Let's consider a scenario where we have multiple tasks with varying priorities, and we need to execute them efficiently. Here's how you can implement this using our priority queue:

def main():
pq = create_priority_queue()

Adding tasks with their respective priorities

enqueue(pq, ("Task1", 3), 2)

enqueue(pq, ("Task2", 5), 4)

enqueue(pq, ("Task3", 1), 1)

enqueue(pq, ("Task4", 4), 3)

while size(pq) > 0:

print(dequeue(pq))

main()


Output:

("Task3", 1)

("Task1", 3)

("Task4", 4)

("Task2", 5)

Common Mistakes

  1. Adding items without specifying their priorities: Always remember to provide a priority for each item when enqueuing.
  2. Using the wrong comparison operator for priorities: Use < instead of > when comparing priorities, as higher priorities should have lower values.
  3. Not handling empty priority queues properly: Check if the priority queue is empty before attempting to dequeue an item.
  4. Incorrectly implementing a custom priority queue without considering edge cases like deleting items with specific priorities or updating item priorities.
  5. Misunderstanding the difference between a max-heap and a min-heap, leading to incorrect implementation of a priority queue.
  6. Not taking into account the time complexity when choosing between a min-heap and a max-heap for specific use cases.
  7. Failing to optimize the priority queue for real-world scenarios that require more advanced features like dynamic priority changes or efficient removal of items with specific priorities.

Practice Questions

  1. Implement a function to find the kth smallest element in a priority queue.
  2. Write a program that sorts n numbers using a priority queue and compare its performance with other sorting algorithms like quicksort, mergesort, etc.
  3. Implement a job scheduler using a priority queue where jobs have deadlines and priorities based on their profits. The goal is to maximize the total profit while meeting all deadlines.
  4. Compare the efficiency of a min-heap and max-heap in terms of time complexity for various operations like insert, delete_min/delete_max, and update.
  5. Implement a custom priority queue using a binary heap without using the heapq module.
  6. Optimize the priority queue implementation to handle dynamic changes in item priorities efficiently.
  7. Write a program that simulates a real-world scenario where tasks have deadlines and varying processing times, and you need to schedule them to minimize the total completion time while meeting all deadlines.
  8. Implement a function to merge two priority queues into one while preserving their order.
  9. Discuss the trade-offs between using a priority queue and other sorting algorithms like quicksort, mergesort, etc., for specific use cases.
  10. Analyze the time complexity of various operations in a priority queue under different scenarios (e.g., best case, average case, worst case).

FAQ

How do I implement a custom priority queue in Python without using the heapq module?

You can create a custom min-heap by maintaining an array where each index represents a node, and its left child is at 2i and right child is at 2i+1. Use binary heap operations like insert, delete_min, and decrease_key to manage the priority queue efficiently.

Can I use a priority queue for sorting large datasets?

While a priority queue can be used for sorting, it may not be the most efficient choice for sorting large datasets due to its high time complexity (O(n log n) in the worst case for heap sorts). Other algorithms like quicksort, mergesort, and radix sort are more suitable for large datasets. However, if you need to process items based on their priorities, a priority queue can be an efficient choice.

What's the difference between a priority queue and a max-heap?

A priority queue is an abstract data type that maintains elements with associated priorities, while a max-heap is a specific implementation of a complete binary tree where each node's key value is greater than or equal to its children's key values. A priority queue can be implemented using a min-heap (where the key value is less than or equal to its children's key values) or a max-heap, depending on the specific use case and priority ordering requirements.

What are some real-world applications of priority queues?

Priority queues have numerous applications in various fields such as computer science, artificial intelligence, operations research, and economics. Some examples include job scheduling, network routing, resource allocation, and game theory. They can also be used for implementing algorithms like Dijkstra's shortest path algorithm, A\* search, and Huffman coding.

Priority Queue (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn