From the course: Python Data Structures and Algorithms

Understand the priority queue data structure - Python Tutorial

From the course: Python Data Structures and Algorithms

Understand the priority queue data structure

- [Instructor] A priority queue is useful in any situation where there are resources needed to be allocated according to some rule or rules of precedence. A classic example is allocation of healthcare resources in a hospital, where the severity of patient's medical needs determines their level of priority. The slide shows some applications of priority queues. This should give you an idea of how useful the data structure is. So they're used a lot in artificial intelligence, including the A* algorithm, which we're going to be studying shortly. Priority queues are used in optimization algorithms. They're used a lot in operating systems with process scheduling. Used, for example, also in bandwidth management, statistical analysis, and spam filtering. So those are just some examples of how useful this data structure is. Now the main operations for a priority queue are get. And that is to retrieve an item with the highest priority, so that's similar to DQ in a queue. Except it's not just to do with the position in the queue, it's to do with the priority associated with the item. Put is equivalent to on queue in a normal queue, whereby you put something into the queue. And is empty, again, it's just very useful when we're iterating through the priority queue to work out if there's anything left. So in short, a priority queue is a collection in which items can be added at any time. But the only item that can be removed is the one with the highest priority.

Contents