![]() |
AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
|
Priority queue based on binary min-heap. More...
Public Member Functions | |
PriorityQueue (int n, Comparator< T > cmp) | |
Create empty priority queue. More... | |
PriorityQueue (Comparator< T > cmp) | |
PriorityQueue (int n) | |
PriorityQueue () | |
T | 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... | |
T | 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 | |
![]() | |
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... | |
![]() | |
Comparator< T > | cmp_ = null |
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.
aud.PriorityQueue< T >.PriorityQueue | ( | int | n, |
Comparator< T > | cmp | ||
) |
Create empty priority queue.
n | expected number of entries (preallocates/reserves storage) |
cmp | compare entries |
Definition at line 65 of file PriorityQueue.java.
aud.PriorityQueue< T >.PriorityQueue | ( | Comparator< T > | cmp | ) |
Definition at line 71 of file PriorityQueue.java.
aud.PriorityQueue< T >.PriorityQueue | ( | int | n | ) |
Definition at line 73 of file PriorityQueue.java.
aud.PriorityQueue< T >.PriorityQueue | ( | ) |
Definition at line 74 of file PriorityQueue.java.
void aud.PriorityQueue< T >.checkConsistency | ( | ) |
check consistency
Definition at line 203 of file PriorityQueue.java.
boolean aud.PriorityQueue< T >.contains | ( | T | x | ) |
Is x
contained in PQ?
Definition at line 85 of file PriorityQueue.java.
Referenced by aud.PriorityQueue< T >.push().
T aud.PriorityQueue< T >.front | ( | ) |
Get minimal element.
Requires !is_empty()
.
NoSuchElementException |
Reimplemented from aud.adt.AbstractPriorityQueue< T >.
Definition at line 76 of file PriorityQueue.java.
Referenced by aud.PriorityQueue< T >.main().
boolean aud.PriorityQueue< T >.is_empty | ( | ) |
Is PQ empty?
Reimplemented from aud.adt.AbstractPriorityQueue< T >.
Definition at line 80 of file PriorityQueue.java.
Referenced by aud.example.graph.PriorityFirstSearch.start().
void aud.PriorityQueue< T >.lower | ( | T | x | ) |
update heap after lowering priority of x
Definition at line 187 of file PriorityQueue.java.
Referenced by aud.PriorityQueue< T >.main().
|
static |
example and test
Definition at line 234 of file PriorityQueue.java.
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().
T aud.PriorityQueue< T >.pop | ( | ) |
Pop minimal element from PQ.
Requires !is_empty()
.
NoSuchElementException |
Reimplemented from aud.adt.AbstractPriorityQueue< T >.
Definition at line 90 of file PriorityQueue.java.
Referenced by aud.PriorityQueue< T >.main(), and aud.example.graph.PriorityFirstSearch.start().
void aud.PriorityQueue< T >.push | ( | T | x | ) |
Add entry to PQ.
x | new entry that must not be already contained in PQ |
RuntimeException | if x is already contained in PQ |
Reimplemented from aud.adt.AbstractPriorityQueue< T >.
Definition at line 153 of file PriorityQueue.java.
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().
void aud.PriorityQueue< T >.raise | ( | T | x | ) |
update heap after raising priority of x
Definition at line 177 of file PriorityQueue.java.
Referenced by aud.PriorityQueue< T >.main().
int aud.PriorityQueue< T >.size | ( | ) |
String aud.PriorityQueue< T >.toString | ( | ) |
Definition at line 198 of file PriorityQueue.java.
Referenced by aud.example.graph.PriorityFirstSearch.start().