ExtrinsicMinPQ

The main task for this assignment will be to develop an array-based implementation of the ExtrinsicMinPQ interface. The priority for each item will be extrinsic to the object–rather than relying on some sort of comparison function to decide which item has less priority than another, we manually assign priorities by passing in numbers along with the items.

Info

Unlike the priority queue presented in class, ExtrinsicMinPQ can only contain unique items. While there cannot be multiple equals items, there can be multiple items with the same priority value. In this case, you can return any item with the minimum priority value.

Signature Description
void add(T item, double priority) Adds an item with the given priority value.
boolean contains(T item) Returns true if the PQ contains the given item; false otherwise.
T peekMin() Returns the item with least-valued priority.
T removeMin() Removes and returns the item with least-valued priority.
void changePriority(T item, double priority) Changes the priority of the given item.
int size() Returns the number of items in the PQ.
boolean isEmpty() Returns true if the PQ is empty, false otherwise.

Reference implementation

Info

This implementation does not throw the correct exception for add when adding duplicate items, since that would make the class too slow to be usable when comparing to our ArrayHeapMinPQ.

The starter code includes a working NaiveMinPQ. The implementation is called “naive” because it uses a simple and slow implementation of the ExtrinsicMinPQ interface. peekMin(), removeMin(), contains(), and changePriority() use Java’s Stream API to do brute force searches over the entire list of items resulting in runtimes proportional to the length of the list, or Θ(n)\Theta(n).

ArrayHeapMinPQ

Task

Complete ArrayHeapMinPQ by implementing the ExtrinsicMinPQ interface.

An enhanced binary heap priority queue supported by a HashMap that associates each item with its heap index to speed-up various methods. Your ArrayHeapMinPQ must follow each of these invariants:

Info

We have included an invariant checker in our tests to make sure your priority queue follows these invariants and is a proper min heap.

  • Required fields:
    • You must store your min-heap in the field named items. It should be an ArrayList of PriorityNodes, the class used to store items along with their priorities.
    • Your heap ArrayList must start at the index indicated by START_INDEX.
  • Required runtimes (where NN is the heap size):
    • peekMin(), contains(), size() and changePriority() must run in O(logN)\mathcal{O}(\log N) time.
    • add() and removeMin() must run in O(logN)\mathcal{O}(\log N) time except for the rare resize operation.
  • You may not import other priority queue implementations. However, you are allowed to import other built-in Java structures (ex. HashMap, TreeMap, ArrayList, etc.) when implementing ArrayHeapMinPQ.

Note

The O()\mathcal{O}(\cdot) notation only denotes upper bounds. It is possible to implement some methods with better runtimes.

Tips

  • Iterating over your entire heap takes linear time, so make sure not to do this in any method! Likewise, be careful if you use ArrayList methods to make sure they follow the correct runtimes for our priority queue.
  • Sometimes, if some computation is slow and expensive, it’s useful to store some extra information to help speed up that computation at the cost of some extra memory usage.
  • We’ve provided some tests in ArrayHeapMinPQTests, but there are essentially no tests for changePriority, nor are there any tests checking that your heap works at larger sizes. It may be helpful to write some tests for changePriority before you start implementing it, since considering the possible test cases can help you plan out your approach.

Warning

You may not share tests with any other students outside your group—all code in your possession, including tests, should be you and your group’s own work. You may discuss different edge cases and share ideas about test cases, but test code falls under the same academic honesty policy as any other project code.

Tips for writing tests:
  • We provide a PrintHeapDemo class that can print out an array in heap format. You’re welcome to adapt this code into your own for debugging.
  • Outside of the provided invariant checks, we don’t recommend running assertions directly on any of your data structure’s fields as they tend to be too simple or complex to identify the source of a bug e.g. changePriority() correctly sets a node’s priority.
  • Focus on writing tests that evaluate correctness based on the outputs of methods that provide output (e.g. peekMin() and removeMin()).
  • A comprehensive test can be done by randomly calling methods that add(), remove(), and get nodes and seeing if the results returned by our more complex ArrayHeapMinPQ matches that of the simple and straightforward NaiveMinPQ.
  • As seen in previous projects, randomized testing makes it easy to determine if your code is buggy but very difficult to find the actual bug. We recommend creating simple tests to identify the source of a bug.
  • Make sure that your priority values are unique. Duplicate priority values can be returned in any order and the order returned by the NaiveMinPQ might not match the order returned by ArrayHeapMinPQ.

Grading Details

Most of the points for this assignment will be allocated towards evaluating the correctness of ArrayHeapMinPQ, but a few points will be allocated towards efficiency. The stress tests on the grader have 3 sequential sections to test add(), changePriority(), and remove(), respectively (all using the same heap instance). Because the sections run sequentially, there are a few cases that could occur during grading:

  • Your code throws an exception. In this case, all subsequent stress tests are skipped, and you get no points from any stress tests.
  • Your code takes an excessively long time to run. In this case, the stress tests will throw an exception, and you get the same outcome as above.
  • Your code runs, but returns incorrect values in the remove section. In this case, no points will be awarded from the stress tests.
  • Your code runs and returns the correct values in the remove section. In this case, the runtimes for each section will be scored independently.

Submission

If you’re working in a group, the partner who submits must add the other partner as a group member on Gradescope. Here’s a video demonstrating that process. You’ll also need to re-add group members whenever you resubmit to the same assignment, even if you already did so on a previous submission.

Warning

Submitting the same code as another student without using Gradescope’s group feature is considered plagiarism, and may have consequences.