AlgoViz

Priority queues

medium 18 min

A priority queue serves the item with the highest (or lowest) priority first, whatever order items arrived in.

Where it shows up

The same pattern appears across everyday software.

  • Operating system schedulers

    Schedulers pick the next task to run by priority. Linux's CFS, and its successor EEVDF, keep runnable tasks in a red-black tree ordered by virtual runtime: a priority queue built on a balanced BST, which also supports removing any task.

  • Network routing

    Link-state routing protocols such as OSPF and IS-IS run Dijkstra's shortest-path algorithm on every router, with a min-priority queue of tentative distances.

  • Huffman coding in compression

    Building a Huffman tree repeatedly removes the two least frequent symbols from a min-priority queue and inserts their merged node. DEFLATE (zip, gzip, PNG) uses Huffman codes.

  • Event-driven simulation and timers

    Discrete-event simulators, and timer queues in runtimes such as Go and Node.js, keep pending events in a min-heap keyed by time so the next event to fire is always at the root.

  • Pathfinding in games and maps

    A* search explores the node with the smallest cost-so-far plus estimated remaining cost, taken from a priority queue, which is how game characters and route planners find paths quickly.

  • Load balancing and top-k analytics

    A min-heap keyed by current load hands each new job to the least loaded server, and size-k heaps produce trending lists such as the top 10 searches out of millions.