0% found this document useful (0 votes)
96 views11 pages

Priority Queues - 1

Uploaded by

Yash Singh Jha
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)
96 views11 pages

Priority Queues - 1

Uploaded by

Yash Singh Jha
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/ 11

Priority Queues- 1

Introduction
● Priority Queues are abstract data structures where each data/value in the
queue has a certain priority.
● A priority queue is a special type of queue in which each element is served
according to its priority.
● If elements with the same priority occur, they are served according to their
order in the queue.
● Generally, the value of the element itself is considered for assigning the
priority.
● For example, the element with the highest value is considered as the highest
priority element. However, in some cases, we may assume the element with
the lowest value to be the highest priority element. In other cases, we can set
priorities according to our needs.

Difference between Priority Queue and Normal Queue


In a queue, the First-In-First-Out(FIFO) rule is implemented whereas, in a priority
queue, the values are removed based on priority. The element with the highest
priority is removed first.

Main Priority Queues Operations


● Insert (key, data): Inserts data with a key to the priority queue. Elements are
ordered based on key.
● DeleteMin/DeleteMax: Remove and return the element with the
smallest/largest key.
● GetMinimum/GetMaximum: Return the element with the smallest/largest
key without deleting it.

1
Auxiliary Priority Queues Operations
● kth - Smallest/kth – Largest: Returns the kth -Smallest/kth –Largest key in
the priority queue.
● Size: Returns the number of elements in the priority queue.
● Heap Sort: Sorts the elements in the priority queue based on priority (key).

Priority Queue Applications


Priority queues have many applications - a few of them are listed below:
● Data compression: Huffman Coding algorithm
● Shortest path algorithms: Dijkstra’s algorithm
● Minimum spanning tree algorithms: Prim’s algorithm
● Event-driven simulation: Customers in a line
● Selection problem: Finding the kth- smallest element

Priority Queue Implementations


Before discussing the actual implementation, let us enumerate the possible
options.

Unordered Array Implementation


● Elements are inserted into the array without bothering about the order.
Deletions (DeleteMax) are performed by searching the key and then deleting.
● Insertions complexity: O(1).
● DeleteMin complexity: O(n)

Unordered List Implementation


● It is very similar to array implementation, but instead of using arrays, linked
lists are used.
● Insertions complexity: O(1).
● DeleteMin complexity: O(n).
Ordered Array Implementation

2
● Elements are inserted into the array in sorted order based on the key field.
Deletions are performed at only one end.
● Insertions complexity: O(n); DeleteMin complexity: O(1).

Ordered List Implementation


● Elements are inserted into the list in sorted order based on the key field.
Deletions are performed at only one end, hence preserving the status of the
priority queue. All other functionalities associated with a linked list ADT are
performed without modification.
● Insertions complexity: O(n); DeleteMin complexity: O(1).

Binary Search Trees Implementation


● Both insertions and deletions take O(log(n)) on average if insertions are
random (refer to Trees chapter).

Balanced Binary Search Trees Implementation


● Both insertions and deletion take O(log(n)) in the worst case (refer to Trees
chapter).
Binary Heap Implementation
In subsequent sections, we will discuss this in full detail.

Heaps

3
● A heap is a tree with some special properties.
● The basic requirement of a heap is that the value of a node must be ≥ (or ≤)
than the values of its children. This is called the heap property.
● A heap also has the additional property that all leaf nodes should be at h or
h – 1 level (where h is the height of the tree) for some h > 0 (complete binary
trees).
● That means the heap should form a complete binary tree (as shown below).

In the examples below, the left tree is a heap (each element is greater than its
children) and the right tree is not a heap (since 11 is greater than 2).

Types of Heaps
Based on the property of a heap we can classify heaps into two types:

4
● Min heap: The value of a node must be less than or equal to the values of its
children.

● Max heap: The value of a node must be greater than or equal to the values
of its children.

Binary Heaps
● In a binary heap, each node may have up to two children.

5
● In practice, binary heaps are enough and we concentrate on binary min
heaps and binary max heaps for the remaining discussion.

Representing Heaps: Before looking at heap operations, let us see how heaps can
be represented. One possibility is using arrays. Since heaps are forming complete
binary trees, there will not be any wastage of locations. For the discussion below let
us assume that elements are stored in arrays, which starts at index 0. The previous
max heap can be represented as:

Heap Operations
Some of the important operations performed on a heap are described below along
with their algorithms.

Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is
used to create a Min-Heap or a Max-Heap.

● Let the input array be

● Create a complete binary tree from the array


● Start from the first index of the non-leaf node whose index is given by n/2 -
1.

6
● Set current element i as largest.

● The index of the left child is given by 2i + 1 and the right child is given by 2i +
2.
● If leftChild is greater than currentElement (i.e. element at the ith index), set
leftChildIndex as largest. #Condition1

● If rightChild is greater than element in largest, set rightChildIndex as


largest. #Condition2
● Swap largest with currentElement. #Condition3
● Repeat steps 3-7 until the subtrees are also heapified.
● For Min-Heap, both leftChild and rightChild must be smaller than the
parent for all nodes.

Python Code

7
def heapify(arr, n, i):
largest = i
l = 2 * i + 1 #Index of Left Child
r = 2 * i + 2 #Index of Right Child

if l < n and arr[i] < arr[l]: #Condition1


largest = l

if r < n and arr[largest] < arr[r]: #Condition2


largest = r

if largest != i: #Condition3
arr[i],arr[largest] = arr[largest],arr[i]
heapify(arr, n, largest)

Insert Element into Heap


Insertion into a heap can be done in two steps:
● Insert the new element at the end of the tree. #Step1

● Heapify the tree. #Step2

8
Python Code
def insert(array, newNum):
size = len(array)
if size == 0:#If empty heap initially
array.append(newNum) #Simply add the newNum
else:
array.append(newNum);#Step1
for i in range((size//2)-1, -1, -1):
heapify(array, size, i) #Step2

Delete Element from Heap


Follow the given steps to delete an element from a Heap:

● Select the element to be deleted. #Step1

● Swap it with the last element.#Step2

9
● Remove the last element .#Step3

● Heapify the tree. #Step4

10
Python Code
def deleteNode(array, num):
size = len(array)
i = 0
for i in range(0, size):
if num == array[i]: #Step1
break

array[i], array[size-1] = array[size-1], array[i] #Step2


array.remove(size-1) #Step3
for i in range((len(array)//2)-1, -1, -1):
heapify(array, len(array), i) #Step4

Implementation of Priority Queue


● Priority queue can be implemented using an array, a linked list, a heap data
structure, or a binary search tree.
● Among these data structures, heap data structure provides an efficient
implementation of priority queues.
● Hence, we will be using the heap data structure to implement the priority
queue in this tutorial.

Insertion and Deletion in a Priority Queue


The first step would be to represent the priority queue in the form of a
max/min-heap. Once it is heapified, the insertion and deletion operations can be
performed similar to that in a Heap. Refer to the codes discussed above for more
clarity.

11

You might also like