Priority Queues
Priority Queues
INTRODUCTION
In this chapter a priority queue is defined. Various uses of priority queues are given. The
abstract Data Structure to be used is discussed. Thereafter the algorithms for inserting and
deleting items into a Priority Queue are given. Their efficiency is discussed. Finally the
use of a Heap, which is a priority queue, to sort records is described.
PRIORITY QUEUES
A priority queue is a queue of records. Each record will contain, in its data, a field that is
used as the criterion of importance to order that item relative to all the other items. Items
arrive in any order. They exit the queue in minimum order.
Example: A file arrives to be printed. The arrival time is recorded. Files are removed
from the queue in arrival-time order. The example below implies that the records are kept
in a list which is ordered on arrival time. This certainly is a possibility but it is not the
optimal one. A different structure, which is more efficient, will be described in this
section.
A PRIORITY QUEUE should be thought of as an Abstract Data Structure with the two
methods INSERT and DELETEMIN:
Insert places a new item in the priority queue in the correct place.
DeleteMin removes the smallest item then the Priority queue is reordered
Printer queue where the priority of a job could be the importance of the person
submitting it. It could also be the arrival time of the job. It could
also be the combination of these two criteria.
1
PRIORITY QUEUE IMPLEMENTED AS A LINKED LIST
The simplest method of implementing a Priority queue would be as a simple linked list.
The list would be ordered on the specific criterion applicable (like the time of arrival).
Assume that there are N items already in the queue. To insert the next value one would,
on average, have to search half the queue before finding the appropriate place. The
amount of work required is .5*N. Using order arithmetic this is O(N).
While this amount of work is not too bad it can be significantly reduced by using a heap
data structure. It this case the amount of work to inset a new record is O(log2 N).
It is note worthy that every item has to be inserted into the priority queue. At some later
stage each item is removed.
2
PRIORITY QUEUE IMPLEMENTED AS A BINARY HEAP
3
STRUCTURE PROPERTY
Using this array data structure there are the following useful & efficient properties.
Examples:
Node J is at position 10 = I
Parent position is I div 2 = 10 div 2 = position 5. This is Node E
Node G is at position 7 = I
Parent position is I div 2 = 7 div 2 = position 3. This is Node C
(Remember with “div” any remainder is omitted.)
Examples:
For Node A at position 1
Its left child Node B is at position 2*I = 2*1 =2
Its right child Node C is at position 2*I + 1 = 2*1 + 1 = 3
These two properties make it trivially easy & efficient to do these computations. It should
be appreciated that when you are at some node in the heap you either wish to get access
to the parent or to one of the children.
3) Height of the Heap. There are N nodes and it is a complete binary tree. Thus the height
of the tree is at most log2 N (actually .Lower bound[log2 N]). This is a consequence of
complete tree of height h having If the tree has A complete tree of height h has between
2h and 2h + 1 – 1 nodes.
Example 1:
4
H 2H 2H + 1 – 1 .
0 20 = 1 2 0+1 – 1 = 2 – 1 = 1 This is just the root node with ht = 0
1 21 = 2 21+1 – 1 = 4 – 1 = 3 With ht = 1 you have 2 or 3 nodes
2 22 = 4 22+1 – 1 = 8 – 1 = 7 With ht = 2 you have 4 to 7 nodes
3 23 = 8 23+1 – 1 = 16 – 1 = 15 With ht = 3 you have 8 to 15 nodes
The importance of the height of the tree is that: we can place any new element in the heap
in an appropriate place by at most 1calculation at each level of the tree. This means that
the work done is at most the height of the tree which is log2 N. This will be demonstrated
in the following sections.
For every node in the heap, except the root, the key of the parent <= key of the child
5
BASIC HEAP OPERATIONS
6
Compare the 17 with the value of it parent which is 35.
Swop these values. This gives the following ‘heap’.
7
Example 2:
Instead of inserting a record with value 17 we had inserted a record with value 7 then:
8
7 is at the root of the tree: Quit. The heap is in order
The heap has a maximum height of : lower bound ( log2 N ) = log2 11 =~ log2 23.3 = 3
As you can see you require at most 3 operations to put the new record in the correct
place. Often you need less operations than this.
To delete the minimum value from the heap is very easy. This value is in the root node. It
is simply removed from there.The heap is now not in order. So it must put back into its
correct order.
9
Example 1:
10
1) The value of the root node has been removed. Consider this to be the ‘hole’ node.
2) Remove the right-most node on the lowest level & keep it in “temp” temp = 38
3) Find the smallest child node of the ‘hole’ node. Its node with value = 17
If it has no children: put ‘temp’ in ‘hole” node; Quit
4) Compare the ‘temp” node with this smallest child node
5) If “temp” node >= smallest child node:
a. Put smallest child record in hole record Put 17 in ‘hole’ node
b. Change ‘hole’ node to smallest child node ‘hole’ is old node 17
c. Go to step 3 Go to step 3
6) If “temp” node < smallest child node:
a. Put “temp” node in ‘hole’ node
b. Quit
11
1) The value of the root node has been removed. Consider this to be the ‘hole’ node.
2) Remove the right-most node on the lowest level & keep it in “temp”
3) Find the smallest child node of the ‘hole’ node. Its node with value = 35
If it has no children: put ‘temp’ in ‘hole” node; Quit
4) Compare the ‘temp” node with this smallest child node
5) If “temp” node >= smallest child node:
a. Put smallest child record in hole record Put 35 in ‘hole’ node
b. Change ‘hole’ node to smallest child node ‘hole’ is old node 35
c. Go to step 3 Go to step 3
6) If “temp” node < smallest child node:
a. Put “temp” node in ‘hole’ node
b. Quit
12
1) The value of the root node has been removed. Consider this to be the ‘hole’ node.
2) Remove the right-most node on the lowest level & keep it in “temp”
3) Find the smallest child node of the ‘hole’ node. Its node with value = 43
If it has no children: put ‘temp’ in ‘hole” node; Quit
4) Compare the ‘temp” node with this smallest child node
5) If “temp” node >= smallest child node:
a. Put smallest child record in hole record
b. Change ‘hole’ node to smallest child node
c. Go to step 3
6) If “temp” node < smallest child node:
a. Put “temp” node in ‘hole’ node Put 38 in ‘hole’ node
b. Quit
13
Example 2:
Had the value 38 in the original tree been 58 instead the process would have continued on
for one further iteration before it was placed. The remaining steps are illustrated below
14
1) The value of the root node has been removed. Consider this to be the ‘hole’ node.
2) Remove the right-most node on the lowest level & keep it in “temp”
3) Find the smallest child node of the ‘hole’ node. Its node with value = 43
If it has no children: put ‘temp’ in ‘hole” node; Quit
4) Compare the ‘temp” node with this smallest child node
5) If “temp” node >= smallest child node:
a. Put smallest child record in hole record Put 43 in ‘hole’ node
b. Change ‘hole’ node to smallest child node ‘hole’ is old node 43
c. Go to step 3 Go to step 3
6) If “temp” node < smallest child node:
a. Put “temp” node in ‘hole’ node
b. Quit
15
1) The value of the root node has been removed. Consider this to be the ‘hole’ node.
2) Remove the right-most node on the lowest level & keep it in “temp”
3) Find the smallest child node of the ‘hole’ node.
If it has no children: put ‘temp’ in ‘hole” node; Quit. Put 58 in ‘hole’ node. Quit
4) Compare the ‘temp” node with this smallest child node
5) If “temp” node >= smallest child node:
a. Put smallest child record in hole record
b. Change ‘hole’ node to smallest child node
c. Go to step 3
6) If “temp” node < smallest child node:
a. Put “temp” node in ‘hole’ node
b. Quit
HEAPSORT
As the name implies Heapsort is an algorithm that can be used to sort records into order.
Naturally it uses a heap to effect the sort.
16
To insert N records takes time: O(N * log2N)
To delete & reorder N items takes O ( N *(1 + log2N) )
In total the order of the sort is O(N * log2N)
At first glance one may think that the space required for the sort will be:
N elements for the unsorted array; and
N elements for the sorted array.
By a clever, but simple, trick only one array of N elements is needed. Essentially the
unsorted array is kept in the left hand side of the array while the sorted array is kept in the
right hand side of the array.
Example: Use heapsort to sort the following records into ascending order
1) Insert all the values into the heap. The picture below shows the original heap after all
the values have been inserted
2) Remove the minimum element. Keep this value however. The new reduced heap
is shown below.
17
3) Observe that the 7th slot in the array is empty. Store the removed element there.
The sorted elements are marked. In other words the element that has been
removed from the array is stored in the slot that has been freed up by the heap
shrinking by one element. The array thus becomes:
18
4) Now remove the minimum element & keep it. Reordeer the heap. Put the
minimum element into the 6th slot. This is the slot that is no longer required by the
decreased heap.
5) Now remove the minimum element & keep it. Reordeer the heap. Put the
minimum element into the 5th slot. This is the slot that is no longer required by the
decreased heap.
19
6) Continue this process until all the elements are in order. This is shown below.
20
If you want the records sorted in ascending order then access them from the last element
of the array to the first. If you want then in descending order access them for the 1st to
the last element.
It is possible to invert the heap order property so that P > X. In this case items in the heap
would be removed from largest to smallest. When using Heapsort the final sorted array
would be the opposite way around
QUESTIONS
1) What is a heap?
4) Insert the following values into a heap: 44, 67, 88, 1, 5, 65, 43, 67
6) Using Heapsort sort the following values into ascending order: 5, 7, 12, 45, 66,
90, 2, 19
ANSWERS
1) A heap is: a binary tree that is completely filled except possibly for the lowest
layer, which is filled from left to right
3) The heap order property is Key of parent X <= Key of X (except for the root)
4) Insert the following values into a heap: 44, 67, 88, 1, 5, 65, 43, 67, 4. The final
heap will be:
21
5) Delete the minimum element & re-order the heap
22
6) Using Heapsort sort the following values into ascending order: 5, 27, 1, 25, 16,
90, 3, 19
The items are now deleted form the heap in the order, 1, 3, 5, 16, 19, 25, 27, 90
23
24
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.