2..2 Priorty Queue

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Priority Queues as Heaps

DR. SHWETA SHARMA


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

Priority Queue can be used for load balancing and interrupt handling in an
operating system
DR. SHWETA SHARMA, PEC CHANDIGARH 2
Priority Queue vs. 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

DR. SHWETA SHARMA, PEC CHANDIGARH 3


Heap Data Structure
 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

 A Heap is a special Tree-based data structure in


which the tree is a complete binary tree
DR. SHWETA SHARMA, PEC CHANDIGARH 4
Full Binary Tree vs. Complete Binary Tree

DR. SHWETA SHARMA, PEC CHANDIGARH 6


Types of Heaps
Min Heap and Max Heap

In a Min Heap, the key at the root must be minimum or equal among all keys
present in Binary Heap. The tree must be a complete binary tree
Value (Parent) ≤ Children

In a Max Heap, the key at the root must be maximum among all keys present in
Binary Heap. The tree must be a complete binary tree.
Value (Parent) > Children

DR. SHWETA SHARMA, PEC CHANDIGARH 7


Types of Heaps

DR. SHWETA SHARMA, PEC CHANDIGARH 8


Types of Heaps
Min and Max Heap are used as priority queues

If a small value has higher priority, then min heap

If a large value has higher priority, then max heap

DR. SHWETA SHARMA, PEC CHANDIGARH 9


Example- Make a Complete Binary Tree

45 31 20 14 7 12 18 11 7

DR. SHWETA SHARMA, PEC CHANDIGARH 10


Example
20 15 8 10 5 7 6 2 9 1

DR. SHWETA SHARMA, PEC CHANDIGARH 11


Array as a Tree
An array can be used to simulate a tree in the following way:
 If we are storing one element at index i in array Ar, then its parent will be
𝑖
stored at index (unless its a root, as root has no parent) and can be
2
𝑖
access by Ar
2

 Its left child can be accessed by Ar[2 * i] and its right child can be
accessed by Ar[2 * i +1]
 Index of root will be 1 in an array

DR. SHWETA SHARMA, PEC CHANDIGARH 12


Example

DR. SHWETA SHARMA, PEC CHANDIGARH 13


Heap Sort Algorithm
HEAPSORT (A)

1 BUILD-MAX-HEAP (A)
2 for i= A.length down to 2
3 exchange A[1] with A[i]
4 A.heapsize = A.heapsize – 1
5 MAX_HEAPIFY (A,1)

DR. SHWETA SHARMA, PEC CHANDIGARH 14


Heap Sort Algorithm
BUILD-MAX-HEAP (A)

1 A.heapsize = A.length
𝐴.𝑙𝑒𝑛𝑔𝑡ℎ
2 for i= down to 1
2
3 MAX-HEAPIFY (A,i)

DR. SHWETA SHARMA, PEC CHANDIGARH 15


Heap Sort Algorithm
MAX-HEAPIFY (A,i)

1 l = LEFT(i)
2 r = RIGHT(i)
3 if l ≤ A.heapsize and A[l] > A[i]
4 Largest = l
5 else largest = i
6 if r ≤ A.heapsize and A[r] > A[largest]
7 largest = r
8 if largest ≠ i
9 Excahnge A[i] with A[largest]
10 MAX-HEAPIFY (A,largest)
DR. SHWETA SHARMA, PEC CHANDIGARH 16
Example 1
Suppose you have 7 elements stored in array Arr.
Create a Max Heap

DR. SHWETA SHARMA, PEC CHANDIGARH 17


Example 1
BUILD-MAX-HEAP (A)
1 A.heapsize = A.length
𝐴.𝑙𝑒𝑛𝑔𝑡ℎ
2 for i= down to 1
2
3 MAX-HEAPIFY (A,i)
MAX-HEAPIFY (A,i)
1 l = LEFT(i)
2 r = RIGHT(i)
3 if l ≤ A.heapsize and A[l] > A[i]
4 Largest = l
5 else largest = i
6 if r ≤ A.heapsize and A[r] > A[largest]
7 largest = r
8 if largest ≠ i
9 Excahnge A[i] with A[largest]
10 MAX-HEAPIFY (A,largest)
DR. SHWETA SHARMA, PEC CHANDIGARH 18
Example 1
Max Heap:

DR. SHWETA SHARMA, PEC CHANDIGARH 19


Example 2
Suppose you have 6 elements stored in array Arr.
Create a Max Heap
Create a sorted Array

DR. SHWETA SHARMA, PEC CHANDIGARH 20


Example 2

Max Heap:

DR. SHWETA SHARMA, PEC CHANDIGARH 21


Example 2

HEAPSORT (A)

1 BUILD-MAX-HEAP (A)
2 for i= A.length down to 2
3 exchange A[1] with A[i]
4 A.heapsize = A.heapsize – 1
5 MAX_HEAPIFY (A,1)

DR. SHWETA SHARMA, PEC CHANDIGARH 22


Example 2

HEAPSORT (A)

1 BUILD-MAX-HEAP (A)
2 for i= A.length down to 2
3 exchange A[1] with A[i]
4 A.heapsize = A.heapsize – 1
5 MAX_HEAPIFY (A,1)

DR. SHWETA SHARMA, PEC CHANDIGARH 23


Example 2

HEAPSORT (A)

1 BUILD-MAX-HEAP (A)
2 for i= A.length down to 2
3 exchange A[1] with A[i]
4 A.heapsize = A.heapsize – 1
5 MAX_HEAPIFY (A,1)

DR. SHWETA SHARMA, PEC CHANDIGARH 24


Example 2

HEAPSORT (A)

1 BUILD-MAX-HEAP (A)
2 for i= A.length down to 2
3 exchange A[1] with A[i]
4 A.heapsize = A.heapsize – 1
5 MAX_HEAPIFY (A,1)

DR. SHWETA SHARMA, PEC CHANDIGARH 25


Example 2

Sorted Array

DR. SHWETA SHARMA, PEC CHANDIGARH 26


Time Complexity of Heap Sort

Time Complexity
Best Ω (nlogn)
Worst O (nlogn)
Average Θ (nlogn)

DR. SHWETA SHARMA, PEC CHANDIGARH 27


Question 1: Create a Max heap

1, 5, 6, 8, 12, 14, 16

Output: 16, 12, 14, 8, 5, 1, 6

DR. SHWETA SHARMA, PEC CHANDIGARH 28


Question 2

1) Create a Max Heap


2) Create a sorted Array (Increasing order)

DR. SHWETA SHARMA, PEC CHANDIGARH 29


Thank you!

DR. SHWETA SHARMA, PEC CHANDIGARH 32

You might also like