Choosing a Priority Queue
A priority queue is a special instance of a queue where the storage order is based on the priority of the items inside. This is typically used for scheduling algorithms, making some items more important in the schedule than others.
There are several ways to get a priority queue in Python. You can use the built-in list
type combined with the sort()
function, to sort based on priority. Or, instead of using .sort()
you can use bisect.insort()
that does insertion sorting. Alternatively, the heapq
library provides ways of storing a max-binary-heap in a list
, where the priority of the item is the sorting criteria in the heap.
All of the above methods are not thread safe. For a thread safe implementation, queue.PriorityQueue
is an implementation of heapq
with atomic locking.
Here are resources and additional documentation about bisect, sorting algorithms, heapq, and PriorityQueue:
- bisect – Array bisection algorithm | Python Documentation
- Sorting Algorithms in Python | Real Python Article
- Binary heap | Wikipedia Article
- heapq – Heap queue algorithm | Python Documentation
- queue – A synchronized queue class: PriorityQueue | Python Documentation
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
The course is the third part of an ongoing series, exploring how to find the right Data Structure for your projects. The first course is Dictionaries and Arrays: Selecting the Ideal Data Structure and the second course is Records and Sets: Selecting the Ideal Data Structure.
00:00
In the previous lesson, I showed you heapq
and the PriorityQueue
object. In this lesson, I’ll wrap up the section on priority queues, giving guidance on how to choose between the different types.
00:12
If you’re just doing something small, a list
and the .sort()
method might be good enough. As you start to manage more data and need performance, insort()
might be a better choice, but it still has the O(n) insertion problem of a list
.
00:27
You can get much better performance by moving to the heapq
library, but it restricts the interface a bit. All three of these methods share the problem that you can always accidentally mess up your priority list by not using one of the chosen methods on an insert, damaging your prioritization.
00:44
You can use the PriorityQueue
object from the queue
library to solve this problem, or to solve the issue of atomicity in concurrent code, but the locking capabilities it has introduces a performance overhead.
00:58
Here are the relevant places in the docs for the bisect
library, where insort()
lives, heapq
, and the PriorityQueue
object in the queue
library.
01:11 For further depth on different sorting algorithms and their performance, you can read this article. Or if you want to become an expert on binary heaps, there’s a load of content at Wikipedia.
01:24 If you’ve been here from the beginning, this three-part course has been a long journey. I hope it was valuable for you. Thanks for your attention.
Become a Member to join the conversation.