CS 361: Algorithms and Data Structures
Homework 3


Priority Queue Programming Assignment

This homework assignment is entirely a programming assignment. It is required that you work with a partner.

In this assignment, you will create a max priority queue that stores pairs (p, e) which consist of a priority and an element. The element is to be stored in the priority queue with the associated priority. To simplify this assignment, all priorities and elements will be integers.

Your priority queue will be implemented using a max heap which (as we discussed in class) is itself implemented using an array. Your priority queue should support the basic methods:

  • push - add an element to the priority queue
  • pop - remove the highest priority element from the queue
  • topElement - return (but don't remove) the element with the highest priority
  • topPriority - return (but don't remove) the highest priority value
  • clear, isEmpty
In addition, your priority queue should support three additional methods:
  • changePriority(int element, int newPriority) - change the priority of an existing element in the queue
  • getPriority(int element) - return the priority for an existing element in the queue
  • isPresent(int element) - returns true if an element exists in the queue and false otherwise

Notice that these three additional methods require you to be able to find an arbitrary element in the heap (i.e. in the array). One way to accomplish this is to do a linear search through the array every time one of these methods is called. However, that requires O(n) work where n is the number of elements in the array.

A more efficient approach is to keep track of the index into the array for every element using a map data structure. The input to the map would be the element and the output would be the index of that element in the heap array. You will use Java's HashMap class for this map data structure. According to the Java documentation for the HashMap class, "This implementation provides constant-time performance for the basic operations (get and put)".

Consider the following example below which shows the state of the heap and the map after pushing the following pairs: (priority=10, element=1), (priority=0, element=2), (priority=1000, element=3), (priority=47, element=4)

The starter code for this assignment can be downloaded here. In addition, complete Java documentation for the priority queue class can be found here.

Here are some final comments that you should read before getting started:

  • A precondition is a statement of what should be true for a method to work correctly. You should check that each precondition holds true inside the method (i.e. using an if-statement). If it is not true, please throw an AssertionError.

  • A Pair class has been included in the starter code. This class uses generic types to implement a generic pair data structure that contains only two values: p and e. For our purposes, p is the priority and e is the element. Example code for creating/using Pairs is shown below:

    • Pair temp = new Pair<>(priority, element); // creates a new pair
    • int priority = temp.priority; // access the priority
    • int element = temp.element; // access the element

  • Here are links to some useful JavaDoc pages: ArrayList and HashMap.

Grading

Your code will be graded on the following:

  • The functionality of your PriorityQueue class. I will test each method to make sure it works correctly (including throwing errors when appropriate).

  • Your adherence to the course style guide

  • The running time of each method -- i.e., did you implement each method as efficiently as possible? For example, you should not be doing linear scans of the underlying array but instead using a HashMap for constant time lookup.


  • Submission Instructions

    Your Java code should be submitted in a zipped directory. You and your partner only need to submit one directory. Please make sure that you put both of your names in the class comment for PriorityQueue.java so I know who you worked with.

    Your code should compile with no errors and it should obey the Java API provided above.

    Please upload your zipped directory to Moodle


    Last modified: Fri Jan 24 10:58:47 PST 2014