Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

 

CS241 - Fall 2021 - Assignment #6

 

Collaboration policy:   The goal of assignment is to give you practice in mastering the course material. Consequently, you are encouraged to collabo- rate with others. In fact, students who form study groups generally do better on exams than do students who work alone. If you do work in a study group, however, you owe it to yourself and your group to be prepared for your study- group meeting. Specifically, you should spend at least 30–45 minutes trying to solve each problem beforehand.  If your study group is unable to solve a problem, it is your responsibility to get help from the instructor before the assignment is due.

For this assignment, you can form a team of up to three members. Each team must write up each problem solution and/or code any programming assignment without external assistance, even if you collaborate with others outside your team for discussions.  You are asked to identify your collabo- rators outside your team.  If you did not work with anyone outside your team, you must write  Collaborators:  none.  If you obtain a solution through research (e.g., on the web), acknowledge your source, but write up the solution in your own words. It is a violation of this policy to submit a problem solution that any member of the team cannot orally explain to the instructor. No other student or team may use your solutions; this includes your writing, code, tests, documentation, etc.  It is a violation of this policy to permit anyone other than the instructor and yourself read-access to the location where you keep your solutions.

 

Submission Guidelines:   Your team has to submit your work on Classes (no email) by the due date. Only one submission per team is necessary. For each class in the programming assignments you must use the header template provided in Classes. Make sure that you identify your team members in the header, and any collaborators outside your team, if none, write “none”. Your code must follow the Java formatting standards posted in Classes.  Format will also be part of your grade.  To complete the submission, you have to

upload two files to Classes: your source file and your class file. Your answers to questions that do not require coding must be included in the remarks section of the header.   The  submission will  not  be  accepted  in  any other format.

Style and Correctness:   Keep in mind that your goal is to communicate. Full credit will be given only to the correct solution which is described clearly. Convoluted and obtuse descriptions might receive low marks, even when they are correct. Also, aim for concise solutions, as it will save you time spent on write-ups, and also help you conceptualize the key idea of the problem.

 

 

Assignment 6

Grading Rubric:

Coding:

 

 

Program

characteristic

Program feature

Credit  possible

 

 

Design

30%

Algorithm

30%

 

 

Functionality

30%

Program runs

without errors

20%

 

 

Correct result given

10%

 

Input

15%

User friendly,

typos, spacing

10%

 

 

Values read in

correctly

5%

 

Output

15%

Output provided

10%

 

 

Proper spelling,

spacing, user friendly

5%

 

Format

10%

Documentation: name,    collaborators, header, etc.

5%

 

 

Clarity: comments,

indentation, etc.

5%

 

 

TOTAL

100%

 

 

 

Non-coding:

Embedded in questions.

 

 

1(20)

2(20)

3(20)

4(20)

5(20)

TOTAL(100)

 

 

 

 

 

 

 

 

 

Assignment:

A Priority Queue is a popular data structure that maintains a collection of items with priorities.  At any given time, one can take out of the queue the item with highest priority or increase the priority of any given item. Of course it also includes an operation to insert new items.

Priorities are usually represented with an integer, the smaller the integer the higher the priority.  That is, an item a with priority 1 is the item with highest priority in the queue, whereas an item b with priority 100 has less priority than a. (Notice that priorities do not need to be consecutive.)

A Priority Queue usually has the following operations:

●  Insert(item,key) : that inserts a new item with the given key.

● ExtractMin(): that extracts and returns the item with highest priority.

● DecreaseKey(item,newkey):  that updates the priority of the given item to newkey.

A frequent implementation of a Priority Queue is a data structure called a Binary Heap.  With such implementation, all the above operations take O(log n) in the worst case. Another possible implementation is to maintain the items in a list, sorted by the priorities. Using a Linked List such imple- mentation achieves constant time for ExtractMin(), but Insert(item,key) and DecreaseKey(item,newkey) in the worst case take O(n).

The purpose of this assignment is to evaluate the time performance of these two implementations of Priority Queue.  For the Binary Heap imple- mentation you have to use the Java API PriorityQueue class. For the Link List implementation you have to use the Java API LinkedList class main- taining the order of priorities (increasing order of keys).

The Java API PriorityQueue class is generic with only one parametric data type.   That is, in order to store pairs  (item,priority) one has to define a new class with such pair and establish how the priorities are com- pared.  Given that we are only interested in time performance, we simplify the experiment storing only the priorities as integers.

In our evaluation we will use the Priority Queue to sort items. To do that, we insert n priorities chosen at random and we ExtractMin() repeatedly until the queue is empty. This sorting algorithm is known as Heap Sort.

Specifically, do the following.

 


1.  (20 points) Write a MyPriorityQueue<T> class implemented with a Java API LinkedList<T> where the priorities are maintained in in- creasing order. Your class must include methods Insert(T priority), that adds a new priority to the ordered list, and ExtractMin(), that removes and returns the priority in the front of the list.

2.  (20 points) Write a test program that does the following.

(a) Measure the running time of inserting n (different) integers chosen

at random in an initially empty Java API PriorityQueue<Integer>, followed by emptying the queue by repeatedly extracting the item    with highest priority. Do not display anything but the time taken.

(b) Measure the running time of inserting n (different) integers cho-

sen at random in an initially empty MyPriorityQueue<Integer>, followed by emptying the queue by repeatedly extracting the item with highest priority. Do not display anything but the time taken.

3.  (20  points) Run your program for various values of n and fill this table with your measurements.

 

=

102

103

104

105

106

Java API PriorityQueue<Integer>

 

 

 

 

 

MyPriorityQueue<Integer>

 

 

 

 

 

As usual, adjust your values of n as needed, but make sure you have 4 or 5 measurements for each queue.

4.  (20 points) What are the (big-O) running times you expect for com- pleting the task (inserting the n integers followed by removing the n integers in order) on each queue? Why?  (Partial credit if you give only one. No credit if you do not explain why.)

5.  (20 points) Are your measurements for each queue consistent with your previous answer? Explain why (or why not).  (No credit if you do not use your measurements in the explanation. No credit for compari- son between the queues.)