Package graph

Class MinPQueue<KeyType>

java.lang.Object
graph.MinPQueue<KeyType>

public class MinPQueue<KeyType> extends Object
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 Link icon

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final record 
    Pairs an element `key` with its associated priority `priority`.
  • Field Summary Link icon

    Fields
    Modifier and Type
    Field
    Description
    ArrayList representing a binary min-heap of element-priority pairs.
    private final Map<KeyType,Integer>
    Associates each element in the queue with its index in `heap`.
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Create an empty queue.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    private void
    add(KeyType key, double priority)
    Add element `key` to this queue, associated with priority `priority`.
    void
    addOrUpdate(KeyType key, double priority)
    If `key` is already contained in this queue, change its associated priority to `priority`.
    boolean
    Return whether this queue contains no elements.
    double
    Return the minimum priority associated with an element in this queue.
    Return an element associated with the smallest priority in this queue.
    Remove and return the element associated with the smallest priority in this queue.
    int
    Return the number of elements contained in this queue.
    private void
    swap(int i, int j)
    Swap the Entries at indices `i` and `j` in `heap`, updating `index` accordingly.
    private void
    update(KeyType key, double priority)
    Change the priority associated with element `key` to `priority`.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details Link icon

    • heap Link icon

      private final ArrayList<MinPQueue.Entry<KeyType>> 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 Link icon

      private final Map<KeyType,Integer> 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 Link icon

    • MinPQueue Link icon

      public MinPQueue()
      Create an empty queue.
  • Method Details Link icon

    • isEmpty Link icon

      public boolean isEmpty()
      Return whether this queue contains no elements.
    • size Link icon

      public int size()
      Return the number of elements contained in this queue.
    • peek Link icon

      public KeyType 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 Link icon

      public double minPriority()
      Return the minimum priority associated with an element in this queue. Throws NoSuchElementException if this queue is empty.
    • swap Link icon

      private void swap(int i, int j)
      Swap the Entries at indices `i` and `j` in `heap`, updating `index` accordingly. Requires 0 <= i,j < heap.size().
    • add Link icon

      private void add(KeyType key, double priority)
      Add element `key` to this queue, associated with priority `priority`. Requires `key` is not contained in this queue.
    • update Link icon

      private void update(KeyType key, double priority)
      Change the priority associated with element `key` to `priority`. Requires that `key` is contained in this queue.
    • addOrUpdate Link icon

      public void addOrUpdate(KeyType key, double priority)
      If `key` is already contained in this queue, change its associated priority to `priority`. Otherwise, add it to this queue with that priority.
    • remove Link icon

      public KeyType 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.