Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A generic priority queue for Python

Tags:

python

queue

I need to use a priority queue in my Python code, and:

  • am looking for any fast implementations for priority queues
  • optimally, I'd like the queue to be generic (i.e. work well for any object with a specified comparison operator).

Looking around for something efficient, I came upon heapq, but:

  • I'm looking for something faster than heapq, which is implemented in native Python, so it's not fast.
  • It looks good, but seems to be specified only for integers. I suppose it works with any objects that have comparison operators, but it doesn't specify what comparison operators it needs.
  • Update: Re comparison in heapq, I can either use a (priority, object) as Charlie Martin suggests, or just implement __cmp__ for my object.
like image 709
Eli Bendersky Avatar asked Jan 02 '09 19:01

Eli Bendersky


People also ask

Is there a priority queue in Python?

The Python priority queue is built on the heapq module, which is basically a binary heap. The get command dequeues the highest priority elements from the queue. Priority-object pairs can also be inserted into the queue. This way every task can be associated with a priority.

How do you use priority queue in Python?

To implement a priority queue in Python, we have to declare an empty Python list into which elements are inserted using the append() method of list class. The list is then sorted in ascending order. The While loop is used to retrieve the elements using the pop() method.

What is Heapdict in Python?

Project description. heapdict implements the MutableMapping ABC, meaning it works pretty much like a regular Python dict. It's designed to be used as a priority queue, where items are added and consumed as follows: hd = heapdict() hd[obj1] = priority1 hd[obj2] = priority2 # ... ( obj, priority) = hd.popitem()

What is priority queue with example?

An ascending order priority queue gives the highest priority to the lower number in that queue. For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order. The new list is as follows: 4, 8, 12, 20.


2 Answers

You can use Queue.PriorityQueue.

Recall that Python isn't strongly typed, so you can save anything you like: just make a tuple of (priority, thing) and you're set.

like image 56
Charlie Martin Avatar answered Sep 18 '22 19:09

Charlie Martin


When using a priority queue, decrease-key is a must-have operation for many algorithms (Dijkstra's Algorithm, A*, OPTICS), I wonder why Python's built-in priority queue does not support it. None of the other answers supply a solution that supports this functionality.

A priority queue which also supports decrease-key operation is this implementation by Daniel Stutzbach worked perfectly for me with Python 3.5.

from heapdict import heapdict  hd = heapdict() hd["two"] = 2 hd["one"] = 1 obj = hd.popitem() print("object:",obj[0]) print("priority:",obj[1])  # object: one # priority: 1 
like image 21
Guy s Avatar answered Sep 19 '22 19:09

Guy s