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

DAA priority queue and Heap notes

A priority queue is an abstract data type that manages a set of elements, each with an associated priority, allowing for the processing of higher priority elements before lower ones. It supports operations such as insertion, finding the minimum or maximum, and deleting elements based on their priority. Priority queues are widely used in applications like operating system job scheduling, event simulation, and various graph algorithms, and can be implemented using linked lists, binary search trees, or binary heaps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DAA priority queue and Heap notes

A priority queue is an abstract data type that manages a set of elements, each with an associated priority, allowing for the processing of higher priority elements before lower ones. It supports operations such as insertion, finding the minimum or maximum, and deleting elements based on their priority. Priority queues are widely used in applications like operating system job scheduling, event simulation, and various graph algorithms, and can be implemented using linked lists, binary search trees, or binary heaps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DAA@Unit-2[Priority

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(SS∪{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

Applications of Priority Queue:


The priority queues are extensive use in
 Implementing schedulers in OS, and distributed systems.
 Representing event lists in discrete event simulation
 Implementation of numerous graph algorithms efficiently
 Selecting Kth largest or Kth smallest element in lists.
 Car rental service
 Sorting Applications

Scheduling the jobs in OS:-

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.

Car Rental service:


A car rental service is a kind of service
This allows the user to use the car on rental bases
User can user the car by paying the charge on hour bases. there is fixed amount of charge per
hour, to implement this application a priority queue is used.
When the user requests for the car his request is kept in priority
If users request is same time then paying money (amount) is kept in priority.

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.

 By using linked list


o For unsorted
 Insertion O(1)
[The items are pairs(priority, element). We can implement insert ()
using insertLast() on the sequence. This takes O(1). However, because
we always insert at the end, irrespective of the key value, our sequence
is not ordered]

 Delete-min(), minimum() O(n)


Thus, for methods such as minimum(), delete-min() we need to look at
all the elements of S. The worst case time complexity for these
methods is O(n).

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 search Tree


Average Worst
Search O(log N) O(N)
Insert O(log N) O(N)
Delete O(log N) O(N)

 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.

Height of a complete binary tree with N elements is


Example:

Another example for Binary Heap:

Array Representation

Heap Order 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.
The heap ordering property can be one of two types: min-heap property & max-heap
property
 The 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.
It supports insert and delete-min operations

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

 Heaps could be binary or d-array.


 Heap order property follows any one property among two (min-heap property &
max-heap property)

 Duplicates are allowed


 No order implied for elements, means which do not share ancestor and descendant
relationship.

Implementation of the heaps:

It tree is complete we use the following implementation.


Consider an array A. Given element at position “i” in the array.
 Minimum element is root
 Left child(i)=at position 2i
 Right child(i)=at position 2i+1

 Parent (i)=at position

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:

 Suppose a heap of n nodes has height h.


 Complete binary tree of height h hash has “2h+1 -1” nodes. (note: here h is consider as
number of levels below the root node)
 Hence 2h -1< n<=2h+1 -1

Page 5 of 21
DAA@Unit-2[Priority
Queues]
Binary heap operations:

Create, insert, deletemin and findminimum

Create:

void buildHeap(int *data, int n) {


int low;
for (low = n/2 - 1; low >= 0; low--) {
insertHeap(data, low, n-1);
}
return;

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.

Consider the following example: Construct Binary heap by using following


elements:13,21,16,24,31,19,68,65,26,32,14

For constructing binary heap we must


follow two properties. Structural property
and heap order property.

First we follow structural proper, we must


arrange the all the elements in complete
binary tree form left to right.

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.

Pseudo code for Insert operation


void insertHeap(int *data, int low, int count) { void addNode(int *data, int val, int n)
int pos = (2 * low) + 1, current = data[low]; {
while (pos <= count) { data[n] = val;
if (pos < count && data[pos] > data[pos + 1]) buildUp(data, n);
pos++; return;
if (current <= data[pos]) }
break; void buildUp(int *data, int index) {
else { int val = data[index];
data[low] = data[pos]; while (data[(index - 1) / 2] >= val)
low = pos; {
pos = (2 * low) + 1; data[index] = data[(index - 1) / 2];
} if (!index)
} break;
data[low] = current; index = (index - 1) / 2;
return; }
} data[index] = val;
return;
}

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.

Example for Binary heap insertion:-

Finding the minimum element:

 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

 Minimum element is always at the root


 Heap decreases by one in size
 Move last element into hole at root
 Percolate (filter) down while heap-order property not satisfied
 It takes O(log N)

Delete operation Pseudo code


void deleteNode(int *data, int n) {
int val = data[0];
data[0] = data[n];
insertHeap(data, 0, n - 1);
printf("%d is deleted from the heap\n", val);
return;
}

Heap DeleteMin

Page 9 of 21
ADS@Unit-3[Priority Queues]

Page 10 of 21

You might also like