What is the Python priority queue?

The priority queue is an advanced type of the queue data structure. Instead of dequeuing the oldest element, a priority queue sorts and dequeues elements based on their priorities.

Priority queues are used to handle scheduling problems where some task…


This content originally appeared on DEV Community and was authored by 221910304004

The priority queue is an advanced type of the queue data structure. Instead of dequeuing the oldest element, a priority queue sorts and dequeues elements based on their priorities.

Priority queues are used to handle scheduling problems where some tasks are prioritized over others.

The queue.PriorityQueue Class
Python provides a built-in implementation of the priority queue data structure.

Since the queue.PriorityQueue class needs to maintain the order of its elements, a sorting mechanism is required every time a new element is enqueued.

Python solves this by using a binary heap to implement the priority queue.

The Python priority queue is built on the heapq module, which is basically a binary heap.

For insertion, the priority queue uses the put function in the following way:
pQueue.put(value)

The get command dequeues the highest priority elements from the queue.
Image description

Priority-object pairs can also be inserted into the queue. This way every task can be associated with a priority.

Image description

Time Complexity

Operation: Work-case Time complexity:
Insertion O(logn)
Deletion O(logn)


This content originally appeared on DEV Community and was authored by 221910304004


Print Share Comment Cite Upload Translate Updates
APA

221910304004 | Sciencx (2021-10-18T05:53:24+00:00) What is the Python priority queue?. Retrieved from https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/

MLA
" » What is the Python priority queue?." 221910304004 | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/
HARVARD
221910304004 | Sciencx Monday October 18, 2021 » What is the Python priority queue?., viewed ,<https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/>
VANCOUVER
221910304004 | Sciencx - » What is the Python priority queue?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/
CHICAGO
" » What is the Python priority queue?." 221910304004 | Sciencx - Accessed . https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/
IEEE
" » What is the Python priority queue?." 221910304004 | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/. [Accessed: ]
rf:citation
» What is the Python priority queue? | 221910304004 | Sciencx | https://www.scien.cx/2021/10/18/what-is-the-python-priority-queue/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.