SlideShare a Scribd company logo
1 of 38
ADITYA ENGINEERING COLLEGE (A)
ADVANCED DATA
STRUCTURES
Aditya Engineering College (A)
Advanced Data Structures
UNIT-II
Priority Queues (Heaps): Introduction, Binary Heaps-Model
and Simple Implementation, Basic Heap Operations, Other
Heap Operations, Applications of Priority Queues,
Binomial Heaps (or Queues), Binomial Heap Structure and
Implementation, Binomial Queue Operations
Aditya Engineering College (A)
Advanced Data Structures
Priority Queues
ā€¢ A priority queue is a data structure in which each element is assigned
a priority.
ā€¢ The priority of the element will be used to determine the order in
which the elements will be processed.
ā€¢ The general rules of processing the elements of a priority queue are
1. An element with higher priority is processed before an element
with a lower priority.
2. Two elements with the same priority are processed on a first-
come-first-served (FCFS) basis.
Introduction
Aditya Engineering College (A)
Advanced Data Structures
ā€¢ A priority queue is a collection of elements, in which each
element has been assigned a priority value. The order in
which the elements will be processed is decided by the priority
of the element.
ā€¢ An element with higher priority is processed first.
ā€¢ If elements with the same priority occur, they are
processed according to the order in which they were
added to the queue.
ā€¢ Usually, a lower priority number means higher priority.
Aditya Engineering College (A)
Advanced Data Structures
Priority Queue - PQ is a queue that is ordered by priority
ā€¢ Priority Queues have the following functions associated with them: ā€“
1. Initialize PQ to be empty
2. Test whether PQ is empty
3. Test whether PQ is full
4. Insert a new item into the PQ
5. Delete item of highest priority
Aditya Engineering College (A)
Advanced Data Structures
There are two ways to implement PQā€™s.
1. Linked list representation
2. Array Representation
Implementation of Priority Queue
Aditya Engineering College (A)
Advanced Data Structures
1.Linked list representation
Implementation of Priority Queue
When a priority queue is implemented with a linked list, each node
will contain three parts:
ā€¢Data.
ā€¢The priority number of the element (PRN).
ā€¢The address of the next element.
It has 4 elements A, B, C, D having priority 1, 2, 2 and 3 respectively. Here A will be processed first
since it has a higher priority. Elements B and C has the same priority and we can say that B was
inserted in the queue before C.
Aditya Engineering College (A)
Advanced Data Structures
Inserting a new element in a linked priority queue
ā€¢Traverse the list until finding a node PTR with a lower priority than the new element.
ā€¢Insert new element in front of the node PTR.
Consider the following linked priority queue where we have to insert a new element S
having priority 4.`
1.Linked list representation: priority queue
PQ before insertion
Inserting into PQ
PQ After insertion
Aditya Engineering College (A)
Advanced Data Structures
ā€¢ The first node in the list will be deleted, and its data will be
processed first.
1.Linked list representation: priority queue
Deleting a new element in a linked priority queue
Aditya Engineering College (A)
Advanced Data Structures
2.Array representation
Implementation of Priority Queue
ā€¢ Priority queues can be also implemented using arrays. For each priority
number, a separate circular queue is used. Each queue will have its own
FRONT and REAR pointers.
Kth row in matrix
maintains the
queue of elements
with priority number
k. FRONT[K] and
REAR[K] contain
the FRONT and
REAR elements of
row K of the queue.
Aditya Engineering College (A)
Advanced Data Structures
2.Array representation
Implementation of Priority Queue
Insertion:
To inset a new element with priority k,
add the element at the rear end of row
k(k-row number as well as the priority
number).
For ex,if we insert element ā€œRā€ with
priority 3, then it will be inserted as
shown.
Before insetion of ā€œRā€
After insertion of ā€œRā€
Deletion:
To delete an new element, we find non
empty queue and then process the front
element of first non empty queue. In the
example, the first non empty queue is one
with priority number 1 and front element is
ā€œAā€. So ā€œ will be deleted and processed
first.(Find the element with smallest k,such
that FRONT[k]!=NULL)
Aditya Engineering College (A)
Advanced Data Structures
Binary Heaps-model and implementation
ā€¢ Think of a priority queue as a kind of bag
that holds priorities. You can put one in, and
you can take out the current highest priority.
ā€¢ A priority queue can be implemented using many of the data
structures (an array, a linked list, or a binary search tree etc.).
However, those data structures do not provide the most
efficient operations. To make all of the operations very
efficient, we'll use a new data structure called a heap.
Aditya Engineering College (A)
Advanced Data Structures
Binary Heaps
ā€¢ A binary heap is a complete binary tree in which every node
satisfies the heap property which states that:
ā€¢ If B is a child of A, then key(A)>= key(B)
ā€¢ For every node n, the value in n is greater than or equal
to the values in its children (and thus is also greater than or
equal to all of the values in its subtrees).
ā€¢ This type of heap is called as ā€œmax-heapā€.
ā€¢ If elements at every node will be either less than or equal to
the element at its left and right child, then it is called as
min-heapā€.
Aditya Engineering College (A)
Advanced Data Structures
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap-structure property
ā€¢ It is a complete binary tree.
ā€¢ A complete binary tree is a special type of binary tree where all the
levels of the tree are filled completely except the lowest level nodes
which are filled from as left as possible.
ā€¢ Each level has all of its nodes(except bottom level).The nodes in the
bottom level are filled from left to right.
Aditya Engineering College (A)
Advanced Data Structures
Aditya Engineering College (A)
Advanced Data Structures
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap-properties
Properties of binary heaps:
ā€¢ Since a heap is defined as a complete binary tree, all its elements can be
stored sequentially in an array.
ā€¢ Being a complete binary tree, all the levels of the tree except the last level are
completely filled.
ā€¢ The height of a binary tree is given as log2n, where n is the number of
elements.
ā€¢ Heaps (also known as partially ordered trees) are a very popular data structure
for implementing priority queues.
Properties
1) structure property
2) Heap-Order property
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap-structure property
Properties of binary heaps:
ā€¢ Since a heap is defined as a complete binary tree, all its elements can be
stored sequentially in an array.
ā€¢ Being a complete binary tree, all the levels of the tree except the last level are
completely filled.
ā€¢ The height of a binary tree is given as log2n, where n is the number of
elements.
ā€¢ Heaps (also known as partially ordered trees) are a very popular data structure
for implementing priority queues.
Binary heap is a useful data structure in which elements can be added randomly
but only the element with the highest value is removed in case of max heap and
lowest value in case of min heap. Binary tree is an efficient data structure, but a
binary heap is more space efficient and simpler.
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap-Order property
ā€¢ The technique for inserting and deleting items from a heap always maintains heap order
property.
ā€¢ The heap order property states that for every node x with parent p, the key in p is smaller
than or equal to the key in x.
Aditya Engineering College (A)
Advanced Data Structures
Build a max heap H from the given set of numbers: 45, 36, 54, 27, 63, 72,
61,and 18
ā€¢ Since a heap is defined as a
completebinary tree, all its
elements can be stored
sequentially in an array.
ā€¢ Being a complete binary
tree, all the levels of the
tree except the last level
are completely filled.
ā€¢ Heap order property also to
be satisfied.
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap Operations:Insertion
ā€¢ Consider a max heap H with n elements. Inserting a new value into heap is done as
follows:
1.Add the new value at the bottom of H in such a way that H is still a complete binary tree but
not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H now becomes a heap as well.
Insertion of 30
Aditya Engineering College (A)
Advanced Data Structures
Binary Heap Operations:Deletion
ā€¢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.
Aditya Engineering College (A)
Advanced Data Structures
Max-Heap has some basic operations:
ā€¢ getMax(): just return the root value. Cost O(1).
ā€¢ ExtractMax(): It removes max element from max-heap.
ā€¢ Insert(): We will attach a new node to any leaf. If it violate the heap
property, we will bubble up(called SiftUp) new node until the heap
property is satisfied.It takes O(logn) time.
Delete(): Delete a key takes O(logn) time.
ā€¢ ChangePriority: We need to change priority of a node. Depend on the
value of new priority we will do ShiftDown or ShiftUp to qualify the heap
property.
ā€¢ Increasekey(): It increase the value of key.Time complexity: O(logn).
Other Heap Operations
Aditya Engineering College (A)
Advanced Data Structures
Applications of Priority Queue
ā€¢ Prim's algorithm implementation can be done using priority queues.
ā€¢ Dijkstra's shortest path algorithm implementation can be done using
priority queues.
ā€¢ A* Search algorithm implementation can be done using priority queues.
ā€¢ Priority queues are used to sort heaps.
ā€¢ It is also used in Operating System for load balancing.
ā€¢ Priority queues are used in operating system for load
balancing and interrupt handling.
ā€¢ Priority queues are used in huffman codes for data compression.
ā€¢ In traffic light, depending upon the traffic, the colors will be given priority.
ā€¢ PQs are also applied to solve common problems like ā€œselection problemā€,
ā€œEvent Simulationā€.
Aditya Engineering College (A)
Advanced Data Structures
In event-driven simulations, a priority queue is used to schedule and process events based on their timestamps. Events with lower
timestamps (i.e., events that should occur earlier) have higher priorities in the queue. This approach ensures that events are
processed in the correct order, leading to accurate simulations in areas such as computer graphics, network simulations, and game
development.
Event-driven Simulation
Selection Problem
Priority queues are widely used to manage the events in a discrete event simulation. The events are added to the queue along with
their simulation time. The simulation time determines their priority. While execution, the event at the top of the queues is pulled out
and executed. This process repeats as long as events Jast in the priority queue.
Solution 1: Sort the elements in an array and return the element in the kth position.Complexity: While simple sorting algorithm takes
o(N?) time, advanced sorting algorithm takeso(N log N) time to execute.
Applications of Priority Queue
Aditya Engineering College (A)
Advanced Data Structures
ā€¢ A Binomial Heap is a collection of
Binomial Trees
Binomial Heap(Queue)
What is a Binomial Tree?
A Binomial Tree of order 0 has 1 node. A Binomial Tree of
order k can be constructed by taking two binomial trees
of order k-1 and making one the leftmost child of the
other.
A Binomial Tree of order k the has following properties.
ā€¢It has exactly 2k nodes.
ā€¢It has depth as k.
ā€¢A Binomial tree Bk is formed by merging 2 Binomial trees
Bk-1 & Bk-1.
ā€¢There are exactly kj nodes at depth i for i = 0, 1, . . . , k.
ā€¢The root has degree k and children of the root are
themselves Binomial Trees with order k-1, k-2,.. 0 from left to
right.
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps( or binomial queues)
A binomial heap H is a set of binomial trees. There are some properties.
ā€¢Each binomial tree in H is heap-ordered. So the key of a node is greater than or equal to the key of its
parent.
ā€¢There is at most one binomial tree in H, whose root has a given degree.
This binomial Heap H consists of
binomial trees B0, B2 and B3.
Which have 1, 4 and 8 nodes
respectively. And in total n = 13
nodes. The root of binomial trees
are linked by a linked list in order
of increasing degree
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps
Linked represntation
binomial heaps structure & implementation
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps structure & implementation
Each node in a binomial heap has 5 fields :
1.Pointer to parent
2.Key
3.Degree
4.Pointer to child (leftmost child)
5.Pointer to sibling which is immediately to its right
ā€¢Pointers in each node:Each node has the following pointers:
ā€¢ A parent pointer pointing to the immediate parent of the node
ā€¢ A left pointer pointing to the first child of the node
ā€¢ A right pointer pointing to the next sibling of the node.
Parent ā€“ Child relationship
between nodes
Single node in the Heap
Sibling relationship between
nodes
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps Operations
Finding the minimum key
The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node
binomial heap H. This implementation assumes that there are no keys with value.
Since a binomial heap is heap-ordered, the
minimum key must reside in a root node.
The BINOMIAL-HEAP-MINIMUM
procedure checks all roots,, saving the
current minimum in min and a pointer to
the current minimum in y.
Aditya Engineering College (A)
Advanced Data Structures
Linking and Uniting two binomial heaps
Aditya Engineering College (A)
Advanced Data Structures
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps Operations
Inserting a node
The following procedure inserts node x into binomial heap H, assuming of course that node x has already
been allocated and key[x] has already been filled in.
The procedure simply makes a one-node binomial
heap H' in O(1) time and unites it with the n-node
binomial heap H in O(1g n) time.
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps Operations
Extracting the node with minimum key
The following procedure extracts the node with the minimum key from binomial heap H and returns a
pointer to the extracted node.
Aditya Engineering College (A)
Advanced Data Structures
Aditya Engineering College (A)
Advanced Data Structures
binomial heaps Operations
Decreasing a key
The following procedure decreases the key of a node x in a binomial heap H to a new value k. It signals an error if k is
greater than x's current key.
Aditya Engineering College (A)
Advanced Data Structures

More Related Content

Similar to ADS UNIT II -Priority queue.pptx

DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptxAnuJoseph95
Ā 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)Mehedi Hasan
Ā 
Data Structures
Data StructuresData Structures
Data StructuresDr.Umadevi V
Ā 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
Ā 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
Ā 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptOliverKane3
Ā 
Data structures
Data structuresData structures
Data structuresSneha Chopra
Ā 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxAhmedEldesoky24
Ā 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
Ā 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfKanchanPatil34
Ā 
queue.pptx
queue.pptxqueue.pptx
queue.pptxDr.Shweta
Ā 

Similar to ADS UNIT II -Priority queue.pptx (20)

Queues
QueuesQueues
Queues
Ā 
Queue
QueueQueue
Queue
Ā 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
Ā 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
Ā 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Ā 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
Ā 
Data Structures
Data StructuresData Structures
Data Structures
Ā 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
Ā 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Ā 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
Ā 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
Ā 
Data structures
Data structuresData structures
Data structures
Ā 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptx
Ā 
Queues
Queues Queues
Queues
Ā 
stack.pptx
stack.pptxstack.pptx
stack.pptx
Ā 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
Ā 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
Ā 
Lec5
Lec5Lec5
Lec5
Ā 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdf
Ā 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
Ā 

Recently uploaded

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
Ā 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
Ā 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
Ā 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
Ā 
Call Girls in Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X79953056974 Low Rate Call Girls In Saket, Delhi NCR
Ā 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
Ā 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
Ā 
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...Nguyen Thanh Tu Collection
Ā 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
Ā 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
Ā 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
Ā 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
Ā 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
Ā 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
Ā 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
Ā 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
Ā 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
Ā 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
Ā 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
Ā 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
Ā 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
Ā 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
Ā 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
Ā 
Call Girls in Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [šŸ”9953056974šŸ”] escort service 24X7
Ā 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
Ā 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
Ā 
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...
80 Đį»€ THI THį»¬ TUYį»‚N SINH TIįŗ¾NG ANH VƀO 10 Sį»ž GD ā€“ ĐT THƀNH PHį» Hį»’ CHƍ MINH NĂ...
Ā 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
Ā 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
Ā 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
Ā 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Ā 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
Ā 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
Ā 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
Ā 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
Ā 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
Ā 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
Ā 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
Ā 

ADS UNIT II -Priority queue.pptx

  • 1. ADITYA ENGINEERING COLLEGE (A) ADVANCED DATA STRUCTURES
  • 2. Aditya Engineering College (A) Advanced Data Structures UNIT-II Priority Queues (Heaps): Introduction, Binary Heaps-Model and Simple Implementation, Basic Heap Operations, Other Heap Operations, Applications of Priority Queues, Binomial Heaps (or Queues), Binomial Heap Structure and Implementation, Binomial Queue Operations
  • 3. Aditya Engineering College (A) Advanced Data Structures Priority Queues ā€¢ A priority queue is a data structure in which each element is assigned a priority. ā€¢ The priority of the element will be used to determine the order in which the elements will be processed. ā€¢ The general rules of processing the elements of a priority queue are 1. An element with higher priority is processed before an element with a lower priority. 2. Two elements with the same priority are processed on a first- come-first-served (FCFS) basis. Introduction
  • 4. Aditya Engineering College (A) Advanced Data Structures ā€¢ A priority queue is a collection of elements, in which each element has been assigned a priority value. The order in which the elements will be processed is decided by the priority of the element. ā€¢ An element with higher priority is processed first. ā€¢ If elements with the same priority occur, they are processed according to the order in which they were added to the queue. ā€¢ Usually, a lower priority number means higher priority.
  • 5. Aditya Engineering College (A) Advanced Data Structures Priority Queue - PQ is a queue that is ordered by priority ā€¢ Priority Queues have the following functions associated with them: ā€“ 1. Initialize PQ to be empty 2. Test whether PQ is empty 3. Test whether PQ is full 4. Insert a new item into the PQ 5. Delete item of highest priority
  • 6. Aditya Engineering College (A) Advanced Data Structures There are two ways to implement PQā€™s. 1. Linked list representation 2. Array Representation Implementation of Priority Queue
  • 7. Aditya Engineering College (A) Advanced Data Structures 1.Linked list representation Implementation of Priority Queue When a priority queue is implemented with a linked list, each node will contain three parts: ā€¢Data. ā€¢The priority number of the element (PRN). ā€¢The address of the next element. It has 4 elements A, B, C, D having priority 1, 2, 2 and 3 respectively. Here A will be processed first since it has a higher priority. Elements B and C has the same priority and we can say that B was inserted in the queue before C.
  • 8. Aditya Engineering College (A) Advanced Data Structures Inserting a new element in a linked priority queue ā€¢Traverse the list until finding a node PTR with a lower priority than the new element. ā€¢Insert new element in front of the node PTR. Consider the following linked priority queue where we have to insert a new element S having priority 4.` 1.Linked list representation: priority queue PQ before insertion Inserting into PQ PQ After insertion
  • 9. Aditya Engineering College (A) Advanced Data Structures ā€¢ The first node in the list will be deleted, and its data will be processed first. 1.Linked list representation: priority queue Deleting a new element in a linked priority queue
  • 10. Aditya Engineering College (A) Advanced Data Structures 2.Array representation Implementation of Priority Queue ā€¢ Priority queues can be also implemented using arrays. For each priority number, a separate circular queue is used. Each queue will have its own FRONT and REAR pointers. Kth row in matrix maintains the queue of elements with priority number k. FRONT[K] and REAR[K] contain the FRONT and REAR elements of row K of the queue.
  • 11. Aditya Engineering College (A) Advanced Data Structures 2.Array representation Implementation of Priority Queue Insertion: To inset a new element with priority k, add the element at the rear end of row k(k-row number as well as the priority number). For ex,if we insert element ā€œRā€ with priority 3, then it will be inserted as shown. Before insetion of ā€œRā€ After insertion of ā€œRā€ Deletion: To delete an new element, we find non empty queue and then process the front element of first non empty queue. In the example, the first non empty queue is one with priority number 1 and front element is ā€œAā€. So ā€œ will be deleted and processed first.(Find the element with smallest k,such that FRONT[k]!=NULL)
  • 12. Aditya Engineering College (A) Advanced Data Structures Binary Heaps-model and implementation ā€¢ Think of a priority queue as a kind of bag that holds priorities. You can put one in, and you can take out the current highest priority. ā€¢ A priority queue can be implemented using many of the data structures (an array, a linked list, or a binary search tree etc.). However, those data structures do not provide the most efficient operations. To make all of the operations very efficient, we'll use a new data structure called a heap.
  • 13. Aditya Engineering College (A) Advanced Data Structures Binary Heaps ā€¢ A binary heap is a complete binary tree in which every node satisfies the heap property which states that: ā€¢ If B is a child of A, then key(A)>= key(B) ā€¢ For every node n, the value in n is greater than or equal to the values in its children (and thus is also greater than or equal to all of the values in its subtrees). ā€¢ This type of heap is called as ā€œmax-heapā€. ā€¢ If elements at every node will be either less than or equal to the element at its left and right child, then it is called as min-heapā€.
  • 14. Aditya Engineering College (A) Advanced Data Structures
  • 15. Aditya Engineering College (A) Advanced Data Structures Binary Heap-structure property ā€¢ It is a complete binary tree. ā€¢ A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible. ā€¢ Each level has all of its nodes(except bottom level).The nodes in the bottom level are filled from left to right.
  • 16. Aditya Engineering College (A) Advanced Data Structures
  • 17. Aditya Engineering College (A) Advanced Data Structures
  • 18. Aditya Engineering College (A) Advanced Data Structures Binary Heap-properties Properties of binary heaps: ā€¢ Since a heap is defined as a complete binary tree, all its elements can be stored sequentially in an array. ā€¢ Being a complete binary tree, all the levels of the tree except the last level are completely filled. ā€¢ The height of a binary tree is given as log2n, where n is the number of elements. ā€¢ Heaps (also known as partially ordered trees) are a very popular data structure for implementing priority queues. Properties 1) structure property 2) Heap-Order property
  • 19. Aditya Engineering College (A) Advanced Data Structures Binary Heap-structure property Properties of binary heaps: ā€¢ Since a heap is defined as a complete binary tree, all its elements can be stored sequentially in an array. ā€¢ Being a complete binary tree, all the levels of the tree except the last level are completely filled. ā€¢ The height of a binary tree is given as log2n, where n is the number of elements. ā€¢ Heaps (also known as partially ordered trees) are a very popular data structure for implementing priority queues. Binary heap is a useful data structure in which elements can be added randomly but only the element with the highest value is removed in case of max heap and lowest value in case of min heap. Binary tree is an efficient data structure, but a binary heap is more space efficient and simpler.
  • 20. Aditya Engineering College (A) Advanced Data Structures Binary Heap-Order property ā€¢ The technique for inserting and deleting items from a heap always maintains heap order property. ā€¢ The heap order property states that for every node x with parent p, the key in p is smaller than or equal to the key in x.
  • 21. Aditya Engineering College (A) Advanced Data Structures Build a max heap H from the given set of numbers: 45, 36, 54, 27, 63, 72, 61,and 18 ā€¢ Since a heap is defined as a completebinary tree, all its elements can be stored sequentially in an array. ā€¢ Being a complete binary tree, all the levels of the tree except the last level are completely filled. ā€¢ Heap order property also to be satisfied.
  • 22. Aditya Engineering College (A) Advanced Data Structures Binary Heap Operations:Insertion ā€¢ Consider a max heap H with n elements. Inserting a new value into heap is done as follows: 1.Add the new value at the bottom of H in such a way that H is still a complete binary tree but not necessarily a heap. 2. Let the new value rise to its appropriate place in H so that H now becomes a heap as well. Insertion of 30
  • 23. Aditya Engineering College (A) Advanced Data Structures Binary Heap Operations:Deletion ā€¢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.
  • 24. Aditya Engineering College (A) Advanced Data Structures Max-Heap has some basic operations: ā€¢ getMax(): just return the root value. Cost O(1). ā€¢ ExtractMax(): It removes max element from max-heap. ā€¢ Insert(): We will attach a new node to any leaf. If it violate the heap property, we will bubble up(called SiftUp) new node until the heap property is satisfied.It takes O(logn) time. Delete(): Delete a key takes O(logn) time. ā€¢ ChangePriority: We need to change priority of a node. Depend on the value of new priority we will do ShiftDown or ShiftUp to qualify the heap property. ā€¢ Increasekey(): It increase the value of key.Time complexity: O(logn). Other Heap Operations
  • 25. Aditya Engineering College (A) Advanced Data Structures Applications of Priority Queue ā€¢ Prim's algorithm implementation can be done using priority queues. ā€¢ Dijkstra's shortest path algorithm implementation can be done using priority queues. ā€¢ A* Search algorithm implementation can be done using priority queues. ā€¢ Priority queues are used to sort heaps. ā€¢ It is also used in Operating System for load balancing. ā€¢ Priority queues are used in operating system for load balancing and interrupt handling. ā€¢ Priority queues are used in huffman codes for data compression. ā€¢ In traffic light, depending upon the traffic, the colors will be given priority. ā€¢ PQs are also applied to solve common problems like ā€œselection problemā€, ā€œEvent Simulationā€.
  • 26. Aditya Engineering College (A) Advanced Data Structures In event-driven simulations, a priority queue is used to schedule and process events based on their timestamps. Events with lower timestamps (i.e., events that should occur earlier) have higher priorities in the queue. This approach ensures that events are processed in the correct order, leading to accurate simulations in areas such as computer graphics, network simulations, and game development. Event-driven Simulation Selection Problem Priority queues are widely used to manage the events in a discrete event simulation. The events are added to the queue along with their simulation time. The simulation time determines their priority. While execution, the event at the top of the queues is pulled out and executed. This process repeats as long as events Jast in the priority queue. Solution 1: Sort the elements in an array and return the element in the kth position.Complexity: While simple sorting algorithm takes o(N?) time, advanced sorting algorithm takeso(N log N) time to execute. Applications of Priority Queue
  • 27. Aditya Engineering College (A) Advanced Data Structures ā€¢ A Binomial Heap is a collection of Binomial Trees Binomial Heap(Queue) What is a Binomial Tree? A Binomial Tree of order 0 has 1 node. A Binomial Tree of order k can be constructed by taking two binomial trees of order k-1 and making one the leftmost child of the other. A Binomial Tree of order k the has following properties. ā€¢It has exactly 2k nodes. ā€¢It has depth as k. ā€¢A Binomial tree Bk is formed by merging 2 Binomial trees Bk-1 & Bk-1. ā€¢There are exactly kj nodes at depth i for i = 0, 1, . . . , k. ā€¢The root has degree k and children of the root are themselves Binomial Trees with order k-1, k-2,.. 0 from left to right.
  • 28. Aditya Engineering College (A) Advanced Data Structures binomial heaps( or binomial queues) A binomial heap H is a set of binomial trees. There are some properties. ā€¢Each binomial tree in H is heap-ordered. So the key of a node is greater than or equal to the key of its parent. ā€¢There is at most one binomial tree in H, whose root has a given degree. This binomial Heap H consists of binomial trees B0, B2 and B3. Which have 1, 4 and 8 nodes respectively. And in total n = 13 nodes. The root of binomial trees are linked by a linked list in order of increasing degree
  • 29. Aditya Engineering College (A) Advanced Data Structures binomial heaps Linked represntation binomial heaps structure & implementation
  • 30. Aditya Engineering College (A) Advanced Data Structures binomial heaps structure & implementation Each node in a binomial heap has 5 fields : 1.Pointer to parent 2.Key 3.Degree 4.Pointer to child (leftmost child) 5.Pointer to sibling which is immediately to its right ā€¢Pointers in each node:Each node has the following pointers: ā€¢ A parent pointer pointing to the immediate parent of the node ā€¢ A left pointer pointing to the first child of the node ā€¢ A right pointer pointing to the next sibling of the node. Parent ā€“ Child relationship between nodes Single node in the Heap Sibling relationship between nodes
  • 31. Aditya Engineering College (A) Advanced Data Structures binomial heaps Operations Finding the minimum key The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node binomial heap H. This implementation assumes that there are no keys with value. Since a binomial heap is heap-ordered, the minimum key must reside in a root node. The BINOMIAL-HEAP-MINIMUM procedure checks all roots,, saving the current minimum in min and a pointer to the current minimum in y.
  • 32. Aditya Engineering College (A) Advanced Data Structures Linking and Uniting two binomial heaps
  • 33. Aditya Engineering College (A) Advanced Data Structures
  • 34. Aditya Engineering College (A) Advanced Data Structures binomial heaps Operations Inserting a node The following procedure inserts node x into binomial heap H, assuming of course that node x has already been allocated and key[x] has already been filled in. The procedure simply makes a one-node binomial heap H' in O(1) time and unites it with the n-node binomial heap H in O(1g n) time.
  • 35. Aditya Engineering College (A) Advanced Data Structures binomial heaps Operations Extracting the node with minimum key The following procedure extracts the node with the minimum key from binomial heap H and returns a pointer to the extracted node.
  • 36. Aditya Engineering College (A) Advanced Data Structures
  • 37. Aditya Engineering College (A) Advanced Data Structures binomial heaps Operations Decreasing a key The following procedure decreases the key of a node x in a binomial heap H to a new value k. It signals an error if k is greater than x's current key.
  • 38. Aditya Engineering College (A) Advanced Data Structures