3. Def:- A Priority Queue is a collection of Elements such that
Each element has been assigned a priority and such that is
the order in which the elements are deleted and processed
From the following rules:-
• An Element of higher priority is processed before any
element of lower priority.
• Two elements with the same priority are processed
According to the order in which they were added to the
queue.
• Any Data Structure that supports the operations of
search of min or max, insert and delete min or max
respectively is called a priority queue.
4. • Example:-Suppose that we are selling the services of a
machine. Each user pays a fixed amount per use.
However, the time needed by each user is different. We
wish to maximize the returns from this machine under
the assumption that the machine is not to be kept idle
unless no user is available. This can be done by
maintaining a priority queue of all persons waiting to
use the machine. Whenever the machine is available,
the user with the smallest time requirement will be
selected. Hence a priority queue that supports delete
min is required. When a new user requests the
machine, his or her request is put into the priority
queue.
If each user needs the same amount of time on the
machine but people are willing to pay different amounts for
5. the service, then the priority queue based on the
amount of payment can be maintained. Whenever the
machine becomes available, the user willing to pay the
most is selected. This requires a delete max operation.
Model of Priority Queue:-
Simple general diagram of the
Priority queue is as shown.
Whenever an element is inserted
into queue, priority Queue inserts
the item according to its order.
Here we're assuming that data
with high value has
low priority
6. Basic Operations on priority Queues:-
• Insert or enqueue:−This operation will add an
item to the rear of the queue.
• Delete or Dequeue:-This operation will remove
an item from the front of the queue.
• Peek − This operation will performed to get the
element at front of the queue.
• isFull − check if queue is full.
• isEmpty − check if queue is empty.
7. Binary Heap
A binary heap is a heap data structure that takes the
form of a binary tree. Binary heaps are a common
way of implementing priority queues.
A binary heap is defined as a binary tree with two
additional constraints:
1.Shape property:- a binary heap is a complete
binary tree that is, all levels of the tree, except
possibly the last one (deepest) are fully filled,
and, if the last level of the tree is not complete,
the nodes of that level are filled from left to right.
8. 2.Heap property: the key stored in each node is
either greater than or equal to (≥) or less than or
equal to (≤) the keys in the node's children,
according to some total order.
Heaps where the parent key is greater than or
equal to (≥) the child keys are called max-heaps;
those where it is less than or equal to (≤) are
called min-heaps.
11. QN:-What is heap order property?
Ans:-A binary heap is a complete binary tree
which satisfies the heap ordering property.
The ordering can be one of two types:
1 min-heap property: the value of each node
is greater than or equal to the value of its
parent, with the minimum-value element at
the root.
2. max-heap property: the value of each node
is less than or equal to the value of its parent,
with the maximum-value element at the root.
12. Basic Heap Operation
The common operations are:
• Basic operations:-
Find-max (or find-min): find a maximum item of a
max-heap, or a minimum item of a min-heap,
respectively(peek operation) .
insert: adding a new key to the heap (push ).
extract-max (or extract-min): returns the node of
maximum value from a max heap [or minimum value
from a min heap] after removing it from the heap (pop
operation).
delete-max (or delete-min): removing the root
node of a max heap (or min heap), respectively.
13. replace: pop root and push a new key. More efficient than
pop followed by push, since only need to balance once, not
twice, and appropriate for fixed-size heaps.
• Creation operations:-
create-heap: This operation creates an empty heap.
merge (union):This operation joining two heaps to
form a valid new heap containing all the elements of both,
preserving the original heaps.
heapify: create a heap out of given array of elements.
14. • Inspection operations:-
size: return the number of items in the heap.
is-empty: return true if the heap is empty,
false otherwise.
• Internal operations:-
increase-key or decrease-key: updating a key within
a max- or min-heap, respectively.
delete: delete an arbitrary node.
sift-up: move a node up in the tree, as long as needed;
used to restore heap condition after insertion.
sift-down: move a node down in the tree, similar to
sift-up; used to restore heap condition after deletion or
replacement.
15. Application Of Priority Queues:-
A* search algorithm implementation can be done using
Priority queues.
Priority queues are used for load balancing and
interrupt handling.
Priority queues are used for Huffman codes for data
compression.
In traffic light, depending upon the traffic, the color will
be given priority.
Dijkstra’s shortest path algorithm using priority queue
when the graph is stored in the form of adjacency list or
matrix, priority queue can be used to extract minimum
efficiency when implementing Dijkstra’s algorithms.
Prims algorithms implementation can be done using
Priority queue & also used to sort heaps.
16. d-array heap:-The d-ary heap or d-heap is a priority
queue data structure, a generalization of the binary heap in
which the nodes have d children instead of 2. Thus, a binary
heap is a 2-heap, and a ternary heap is a 3-heap.
References:-
1.wikipedia.
2.Data Structures by Seymour lipschutz.
3.Fundamentals of Data Structures by Sartaj Sahini
4.World wide web.