This document discusses priority queues and binary heaps. It describes how priority queues can be implemented using binary heaps. A binary heap is a complete binary tree that satisfies the heap property - a node's value is greater than or equal to its children's values. Basic heap operations like insertion and deletion maintain this property. The document also covers binomial heaps, which are collections of binomial trees linked in a list. Binomial heaps support operations like finding the minimum, insertion, extraction, and decreasing a key in logarithmic time. Priority queues have applications in algorithms like Dijkstra's and Prim's and in event-driven simulations.
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ā.
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.
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.
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.
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.