Priority Queue
We'll focus on max-priority queue here, where the higher priority value is served before lower priority value.
Operations
insertextractMaxpeekMax
decreaseKey/increaseKey
Info
Each element has priority (key) and a value.
The element with the higher priority will be processed first.
The decreaseKey requires searching the data structure by the value.
Implementations
When to use PQ?
If we want to find maximum element in an array, we can linear search the array in \(O(n)\) time.
Using a PQ (backed by Binary Heap), we'll first heapify in \(O(n)\),
followed by calling extractMax in \(O(\log n)\).
The implementations have similar runtimes but linear search is way more straightforward to write.
When does PQ truly shine?
I think PQ is really efficient if the data is dynamic (it can change over time).