0% found this document useful (0 votes)
20 views

Lecture 05 - Priority Queue, Heap

The document discusses priority queues and binary heaps. It explains that priority queues allow elements to be dequeued based on priority. Binary heaps can implement priority queues efficiently using a complete binary tree structure where child nodes have lower priority than parent nodes. Operations like insertion and deletion in a binary heap take O(logN) time.

Uploaded by

ahababimtiaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 05 - Priority Queue, Heap

The document discusses priority queues and binary heaps. It explains that priority queues allow elements to be dequeued based on priority. Binary heaps can implement priority queues efficiently using a complete binary tree structure where child nodes have lower priority than parent nodes. Operations like insertion and deletion in a binary heap take O(logN) time.

Uploaded by

ahababimtiaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ِ‫ﺑِﺳْ مِ ٱ ﱠٰ ِ ٱﻟرﱠ ﺣْ َٰﻣ ِن ٱﻟرﱠ ﺣِﯾم‬

In the name of Allah, Most Gracious, Most Merciful

CSE 4303
Data Structure

Topic:Priority Queue, Heap

Asaduzzaman Herok
Lecturer | CSE | IUT
[email protected]
Priority Queue

With queues
❖ The order may be summarized by first in, first
out

But, if each object is associated with a priority, we


may wish to pop that object which has highest priority
❖ With each pushed object, we will associate a
nonnegative integer (0, 1, 2, ...) where:
❖ The value 0 has the highest priority, and
❖ The higher the number, the lower the priority
❖ Other way 0 can have the lowest priority and
higher number has higher priority.

A Priority Queue is a particular type of data structure in which every element is assigned a priority.
Elements with higher priorities are dequeued before those with lower ones. In the event of similar
priorities, elements are dequeued based on their existing order in the queue.

Asaduzzaman Herok, Lecturer


14 September, 2023 2
Department of Computer Science and Engineering, IUT
Operations
❖ The top of a priority queue is the object with highest priority

❖ Popping from a priority queue removes the current highest priority object:

❖ Push places a new object into the appropriate place

Asaduzzaman Herok, Lecturer


14 September, 2023 3
Department of Computer Science and Engineering, IUT
Lexicographical Priority

Priority may also depend on multiple variables:


❖ Two values can specify a priority: (a, b)
➢ A pair (a, b) has higher priority than (c, d) if:
■ a < c, or
■ a = c and b < d

❖ For example, (5, 19), (13, 1), (13, 24), and (15, 0) all have higher priority
than (15, 7)

Asaduzzaman Herok, Lecturer


14 September, 2023 4
Department of Computer Science and Engineering, IUT
Priority Queue Applications

❖ Any event/job management that assign priority to events/jobs


❖ Priority-based OS process scheduler
❖ Event-driven simulation (traffic flows)
❖ Artificial intelligence search algorithms
❖ Used in the Dijkstra's shortest path algorithm.
❖ Used in prim's (minimum spanning tree) algorithm
❖ Used in data compression techniques like Huffman code.

Asaduzzaman Herok, Lecturer


14 September, 2023 5
Department of Computer Science and Engineering, IUT
Implementations of Priority Queues

❖ Array Implementation
❖ Linked List Implementation
❖ Heap Implementation
➢ Array based
➢ Linked List based

Asaduzzaman Herok, Lecturer


14 September, 2023 6
Department of Computer Science and Engineering, IUT
Array Implementation of Priority Queues
Suppose items with priorities 16, 14, 10, 9, 8, 7, 4, 3, 1 are to be stored in a priority queue.

One implementation:

Suppose an item with priority 15 is added:

Thus inserting takes O(N) time: linear

Asaduzzaman Herok, Lecturer


14 September, 2023 7
Department of Computer Science and Engineering, IUT
Linked List Implementation of Priority Queues

Suppose an item with priority 2 is to be added:

Only O(1) (constant) pointer changes required, but it takes O(N) pointer traversals to find the location
for insertion.

Wanted: a data structure for PQs that can be both searched and updated in better than O(N) time.

Asaduzzaman Herok, Lecturer


14 September, 2023 8
Department of Computer Science and Engineering, IUT
Binary Heaps
A binary heap is a complete binary tree in which every node satisfies the heap property which stats
that, for
❖ Max Heap: If B is a child of A, then key(A) ≥ key(B)
❖ Min Heap: If B is a child of A, then key(A) ≤ key(B)

This implies that elements at every node will be either greater(max heap) / less(min heap) than or
equal to the element at its left and right child.

Asaduzzaman Herok, Lecturer


14 September, 2023 9
Department of Computer Science and Engineering, IUT
Implementation of Binary Heaps
Linked List Based: Array Based: index starting from 1.

struct Node Root: arr[1]


{
Node *parent; Node: value at i = arr[i]
int val; Parent: arr[i/2]
Node *left; Left child: arr[2*i]
Node *right; Right child: arr[2*i+1]
};

Pseudomonas implementation details will be shown in board

Asaduzzaman Herok, Lecturer


14 September, 2023 10
Department of Computer Science and Engineering, IUT
Inserting a New Element in a Binary Heap

Existing Heap Insert new node(99) at a leaf


(max heap) node

Asaduzzaman Herok, Lecturer


14 September, 2023 11
Department of Computer Science and Engineering, IUT
Inserting a New Element in a Binary Heap

Heapify the binary heap (max heap)

Asaduzzaman Herok, Lecturer


14 September, 2023 12
Department of Computer Science and Engineering, IUT
Creating a max heap
Build a max heap H from the given set of numbers: 45, 36, 54, 27, 63, 72, 61, and 18. Also draw the
memory representation of the heap.

Asaduzzaman Herok, Lecturer


14 September, 2023 13
Department of Computer Science and Engineering, IUT
Deleting an Element from a Binary Heap
1. Replace the root node’s value with the last node’s value so that heap
is still a complete binary tree but not necessarily a heap.
2. Delete the last node.
3. Sink down the new root node’s value so that H satisfies the heap
property. In this step, interchange the root node’s value with its child
node’s value (whichever is largest among its children).
Given Heap (max heap)
Deleting the root

Asaduzzaman Herok, Lecturer


14 September, 2023 14
Department of Computer Science and Engineering, IUT
Complexity of Binary Heap Insertion & Deletion
What is the maximum number of parent shifts necessary?

The height of the binary heap = floor(log2N)


So binary heap add can be done in O(logN) time.

Asaduzzaman Herok, Lecturer


14 September, 2023 15
Department of Computer Science and Engineering, IUT
Acknowledgements

Rafsanjany Kushol
PhD Student, Dept. of Computing Science,
University of Alberta

Sabbir Ahmed
Assistant Professor
Department of CSE, IUT

Asaduzzaman Herok, Lecturer


14 September, 2023 16
Department of Computer Science and Engineering, IUT

You might also like