Priority Queue using Queue and Heapdict module in Python


The priority Queue is the abstract data type which is similar to the queue or stack, in which each element is attached with a priority. Here the priority determines the order of the elements to be dequeued from the queue and the higher priority elements are dequeued before the elements with the lower priority.

The priority queues will be implemented using different data structures such as heaps, arrays or balanced trees. The most commonly used implementation is the heap, which is the binary tree based data structure having the value of each node greater than or equal to the values of the child.

Types of Priority Queues

Mainly there are two types of priority queues min priority queue and the max priority queue. In min priority queue the element having the lower priority will be dequeued first and in max priority queue the element with the higher priority will be dequeued first. The Priority queue is used in wide range of applications such as task scheduling, network routing, job prioritization and Huffman coding.

Priority Queues using the heapdict module

In python we have the modules heapq, queue and heapdict. In this article let’s see how we can implement the priority queue by using the queue and the heapdict modules. In heapdict module we have the methods push and pop which are used to add the item into the queue based on the priority and to remove the item from the queue.

The push method takes the items and the priority as the input arguments and also adds the item to the priority queue and to the heapdict. The pop method removes the item and returns the removed item as well as removed the item and the priority of the item in the heapdict.

Example

In this example we will create the class with the name PriorityQueueWithHeapdict with the function push to add the items into the queue based on the given priority. This function takes the item and the priority of the items as the input arguments. Next we will add the items to the priority queue by using the defined class and function.

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

q = PriorityQueueWithHeapdict()
q.push('task 1', 3)
q.push('task 2', 1)
q.push('task 3', 2)
print("The items added to the priority queue and heapdict based on the priority")

Output

The items added to the priority queue and heapdict based on the priority

Example

In the previous example we created the class with the push function, now we are going to create the pop function within the same class. The pop function is used to remove the item which has the highest priority and also remove the items in the 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

   def pop(self):
      priority = self._queue.get()
      item = self._heapdict[priority]
      del self._heapdict[priority]
      return item

   def is_empty(self):
      return self._queue.empty()
q = PriorityQueueWithHeapdict()
q.push('task 1', 3)
q.push('task 2', 1)
q.push('task 3', 2)

while not q.is_empty():
   print(q.pop())

Output

task 2
task 3
task 1

Updated on: 19-Oct-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements