0% found this document useful (0 votes)
14 views

Priority

The document discusses different implementations of a priority queue abstract data type. It can be implemented using an unordered array, ordered array, or heap. An unordered array allows for O(1) insertion but O(n) time for deletion and finding the minimum. An ordered array provides O(1) time for finding the minimum and deletion but O(n) time for insertion. A heap provides O(log n) time for insertion, deletion, and finding the minimum.

Uploaded by

stanyatan10
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Priority

The document discusses different implementations of a priority queue abstract data type. It can be implemented using an unordered array, ordered array, or heap. An unordered array allows for O(1) insertion but O(n) time for deletion and finding the minimum. An ordered array provides O(1) time for finding the minimum and deletion but O(n) time for insertion. A heap provides O(log n) time for insertion, deletion, and finding the minimum.

Uploaded by

stanyatan10
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

PRIORITY QUEUE

Abstract Data Type: Priority Queue

 A priority queue is a collection of zero or more items,


 Priority is associated with each item
 A priority queue has at least three operations
 insert(item i) (enqueue) a new item
 delete() (dequeue) the member with the highest priority
 find() the item with the highest priority
 Note that in a priority queue "first in first out" does not
apply in general.

f00 pq 2
Priority Queues: Assumptions

 The highest priority can be either the minimum value of


all the items, or the maximum.
 We will assume the highest priority is the minimum.
 Call the delete operation deleteMin().
 Call the find operation findMin().

 Assume the priority queue has n items.

f00 pq 3
Priority Queue Implementation

Insert() deleteMin() findMin()

Unordered
O(1) O(n) O(n)
Array

Ordered Array O(n) O(1) O(1)

heap O(log(n)) O(log(n)) O(1)

f00 pq 4
Implementation with an Unsorted Sequence

 Let’s try to implement a priority queue with an unsorted


sequence S.
 The elements of S are a composition of two elements, k,
the key, and e, the element.
 We can implement insert() by using insertLast() on the
sequence. This takes O(1) time.

 However, because we always insert at the end,


irrespectively of the key value, our sequence is not
ordered. 5
Implementation with an Unsorted Sequence (contd.)

 Thus, for methods such as findMin(), and deleteMin(),


we need to look at all the elements of S. The worst
case time complexity for these methods is O(n).

•Performance summary
Operations Complexity
Insert() O(1)
deleteMin() O(n)
findMin() O(n)
6
Algorithm of priority queue using an unordered array
Algorithm enqueue(x)
if isfull() then throw FullQueueException
else
r  r+1
Q[r]  x

Algorithm dequeue()
if isEmpty() then throw EmptyQueueException
min  f;
for i  1 to r do
if Q[min] > Q[i] then min  i
item  Q[min]
// replace the min with the last element
Q[min]  Q[r];
r  r - 1;
return item;

7
Implementation with Sorted Sequence

 Another implementation uses a sequence S, sorted


by increasing keys
 findMin() and deleteMin() take O(1) time

• However, to implement insert(), we must now scan


through the entire sequence in the worst case. Thus insert()
runs in O(n) time

8
Algorithm of priority queue using an ordered
array
Algorithm enqueue(x)
if isfull() then throw FullQueueException
else
in-1
while (i >= 0 && x < Q[i]) do
Q[i + 1]  Q[i]
i  i-1
Q[i + 1]  item
n  n+1

Algorithm dequeue()
if isEmpty() then throw EmptyQueueException
else
item  Q[f]
f  f+1
return item

9
Heaps

Procedure Binary heap Binomial heap


(worst-case) (worst-case)
Insert (lg n) O(lg n)
findMin (1) O(lg n)
deleteMin (lg n) O(lg n)
merge (n) O(lg n)
decreaseKey (lg n) O(lg n)

You might also like