← Course home

Lookup and priority · Lesson 33 of 52

Priority queues

Choose a priority-queue operation and explain the heap cost behind it.

Full lessonAbout 10 min

You will learn to map priority-queue operations to heap operations and choose between repeated extraction and sorting once.

Recommended first: Heap operations.

On this page 6 sections

The idea

push 5, then 3, then 4. The first pop returns priority 3 and item b.

A priority queue returns the best priority, not the oldest item and not the newest item. The library's PriorityQueue is a binary min-heap. push(5, a), push(3, b), and push(4, c) leave 3 at the root. The first pop returns the pair 3, b. Later pops return 4, c and then 5, a. Each push returns a token. decrease_key uses that token to find the heap index without scanning. The heap stores priority, token, and item. The token breaks ties so two equal priorities stay distinct. This is the structure Dijkstra and Prim need when they repeatedly ask for the next smallest tentative weight. Sorting the whole set once is cheaper when you need every item in order and you will not insert anything in between.

A worked trace

StepStateWhat changed
Pushpriority = 5Push priority 5 for item a.
Pushpriority = 3Push priority 3 for item b.
Pushpriority = 4Push priority 4 for item c.
Popitem = bThe first pop returns priority 3 and item b.

Why it is correct

The heap invariant says every parent priority is less than or equal to its children. push appends and sifts up until the invariant holds. pop swaps the root with the last item, removes the old root, and sifts down. The item that leaves is the minimum because the root is the minimum. decrease_key is allowed to move a priority down, toward a better min, and then sifts up. An equal priority is not a decrease. The map from token to index is updated on every swap, so the next decrease still finds the item.

A common mistake

Push order is not pop order. The smallest priority leaves first.

What it costs

push and pop are logarithmic in the number of stored items. The token map makes the find step constant time, so it does not hide a linear scan inside the logarithmic sift. Extra memory is the heap array plus the map, both linear. n pushes followed by n pops are n log n, the same family as heapsort. Prefer the queue when the set of candidates changes between extractions. Prefer a sort when the set is fixed. push and pop are Θ(log n) time, and extra memory is Θ(n).

Run it

python
from bitsandbytes.heaps.priority_queue import PriorityQueuequeue = PriorityQueue()queue.push(5, 'a')queue.push(3, 'b')queue.push(4, 'c')print(queue.pop())# prints: (3, 'b') 

Practice and next step

Open Graph representations next. Graph representations decide whether neighbor scans read a list or a matrix row.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.heaps.priority_queue

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Pop the smallest priority

push 5, then 3, then 4. The first pop returns priority 3 and item b.

priority
5

State

  1. 05comparing

Push priority 5 for item a.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Priorities 5, 3, and 4 are pushed. Which item leaves first?
2You will insert more items between extractions. Which structure matches that workload?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.