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

Priority Queues

The document discusses priority queues, which are queues where each item contains a field used to order it relative to other items. Items enter in any order but exit in minimum order. A priority queue can be implemented as a linked list or binary heap. A binary heap implementation is more efficient, with insertion and deletion operations taking O(logN) time rather than O(N) for a linked list. The key operations on a binary heap are inserting an item by "percolating" it up and deleting the minimum item by removing it and percolating down the replacement item.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Priority Queues

The document discusses priority queues, which are queues where each item contains a field used to order it relative to other items. Items enter in any order but exit in minimum order. A priority queue can be implemented as a linked list or binary heap. A binary heap implementation is more efficient, with insertion and deletion operations taking O(logN) time rather than O(N) for a linked list. The key operations on a binary heap are inserting an item by "percolating" it up and deleting the minimum item by removing it and percolating down the replacement item.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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.

Arrivals Priority Queue (Symbolic)


File 1 9h25 File 1, 0925
File 2 10h27 File 1, 0925; File 2 1027
File 3 6h23 File 3 623; File 1, 0925; File 2 1027
File 4 10h05 File 3 623; File 1, 0925; File 4 1005; File 2 1027

A PRIORITY QUEUE should be thought of as an Abstract Data Structure with the two
methods INSERT and DELETEMIN:

INSERT (item) ===> Priority Queue ===> DELETEMIN(item)

Insert places a new item in the priority queue in the correct place.
DeleteMin removes the smallest item then the Priority queue is reordered

EXAMPLES OF PRIORITY QUEUES

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.

Scheduler for an Operating System


Queue in a shortest path graph algorithm
A queue for Discrete Event Simulation
Sorting method using Heapsort

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.

The TOTAL work of any one item is:


For the linked list implementation:
Insertion O(N)
Removal 1
Total 1 + O(N) = O(N)
For the heap implementation:
Insertion O(log2N)
Removal 1 + O(log2N) (1 -- To remove root +
O(log2N) -- to reorder heap)
Total 1 + O(log2N) + O(log2N) = O(log2N)

Clearly the heap implementation is favoured because of its efficiency.

2
PRIORITY QUEUE IMPLEMENTED AS A BINARY HEAP

A HEAP is a special BINARY TREE. It is a COMPLETE binary tree except possibly


for the lowest (or leaf) level. This leaf level is filled strictly from left to right as illustrated
below. (If another node is added it would be added as the right child of node “E”.

A heap has 2 special properties:


1) The STRUCTURE property; and
2) The HEAP ORDER property

A Binary Heap requires no pointers in its implementation and is optimally represented as


an array .

3
STRUCTURE PROPERTY

The STRUCTURE PROPERTY is simply that the Heap is a COMPLETELY filled


binary tree with the possible exception of the bottom level. The bottom level is filled
from left to right.

Using this array data structure there are the following useful & efficient properties.

1) For any node at position I:


Except for the root. The parent of the node I is in position I div 2

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.)

2) For any element in the heap in position I


Its left child will be found at position 2*I
Its right child will be found at position 2*I + 1

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

For Node D at position 4


Its left child Node B is at position 2*I = 2*4 = 8
Its right child Node C is at position 2*I + 1 = 2*4 + 1 = 9

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.

HEAP ORDER PROPERTY

For every node in the heap, except the root, the key of the parent <= key of the child

Below is an example of a heap and a non-heap:

5
BASIC HEAP OPERATIONS

1) INSERT an ITEM in a PRIORITY QUEUE

The algorithm is as follows:


1) Insert the new item at the next position in the leaf (or lowest) layer of the heap.
Assume its value is “X”. If the lowest layer is completely full start a new layer &
insert the item in th e first position of that layer.
2) If “X” is at the root of the tree: Quit. The heap is in order
Compare “X” with the value of is parent.
If “X” >= value of parent: Quit. The heap is in order.
If “X” < value of parent: Swop these values.
REPEAT step 2 (starting at the new position of “X”).

This is known as a “percolate up” strategy.

Example: Insert the item with value 17 into the heap.


The initial state of the unordered ‘heap’ is shown below.

Compare the 17 with the value of it parent which is 38.


Swop these values.
This gives the following ‘heap’.

6
Compare the 17 with the value of it parent which is 35.
Swop these values. This gives the following ‘heap’.

Compare the 17 with the value of it parent which is 12.


It is less than the value of its parent. The heap is in order. QUIT.

7
Example 2:
Instead of inserting a record with value 17 we had inserted a record with value 7 then:

The same sequence would occur.

Compare the 7 with the value of it parent which is 12.


Swop these values. This gives the following ‘heap’

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.

2) DELETE the MINIMUM VALUE from the HEAP

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.

The algorithm is:


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
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

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.

The algorithm is as follows:


1) Insert all the N records in a heap;
2) While heap not empty deleteMin.

This is a very efficient algorithm:

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?

2) For a heap represented as an array. For any element at position X.


What is the position of the parent of X?
What is the position of the left son of X?
What is the position of the right son ox X?

3) What is the heap order property:

4) Insert the following values into a heap: 44, 67, 88, 1, 5, 65, 43, 67

5) Delete the minimum element & re-order the heap

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

2) For a heap represented as an array. For any element at position X.


What is the position of the parent of X? X div 2
What is the position of the left son of X? 2*X
What is the position of the right son ox X? 2 * X + 1

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

6) Again 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

The next three reduced heaps are given:

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy