next up previous contents
Next: Priority Queues with Implementation Up: Priority Queues Previous: Priority Queues

   
Priority Queues (p_queue)

Definition

An instance Q of the parameterized data type p_queue<P,I> is a collection of items (type pq_item). Every item contains a priority from a linearly ordered type P and an information from an arbitrary type I. P is called the priority type of Q and I is called the information type of Q. The number of items in Q is called the size of Q. If Q has size zero it is called the empty priority queue. We use <p,i> to denote a pq_item with priority p and information i.

Creation

p_queue<P,I> Q; creates an instance Q of type p_queue<P,I> based on the linear order defined by the global compare function compare(const P&, const P&) and initializes it with the empty priority queue.

p_queue<P,I>

Q(int (*cmp)(P, P )); creates an instance Q of type p_queue<P,I> based on the linear order defined by the compare function cmp and initializes it with the empty priority queue. Precondition: cmp must define a linear order on P.

   

Operations

P Q.prio(pq_item it) returns the priority of item it.
Precondition: it is an item in Q.
I Q.inf(pq_item it) returns the information of item it.
Precondition: it is an item in Q.
I& Q[pq_item it] returns a reference to the information of item it.
Precondition: it is an item in Q.
pq_item Q.insert(P x, I i) adds a new item <x,i> to Q and returns it.
pq_item Q.find_min() returns an item with minimal priority (nil if Q is empty).
P Q.del_min() removes the item it = Q.find_min() from Q and returns the priority of it.
Precondition: Q is not empty.
void Q.del_item(pq_item it) removes the item it from Q.
Precondition: it is an item in Q.
void Q.change_inf(pq_item it, I i)
    makes i the new information of item it.
Precondition: it is an item in Q.
void Q.decrease_p(pq_item it, P x)
    makes x the new priority of item it.
Precondition: it is an item in Q and x is not larger then prio(it).
int Q.size() returns the size of Q.
bool Q.empty() returns true, if Q is empty, false otherwise.
void Q.clear() makes Q the empty priority queue.

Implementation

Priority queues are implemented by Fibonacci heaps [32]. Operations insert, del_item, del_min take time O(log n), find_min, decrease_p, prio, inf, empty take time O(1) and clear takes time O(n), where n is the size of Q. The space requirement is O(n).

Example

Dijkstra's Algorithm (cf. section Graph and network algorithms)


next up previous contents
Next: Priority Queues with Implementation Up: Priority Queues Previous: Priority Queues
LEDA research project
1998-10-02