Search⌘ K
AI Features

Priority Queues

Explore the concept of priority queues where elements are processed based on priority rather than arrival time. Understand how priority queues differ from standard FIFO queues, discover common implementations including binary heaps, and see their applications in CPU scheduling, shortest path algorithms, and network routing. This lesson helps you grasp how priority queues improve system responsiveness and efficiency in real-world problems.

A standard queue enforces strict arrival-order processing by design. It processes items strictly in first in, first out (FIFO) order. However, strict ordering is not always suitable for system requirements. For example, an operating system managing multiple processes may have a critical system process and a low-priority background task in the ready queue. Processing the background task first due to arrival order can delay critical tasks and impact system responsiveness.

The same problem appears in hospital triage, network traffic management, and shortest-path algorithms. In all of these cases, the order in which work should be handled depends on its importance, not on when it arrived. The priority queue was designed to address exactly this.

What is a priority queue?

A priority queue is a special type of queue in which each element has a priority. Elements are processed based on priority rather than in the order they were inserted.

  • In a max-priority queue, the element with the highest priority value is removed first.

  • In a min-priority queue, the element with the lowest priority value is removed first.

The choice between a max-priority queue and a min-priority queue depends on your application’s specific requirements.

How it differs from a normal queue

In a normal queue, the removal order is determined entirely by arrival time. The first element inserted is always the first removed. In a priority queue, an element that arrives later may be removed before an ...