0% found this document useful (0 votes)
112 views68 pages

Understanding Priority Queues in Data Structures

DSA

Uploaded by

perewa7600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views68 pages

Understanding Priority Queues in Data Structures

DSA

Uploaded by

perewa7600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRIORITY QUEUE

Dr. CHANDRALEKHA M
ASSISTANT PROFESSOR
DEPT. OF COMPUTER SCIENCE AND ENGINEERING
AMRITA SCHOOL OF ENGINEERING, CHENNAI CAMPUS
Mob. No: +91 9442414745
What is a priority queue?
• A priority queue is a special type of queue in which each element is
associated with a priority value. And, elements are served on the
basis of their priority. That is, higher priority elements are served first.
• However, if elements with the same priority occur, they are served
according to their order in the queue.
Consider a situation where you have 4 assignments, as of Monday:
[Link] Management Systems, due on Thursday
[Link], due on Tuesday
[Link] Structures and Algorithms, due on Wednesday
[Link] System, due on Friday
As a student, overburdened with work, if you do first come, first serve,
you might not be able to submit all your assignments on time. So
instead, you assign a priority to each of them and get them done in that
order. In this case:
[Link] Management Systems, due on Thursday Priority Number 3
[Link], due on Tuesday Priority Number 1
[Link] Structures and Algorithms, due on Wednesday Priority Number
2
[Link] System, due on Friday Priority Number 4
It is important to note that a priority queue only supports elements that
are comparable.
Elements are called comparable if they are either lesser than, equal to or
greater than one another, i.e. if the two elements are a and b, they should
follow one of the conditions:
i) a<b
ii) a=b
iii) a>b
• As a result, the items in the priority queue are arranged in ascending or
descending order.
• It is an Abstract Data Type that functions a little differently than a normal
Queue. In a normal Queue, first-in-first-out is followed which means that
whichever elements enters the queue first, will be the first to leave. Just
like any queue we form, the person that stands first in the queue, will be
addressed first and similarly, even in computer science queues, the same
rule applies.
• However, in a Priority Queue, each item in the queue is assigned a
priority, and items are dequeued or removed based on this priority value
assigned to them. Hence, the items in the priority queue are arranged in
either ascending or descending order depending on the priority value
taken by the user. (in such a way that the highest priority elements are
moved to the beginning of the queue and the lowest priority elements are
moved to the back of the queue).
Assigning Priority Value
• Generally, the value of the element itself is considered for assigning
the priority.
For example,
• The element with the highest value is considered the highest priority
element. However, in other cases, we can assume the element with the
lowest value as the highest priority element.
• We can also set priorities according to our needs.
Characteristics of a Priority queue
A priority queue is an extension of a queue that contains the following
characteristics:
• Every element in a priority queue has some priority associated with it.
• An element with the higher priority will be deleted before the deletion
of the lesser priority.
• If two elements in a priority queue have the same priority, they will be
arranged using the FIFO principle.
Let's understand the priority queue through an example.
• We have a priority queue that contains the following values:
• 1, 3, 4, 8, 14, 22
• All the values are arranged in ascending order.
Now, we will observe how the priority queue will look after performing
the following operations:
poll(): This function will remove the highest priority element from the
priority queue. In the above priority queue, the '1' element has the
highest priority, so it will be removed from the priority queue.
add(2): This function will insert '2' element in a priority queue. As 2 is
the smallest element among all the numbers so it will obtain the highest
priority.
poll(): It will remove '2' element from the priority queue as it has the
highest priority queue.
add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser
than 8, so it will obtain the third highest priority in a priority queue.
Difference between Priority Queue and Normal Queue
In a queue, the first-in-first-out rule is implemented whereas, in a
priority queue, the values are removed on the basis of priority. The
element with the highest priority is removed first.
Types of Priority Queue
There are two types of priority queue:
Ascending order priority queue: In ascending order priority queue, a
lower priority number is given as a higher priority in a priority queue.
For example, we take the numbers from 1 to 5 arranged in an ascending
order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the
highest priority in a priority queue.
Descending order priority queue: In descending order priority queue,
a higher priority number is given as a higher priority in a priority queue.
For example, we take the numbers from 1 to 5 arranged in descending
order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as
the highest priority in a priority queue.
Representation of priority queue
Now, we will see how to represent the priority queue through a one-way
list.
We will create the priority queue by using the list given below in which
INFO list contains the data elements, PRN list contains the priority
numbers of each data element available in the INFO list, and LINK
basically contains the address of the next node.
Let's create the priority queue step by step.
In the case of priority queue, lower priority number is considered the higher
priority, i.e., lower priority number = higher priority.
Step 1: In the list, lower priority number is 1, whose data value is 300, so it will
be inserted in the list as shown in the below diagram:
Step 2: After inserting 300, priority number 2 is having a higher priority, and
data values associated with this priority are 200 and 100. So, this data will be
inserted based on the FIFO principle; therefore 200 will be added first and then
100.
Step 3: After inserting the elements of priority 2, the next higher priority number
is 3, and the value associated with priority 3 is 600.
Step 4: After inserting the elements of priority 3, the next higher priority number
is 4 and data elements associated with 4 priority numbers are 400, 500, 700. In
this case, elements would be inserted based on the FIFO principle; therefore, 400
will be added first, then 500, and then 700.
Implementation of Priority Queue

• The priority queue can be implemented in four ways that include


arrays, linked list, heap data structure and binary search tree.
• The heap data structure is the most efficient way of implementing the
priority queue, so we will implement the priority queue using a heap
data structure.
Complete Binary Tree
• A complete binary tree is a binary tree in which all the levels are
completely filled except possibly the lowest one, which is filled from
the left.
• A complete binary tree is just like a full binary tree, but with two
major differences
✓All the leaf elements must lean towards the left.
✓The last leaf element might not have a right sibling i.e. a complete
binary tree doesn't have to be a full binary tree.
NOT A COMPLETE BINARY TREE
What is a Full binary tree?
• A full binary tree can be defined as a binary tree in which all the nodes
have 0 or two children.
• In other words, the full binary tree can be defined as a binary tree in
which all the nodes have two children except the leaf nodes.
Creation of Complete Binary Tree
Suppose we have an array of 6 elements shown as below:
The above array contains 6 elements, i.e., 1, 2, 3, 4, 5, 6. The following
are the steps to be used to create a complete binary tree:
Step 1: First, we will select the first element of the array, i.e., 1, and
make a root node of the tree. The number of elements available in the
first level is 1.
Step 2: Now, we will select the second and third elements of the array.
Keep the second element and third element of the array as the left and
right child of the root node respectively shown as below:
As we can observe that the number of elements available in the second
level is 2.
Step 3: Now, we will select the next two elements from the array, i.e., 4
and 5. Keep these two elements on the left and right of node 2 shown as
below:
As we can observe that nodes 4 and 5 are the left and right child of node
2 respectively.
Step 4: Now, we will select the last element of the array, i.e., 6, and
keep it as left child of the node 3 as we know that in a complete binary
tree, the nodes are filled from the left side shown as below:
As we can observe that the second level contains 3 elements.
Heap data structure is a complete binary tree that satisfies the heap
property, where any given node is
• always greater than its child node/s and the key of the root node is the
largest among all other nodes. This property is also called max heap
property.

• always smaller than the child node/s and the key of the root node is the
smallest among all other nodes. This property is also called min heap
property.
Max heap: The max heap is a heap
in which the value of the parent node
is greater than the value of the child
nodes.
Min heap: The min heap is a heap in which
the value of the parent node is less than the
value of the child nodes.
• This type of data structure is also called a binary heap.
• Both the heaps are the binary heap, as each has exactly two child
nodes.
Heap Operations
• Every data structure has some operations like insertion, deletion
associated with them.
• Heaps are no different, and there are many operations that can be
performed on heap data structures.
• We will be discussing these operations over a max-heap since the same
operations can be applied to a min-heap also.
Insertion in Heap
• Consider we have an array with elements 10, 8, 5, 15, 6 in it.
• To build a max-heap using the given elements, Follow the under-
given steps –
[Link] the first element, 10 in the tree. It is the root element in the tree:
2. Add the next element in the tree. Compare it with the parent
element.
• If it is greater than its parent element, swap their positions. It is done
to ensure that the tree follows heap conditions, and a max-heap is
maintained each time an element is added.
• Here, we should add the second element, 8 as the left child of the root
element because a heap is filled from left to right.
• No swapping occurs here because 10 is greater than 8.
3. Repeat the above-given step with the next element, 5.
4. Add the next element, 15 in the tree.
Now since 15 is greater than its parent element 8, this tree is not a max-
heap anymore. To make it a heap again, we will swap the positions
of 8 and 15.
Again the obtained tree is not a max-heap since 15 is greater than its
parent element, 10. We will again swap the positions of 10 & 15.
• This step is performed using recursion, and done until the inserted
element finds its correct position in the heap.
• Notice that 15 was first added at the bottom of the tree and then moved
up to its correct position. This moving up of elements is known as
bubbling up.
5. Add the last element, 6 in the heap by comparing it with the parent.
• With this, we have added all the elements of the given array into a
heap.
• One thing to note here is that a comparison is done each time an
element is added. The number of comparisons also depends on the
height of the tree. In the above case, a total of 5 comparisons were
made.
• But we can reduce the number of comparisons by using a method
called heapify where elements are first added into the tree and then
arranged in a bottom-up fashion. It helps in reducing the number of
comparisons, and thus the time complexity of the overall algorithm.
• So, let us understand how to heapify a binary tree.
How to Heapify a Binary Tree?
• Heapify is the process of rearranging the elements to form a tree that
maintains the properties of the heap data structure.
• Recall the list/array that had the elements – 10, 8, 5, 15, 6 in it.
• To heapify these elements, and form a max-heap, let us follow the
under-given steps –
[Link] all the elements of the list as a complete binary tree –
Treat the elements of the given array as the nodes of a tree. To visualize an array
as a binary tree, refer to the part where we have discussed the array
representation of the binary tree.
Notice how the above-given binary tree is a complete
binary tree but does not satisfy the properties of a max-
heap since element 8 has an element greater than itself
as its child.
2. Start from comparing the values of children nodes with that of the
parent. If the value of the parent is smaller than the values of the
children, swap it. Swapping is done with a larger of two children. This
process is repeated until every node satisfy the properties of a max-
heap –
Here, we start comparing 8 with 15 and 6. Now, since 15 is greater
than 8, we will swap their positions.
Again, the property of max-heap is not satisfied since 15 is greater
than 10. Therefore, we will once again perform the above step.
Now that we have obtained a max-heap, we can stop this step.
• One interesting thing to note here is that a node can be heapified if and
only if all the children nodes are already heapified. This is the reason
why we start from the bottom-most sub-tree.
• This step is also performed using recursion. We create a function
called heapify() that works by dividing the tree into smaller sub-trees
and then comparing the values of parents with that of children in each
sub-tree.
• Notice that the number of comparisons are reduced slightly. This way,
we can significantly reduce the comparisons if there are many
elements to be added.
Deletion from Heap
Let us consider the following max-heap that we created in the last step-
To delete the elements from a heap, we follow the under-given steps –
1. Search for the element to be deleted and swap it with the last
element in the heap, let’s say we want to delete 10-

Here, we have swapped the position of 10 with the last element that is 6
2. Remove the element from the tree –

But now, the remaining heap is not a max-heap anymore. So, as the next
step, we should heapify it once again.
3. Heapify the tree again-

Here, we have once again heapified the given tree to form a max-heap.
Thus, we have successfully removed 10 from the heap.
Priority Queue Operations
The operations on a Priority Queue in a heap are done in the following
way:
• A heap is a tree based data structure. A max-heap is one where the key
value of the parent node is always greater than or equal to the key
value of the child node. Hence, the element with the largest key value
comes at the root.
• Conversely, a min-heap is one where the key value of the parent node
is always lesser than or equal to the key value of the child node.
Hence, the element with the lowest key value comes at the root.
Let’s say the elements are 1,4,2,7,8,5. The max-heap of these elements
would look like:
Now, let’s try to insert a new element, 6. Since there are nodes present
in the heap, we insert this node at the end of heap so it looks like this:
Then, heapify operation is implemented. After which, the heap will look
like this:
Now, let’s try to delete an element, 6. Since this is not a leaf node, we
swap it with the last leaf node so it looks like this:
Then, we remove the leaf node so it looks like this:
Then, heapify operation is implemented. After which, the heap will look
like this:
a. Peeking from the Priority Queue
This will return the maximum element if a max-heap is used and the
minimum number if a min-heap is used. To do both of these, we return
root node. This is because in the max-heap or the min-heap the maximum
or minimum element will be present at the root node respectively.
b. Extract-Max/Min from the Priority Queue
This also returns the maximum element if a max-heap is used and the
minimum number if a min-heap is used. However, in this case, the
maximum/minimum element will also be removed from the heap data
structure. It hence returns the maximum or minimum after extracting or
removing it from the heap.
Applications of Priority queue
The following are the applications of a Priority Queue:
• It is used in Djikstra’s Algorithm – To find the shortest path between nodes in a
graph.
• It is used in Prim’s Algorithm – To find the Minimum Spanning Tree in a weighted
undirected graph.
• It is used in Heap Sort – To sort the Heap Data Structure
• It is used in Huffman Coding – A Data Compression Algorithm
• It is used in Operating Systems for:
✓Priority Scheduling – Where processes must be scheduled according to their
priority.
✓Load Balancing – Where network or application traffic must be balanced across
multiple servers.
✓Interrupt Handling – When a current process is interrupted, a handler is assigned
to the same to rectify the situation immediately.
• A* Search Algorithm – A graph traversal and a path search algorithm
Priority Queue using Linked list

• A priority queue is a type of queue in which each element in a queue is


associated with some priority, and they are served based on their
priorities. If the elements have the same priority, they are served based
on their order in a queue.
• Mainly, the value of the element can be considered for assigning the
priority. For example, the highest value element can be used as the
highest priority element. We can also assume the lowest value element
to be the highest priority element. In other cases, we can also set the
priority based on our needs.
The following are the functions used to implement priority queue using
linked list:
push(): It is used to insert a new element into the Queue.
pop(): It removes the highest priority element from the Queue.
peep(): This function is used to retrieve the highest priority element
from the queue without removing it from the queue.
• The linked list of priority queue is created in such a way that the
highest priority element is always added at the head of the queue.
Let's understand through an example.
Consider the below-linked list that consists of elements 2, 7, 13, 15.
• Suppose we want to add the node that contains the value 1.
• Since the value 1 has more priority than the other nodes so we will
insert the node at the beginning of the list shown as below:
• Now we have to add 7 element to the linked list.
• We will traverse the list to insert element 7.
• First, we will compare element 7 with 1; since 7 has lower priority
than 1, so it will not be inserted before 7.
• Element 7 will be compared with the next node, i.e., 2; since element 7
has a lower priority than 2, it will not be inserted before 2..
• Now, the element 7 is compared with a next element, i.e., since both
the elements have the same priority so they will be served based on the
first come first serve.
• The new element 7 will be added after the element 7 shown as below:
THANK YOU

You might also like