LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 8 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What is a priority queue, and which operations does it offer?

Answer

arrival order 3 · arm 1 · heart 5 · ankle 1st 2nd 3rd 1 · heart the heart attack arrived 2nd, goes 1st key value key = priority · value = payload priority queue 3 · arm 5 · ankle 1 · heart insert(3, arm) · insert(1, heart) · insert(5, ankle) 1 · heart served removeMin() → (1, heart) 3 · arm min() → (3, arm) size() → 2 min() looks, removeMin() takes smallest key first, whatever the arrivalkeys only need to be comparable

Explanation in 6 steps

A priority queue stores entries, each a (key, value) pair, and always hands out the entry with the smallest key first. Its core operations are insert(k, v) and removeMin(); min(), size() and isEmpty() complete it.

An ordinary queue serves elements in the order they arrived. A priority queue ignores arrival order: the key of each entry is its priority, and whatever entry has the smallest key is served next, no matter when it came in. The value is the payload that travels with it (a name, a task, an order).

Operation What it does
insert(k, v) adds a new entry (k, v)
removeMin() removes the entry with the smallest key and returns it
min() returns the entry with the smallest key without removing it (like a stack's top() next to pop())
size(), isEmpty() number of entries / whether there are none

Keys must be comparable, either naturally (numbers, strings) or through a comparator supplied to the queue. That total order is all the queue needs to know.

Typical uses are anything where "most urgent first" beats "first come, first served": the best bid in an auction, the best price in a stock exchange order book, a waiting system such as hospital triage or a process scheduler.

Tip: "smallest key = highest priority" is a convention. A queue that serves the largest key first works identically with the comparison reversed.

Go deeper:

  • doc Priority queue — the abstract data type, its implementations and its applications, from Dijkstra to Huffman coding.
  • doc java.util.PriorityQueue — the standard Java heap-based priority queue, with the cost of each method.
  • doc Python heapq — the standard-library min-heap on a plain list, with notes on priority-queue use.
or press any other key

Question

Starting from an empty priority queue, what does each min() / removeMin() return in this sequence: insert(5,A), insert(9,C), insert(3,B), min(), removeMin(), insert(7,D), removeMin(), removeMin(), removeMin(), removeMin()?

Answer

the queue insert(5,A) insert(9,C) insert(3,B) 5 · A 9 · C 3 · B no order inside: only the key will matter min() →3 · B 3 · B min() only looks removeMin() →3 · B 5 · A 9 · C removeMin() takes it out insert(7,D) 7 · D inserted after (9,C) removeMin() removeMin() →5 · A 9 · C → 7 · D still waiting only the key decides, never arrival removeMin() removeMin() →9 · C →null empty empty queue: removeMin() → null 3B · 3B · 5A · 7D · 9C · nullthe key decides, never the arrival time

Explanation in 7 steps

min() → (3,B); then removeMin() returns (3,B), (5,A), (7,D), (9,C), and finally null because the queue is empty.

Operation Returns Contents afterwards
insert(5,A) {(5,A)}
insert(9,C) {(5,A), (9,C)}
insert(3,B) {(3,B), (5,A), (9,C)}
min() (3,B) unchanged
removeMin() (3,B) {(5,A), (9,C)}
insert(7,D) {(5,A), (7,D), (9,C)}
removeMin() (5,A) {(7,D), (9,C)}
removeMin() (7,D) {(9,C)}
removeMin() (9,C) {}
removeMin() null {}

Two things to notice. First, (7,D) was inserted after (5,A) and (9,C), yet it overtakes (9,C): only the key decides, never the arrival time. Second, min() only looks, so the next removeMin() returns the same entry.

The contents column is written as a sorted list for readability, but that is not a requirement. A priority queue may organise its entries internally however it likes (unsorted list, sorted list, heap); the only promise is that removeMin() returns the entry with the smallest key.

or press any other key