0% found this document useful (0 votes)
47 views25 pages

Priority Queues (Heaps)

Priority queues are data structures where each element has a priority associated with it. Elements with higher priority are served before those with lower priority. There are two main types: ascending order queues prioritize the lowest numbers, while descending order queues prioritize the highest numbers. Priority queues can be implemented using arrays or linked lists. With arrays, elements are inserted and deleted based on priority. With linked lists, each node contains data, a next pointer, and a priority value.

Uploaded by

MOSES ALLEN
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)
47 views25 pages

Priority Queues (Heaps)

Priority queues are data structures where each element has a priority associated with it. Elements with higher priority are served before those with lower priority. There are two main types: ascending order queues prioritize the lowest numbers, while descending order queues prioritize the highest numbers. Priority queues can be implemented using arrays or linked lists. With arrays, elements are inserted and deleted based on priority. With linked lists, each node contains data, a next pointer, and a priority value.

Uploaded by

MOSES ALLEN
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
You are on page 1/ 25

DATA STRUCTURES AND ALGORITHMS

Priority Queues (Heaps).


Priority Queues
Priority Queue is an abstract data type that is similar to a queue, and every element has some priority
value associated with it. The priority of the elements in a priority queue determines the order in which
elements are served (i.e., the order in which they are removed). If in any case the elements have same
priority, they are served as per their ordering in the queue. You can think of a priority queue as several
patients waiting in line at a hospital. Here, the situation of the patient defines the priority order. The
patient with the most severe injury would be the first in the queue.

Therefore all the elements are either arranged in an ascending or descending order.

Properties of Priority Queue

So, a priority Queue is an extension of the queue with the following properties.
• Every item has a priority associated with it.
• An element with high priority is dequeued before an element with low priority.
• If two elements have the same priority, they are served according to their order in the queue.
(the priority queue follows the first-in-first-out principle for de queue operation).

In the below priority queue, an element with a maximum ASCII value will have the highest priority.
The elements with higher priority are served first.

Difference between Priority Queue and Normal Queue?

There is no priority attached to elements in a queue, the rule of first-in-first-out(FIFO) is


implemented whereas, in a priority queue, the elements have a priority. The elements with higher
priority are served first.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 1


Types of Priority Queue:

Types of Priority Queues

1) Ascending Order Priority Queue


As the name suggests, in ascending order priority queue, the element with a lower priority value is
given a higher priority in the priority list. An ascending order priority queue gives the highest priority
to the lower number in that queue. For example, you have six numbers in the priority queue that are
4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order. The new list is as
follows: 4, 8, 12, 20. 35, 45. In this list, 4 is the smallest number. Hence, the ascending order priority
queue treats number 4 as the highest priority.
4 8 12 20 35 45
In the above table, 4 has the highest priority, and 45 has the lowest priority.

2) Descending order Priority Queue


The root node is the maximum element in a max heap, as you may know. It will also remove the
element with the highest priority first. As a result, the root node is removed from the queue. This
deletion leaves an empty space, which will be filled with fresh insertions in the future. The heap
invariant is then maintained by comparing the newly inserted element to all other entries in the queue.
A descending order priority queue gives the highest priority to the highest number in that queue. For
example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will
arrange these numbers in ascending order. The new list is as follows: 45, 35, 20, 12, 8, 4. In this list,
45 is the highest number. Hence, the descending order priority queue treats number 45 as the highest
priority.

45 35 20 12 8 4
In the above table, 4 has the lowest priority, and 45 has the highest priority.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 2


How to Implement Priority Queue:

Priority queue can be implemented using the following data structures:


A) Implementing Priority Queues using Arrays:

Priority Queue implementation using an array is one of the basic methods to implement a queue. In
this element, it is inserted and deleted based on its priority. The elements in the priority queue have
some priority. The priority of the element is used to determine the order in which the elements will
be processed.
We can store a heap in an array by mapping its elements row by row – from top left to bottom right
– to the array:

Mapping a min-heap to an array


Our example heap looks like this as an array:

Array representation of the min-heap

In a min-heap, the smallest element is always at the top, i.e., in the array, it is always at the first
position. This is why, when you print a Java PriorityQueue as a string, you see the smallest element
on the left. What you see is the array representation of the min-heap underlying the PriorityQueue

These are the rules for processing elements in a priority queue.

✓ The element with the highest priority is processed first.


✓ The two elements with equal priority are processed based on a First Come First
Serve(FCFS) basis.
The priority of the elements is set based on several factors. This is basically used in the operating
system to execute the most priority process first. If this is set on the CPU time, then the element
with the lowest execution time is executed first. Suppose there are two processes in a queue, one has
5ns execution time and the other has 10ns execution time, then the process having 5ns executes
first.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 3


C program to implement Priority Queue using array
In the given example, we have asked the user for operations like create, insert, delete, check priority,
and display. As indicated by the choice entered by the user, access its respective function using the
switch statement.

❖ In the function insert_element(), initially check if the queue is full. If it is, then print the
result as "Queue Overflow". If not, take the number to be inserted as input and insert a new
element into the priority queue.
❖ In the function delete_element(), firstly check if the queue is empty. If it is, then print the
result as "Queue Underflow". Otherwise, remove the element and the priority from the
front of the queue.
❖ In the function display_priorityqueue(), using for loop print all the elements of the queue
starting from front to rear.
❖ The function check_priority() is used to check the priority and place element.

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

void create_queue();
void insert_element(int);
void delete_element(int);
void check_priority(int);
void display_priorityqueue();

int pqueue[MAX];
int front, rear;

void main()
{
int n, choice;
printf("\nEnter 1 to insert element by priority ");
printf("\nEnter 2 to delete element by priority ");
printf("\nEnter 3 to display priority queue ");
printf("\nEnter 4 to exit");
create_queue();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert : ");
scanf("%d",&n);
insert_element(n);
break;
case 2:
printf("\nEnter element to delete : ");
scanf("%d",&n);

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 4


delete_element(n);
break;
case 3:
display_priorityqueue();
break;
case 4:
exit(0);
default:
printf("\n Please enter valid choice");
}
}
}
void create_queue()
{
front = rear = -1;
}
void insert_element(int data)
{
if (rear >= MAX - 1)
{
printf("\nQUEUE OVERFLOW");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pqueue[rear] = data;
return;
}
else
check_priority(data);
rear++;
}
void check_priority(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pqueue[i])
{
for (j = rear + 1; j > i; j--)
{
pqueue[j] = pqueue[j - 1];
}
pqueue[i] = data;
return;
}
}
pqueue[i] = data;
}
void delete_element(int data)
{
int i;

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 5


if ((front==-1) && (rear==-1))
{
printf("\nEmpty Queue");
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pqueue[i])
{
for (; i < rear; i++)
{
pqueue[i] = pqueue[i + 1];
}
pqueue[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d element not found in queue", data);
}
void display_priorityqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nEmpty Queue ");
return;
}
for (; front <= rear; front++)
{
printf(" %d ", pqueue[front]);
}
front = 0;
}

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 6


OUTPUT:

B) Implementing Priority Queues using Linked List:


A node of a linked list for implementing priority queue will contain three parts −

• Data − It will store the integer value.


• Address − It will store the address of a next node
• Priority −It will store the priority which is an integer value. It can range from 0-10 where 0
represents the highest priority and 10 represents the lowest priority.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 7


Example

Input

Output

Operations

• Push: Pushing/inserting a new item in the priority queue at a position in accordance with

its priority

• Pop: Popping/removing a new item in the priority queue in accordance with its priority

• Peek: Displaying the topmost priority item of the queue

C- Program to implement Priority Queue using Linked List.


#include <stdio.h>
#include <stdlib.h>
// priority Node
typedef struct node
{
int data;
int priority;
struct node* next;
} Node;
Node* newNode(int d, int p)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = d;
temp->priority = p;
temp->next = NULL;
return temp;
}
int peek(Node** head)
{
return (*head)->data;

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 8


}
void pop(Node** head)
{
Node* temp = *head;
(*head) = (*head)->next;
free(temp);
}
void push(Node** head, int d, int p)
{
Node* start = (*head);
Node* temp = newNode(d, p);
if ((*head)->priority > p)
{
temp->next = *head;
(*head) = temp;
}
else
{
while (start->next != NULL &&
start->next->priority < p)
{
start = start->next;
}
// Either at the ends of the list
// or at required position
temp->next = start->next;
start->next = temp;
}
}
// Function to check the queue is empty
int isEmpty(Node** head)
{
return (*head) == NULL;
}
// main function
int main()
{
Node* pq = newNode(7, 1);
push(&pq, 1, 2);
push(&pq, 3, 3);
push(&pq, 2, 0);
while (!isEmpty(&pq))
{
printf("%d ", peek(&pq));
pop(&pq);
}
return 0;
}

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 9


OUTPUT:

C) Implementing Priority Queues using Binary heaps.

Heap data structure can be used to implement a priority queue. A heap data structure should not be
confused with the heap, a pool of memory used for dynamic memory allocation. A common
implementation of a heap is the binary heap, which is defined as a binary tree with two additional
properties:

Properties of Heap
All right, now with the basics out of the way, let's take a closer look at the specific properties of the
heap data structure.
1. Ordering
Nodes must be arranged in an order according to values. The values should follow min-heap or
max-heap property.
In min-heap property, the value of each node, or child, is greater than or equal to the value of its
parent, with the minimum value at the root node.

Min-heap

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 10


In max-heap property, the value of each node, or child, is less than or equal to the value of its
parent, with the maximum value at the root node.

Max-heap

2. Structural
All levels in a heap should be full. In other words, it should be a complete binary tree:

• All levels of heap should be full, except the last one.


• Nodes or child must be filled from left to right strictly.
• Heap doesn't follow binary search tree principle. The values in right and left child or nodes
don't matter.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 11


Min Heap and Max Heap
A heap can be classified further as either a “max-heap” or a “min-heap”.
• In a max-heap, the keys of parent nodes are always greater than or equal to those of the
children, and the highest key is in the root node.
• In a min-heap, the keys of parent nodes are less than or equal to those of the children, and the
lowest key is in the root node.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 12


A binary heap is a complete binary tree, but we usually never use a binary tree for implementing
heaps. We store keys in an array and use their relative positions within that array to represent child-
parent relationships. The following diagram shows an array representing a min-heap:

The complete binary tree maps the binary tree structure into the array indices, where each array
index represents a node. The index of the left or the right child of the parent node is simple
expressions. For a zero-based array, the root node is stored at index 0. If i is the index of the current
node, then,

PARENT(i) = floor((i – 1)/2)


LEFT-CHILD(i) = 2×i + 1
RIGHT_CHILD(i) = 2×i + 2

Min Heap Property: A[PARENT[i]] <= A[i]


Max Heap Property: A[PARENT[i]] >= A[i]

floor() function always rounds down and returns the largest integer less than or equal to a given
number.

Suppose there are N Jobs in a queue to be done, and each job has its own priority. The job with
maximum priority will get completed first than others. At each instant, we are completing a job with
maximum priority and at the same time we are also interested in inserting a new job in the queue with
its own priority.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 13


So at each instant we have to check for the job with maximum priority to complete it and also insert
if there is a new job. This task can be very easily executed using a heap by considering N jobs
as N nodes of the tree.
As you can see in the diagram below, we can use an array to store the nodes of the tree. Let’s say we
have 7 elements with values {6, 4, 5, 3, 2, 0, 1}.

Note: An array can be used to simulate a tree in the following way. If we are storing one element at
index i in array Arr, then its parent will be stored at index i/2 (unless its a root, as root has no parent)
and can be accessed by Arr[i/2], and its left child can be accessed by Arr[2∗i] and its right child can
be accessed by Arr[2∗i+1]. Index of root will be 1 in an array.

Insertion and Deletion in Heaps

a) Process of Insertion:

Elements can be inserted to the heap following a similar approach for deletion. The idea is to:
• First increase the heap size by 1, so that it can store the new element.
• Insert the new element at the end of the Heap.
• This newly inserted element may distort the properties of Heap for its parents. So, in order to
keep the properties of Heap, heapify this newly inserted element following a bottom-up
approach.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 14


Max Heap Construction Algorithm

We shall use the same example to demonstrate how a Max Heap is created. The procedure to create
Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any point of
time, heap must maintain its property. While insertion, we also assume that we are inserting a node in
an already heapified tree.
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to be less than
that of the child node.
The item at the root of the heap has the highest priority among all elements. If we want to add a
new node to a binary heap, we need to ensure that our two properties of the heap are
maintained after the new node is added. At first, we insert the new item at the end of the
priority queue. If it is found greater than its parent node, elements are swapped. This process
continues until the new item is placed in the correct position. Let’s go through an example to
understand the insertion process. If we add to the priority queue, we end up with:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 15


We notice that is greater than its parent , so we swap them:

Then, is still greater than its new parent , so we swap them:

Now, we
notice that is smaller than its parent, so we stop and reach our priority queue:

The

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 16


Algorithm for insertion operation in the priority queue
1) if no node is present,
create a newNode.
2) else (the node is present)
insert the newNode at the end
3) perform heapify

If we insert an element in a priority queue, it will move to the empty slot by looking from top to
bottom and left to right.

If the element is not in a correct place then it is compared with the parent node; if it is found out of
order, elements are swapped. This process continues until the element is placed in a correct position.

b) Deletion in Heap
Given a Binary Heap and an element present in the given Heap. The task is to delete an element from this Heap.

The standard deletion operation on Heap is to delete the element present at the root node of the
Heap. That is if it is a Max Heap, the standard deletion operation will delete the maximum
element and if it is a Min heap, it will delete the minimum element.

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 17


Process of Deletion:
Since deleting an element at any intermediary position in the heap can be costly, so we can simply
replace the element to be deleted by the last element and delete the last element of the Heap.

• Replace the root or element to be deleted by the last element.


• Delete the last element from the Heap.
• Since, the last element is now placed at the position of the root node. So, it may not follow the
heap property. Therefore, heapify the last node placed at the position of root.

Max Heap Deletion Algorithm

An algorithm to delete from max heap. Deletion in Max (or Min) Heap always happens at the root to
remove the Maximum (or minimum) value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

The root node is the item with the highest priority in a max heap. When removing the root node, we
replace it with the last item of the priority queue. Then, this item is compared with the child nodes
and swap with the greater one. It keeps moving down the tree until the heap property is restored.
Now, let’s apply to an example to understand how this process works. First, we replace the root
node with the last item :

The new priority queue is now violent the max heap’s property where the root node is smaller than
its children:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 18


Then, we swap node with the greater child until it reaches the leaf or greater than both children:

Now, is greater than its child of , we stop and reach our priority queue:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 19


Algorithm for deletion operation in the priority queue
1) if nodeToBeDeleted is the leafNode then
delete the node
2) else swap lastLeafNode with the nodeToBeDeleted
delete nodeToBeDeleted
3) perform heapify

C Program to implement a heap & provide insertion & deletion operation.


#include <stdio.h>
int array[100], n;
main()
{
int choice, num;
n = 0;/*Represents number of nodes in the heap*/
while(1)
{
printf("1.Insert the element \n");
printf("2.Delete the element \n");
printf("3.Display all elements \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 20


printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}/*End of switch */
}/*End of while */
}/*End of main()*/
display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}/*End of display()*/

insert(int num, int location)


{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return;
}
array[location] = array[parentnode];
location = parentnode;
}/*End of while*/
array[0] = num; /*assign number to the root node */
}/*End of insert()*/

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 21


delete(int num)
{
int left, right, i, temp, parentnode;

for (i = 0; i < num; i++)


{
if (num == array[i])
break;
}
if (num != array[i])
{
printf("%d not found in heap list\n", num);
return;
}
array[i] = array[n - 1];
n = n - 1;
parentnode =(i - 1) / 2; /*find parentnode of node i */
if (array[i] > array[parentnode])
{
insert(array[i], i);
return;
}
left = 2 * i + 1; /*left child of i*/
right = 2 * i + 2; /* right child of i*/
while (right < n)
{
if (array[i] >= array[left] && array[i] >= array[right])
return;
if (array[right] <= array[left])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
i = left;
}
else
{
temp = array[i];
array[i] = array[right];
array[right] = temp;
i = right;
}
left = 2 * i + 1;

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 22


right = 2 * i + 2;
}/*End of while*/
if (left == n - 1 && array[i])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
}
}
OUTPUT:

Applications of a priority queue:

Priority queues are widely applied on other algorithms as well as in real-world systems. The main
applications include:

• Algorithms: Certain foundational algorithms rely on priority queues, such as Dijkstra’s


shortest path algorithm, prim’s algorithm, and heap sort algorithm, etc.
• Data compression: It is used in data compression techniques like Huffman code

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 23


• Operating Systems: Priority queues are used to select the next process to run, ensuring
high-priority tasks run before low-priority ones. It is also applied for load balancing, and
interrupt handling (Priority queue is used in Operating System as the scheduling algorithm, It
is used in interrupt handling of your system, CPU scheduling and Disk scheduling algorithm)
• Bandwidth Management: Priority queues are utilized to prioritize the important data
packet, so the network can make sure that those packets reach the destination as quickly as
possible.
• It is used in artificial intelligence algorithms such as the A* search algorithm
• It is also used in Stack implementation

Difference between Queue and Priority Queue


Priority Queue Queue

Process the elements with the No priority Exists


highest priority

No principle applied Follows FIFO principle

If more than one element with


the same priority exists, the order No such process undergoes
of queue is taken

Advantages of Priority Queue

• Easy to implement
• Processes with different priorities can be handled efficiently
• Application with differing requirements

Conclusion
Identifying the priorities and doing tasks accordingly allows you with faster and effective results.
Priority queue is one such data structure that helps you arrange your data according to your priority
and make your tasks easy.

D-heaps;

A d-heap (sometime called a d-ary heap) data structure is similar to a binary heap. A binary heap is
actually a special case of the d-heap data structure where d = 2. Here is an example of min binary
heap on the left and max binary heap on the right:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 24


The binary heap has the properties of being a complete tree (missing items are only on the bottom
of the tree and all nodes are as far left as possible) and satisfying the min/max heap property (in the
case of a min heap, every node is less than or equal to to it’s children).

The only difference between a binary heap and the d-heap is that instead of each node have 2
children, each node has d children.

Here is an example of a 3-ary max heap:

Here is an example of a 5-ary min heap:

GEORGE WAINAINA 0718313173/0731919231 [email protected] Page 25

You might also like