Package graph
Class MinPQueue<KeyType>
java.lang.Object
graph.MinPQueue<KeyType>
A min priority queue of distinct elements of type `KeyType` associated with (extrinsic) double
priorities, implemented using a binary heap paired with a hash table.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordPairs an element `key` with its associated priority `priority`. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidAdd element `key` to this queue, associated with priority `priority`.voidaddOrUpdate(KeyType key, double priority) If `key` is already contained in this queue, change its associated priority to `priority`.booleanisEmpty()Return whether this queue contains no elements.doubleReturn the minimum priority associated with an element in this queue.peek()Return an element associated with the smallest priority in this queue.remove()Remove and return the element associated with the smallest priority in this queue.intsize()Return the number of elements contained in this queue.private voidswap(int i, int j) Swap the Entries at indices `i` and `j` in `heap`, updating `index` accordingly.private voidChange the priority associated with element `key` to `priority`.
-
Field Details
-
heap
ArrayList representing a binary min-heap of element-priority pairs. Satisfies `heap.get(i).priority() >= heap.get((i-1)/2).priority()` for all `i` in `[1..heap.size())`. -
index
Associates each element in the queue with its index in `heap`. Satisfies `heap.get(index.get(e)).key().equals(e)` if `e` is an element in the queue. Only maps elements that are in the queue (`index.size() == heap.size()`).
-
-
Constructor Details
-
MinPQueue
public MinPQueue()Create an empty queue.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Return whether this queue contains no elements. -
size
public int size()Return the number of elements contained in this queue. -
peek
Return an element associated with the smallest priority in this queue. This is the same element that would be removed by a call to `remove()` (assuming no mutations in between). Throws NoSuchElementException if this queue is empty. -
minPriority
public double minPriority()Return the minimum priority associated with an element in this queue. Throws NoSuchElementException if this queue is empty. -
swap
private void swap(int i, int j) Swap the Entries at indices `i` and `j` in `heap`, updating `index` accordingly. Requires0 <= i,j < heap.size(). -
add
Add element `key` to this queue, associated with priority `priority`. Requires `key` is not contained in this queue. -
update
Change the priority associated with element `key` to `priority`. Requires that `key` is contained in this queue. -
addOrUpdate
If `key` is already contained in this queue, change its associated priority to `priority`. Otherwise, add it to this queue with that priority. -
remove
Remove and return the element associated with the smallest priority in this queue. If multiple elements are tied for the smallest priority, an arbitrary one will be removed. Throws NoSuchElementException if this queue is empty.
-