AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.PriorityQueue< T > Class Template Reference

Priority queue based on binary min-heap. More...

+ Inheritance diagram for aud.PriorityQueue< T >:
+ Collaboration diagram for aud.PriorityQueue< T >:

Public Member Functions

 PriorityQueue (int n, Comparator< T > cmp)
 Create empty priority queue. More...
 
 PriorityQueue (Comparator< T > cmp)
 
 PriorityQueue (int n)
 
 PriorityQueue ()
 
front ()
 Get minimal element. More...
 
boolean is_empty ()
 Is PQ empty? More...
 
boolean contains (T x)
 Is x contained in PQ? More...
 
int size ()
 get number of entries More...
 
pop ()
 Pop minimal element from PQ. More...
 
void push (T x)
 Add entry to PQ. More...
 
void raise (T x)
 update heap after raising priority of x
More...
 
void lower (T x)
 update heap after lowering priority of x
More...
 
String toString ()
 
void checkConsistency ()
 check consistency More...
 
abstract boolean is_empty ()
 Is PQ empty? More...
 
abstract T front ()
 Get minimal element. More...
 
abstract T pop ()
 Pop minimal element from PQ. More...
 
abstract void push (T x)
 Push x into PQ. More...
 

Static Public Member Functions

static void main (String[] args)
 example and test More...
 

Additional Inherited Members

- Protected Member Functions inherited from aud.adt.AbstractPriorityQueue< T >
 AbstractPriorityQueue (java.util.Comparator< T > cmp)
 create empty PQ and use cmp_ for comparison of priorities More...
 
 AbstractPriorityQueue ()
 create empty PQ More...
 
boolean less (T a, T b)
 test for a<b, uses Comparator if one was provided or Comparable else. More...
 
- Protected Attributes inherited from aud.adt.AbstractPriorityQueue< T >
Comparator< T > cmp_ = null
 

Detailed Description

Priority queue based on binary min-heap.

This data structure additionally allows for limited set semantics (contains) and updating priorities of contained entries: lower, raise.

This functionality is required, e.g., by Dijkstra's algorithm. It is implemented as an additional HashMap that maps entries T (keys) to position_s (values), i.e., for any entry we know its position in the heap and can therefore apply upheap and downheap.

Note that using the above feature requires that entries in the PQ are unique! Therefore push throws an exception if the same entry (tested by equals) is added twice!

Hint: For an efficient implementation of Dijkstra's algorithm, it is usually better to store the map position_ as an int attribute of nodes in the graph (efficient and direct access). This, however, requires that the implementation of the graph data structure and the priority queue are closely coupled.

Note: You can alternatively use Java's java.util.PriorityQueue to implement Dijkstra's algorithm. In this case you need to replace lower by calling remove followed by add, which changes priority.
Note that, e.g., the C++ standard library provides only a data structure std::priority_queue that does neither provide direct update of priority nor removal of arbitrary entries! (You need to implement you own data structure similar to this one!)

Definition at line 46 of file PriorityQueue.java.

Constructor & Destructor Documentation

◆ PriorityQueue() [1/4]

aud.PriorityQueue< T >.PriorityQueue ( int  n,
Comparator< T >  cmp 
)

Create empty priority queue.

Parameters
nexpected number of entries (preallocates/reserves storage)
cmpcompare entries

Definition at line 65 of file PriorityQueue.java.

65 {
66 super(cmp);
67 heap_=new Vector<T>();
68 heap_.reserve(n);
69 position_=new HashMap<T,Integer>(n);
70 }

◆ PriorityQueue() [2/4]

aud.PriorityQueue< T >.PriorityQueue ( Comparator< T >  cmp)

Definition at line 71 of file PriorityQueue.java.

71{ this(MINENTRIES,cmp); }

◆ PriorityQueue() [3/4]

Definition at line 73 of file PriorityQueue.java.

73{ this(n,null); }

◆ PriorityQueue() [4/4]

Definition at line 74 of file PriorityQueue.java.

74{ this(MINENTRIES,null); }

Member Function Documentation

◆ checkConsistency()

void aud.PriorityQueue< T >.checkConsistency ( )

check consistency

Definition at line 203 of file PriorityQueue.java.

203 {
204 System.err.println("size="+heap_.size());
205 // check mapping between heap_ and position_
206 if (heap_.size()!=position_.size())
207 throw new RuntimeException("size mismatch");
208 for (int i=0;i<heap_.size();++i)
209 if (position_.find(heap_.at(i))==null || position_.find(heap_.at(i))!=i)
210 throw new RuntimeException("invalid mapping for key='"+heap_.at(i)+"'");
211
212 // check heap property
213 for (int i=0;i<heap_.size();++i) {
214 if (i>0) {
215 int p=getParent(i);
216 if (!leq(p,i))
217 throw new RuntimeException("heap property violated (parent");
218 }
219 int left=getLeftChild(i);
220 if (left<heap_.size()) {
221 if (!leq(i,left))
222 throw new RuntimeException("heap property violated (left)");
223
224 int right=left+1;
225 if (right<heap_.size()) {
226 if (!leq(i,right))
227 throw new RuntimeException("heap property violated (right)");
228 }
229 }
230 }
231 }
Value find(Key key)
find value for key @endiliteral
Definition: HashMap.java:281
int size()
get current number of entries
Definition: HashMap.java:153

◆ contains()

boolean aud.PriorityQueue< T >.contains ( x)

Is x contained in PQ?

Definition at line 85 of file PriorityQueue.java.

85{ return position_.contains(x); }
boolean contains(Key key)
Is key contained in map?
Definition: HashMap.java:295

Referenced by aud.PriorityQueue< T >.push().

+ Here is the caller graph for this function:

◆ front()

T aud.PriorityQueue< T >.front ( )

Get minimal element.

Requires !is_empty().

Exceptions
NoSuchElementException
Returns
top

Reimplemented from aud.adt.AbstractPriorityQueue< T >.

Definition at line 76 of file PriorityQueue.java.

76 {
77 return heap_.front(); // may throw
78 }

Referenced by aud.PriorityQueue< T >.main().

+ Here is the caller graph for this function:

◆ is_empty()

boolean aud.PriorityQueue< T >.is_empty ( )

Is PQ empty?

Reimplemented from aud.adt.AbstractPriorityQueue< T >.

Definition at line 80 of file PriorityQueue.java.

80 {
81 return heap_.empty();
82 }

Referenced by aud.example.graph.PriorityFirstSearch.start().

+ Here is the caller graph for this function:

◆ lower()

void aud.PriorityQueue< T >.lower ( x)

update heap after lowering priority of x

Definition at line 187 of file PriorityQueue.java.

187 {
188 assert(position_.contains(x));
189 int k=position_.find(x);
190
191 // Assert that priority was lowered!
192 assert(getLeftChild(k) >=heap_.size() || less(k,getLeftChild(k)));
193 assert(getLeftChild(k)+1>=heap_.size() || less(k,getLeftChild(k)+1));
194
195 upheap(k);
196 }

Referenced by aud.PriorityQueue< T >.main().

+ Here is the caller graph for this function:

◆ main()

static void aud.PriorityQueue< T >.main ( String[]  args)
static

example and test

Definition at line 234 of file PriorityQueue.java.

234 {
235
236 {
237 // use as a "standard" priority queue
238
239 PriorityQueue<Integer> pq=new PriorityQueue<Integer>();
240
241 pq.push(2); System.out.println(pq);
242 pq.push(4); System.out.println(pq);
243 pq.push(3); System.out.println(pq);
244 pq.push(1); System.out.println(pq);
245
246 System.out.println(pq.pop());
247 System.out.println(pq);
248
249 System.out.println(pq.pop());
250 System.out.println(pq);
251 }
252 {
253 // now the following provides a mapping from entries to priorities
254 final HashMap<String,Integer> priority=new HashMap<String,Integer>();
255 priority.insert("a",1);
256 priority.insert("b",2);
257 priority.insert("c",3);
258 priority.insert("d",4);
259
260 PriorityQueue<String> pq=
261 new PriorityQueue<String>
262 (new Comparator<String>() {
263 @Override public int compare(String a,String b) {
264 return priority.find(a)-priority.find(b);
265 }
266 });
267
268 // first we do the same as above
269
270 pq.push("b"); System.out.println(pq);
271 pq.push("d"); System.out.println(pq);
272 pq.push("c"); System.out.println(pq);
273 pq.push("a"); System.out.println(pq);
274
275 System.out.println(pq.pop());
276 System.out.println(pq);
277
278 System.out.println(pq.pop());
279 System.out.println(pq);
280
281 // restore what we had before
282 pq.push("b"); System.out.println(pq);
283 pq.push("a"); System.out.println(pq);
284
285 // now we gonna change the priority mapping and update pq
286
287 System.out.println("front="+pq.front());
288 priority.insert("a",5); // RAISE priority ...
289 System.out.println("new prioities: "+priority);
290 pq.raise("a"); // ... and update pq
291 System.out.println("front="+pq.front());
292 priority.insert("c",0); // LOWER priority ...
293 System.out.println("new prioities: "+priority);
294 pq.lower("c"); // ... and update pq
295 System.out.println("front="+pq.front());
296 }
297 }

References aud.HashMap< Key, Value >.find(), aud.PriorityQueue< T >.front(), aud.HashMap< Key, Value >.insert(), aud.PriorityQueue< T >.lower(), aud.PriorityQueue< T >.pop(), aud.PriorityQueue< T >.push(), and aud.PriorityQueue< T >.raise().

+ Here is the call graph for this function:

◆ pop()

T aud.PriorityQueue< T >.pop ( )

Pop minimal element from PQ.

Requires !is_empty().

Exceptions
NoSuchElementException
Returns
removed (minimal) element

Reimplemented from aud.adt.AbstractPriorityQueue< T >.

Definition at line 90 of file PriorityQueue.java.

90 {
91 T root=heap_.front(); // may throw
92 position_.remove(root);
93
94 if (heap_.size()>1) {
95 position_.insert((heap_.data_[0]=heap_.back()),0);
96 heap_.pop_back();
97 downheap(0);
98 }
99 else
100 heap_.pop_back();
101
102 return root;
103 }
Value insert(Key key, Value value)
insert key-value pair
Definition: HashMap.java:260
Value remove(Key key)
remove entry (equivalent to insert(key,null))
Definition: HashMap.java:273

Referenced by aud.PriorityQueue< T >.main(), and aud.example.graph.PriorityFirstSearch.start().

+ Here is the caller graph for this function:

◆ push()

void aud.PriorityQueue< T >.push ( x)

Add entry to PQ.

Parameters
xnew entry that must not be already contained in PQ
Exceptions
RuntimeExceptionif x is already contained in PQ

Reimplemented from aud.adt.AbstractPriorityQueue< T >.

Definition at line 153 of file PriorityQueue.java.

153 {
154 if (contains(x))
155 throw new RuntimeException(x.toString()+" already contained in PQ");
156
157 int k=heap_.size();
158 heap_.push_back(x);
159 position_.insert(x,k);
160 upheap(k);
161 }
boolean contains(T x)
Is x contained in PQ?

References aud.PriorityQueue< T >.contains().

Referenced by aud.example.grid.Grid.astar(), aud.example.grid.Grid2.astar(), aud.example.grid.Grid.dijkstra(), aud.example.grid.Grid2.dijkstra(), aud.PriorityQueue< T >.main(), and aud.example.graph.PriorityFirstSearch.start().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raise()

void aud.PriorityQueue< T >.raise ( x)

update heap after raising priority of x

Definition at line 177 of file PriorityQueue.java.

177 {
178 assert(position_.contains(x));
179 int k=position_.find(x);
180
181 assert(k==0 || less(getParent(k),k)); // priority was raised!
182
183 downheap(k);
184 }

Referenced by aud.PriorityQueue< T >.main().

+ Here is the caller graph for this function:

◆ size()

int aud.PriorityQueue< T >.size ( )

get number of entries

Definition at line 88 of file PriorityQueue.java.

88{ return heap_.size(); }

◆ toString()

String aud.PriorityQueue< T >.toString ( )

Definition at line 198 of file PriorityQueue.java.

198 {
199 return position_.toString();
200 }
String toString()
Definition: HashMap.java:299

Referenced by aud.example.graph.PriorityFirstSearch.start().

+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: