Programming Assignment

Due:

Please make sure you adhere to the policies on academic honesty.

Please see the general programming webpage for details about the programming environment for this course, and specifically for directions in how to submit your programming assignment electronically.

The files you need for this assignment can be downloaded here.


Contents:

  • Overview
  • The Drivers
  • Your Tasks
  • Files you will need
  • Files to Submit

  • Overview

    This assignment will serve as our introduction to C++. Our goal will be to provide two different implementations for the Priority Queue ADT.


    Your Tasks

    The file PQ.h is an abstract class which serves as our Priority Queue interface for this assignment. It is modeled very closely after the treatment of priority queue's from the C++ version of our text (section 7.1.3). You will be providing two distinct implementations of the interface, both of which rely on using an array to store the underlying objects.

    This assignment will also make use of the Composition and Comparator design patterns discussed in Section 7.1.4. We review those concepts here:

  • Composition
    The priority queue must keep track of two distinct pieces of information for each item which is inserted into the priority queue: an element, and the key (i.e., priority) of the element. Our implementations could internally keep all of the keys in one array, and all of the elements in a corresponding array, but then we would need to be careful to make sure items are reorganized in tandem.

    A significantly easiser approach is to create a new class, allowing us to store the (key,element) pair as a single object. For this assignment, we have done this for you by defining a new type Item for this purpose. Therefore, you can declare an array of Item's.

    The class is defined to support the following:

  • Given a key k, and element e, a new Item can be declared using the constructor of the form Item(k,e).
  • An Item supports methods key() and element() which return the corresponding key and element. It also will support methods setKey(k) and setElement(e).
  • Comparator
    To design the most general priority queue implementation, we have allowed keys to be of arbitrary type. We also would like to have the user be able to define how to compare the relative priority of two such keys.

    Our solution to this is to have the user define a class which will serve as a Comparator. In this program, we have set up your implementations so that two keys a and b can be compared by using the syntax comp(a,b), which in turn invokes the user's comparison routine. This method will return value -1 if a<b; value 0 if a=b; and value +1 if a > b.

  • Now we are ready to describe your two tasks.

  • SlowPQ

    The first task is to try to get a simple (but correct) implementation of a priority queue, even if it is not very efficient. Specifically, we want you to implement the priority queue using an array of Item's, where items are kept in an arbitrary order. To simplify matters, the priority queue constructor will be sent a parameter cap which is a predefined maximum capacity (thus allowing you to initialize your array).

    Conceptually, this should be very easy. Our purpose for having you do this implementation is more for the challenge of using the required syntax. Also, we will then be able to use this for comparison of efficiency with our second implementation.

    We have started your work by providing two files SlowPQ.h and SlowPQ.tcc which form the outline of your implementation. As you find need for additional member variables for your class, you must add those variables to the header file SlowPQ.h. For all of the required methods, you must provide an implementation in SlowPQ.tcc. If you find need to introduce additional methods for your own use, you will need to declare such a method initially within SlowPQ.h and then implement the method in SlowPQ.tcc.

  • FastPQ

    The second implementation, titled FastPQ, will be based on a heap as specified in Section 7.3.2. Although we intuitively think of a heap as a binary tree, we will ask you to design your implementation based on the array representation of a heap (equivalently the "vector" representation as discussed on page 305 of the text). Again, you will use an array of Item's, but rather than keep them unordered, you will partially order them based on the heap property. Section 7.3.3 of the text gives an implementation of a priority queue with a heap, based on using the BinaryTree interface. Though this is different syntactically than what you must do, you may choose to look closely in understanding the algorithmic process.

    Again, we have started you out with files FastPQ.h and FastPQ.tcc which you should adapt.

  • Experiments

    After you have both of your implementations working (hooray!), we want you to perform some experiments which will be reported in your 'readme' file. This will allow you to compare the efficiency of the two priority queue implementations. Your timing information will be gathered using a driver PQTimer, described in more detail later.

    Report the running times of PQTimer, using the SlowPQ, on values of N as large as your program can handle in a reasonable time limit. Start with N=100, 1000, 10000, 100000, 1000000, 10000000, continuing until the time becomes unreasonable. Record the running times in your readme file.

    Now, repeat these experiments using the FastPQ implementation, again using values of N as large as the program can handle. Record these running times as well.


  • The Drivers

  • PQDriver

    This menu-driven driver should have a very familiar feel. It allows you to test either of your PQ implementations, method-by-method. To run the driver, type ./PQDriver from your command prompt. By default, it reads input from the keyboard. As we have done with earlier assignments, if you wish to type your input into a file, you can use that file to drive the behavior, executing ./PQDriver inputfile.

    (Note: we are not asking you to submit an inputfile for this assignment)

  • PQTimer

    This driver allows us to measure efficiency of your implementations by running a large number of operations. The program will keep track of the running times for you and output the results.

    To run this driver, you should specify two additional command line arguements, as ./PQTimer [pqimplementation] [N], where [pqimplmentation] is either S for SlowPQ or F for FastPQ, and [N] is a positive integer.

    The driver performs two experiments. In the first, it will insert N items, with randomly chosen keys, after which it will call removeMin() N times. In the second experiment, it will start over with an empty queue, then insert N items, then perform an additional N operations which will be a random mix of insertions and removals.


  • Files you will need

    The files you need for this assignment can be downloaded here.


    Files to Submit

    You must submit the files: readme, SlowPQ.h, SlowPQ.tcc, FastPQ.h and FastPQ.tcc.


    Last modified: Friday, 14 November 2003