0% found this document useful (0 votes)
4 views14 pages

Application of Heap Data Structure-Priority Queues 26

The document explains the concept of priority queues, which are similar to regular queues but prioritize elements based on their priority levels. It details the implementation of priority queues using heaps, specifically focusing on max-priority queues, and outlines operations such as maximum retrieval, extraction of the maximum, increasing values, and inserting new values, along with their time complexities. Additionally, it briefly mentions the application of priority queues in job scheduling and poses a question regarding min-priority queue operations.

Uploaded by

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

Application of Heap Data Structure-Priority Queues 26

The document explains the concept of priority queues, which are similar to regular queues but prioritize elements based on their priority levels. It details the implementation of priority queues using heaps, specifically focusing on max-priority queues, and outlines operations such as maximum retrieval, extraction of the maximum, increasing values, and inserting new values, along with their time complexities. Additionally, it briefly mentions the application of priority queues in job scheduling and poses a question regarding min-priority queue operations.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Application of Heap Data

Structure
Priority Queues
Priority Queue
• Priority Queue is similar to queue where we insert
an element from the back and remove an element
from front, but with a one difference that the logical
order of elements in the priority queue depends on
the priority of the elements.
• The element with highest priority will be moved to
the front of the queue and one with lowest priority
will move to the back of the queue.
• Thus it is possible that when you enqueue an
element at the back in the queue, it can move to
front because of its highest priority.
Example:
• Let’s say we have an array of 5 elements :
{4, 8, 1, 7, 3} and we have to insert all the
elements in the max-priority queue.
Naive Approach:
– Suppose we have N elements and we have to insert
these elements in the priority queue.
– We can use list and can insert elements in O(N)
time and can sort them to maintain a priority
queue in O(N logN ) time.
Efficient Approach:
– We can use heaps to implement the priority queue.
– It will take O(log N) time to insert and delete each
element in the priority queue.
– Based on heap structure, priority queue also has
two types max- priority queue and min - priority
queue.
• Max Priority Queue is based on the structure
of max heap and can perform following
operations:

maximum(Arr) : It returns maximum element


from the Arr.
extract_maximum (Arr) - It removes and return
the maximum element from the Arr.
increase_val (Arr, i , val) - It increases the key of
element stored at index i in Arr to new value val.
insert_val (Arr, val ) - It inserts the element with
value val in Arr.
Maximum :

int maximum(int Arr[ ])


{
return Arr[ 1 ];
//as the maximum element is the root element in the max heap.
}

Complexity: O(1)
Extract Maximum: In this operation, the maximum element will be returned
and the last element of heap will be placed at index 1 and max_heapify will
be performed on node 1 as placing last element on index 1 will violate the
property of max-heap.
int extract_maximum (int Arr[ ])
{
if(length == 0)
{ cout<< “Can’t remove element as queue is empty”;
return -1;
}
int max = Arr[1];
Arr[1] = Arr[length];
length = length -1;
max_heapify(Arr, 1);
return max;
} Complexity: O(logN)
Increase Value: In case increasing value of any node, may violate the
property of max-heap, so we will swap the parent’s value with the node’s
value until we get a larger value on parent node.

void increase_value (int Arr[ ], int i, int val)


{
if(val < Arr[ i ])
{ cout<<”New value is less than current value, No need to
swap” <<endl;
return;
}
Arr[ i ] = val;
while( i > 1 and Arr[ i/2 ] < Arr[ i ]) // Bubble up
{ swap (Arr[ i/2 ], Arr[ i ]);
i = i/2;
}
Insert Value :
void insert_value (int Arr[ ], int val)
{
length = length + 1;
Arr[ length ] = -1;
//assuming all the numbers greater than 0
are to be inserted in queue.
increase_val (Arr, length, val);
}

Complexity: O(log N)
Example:
Initially there are 5 elements in priority queue.
Operation: Insert Value(Arr, 6)
Operation: Extract Maximum:
In the diagram below, after removing 8 and placing 4 at node 1,
violates the property of max-priority queue. So max_heapify(Arr, 1)
will be performed which will maintain the property of max - priority
queue.
• As discussed above, like heaps we can use
priority queues in scheduling of jobs.
• When there are N jobs in queue, each having its
own priority.
• If the job with maximum priority will be
completed first and will be removed from the
queue, [we can use priority queue’s operation
extract_maximum here].
• If at every instant we have to add a new job in
the queue, we can use insert_value operation as
it will insert the element in O(log N) and will also
maintain the property of max heap.
Now, let’s give you a problem to ponder on:
1) How will you perform below operations for min
priority queue?

a. minimum (Arr) - return minimum element from


Arr.
b. extract_minimum (Arr) - removes and returns
minimum element from Arr.
c. decrease_value (Arr, i, val) - decrease value of
element index at i in Arr to val. It is assumed that
val is at least as small as current value of Arr[i].
d. insert_value (Arr, val) - insert value in Arr.
Wishing you all
the Best

You might also like