DAA priority queue and Heap notes
DAA priority queue and Heap notes
Queues]
Page 1 of 21
DAA@Unit-2[Priority
Queues]
Priority Queue:-
A priority queue is an ADT (abstract data type) for maintaining a set S of elements,
each with an associated value called priority.
Means priority queue is like a regular queue or stack data structure, but where
additionally each element has a “priority” associated with it. That’s why this data structure
has “priority” name.
Properties:
The assigned priority and such that order in which elements are deleted and processed
comes 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.
A priority queue supports the following operations:-
Insert(x)insert element ‘ x’ in set S(SS∪{x})
Minimum ( ) return the element of S with smallest priority
Delete-min() returns and removes the elements of S with smallest priority.
Maximum() return the element of S with highest priority
Delete-Max() return and removes the element of S with highest priority
A typically application of the Priority Queue is scheduling the jobs in operating system
(OS). The os allocates priority to the jobs. The jobs are placed in the queue & position of the
job in the queue determines their priorities in OS. There are three kinds of jobs in OS.
1. Real time jobs
2. Foreground jobs
3. Background jobs
The OS always schedules the real time jobs first, if there are no real time jobs in pending
then it schedules the foreground jobs. Finally it schedules the back ground jobs.
Page 2 of 21
DAA@Unit-2[Priority
Queues]
Implementation of Priority Queue:
Priority queue can be implemented by using linked list (Sorted & unsorted), binary search
tree, Binary Heap.
o For Sorted
Another implementation uses a sequence S, sorted by increasing priorities
Minimum() and delete-min() takes O(1)
Because the minimum value of given list is always placed in root in the
sorted list.
Insert() O(1).
However, to implement insert(), we must now scan through the entais
sequence in the worst cast. Thus insert() runs in O(n) time.
Binary Heap:
Average Worst
Search O(N) O(N)
Insert O(log N) O(log N)
Delete O(log N) O(log N)
Heap: Heap is a specialized tree based data structure that satisfies the heap property.
Heap Property:- all nodes are either “greater than or equal to” or “less than or Equal to”
each of its children, according to a comparison “predicate” defined for the heap.
Binary Heap:
Introduction: Binary heaps are special form of binary trees.
Binary heap were first introduced by Williams in 1964
Definition:
A binary heap is a binary tree with two properties; those are structure property
and Heap-order property.
Page 3 of 21
DAA@Unit-2[Priority
Queues]
Structure Property:
A binary heap is a complete binary tree that is all the levels of the tree, except
possibly the last one (deepest) are fully field, and if the last level of the tree is not complete,
the nodes of that level are filled form left to right.
Array Representation
Page 4 of 21
DAA@Unit-2[Priority
Queues]
The 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.
It support insert and delete-max operation
The minimum element will always be present at the root of the heap. Thus the find min
operation will have worst-case O (1) running time.
Height of a heap:
Page 5 of 21
DAA@Unit-2[Priority
Queues]
Binary heap operations:
Create:
Step 1: we will design a binary tree by storing the given elements in an array for concatening
heap.
Step 2: if we want to construct a min heap in a binary tree in which each parent node is less
than its child node.
Step 3: if we want to construct a max heap in binary tree in which each parent node is greater
than its child node.
Page 6 of 21
DAA@Unit-2[Priority
Queues]
Then we follow the heap order property.
Insert:
Insert new element into the heap at the next available slot, according to maintains a structure
property.
Then “percolate” (filter) the element up the heap while heap-order property not satisfied.
Page 7 of 21
DAA@Unit-2[Priority
Queues]
Step 1:- insert an element at the last portion of in a heap.
Step 2: compare with parent element & swap if it violates min-heap property (parent node
value is always less than the child).
Step 3: continue comparing process and do swapping parent, newly inserted element up to
fulfil the min-heap property to entire heap.
The element with smallest priority always sits at the root of the heap.
This because if it was elsewhere, it would have a parent with large priority and this
would violate the heap property.
Hence minimum() can be done in O(1).
Page 8 of 21
DAA@Unit-2[Priority
Queues]
Delete Operation:
Delete operation is performed to delete the minimum element, because the minimum element
has high priority in min heap priority
Heap DeleteMin
Page 9 of 21
ADS@Unit-3[Priority Queues]
Page 10 of 21