Priority Queues
Priority Queues
An ordered array
A linked list
Heap
Implementing Priority Queue
using Unordered Array
Data Items stored in an unordered array
Enqueue operation same as enqueue operation in
ordinary FIFO queues
To implement dequeue, we first scan the whole of the
priority queue to look for element with highest priority
and then swap it with the element at the front and then
perform dequeue operation as in queues.
enqueue(23)
front
23
rear
enqueue(11)
front
23 11
rear
enqueue(17)
front
23 11
rear
enqueue(17)
front
23 11 17
rear
dequeue()
front
23 11 17
rear
dequeue()
front
11 17
rear
enqueue(12)
front
11 17 12
rear
enqueue(22)
front
11 17 12
rear
enqueue(22)
front
11 17 12 22
rear
dequeue()
front
11 17 12 22
rear
dequeue()
front
11 17 12 22
rear
dequeue()
front
11 17 12
rear
enqueue(20)
front
11 17 12
rear
enqueue(20)
front
11 17 12 20
rear
enqueue(16)
front
11 17 12 20
rear
enqueue(16)
front
11 17 12 20 16
rear
dequeue()
front
11 17 12 20 16
rear
dequeue()
front
11 17 12 16
rear
dequeue()
front
11 17 12 16
rear
dequeue()
front
11 12 16
rear
enqueue(5)
front
11 12 16 5
rear
dequeue()
front
11 12 16 5
rear
dequeue()
front
11 12 16 5
rear
dequeue()
front
11 12 5
rear
Final Queue
front
11 12 5
rear
Implementing Priority Queue
using ordered Array
Data Items stored in an ordered array
Dequeue operation same as dequeue operation in
ordinary FIFO queues
To implement enqueue, we first search where the new
element has to be inserted and then we need to shift
elements to make room for the insertion.
enqueue(23)
front
23
rear
enqueue(11)
front
23 11
rear
enqueue(17)
front
23 11
rear
enqueue(17)
front
23 11
rear
enqueue(17)
front
23 17 11
rear
dequeue()
front
23 17 11
rear
dequeue()
front
17 11
rear
enqueue(12)
front
17 11
rear
enqueue(12)
front
17 11
rear
enqueue(12)
front
17 12 11
rear
enqueue(22)
front
17 12 11
rear
enqueue(22)
front
17 12 11
rear
enqueue(22)
front
22 17 12 11
rear
dequeue()
front
22 17 12 11
rear
dequeue()
front
17 12 11
rear
enqueue(20)
front
17 12 11
rear
enqueue(20)
front
20 17 12 11
rear
enqueue(16)
front
20 17 12 11
rear
enqueue(16)
front
20 17 12 11
rear
enqueue(16)
front
20 17 16 12 11
rear
dequeue()
front
20 17 16 12 11
rear
dequeue()
front
17 16 12 11
rear
dequeue()
front
17 16 12 11
rear
dequeue()
front
16 12 11
rear
enqueue(5)
front
16 12 11 5
rear
dequeue()
front
16 12 11 5
rear
dequeue()
front
16 12 11 5
rear
dequeue()
front
12 11 5
rear
Applications of Priority
Queues
Network Routing
Sorting
Compression
CPU Scheduling
Interrupt Handling
Simulation
Greedy Algorithms
Incremental /Partial sorting
Find the k largest elements
Applications of Priority
Queues
Network Routing
Applications of Priority Queue
Sorting
We can use a priority queue to sort a set of
comparable elements
1. Insert the elements one by one with a series of
push(e) operations
2. Remove the elements in sorted order with a series of
pop() operations
The running time of this sorting method depends
on the priority queue implementation
Applications of Priority Queue
Compression
One of the earliest data
algorithm, Huffman algorithm compression
uses priority queue. Huffman extensively
used to compress a block of data into a
smaller space. The Codes algorithm starts
collecting the frequencies by
characters are block all then
of in the data the
and
processes each one in order of descending
frequency by using a priority queue.
Applications of Priority Queue
CPU Scheduling
A CPU can only run one process at a time, but
there may be many jobs waiting to be run. A
priority can be associated with each process, and
the CPU is allocated to the process with the
highest priority. A Priority Queue can be used to
store the processes and quickly select the next
process to run based upon its priority.
Applications of Priority Queue
Event Simulation
Priority queues are most commonly used as "event
queues" in simulations. Each value on the queue is
an event that is expected to take place, and each key is the
time it is expected to take place.
3
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
1 3
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
1 3 9
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
1 3 8 9
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
1 3 6 8 9
2 3 1 2
3 1 9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 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
4<13
13 22 28 31 32
9 2 3 1 2
3 1 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
81>13
13 22 28 31 32
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
22 28 31 32 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
72>22
22 28 31 32 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
28 31 32 72 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
5<28
28 31 32 72 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
17<2
8
28 31 32 72 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
19<28
28 31 32 72 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
2<28
28 31 32 72 81
2 3 1 2
3 1 9 8 6 7
1 2 3 2
2 1 8 7 1 1
11 4 5 2
8 2 1 2 7 9
28 31 32 72 81
Evaluating implementations
When we choose a data structure, it is important to look at
usage patterns
If we load an array once and do thousands of searches on
it, we want to make searching fast—so we would
probably sort the array
If we load a huge array and expect to do only a few
searches, we probably don’t want to spend time sorting
the array
For almost all uses of a queue (including a priority
queue), we eventually remove everything that we add
Hence, when we analyze a priority queue, neither “add”
nor “remove” is more important—we need to look at the
timing for “add + remove”
Array implementations
A priority queue could be implemented as an unsorted
array (with a count of elements)
Adding an element would take O(1) time (why?)
Removing an element would take O(n) time (why?)
Hence, adding and removing an element takes O(n)
time
This is an inefficient representation
A priority queue could be implemented as a sorted array
(again, with a count of elements)
Adding an element would take O(n) time (why?)
Removing an element would take O(1) time (why?)
Hence, adding and removing an element takes O(n)
time
100
Again, this is inefficient
Linked list implementations
A priority queue could be implemented as an unsorted
linked list
Adding an element would take O(1) time (why?)
Removing an element would take O(n) time (why?)
A priority queue could be implemented as a sorted linked list
Adding an element would take O(n) time (why?)
Removing an element would take O(1) time (why?)
As with array representations, adding and removing an
element takes O(n) time
Again, these are inefficient implementations
101
Binary tree implementations
A priority queue could be represented as a (not
necessarily balanced) binary search tree
Insertion times would range from O(log n) to
O(n)
(why?)
Removal times would range from O(log n) to
O(n)
(why?)
A priority queue could be represented as a
balanced
binary search tree
Insertion and removal could destroy the balance
102
We need an algorithm to rebalance the binary
Heap implementation
Better than BST because it does not support
links
- Insert O(logN)
- Find minimum O(logN)
Deleting the minimal element takes a constant
time, however after that the heap structure has to be
adjusted, and this requires O(logN) time.
Programming Assignment
Dynamic-median finding. Design a data
type that supports insert in logarithmic time,
find the median in constant time,
and remove the median in logarithmic
time.
Dynamic-median finding
You need two heaps: one min-heap and one max-heap. Each heap contains about
one half of the data. Every element in the min-heap is greater or equal to the
median, and every element in the max-heap is less or equal to the median.
When the min-heap contains one more element than the max-heap, the median is
in the top of the min-heap. And when the max-heap contains one more element
than the min-heap, the median is in the top of the max-heap.
When both heaps contain the same number of elements, the total number of
elements is even. In this case median is the mean of the two middle elements.
Every time you insert, compare the new element with those at the top of the heaps
in order to decide where to insert it. If the new element is greater than the current
median, it goes to the min-heap. If it is less than the current median, it goes to the
max heap. Then you might need to rebalance. If the sizes of the heaps differ by
more than one element, extract the min/max from the heap with more elements and
insert it into the other heap.