/* PQTimer.java
A simple driver program to run timing tests on your ArrayPQ
and BinaryHeapPQ.
Programming Assignment #2
*/
import java.util.Iterator;
import java.util.ConcurrentModificationException;
import data_structures.*;
public class PQTimer {
public static void main(String [] args) {
///////////////////////////////////////////////////////////
/// Change this variable to something smaller if timing takes too long.
final int INITIAL_SIZE = 15000;
///////////////////////////////////////////////////////////
final int ITERATIONS = 10000;
final int NUMBER_OF_STEPS = 15;
final int MAX_SIZE = INITIAL_SIZE * NUMBER_OF_STEPS +1;
final int NUMBER_OF_PRIORITIES = 20;
int size = INITIAL_SIZE;
long sequence_number = 0;
long start, stop;
int priority;
PQTestObject [] array = new PQTestObject[MAX_SIZE];
for(int i=0; i < MAX_SIZE; i++)
array[i] = new PQTestObject((int) ((10000*Math.random() %
NUMBER_OF_PRIORITIES) +1), sequence_number++);
for(int j=0; j < 15; j++) {
PriorityQueue<PQTestObject> queue =
new HeapPriorityQueue<PQTestObject>(MAX_SIZE);
queue.clear();
for(int i = 0; i < size; i++)
queue.insert(array[i]);
start = System.currentTimeMillis(); // start the timer
for(int i = 0; i < ITERATIONS; i++) {
queue.insert(array[(int)(100000*Math.random() %
MAX_SIZE)]);
queue.remove();
}
stop = System.currentTimeMillis();
System.out.println("HeapPQ, Time for n=" + size + ": " +
(stop-start));
queue.clear();
queue = new ArrayPriorityQueue<PQTestObject>(MAX_SIZE);
for(int i = 0; i < size; i++)
queue.insert(array[i]);
start = System.currentTimeMillis(); // start the timer
for(int i = 0; i < ITERATIONS; i++) {
queue.insert(array[(int)(100000*Math.random() %
MAX_SIZE)]);
queue.remove();
}
stop = System.currentTimeMillis();
System.out.println("ArrayPQ, Time for n=" + size + ": " +
(stop-start)+"\n");
size += INITIAL_SIZE;
}
}
}
/* PQTestObject.java
A simple testing object that has a priority. The sequence
number in this class is NOT the insertion sequence number
you will have in the BinaryHeapPQ class. It is only used
for verification of correct behavior, and never used in
ordering objects of this class.
*/
public class PQTestObject implements Comparable<PQTestObject> {
private int priority;
private long sequence_number;
public PQTestObject(int p, long s) {
priority = p;
sequence_number = s;
}
public int compareTo(PQTestObject o) {
return priority - o.priority;
}
public String toString() {
...
PQTimer.java A simple driver program to run timing t.docx
1. /* PQTimer.java
A simple driver program to run timing tests on your ArrayPQ
and BinaryHeapPQ.
Programming Assignment #2
*/
import java.util.Iterator;
import java.util.ConcurrentModificationException;
import data_structures.*;
public class PQTimer {
public static void main(String [] args) {
///////////////////////////////////////////////////////////
/// Change this variable to something smaller if timing takes too
long.
final int INITIAL_SIZE = 15000;
///////////////////////////////////////////////////////////
final int ITERATIONS = 10000;
final int NUMBER_OF_STEPS = 15;
final int MAX_SIZE = INITIAL_SIZE * NUMBER_OF_STEPS
+1;
final int NUMBER_OF_PRIORITIES = 20;
int size = INITIAL_SIZE;
long sequence_number = 0;
long start, stop;
int priority;
PQTestObject [] array = new PQTestObject[MAX_SIZE];
2. for(int i=0; i < MAX_SIZE; i++)
array[i] = new PQTestObject((int)
((10000*Math.random() %
NUMBER_OF_PRIORITIES) +1), sequence_number++);
for(int j=0; j < 15; j++) {
PriorityQueue<PQTestObject> queue =
new
HeapPriorityQueue<PQTestObject>(MAX_SIZE);
queue.clear();
for(int i = 0; i < size; i++)
queue.insert(array[i]);
start = System.currentTimeMillis(); // start the timer
for(int i = 0; i < ITERATIONS; i++) {
queue.insert(array[(int)(100000*Math.random() %
MAX_SIZE)]);
queue.remove();
}
stop = System.currentTimeMillis();
System.out.println("HeapPQ, Time for n=" + size + ": " +
(stop-start));
queue.clear();
queue = new
ArrayPriorityQueue<PQTestObject>(MAX_SIZE);
for(int i = 0; i < size; i++)
queue.insert(array[i]);
start = System.currentTimeMillis(); // start the timer
for(int i = 0; i < ITERATIONS; i++) {
3. queue.insert(array[(int)(100000*Math.random() %
MAX_SIZE)]);
queue.remove();
}
stop = System.currentTimeMillis();
System.out.println("ArrayPQ, Time for n=" + size + ": " +
(stop-start)+"n");
size += INITIAL_SIZE;
}
}
}
/* PQTestObject.java
A simple testing object that has a priority. The sequence
number in this class is NOT the insertion sequence number
you will have in the BinaryHeapPQ class. It is only used
for verification of correct behavior, and never used in
ordering objects of this class.
*/
public class PQTestObject implements
Comparable<PQTestObject> {
private int priority;
private long sequence_number;
public PQTestObject(int p, long s) {
priority = p;
sequence_number = s;
}
4. public int compareTo(PQTestObject o) {
return priority - o.priority;
}
public String toString() {
return priority + " " + sequence_number;
}
}
/* PQTester.java
A simple driver program to test your ArrayPQ and
BinaryHeapPQ.
NOTE: Does not test all of the methods in your PQs. The
fact
that your program runs with this driver does not mean that it
is error free.
Requires PQTestObject class
Alan Riggins
CS310 Fall 2012
Programming Assignment #2
*/
import java.util.Iterator;
import java.util.ConcurrentModificationException;
import data_structures.*;
public class PQTester {
public static void main(String [] args) {
final int SIZE = 50;
long sequence_number = 0;
int priority;
5. //////////////////////////////////////////////////////////////////////
/// Select the implementation you wish to test
PriorityQueue<PQTestObject> queue =
new ArrayPriorityQueue<PQTestObject>(SIZE);
//////////////////////////////////////////////////////////////////////
System.out.println("Now testing the ARRRAY
implementation");
PQTestObject [] array = new PQTestObject[SIZE];
for(int i=0; i < SIZE; i++)
array[i] = new PQTestObject((int)
((1000*Math.random() % 20) +1),
sequence_number++);
for(int i=0; i < SIZE; i++)
queue.insert(array[i]);
// check to see what happens if insertion beyond capacity
try {
if(queue.insert(new PQTestObject(1,1)))
throw new RuntimeException("ERROR, inserted
beyond
capacity");
}
catch(Exception e) {
e.printStackTrace();
}
if(queue.size() != SIZE)
System.out.println("ERROR, wrong size!");
System.out.println("Now printing with the iterator. " +
"They should come out in any order.n" +
"Priorty SequenceNumber");
Iterator<PQTestObject> iter = queue.iterator();
while(iter.hasNext())
System.out.print(iter.next()+ " ");
7. //////////////////////////////////////////////////////////////////////
queue = new HeapPriorityQueue<PQTestObject>(SIZE);
//////////////////////////////////////////////////////////////////////
System.out.println("nnNow testing the HEAP
implementation");
array = new PQTestObject[SIZE];
for(int i=0; i < SIZE; i++)
array[i] = new PQTestObject((int)
((1000*Math.random() % 20) +1),
sequence_number++);
for(int i=0; i < SIZE; i++)
queue.insert(array[i]);
// check to see what happens if insertion beyond capacity
try {
if(queue.insert(new PQTestObject(1,1)))
throw new RuntimeException("ERROR, inserted
beyond
capacity");
}
catch(Exception e) {
e.printStackTrace();
}
if(queue.size() != SIZE)
System.out.println("ERROR, wrong size!");
System.out.println("Now printing with the iterator. " +
"They should come out in any order.n" +
"Priorty SequenceNumber");
iter = queue.iterator();
9. }
The Program:
For this assignment, you will write two implementations of a
Priority Queue. For this ADT, removal
operations always return the object in the queue of highest
priority that has been in the queue the
longest. That is, no object of a given priority is ever removed as
long as the queue contains one or more
objects of a higher priority. Within a given priority FIFO order
must be preserved. Your two
implementations will be:
Both implementations must have identical behavior, and must
implement the PriorityQueue interface
(provided). Both implementations must have two constructors,
a default constructor with no
arguments that uses the DEFAULT_MAX_CAPACITY constant
from the PriorityQueue.java interface, and
a constructor that takes a single integer parameter that
represents the maximum capacity of the
PQ. The PriorityQueue interface follows:
/* The PriorityQueue ADT may store objects in any order.
However,
10. removal of objects from the PQ must follow specific criteria.
The object of highest priority that has been in the PQ longest
must be the object returned by the remove() method. FIFO
return
order must be preserved for objects of identical priority.
Ranking of objects by priority is determined by the
Comparable
interface. All objects inserted into the PQ must implement
this
interface.
*/
package data_structures;
import java.util.Iterator;
public interface PriorityQueue<E> extends Iterable<E> {
static final int DEFAULT_MAX_CAPACITY = 1000;
// Inserts a new object into the priority queue. Returns true
11. if
// the insertion is successful. If the PQ is full, the insertion
// is aborted, and the method returns false.
public boolean insert(E object);
// Removes the object of highest priority that has been in the
// PQ the longest, and returns it. Returns null if the PQ is
empty.
public E remove();
// Returns the object of highest priority that has been in the
// PQ the longest, but does NOT remove it.
// Returns null if the PQ is empty.
public E peek();
// Returns the number of objects currently in the PQ.
public int size();
// Returns an iterator of the objects in the PQ, in no
particular
// order. The iterator must be fail-fast.
12. public Iterator<E> iterator();
// Returns an iterator of the objects in the PQ, in exactly the
// same order in which they will be dequeued. The iterator
must
// be fail-fast.
public Iterator<E> orderedIterator();
// Returns the PQ to an empty state.
public void clear();
// Returns true if the PQ is empty, otherwise false
public boolean isEmpty();
// Returns true if the PQ is full, otherwise false. List based
// implementations should always return false.
public boolean isFull();
}
Thus, your project will consist of the following files. You must
13. use exactly these filenames.
The ordered array must use binary
search to identify the correct insertion point for new additions.
heap must be stable.
Additional Details:
les specified, no
additional source code files are
permitted. (Do not hand in a copy of PriorityQueue.java, as it is
provided to you).
interface provided. I will grade your
project with my copy of this file.
number at the beginning of the file.
'data_structures'.
java.util.NoSuchElementException, and
ConcurrentModificationException only. If you feel that you
need to import anything else, let
me know. You are expected to write all of the code yourself,
14. and you may not use the Java API
for any containers.
nt anything.
catch any exceptions.
Your iterators should throw:
o ConcurrentModificationException
o NoSuchElementException
o UnsupportedOperationException
but must handle any/all error
conditions gracefully. i.e. if the
user attempts to call the clear() method on an empty PQ, or
remove an item from an empty PQ,
the program should not crash.
provided. You may not add any public
methods to the implementations, but you may add private ones,
or inner classes if needed.
compiled, but it must compile and run
correctly on rohan using JDK1.6 to receive any credit.
binary heap implementation. Sorting
methods are available in the class lecture notes, and you are
welcome to use the code (now and
in the future). However, you should create a private method in
the appropriate place rather
15. than using any external class directly. You should select a O(n
logn) method that is stable.
The Report:
In addition to the source code, you will write a report that
contains an analysis of the runtime
complexity of the insert and remove methods for both
implementations. You will also perform timing
tests on your code, and provide empirical proof that your
methods perform in accordance with your
analysis. Provide a summary that highlights the strengths and
weaknesses of each implementation, and
recommend the implementation that you think would be best for
this application. We will discuss
methods for timing your structures in class.