Priority Queue using Queue and Heapdict module in Python

A priority queue is an abstract data type similar to a regular queue, but each element has an associated priority that determines the order of removal. Elements with higher priority are dequeued before those with lower priority.

Priority queues are commonly implemented using heaps, arrays, or balanced trees. The most efficient implementation uses a heap, which is a binary tree where each node's value is greater than or equal to its children's values.

Types of Priority Queues

There are two main types of priority queues ?

  • Min Priority Queue Elements with lower priority values are dequeued first
  • Max Priority Queue Elements with higher priority values are dequeued first

Priority queues are used in task scheduling, network routing, job prioritization, and Huffman coding algorithms.

Priority Queue Implementation using Queue and Heapdict

Python provides several modules for implementing priority queues: heapq, queue, and heapdict. The heapdict module combines the functionality of a heap with a dictionary, allowing efficient priority-based operations.

The heapdict module provides push and pop methods that add items based on priority and remove the highest-priority item respectively.

Creating a Custom Priority Queue Class

Let's create a custom priority queue class that combines Python's PriorityQueue with heapdict ?

from queue import PriorityQueue
from heapdict import heapdict

class PriorityQueueWithHeapdict:
    def __init__(self):
        self._queue = PriorityQueue()
        self._heapdict = heapdict()
    
    def push(self, item, priority):
        self._queue.put(priority)
        self._heapdict[priority] = item
        print(f"Added '{item}' with priority {priority}")

# Create and test the priority queue
pq = PriorityQueueWithHeapdict()
pq.push('High priority task', 1)
pq.push('Low priority task', 3)
pq.push('Medium priority task', 2)
Added 'High priority task' with priority 1
Added 'Low priority task' with priority 3
Added 'Medium priority task' with priority 2

Adding Pop and Empty Check Methods

Now let's add methods to remove items and check if the queue is empty ?

from queue import PriorityQueue
from heapdict import heapdict

class PriorityQueueWithHeapdict:
    def __init__(self):
        self._queue = PriorityQueue()
        self._heapdict = heapdict()
    
    def push(self, item, priority):
        self._queue.put(priority)
        self._heapdict[priority] = item
    
    def pop(self):
        if self.is_empty():
            return None
        priority = self._queue.get()
        item = self._heapdict[priority]
        del self._heapdict[priority]
        return item
    
    def is_empty(self):
        return self._queue.empty()

# Create priority queue and add tasks
pq = PriorityQueueWithHeapdict()
pq.push('Database backup', 3)
pq.push('System update', 1)
pq.push('Email processing', 2)

# Process tasks in priority order
print("Processing tasks by priority:")
while not pq.is_empty():
    task = pq.pop()
    print(f"Executing: {task}")
Processing tasks by priority:
Executing: System update
Executing: Email processing
Executing: Database backup

Key Features

Method Purpose Time Complexity
push(item, priority) Add item with priority O(log n)
pop() Remove highest priority item O(log n)
is_empty() Check if queue is empty O(1)

Conclusion

Combining Python's PriorityQueue with heapdict creates an efficient priority queue implementation. Lower priority values are processed first, making this ideal for task scheduling and resource management applications.

Updated on: 2026-03-27T15:45:16+05:30

597 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements