Docsity

Project 4: Priority Queue - Algorithms and Data Structures | CS 2420, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS 2420 – Summer 2008
Program 4 – 20 points Due July 9, 2008
Priority Queue
The PQueue Interface
The priority queue will initially be a collection of integers, where the integer itself is used as the priority.
Larger integers should be considered higher priority than smaller ones and, thus, removed before smaller
values. Here are the functions that make up the methods of the priority queue:
class PQueue {
public:
PQueue();
int size();
void insert(int elem);
int deleteMax();
int sizeOf();
private:
// implementation dependent member variables and helper methods
};
insert is used to add a new element to the priority queue. deleteMax returns the value of highest priority
(i.e., largest) element in the queue and removes it. For the detailed specification of the behavior and
usage of these functions, see the starter files in prog4.zip.
Implementing the priority queue
There are many data structures you could choose to represent and manipulate a priority queue, with
various tradeoffs between the amount of memory required, speed of the insert and extract operations,
complexity of code to get the job done, etc. The first implementation will store the priority queue elements
in an unsorted array. The second implementation will represent the queue as a sorted linked list. The third
implementation is binomial queue.
Unsorted Vector implementation
The unsorted vector implementation is already written and given to you in the starter project. You do not
have to write much code for this implementation, just go over and be familiar with the code provided. Its
insert strategy is simply to add the new element to the end. When it comes time to deleteMax the
maximum element, it performs a linear search to find it. You will run time trials on this implementation as it
stands to get an idea of its strengths and weaknesses and become familiar with the test and time trial
functions we've provided for you.
Sorted linked list implementation
The sorted linked list implementation is also given to you so you can observe and test it. The linked list
implementation is a singly linked list of values, where the values are kept in reverse-sorted (i.e., largest to
smallest) order to facilitate finding and removing the largest element quickly. However, inserting a value is
a bit trickier because of the need to search for the right position to insert the new value. You will also run
time trials on this implementation as part of your analysis.
Binomial Queue Implementation
For this assignment, you will implement a priority queue using a binomial queue.
The forest must be printable in a readable format so that one can tell the binomial queues have been
implemented correctly. The printList for the Binomial Queue MUST print the trees “prettily” so we can tell
the binomial forest is working correctly.
pf2

Partial preview of the text

Download Project 4: Priority Queue - Algorithms and Data Structures | CS 2420 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS 2420 – Summer 2008

Program 4 – 20 points Due July 9, 2008

Priority Queue

The PQueue Interface The priority queue will initially be a collection of integers, where the integer itself is used as the priority. Larger integers should be considered higher priority than smaller ones and, thus, removed before smaller values. Here are the functions that make up the methods of the priority queue:

class PQueue { public: PQueue(); int size(); void insert(int elem); int deleteMax(); int sizeOf(); private: // implementation dependent member variables and helper methods }; insert is used to add a new element to the priority queue. deleteMax returns the value of highest priority (i.e., largest) element in the queue and removes it. For the detailed specification of the behavior and usage of these functions, see the starter files in prog4.zip.

Implementing the priority queue There are many data structures you could choose to represent and manipulate a priority queue, with various tradeoffs between the amount of memory required, speed of the insert and extract operations, complexity of code to get the job done, etc. The first implementation will store the priority queue elements in an unsorted array. The second implementation will represent the queue as a sorted linked list. The third implementation is binomial queue.

Unsorted Vector implementation The unsorted vector implementation is already written and given to you in the starter project. You do not have to write much code for this implementation, just go over and be familiar with the code provided. Its insert strategy is simply to add the new element to the end. When it comes time to deleteMax the maximum element, it performs a linear search to find it. You will run time trials on this implementation as it stands to get an idea of its strengths and weaknesses and become familiar with the test and time trial functions we've provided for you.

Sorted linked list implementation The sorted linked list implementation is also given to you so you can observe and test it. The linked list implementation is a singly linked list of values, where the values are kept in reverse-sorted (i.e., largest to smallest) order to facilitate finding and removing the largest element quickly. However, inserting a value is a bit trickier because of the need to search for the right position to insert the new value. You will also run time trials on this implementation as part of your analysis.

Binomial Queue Implementation For this assignment, you will implement a priority queue using a binomial queue.

The forest must be printable in a readable format so that one can tell the binomial queues have been implemented correctly. The printList for the Binomial Queue MUST print the trees “prettily” so we can tell the binomial forest is working correctly.

Timing Summarize your implementation results using this format in excel spreadsheet. Submit the results as “results.xls” with your assignment. Fast operations may not even register as taking any time at all given the coarse granularity of the system clock, but slower operations should register as you increase the priority queue size. Run the performance trial on at least three different sizes—try something like N = 10000, so you record times for 10000 (=N), 20000 (=2N), and 40000 (=4N). You may have to use a larger value of N to register the time on faster computers. Record the time reported for the given operations and memory used. Analyze your algorithms and determine the Big-Oh analysis of the worst-case performance of each operation as a function of N, the number of elements in the PQueue. You will have to insert more timings in order to get all this information. The PQSort involves inserting all N items and then deleting N items (so you have a completely sorted list).

Unsorted Vector Sorted Linked List Binomial Queue Memory Used N Memory Used 2N Memory Used 4N insert N Insert 2N insert 4N insert Big-Oh deleteMax N deleteMax 2N deleteMax 4N deleteMax Big-Oh PQSort N PQSort 2N PQSort 4N PQSort Big-Oh