-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastPriorityQueue.java
More file actions
286 lines (258 loc) · 7.5 KB
/
Copy pathFastPriorityQueue.java
File metadata and controls
286 lines (258 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.rutgers.util;
import java.io.Serializable;
import java.util.NoSuchElementException;
/**
* A priority queue based on a binary heap.
* Note that this implementation does not efficiently support
* containsKey or getPriority. Removal is not supported.
* If you set the priority of a key multiple times,
* it will NOT be promoted or demoted, but rather it will be
* inserted in the queue once multiple times, with the various
* priorities.
*/
public class FastPriorityQueue <E> implements PriorityQueue<E>, Serializable {
private static final long serialVersionUID = 5724671156522771658L;
int size;
int capacity;
Object[] elements;
double[] priorities;
public boolean containsKey(E e) {
for (int i = 0; i < elements.length; i++) {
Object element = elements[i];
if (e == null && element == null) return true;
if (e != null && e.equals(element)) return true;
}
return false;
}
public double getPriority(E e) {
double bestPriority = Double.NEGATIVE_INFINITY;
for (int i = 0; i < elements.length; i++) {
Object element = elements[i];
if ((e == null && element == null) ||
(e != null && e.equals(element))) {
if (priorities[i] > bestPriority) {
bestPriority = priorities[i];
}
}
}
return bestPriority;
}
public double removeKey(E e) {
throw new UnsupportedOperationException();
}
protected void grow(int newCapacity) {
Object[] newElements = new Object[newCapacity];
double[] newPriorities = new double[newCapacity];
if (size > 0) {
System.arraycopy(elements, 0, newElements, 0, elements.length);
System.arraycopy(priorities, 0, newPriorities, 0, priorities.length);
}
elements = newElements;
priorities = newPriorities;
capacity = newCapacity;
}
protected int parent(int loc) {
return (loc - 1) / 2;
}
protected int leftChild(int loc) {
return 2 * loc + 1;
}
protected int rightChild(int loc) {
return 2 * loc + 2;
}
protected void heapifyUp(int loc) {
if (loc == 0) return;
int parent = parent(loc);
if (priorities[loc] > priorities[parent]) {
swap(loc, parent);
heapifyUp(parent);
}
}
protected void heapifyDown(int loc) {
int max = loc;
int leftChild = leftChild(loc);
if (leftChild < size()) {
double priority = priorities[loc];
double leftChildPriority = priorities[leftChild];
if (leftChildPriority > priority)
max = leftChild;
int rightChild = rightChild(loc);
if (rightChild < size()) {
double rightChildPriority = priorities[rightChild(loc)];
if (rightChildPriority > priority && rightChildPriority > leftChildPriority)
max = rightChild;
}
}
if (max == loc)
return;
swap(loc, max);
heapifyDown(max);
}
protected void swap(int loc1, int loc2) {
double tempPriority = priorities[loc1];
Object tempElement = elements[loc1];
priorities[loc1] = priorities[loc2];
elements[loc1] = elements[loc2];
priorities[loc2] = tempPriority;
elements[loc2] = tempElement;
}
public E removeFirst() {
if (size < 1) throw new NoSuchElementException();
Object element = elements[0];
swap(0, size - 1);
size--;
elements[size] = null;
heapifyDown(0);
return (E) element;
}
/**
* Returns true if the priority queue is non-empty
*/
public boolean hasNext() {
return !isEmpty();
}
/**
* Returns the element in the queue with highest priority, and pops it from the queue.
*/
public E next() {
return removeFirst();
}
/**
* Not supported -- next() already removes the head of the queue.
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Returns the highest-priority element in the queue, but does not pop it.
*/
public E getFirst() {
if (size < 1) throw new NoSuchElementException();
return (E) elements[0];
}
/**
* Gets the priority of the highest-priority element of the queue.
*/
public double getPriority() {
if (size() > 0)
return priorities[0];
throw new NoSuchElementException();
}
/**
* Number of elements in the queue.
*/
public int size() {
return size;
}
/**
* True if the queue is empty (size == 0).
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Adds a key to the queue with the given priority. If the key is already in the queue, it will be added an
* additional time, NOT promoted/demoted.
*
* @param key
* @param priority
*/
public void setPriority(E key, double priority) {
if (size == capacity) {
grow(2 * capacity + 1);
}
elements[size] = key;
priorities[size] = priority;
heapifyUp(size);
size++;
}
/**
* Returns a representation of the queue in decreasing priority order.
*/
public String toString() {
return toString(size());
}
/**
* Returns a representation of the queue in decreasing priority order, displaying at most maxKeysToPrint elements.
*
* @param maxKeysToPrint
*/
public String toString(int maxKeysToPrint) {
PriorityQueue<E> pq = deepCopy();
StringBuilder sb = new StringBuilder("[");
int numKeysPrinted = 0;
while (numKeysPrinted < maxKeysToPrint && ! pq.isEmpty()) {
double priority = pq.getPriority();
E element = pq.removeFirst();
sb.append(element.toString());
sb.append(" : ");
sb.append(priority);
if (numKeysPrinted < size() - 1)
// sb.append("\n");
sb.append(", ");
numKeysPrinted++;
}
if (numKeysPrinted < size())
sb.append("...");
sb.append("]");
return sb.toString();
}
/**
* Returns a counter whose keys are the elements in this priority queue, and whose counts are the priorities in this
* queue. In the event there are multiple instances of the same element in the queue, the counter's count will be the
* sum of the instances' priorities.
*
* @return
*/
public Counter asCounter() {
PriorityQueue<E> pq = deepCopy();
Counter<E> counter = new Counter<E>();
while (! pq.isEmpty()) {
double priority = pq.getPriority();
E element = pq.removeFirst();
counter.incrementCount(element, priority);
}
return counter;
}
/**
* Returns a clone of this priority queue. Modifications to one will not affect modifications to the other.
*/
public FastPriorityQueue<E> deepCopy() {
FastPriorityQueue<E> clonePQ = new FastPriorityQueue<E>();
clonePQ.size = size;
clonePQ.capacity = capacity;
clonePQ.elements = new Object[capacity];
clonePQ.priorities = new double[capacity];
if (size() > 0) {
System.arraycopy(elements, 0, clonePQ.elements, 0, size());
System.arraycopy(priorities, 0, clonePQ.priorities, 0, size());
}
return clonePQ;
}
public FastPriorityQueue() {
this(15);
}
public FastPriorityQueue(int capacity) {
int legalCapacity = 0;
while (legalCapacity < capacity) {
legalCapacity = 2 * legalCapacity + 1;
}
grow(legalCapacity);
}
public static void main(String[] args) {
PriorityQueue<String> pq = new FastPriorityQueue<String>();
System.out.println(pq);
pq.setPriority("one", 1);
System.out.println(pq);
pq.setPriority("three", 3);
System.out.println(pq);
pq.setPriority("one", 1.1);
System.out.println(pq);
pq.setPriority("two", 2);
System.out.println(pq);
System.out.println(pq.toString(2));
while (pq.hasNext()) {
System.out.println(pq.next());
}
}
}